MATLAB Answers

4

Hardware OpenGL Failure on Ubuntu 16.04.3 - Can not initialize shared resources

Asked by Andrew Cox on 17 Jan 2018
Latest activity Commented on by Andrew Cox on 18 May 2018

First, my system info:

  • 4.13.0-26-generic #29~16.04.2-Ubuntu SMP Tue Jan 9 22:00:44 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  • Clean installation of Matlab R2016b (64-bit)
  • Video card: AMD/ATI Pitcairn PRO (Radeon HD 7850)

Problems with Vanilla Install

So, I start up Matlab from the command line with no arguments. I've set two environment variables, LIBGL_DEBUG=verbose and MESA_DEBUG=1, to get an idea of any bugs. The software starts with no errors. Next, I issue a simple plot command:

    plot(1,1, 'b*'); grid on;

Matlab creates the plot successfully, but issues a series of errors, saying that there was a low-level graphics error. From the command line I see the following messages:

    libGL: pci id for fd 570: 1002:6819, driver radeonsi
    libGL: OpenDriver: trying /usr/lib/x86_64-linux-gnu/dri/tls/radeonsi_dri.so
    libGL: OpenDriver: trying /usr/lib/x86_64-linux-gnu/dri/radeonsi_dri.so
    libGL: dlopen /usr/lib/x86_64-linux-gnu/dri/radeonsi_dri.so failed (/opt/local/MATLAB/R2016b/bin/glnxa64/../../sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /usr/lib/x86_64-linux-gnu/dri/radeonsi_dri.so))

There are many more, but the end result is the same: the linker cannot load the graphics drivers with the libstdc++.so file Matlab has loaded. To fix this issue, I rename the relevant libraries that come with Matlab to force it to use the system libraries (which include version GLIBCXX_3.4.21):

    cd /opt/local/MATLAB/R2016b/sys/os/glnxa64
    mv libstdc++.so.6 libstdc++.so.6.bak
    mv libstdc++.so.6.0.20 libstdc++.so.6.0.20.bak

Problems with Modified Install

With the C++ libraries renamed to force Matlab to link with the system libraries, I restart Matlab. The environment variables are still set, so any libgl or mesa errors should be reported to the terminal. Immediately after the program starts, I receive the following error in Matlab:

    com.jogamp.opengl.GLException: X11GLXDrawableFactory - Could not initialize shared resources for X11GraphicsDevice[type .x11, connection :1, unitID 0, handle 0x0, owner false, ResourceToolkitLock[obj 0x7c1bc33e, isOwner false, <2af5b1a, 46860956>[count 0, qsz 0, owner <NULL>]]]
	    at jogamp.opengl.x11.glx.X11GLXDrawableFactory$SharedResourceImplementation.createSharedResource(X11GLXDrawableFactory.java:326)
	at jogamp.opengl.SharedResourceRunner.run(SharedResourceRunner.java:297)
	at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NullPointerException
	at jogamp.opengl.GLContextImpl.makeCurrent(GLContextImpl.java:688)
	at jogamp.opengl.GLContextImpl.makeCurrent(GLContextImpl.java:580)
	at jogamp.opengl.x11.glx.X11GLXDrawableFactory$SharedResourceImplementation.createSharedResource(X11GLXDrawableFactory.java:297)
	... 2 more
    Exception in thread "Startup Class Loader" java.lang.InternalError: XXX0 profile[1]: GL3bc -> profileImpl GL4bc !!! not mapped 
	at com.jogamp.opengl.GLProfile.computeProfileMap(GLProfile.java:2047)
	at com.jogamp.opengl.GLProfile.initProfilesForDeviceCritical(GLProfile.java:1917)
	at com.jogamp.opengl.GLProfile.initProfilesForDevice(GLProfile.java:1866)
	at com.jogamp.opengl.GLProfile.initProfilesForDefaultDevices(GLProfile.java:1835)
	at com.jogamp.opengl.GLProfile.access$000(GLProfile.java:79)
	at com.jogamp.opengl.GLProfile$1.run(GLProfile.java:229)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.jogamp.opengl.GLProfile.initSingleton(GLProfile.java:215)
	at com.mathworks.hg.peer.JavaSceneServerPeer.initializeJOGL(JavaSceneServerPeer.java:147)
	at com.mathworks.hg.peer.JavaSceneServerPeer.<clinit>(JavaSceneServerPeer.java:117)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Unknown Source)
	at com.mathworks.mde.desk.StartupClassLoader.loadClass(StartupClassLoader.java:262)
	at com.mathworks.mde.desk.StartupClassLoader.access$900(StartupClassLoader.java:25)
	at com.mathworks.mde.desk.StartupClassLoader$2.run(StartupClassLoader.java:248)
	at java.lang.Thread.run(Unknown Source)

Additionally, the following messages are displayed in the Terminal output:

    libGL: pci id for fd 570: 1002:6819, driver radeonsi
    libGL: OpenDriver: trying /usr/lib/x86_64-linux-gnu/dri/tls/radeonsi_dri.so
    libGL: OpenDriver: trying /usr/lib/x86_64-linux-gnu/dri/radeonsi_dri.so
    libGL: Using DRI3 for screen 0

These messages don't indicate any troubles; it looks like the radeonsi_dri object is loaded successfully. When I try the same plot command as above, I receive the following message in Matlab:

    Caught unexpected fl::except::IInternalException

The figure shows an empty image; the plot has not been created.

Other Notes

  • I've also tried renaming the libgcc_s.so.1 library in Matlab's directory so that it uses the system version, but that doesn't change anything.
  • There are no errors (EE) in my Xorg.0.log file
  • I've successfully benchmarked OpenGL and the video card with glmark2, i.e., OpenGL runs very well with hardware accelleration on my machine, but Matlab has some issue.
  • Matlab R2016b ships with libGL.so.1.6.0 (in R2016b/sys/opengl/lib/glnxa64) but my OS version is libGL.so.1.2.0). Could this cause an issue? I tried renaming the libGL files shipped with Matlab to force it to use the system versions, but the error messages are still the same.
  • With the modified libstdc++ files, I can run Matlab in software openGL mode via the command matlab -softwareopengl. Plotting works fine, though Mesa spits out the warning/error, "User error: GL_INVALID_ENUM in glGetIntegerv(pname=GL_MAJOR_VERSION)"

  12 Comments

Good to know, Andrew. ...this is looking more and more like something got screwed up in the opensource driver stack early this year.

I did a few iterations of clean installs of Ubuntu 16.04 and 17.10, and all paths end up in the same failure state after running 'sudo apt update && sudo apt upgrade'. ...I have only new/clean installs to work with, and starting from a fresh Ubuntu 16.04 there are currently 200+ packages with available updates, thus potential culprits for screwing up opengl links (more experienced linux users surely know ways of narrowing that down...?).

As time passes and more [unfortunate] people run 'apt upgrades' that trigger this error , there seem to be more people slipping into the same non-functional state, so hopefully this will get sorted out soon.

I posted similar/cross-referenced information about this error in this Psychtoolbox forum message : https://groups.yahoo.com/neo/groups/psychtoolbox/conversations/messages/22674 So keep an eye out there, and we'll try to cross pollinate any viable solutions & workarounds as the come up.

Hello,

I can confirm this problem with Matlab R2017b on a Gentoo Linux installation. It looks like the problem is related to an update of the media-libs/mesa package (on Gentoo). They changed the naming of the renderer and the java opengl implementation in Matlab is not longer able to correctly identify it. Look here: https://jogamp.org/bugzilla/show_bug.cgi?id=1357#c5

A workaround is to downgrade the mesa package (for me it was from 17.2.8 to 17.1.10). After that, hardware acceleration works as expected. Anyways, I also would be interested in a solution (the java libraries shipped with Matlab have to be updated).

Best regards :)

PS: For Ubuntu, there is maybe a solution available, see here https://bugs.launchpad.net/ubuntu/+source/scilab/+bug/1742894

Moritz, you've nailed it! See the script(s) below for a fix you might be able to leverage on Gentoo.

Sign in to comment.

4 Answers

Answer by Andrew Cox on 9 Feb 2018
Edited by Andrew Cox on 9 Feb 2018
 Accepted Answer

The solution from Thad (below) works, but includes some unnecessary complexity if you're not a PsychToolbox User, so I'm summarizing the minimal set of steps below. Step #1 (below) need only be completed once, while steps 2, 3, 4 must be completed for each version of Matlab installed on your machine.

1. Install the bleeding-edge libjogl2 library:

 # Add Xenial-Proposed repository (necessary as of Feb. 2018)
 sudo sh -c "echo 'deb http://archive.ubuntu.com/ubuntu/ \
 xenial-proposed restricted main multiverse universe' \
 > /etc/apt/sources.list.d/xenial-proposed.list"
 sudo apt update -y
 # ONLY INSTALL THE FOLLOWING from xenial-proposed!
 # (you don't want to upgrade ALL your packages to bleeding-edge)
 sudo apt install -y libjogl2-java/xenial-proposed #includes libjogl2-jni
 # REMOVE XENIAL-PROPOSED to avoid unwanted updates to other packages
 sudo rm /etc/apt/sources.list.d/xenial-proposed.list
 sudo apt update -y

2. Rename C++ standard library files (repeated from question, above). This ensures that the graphics drivers are loaded with the same standard library as they need rather than the ones Matlab comes bundled with.

 cd $matlabroot/sys/os/glnxa64
 mv libstdc++.so.6 libstdc++.so.6.bak
 mv libstdc++.so.6.0.20 libstdc++.so.6.0.20.bak

3. Change the classpath.txt and librarypath.txt files in $MATLABROOT/toolbox/local. Using my original paths, this path expands to /opt/local/MATLAB/R2017b/toolbox/local

  • In classpath.txt, find and comment out the following two lines:
 $matlabroot/java/jarext/gluegen-rt.jar
 $matlabroot/java/jarext/jogl-all.jar

And replace them with the following

 /usr/share/java/jogl2.jar
 /usr/share/java/gluegen2-rt.jar
  • In librarypath.txt, add a new line at the end of the file with
 /usr/lib/jni

4. According to the original script provided above, Matlab should now run without any errors. However, to be sure that the default included libraries do not interfere, rename the following libraries in $matlabroot/bin/glnxa64:

 cd $matlabroot/bin/glnxa64
 sudo mv libjogl_desktop.so libjogl_desktop.so.bak
 sudo mv libgluegen-rt.so libgluegen-rt.so.bak
 sudo mv libnativewindow_awt.so libnativewindow_awt.so.bak
 sudo mv libnativewindow_x11.so libnativewindow_x11.so.bak

Finally, in $matlabroot/toolbox/local/classpath rename the 3p_jogl file:

 cd $matlabroot/toolbox/local/classpath
 sudo mv 3p_jogl.jcp 3p_jogl.jcp.bak

And that's it! Problem solved! Hopefully those Xenial-Proposed packages will roll out to the stable branch in the not-too-distant future.

  1 Comment

Making these changes on Fedora 27 results in an error:

    Exception in thread "Startup Class Loader" java.lang.NoClassDefFoundError: com/jogamp/opengl/GLException
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Unknown Source)
	at com.mathworks.mde.desk.StartupClassLoader.loadClass(StartupClassLoader.java:262)
	at com.mathworks.mde.desk.StartupClassLoader.access$900(StartupClassLoader.java:25)
	at com.mathworks.mde.desk.StartupClassLoader$2.run(StartupClassLoader.java:248)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.jogamp.opengl.GLException
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	... 6 more

Additionally, for the benefit of anyone else reading this, the required package for Fedora is jogl2 from the rawhide repository. The stable version is not recent enough currently. You will need to download the source package and use rpmbuild --rebuild to build the package for a stable version of Fedora. It will probably give you an error the first time you try to rebuild it, just install the packages listed using dnf and it should compile fine.

About the exception message, can anyone figure out what's wrong? Plotting fails with another error

    Caught unexpected fl::except::IInternalException

Edit: I double checked and realized I actually didn't have the .jar files in my /usr/share directory. After copying the .jar files from their correct directories, I get

    Warning: librarypath.txt in the startup directory no longer supported. See The Static Path.
Exception in thread "Startup Class Loader" java.lang.UnsupportedClassVersionError: com/jogamp/opengl/GLException : Unsupported major.minor version 52.0
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(Unknown Source)
	at java.security.SecureClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.access$100(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Unknown Source)
	at com.mathworks.mde.desk.StartupClassLoader.loadClass(StartupClassLoader.java:262)
	at com.mathworks.mde.desk.StartupClassLoader.access$900(StartupClassLoader.java:25)
	at com.mathworks.mde.desk.StartupClassLoader$2.run(StartupClassLoader.java:248)
	at java.lang.Thread.run(Unknown Source)

I also get this error if I change the paths in classpath.txt to the original paths of the .jar files.

Sign in to comment.


Answer by Thad C
on 9 Feb 2018
Edited by Thad C
on 9 Feb 2018

There's a workaround solution developing on the Psychtoolobx forum in topic 22674: OpenGL problem in Linux

It's a bug in jogl related to detecting the Mesa drivers by string matching for "Gallium" in the glRenderer string, while the Mesa ppl apparently recently implemented a change in how the open-source driver/version is reported. (...or a rough synopsis of this bug report )

Seems like Matlab R2017b might be doing something similarly rudimentary, because my install still seems to be falling back to OpenGL 3.0 (compatibility profile) instead of enabling the full 4.x core version available from my gfx card. ...tbd.

-Thad.

Save the following as a shell script, then execute from terminal in Ubuntu 16.04

(credit to Andreas Widmann's post on the PTB forum for this workaround...I'm just the messenger)

#!/bin/bash
# ubuntuFixJogl2018.sh
# For Ubuntu 16.04 LTS
# Install Psychtoolbox from NeuroDebian, fix jogl install & matlab links
#
# Script based on proposed solution from Andreas Widmann
# for issue reported in:
# https://groups.yahoo.com/neo/groups/PSYCHTOOLBOX/conversations/topics/22674
#
# blame t.b.czuba 2018-02-09
# verify that the computer is running a Debian derivative
if ! command -v dpkg &> /dev/null; then
    echo "this script is meant to be run on an Ubuntu system"
    exit 1
fi
cd $HOME
# global variables
MATLABROOT="/usr/local/MATLAB/R2017b"  # ...update with your Matlab version/location as needed
TOOLROOT="$HOME/MLtoolbox"
echo "you are logged in as user $USER"
echo "matlab toolboxes will be installed in $TOOLROOT"
# configuration
################################################################################
    # Add repositories for Psychtoolbox-3 installation (via NeuroDebian)
    #   - copy-pasta from 'all software' repository for Ubuntu 16.04
    #   - for different OS options see: http://neuro.debian.net/install_pkg.html?p=matlab-psychtoolbox-3-nonfree
    wget -O- http://neuro.debian.net/lists/xenial.us-nh.full | sudo tee /etc/apt/sources.list.d/neurodebian.sources.list
    sudo apt-key adv --recv-keys --keyserver hkp://pool.sks-keyservers.net:80 0xA5D32F012649A5A9
    sudo apt update
      # Install psychtoolbox
      sudo apt install -y matlab-psychtoolbox-3-nonfree
      # Add directories for Matlab toolboxes
      sudo mkdir -v -m 777 "$TOOLROOT"
      sudo chown -v "$USER":"$USER" "$TOOLROOT"
      # symlink default PTB install location to ~/MLtoolbox
      sudo ln -v -s /usr/share/psychtoolbox-3 $TOOLROOT/Psychtoolbox # def PTB location hardcoded by neurodebian
# Fix jogl errors in Matlab (as of Feb 7, 2018)
###################
# (1)
		# ADD XENIAL-PROPOSED repository (necessary as of Feb. 2018)
		sudo sh -c "echo 'deb http://archive.ubuntu.com/ubuntu/ \
		xenial-proposed restricted main multiverse universe' \
		> /etc/apt/sources.list.d/xenial-proposed.list"
		sudo apt update -y
	
	# ONLY INSTALL THE FOLLOWING from xenial-proposed!
	#   (...you don't want to upgrade ALL your packages with bleeding-edge versions)
	sudo apt install -y libjogl2-java/xenial-proposed
	# sudo apt install -y libjogl2-jni/xenial-proposed #...this is included with libjogl2-java in xenial-proposed
	
		# REMOVE XENIAL-PROPOSED repository
		sudo rm /etc/apt/sources.list.d/xenial-proposed.list
		sudo apt update -y
		
###################
# (2)
# Change the classpath.txt and librarypath.txt files in $MATLABROOT/toolbox/local.
#
# In classpath.txt find and comment or delete the following two lines (~line 450):
# $MATLABROOT/java/jarext/gluegen-rt.jar
# $MATLABROOT/java/jarext/jogl-all.jar
# and add instead:
# /usr/share/java/jogl2.jar
# /usr/share/java/gluegen2-rt.jar
#
# In librarypath.txt add at the end of the file (incl. a newline):
# /usr/lib/jni
###################
# (3)
# Now it should already work. To be sure that the MATLAB included libraries
# do not interfere I additionally renamed the following libraries in matlabroot/bin/glnxa64:
# libjogl_desktop.so
# libgluegen-rt.so
# libnativewindow_awt.so
# libnativewindow_X11.so
# which are now provided by the packages installed in (1).
cd $MATLABROOT/bin/glnxa64
sudo mv -v ./libjogl_desktop.so ./libjogl_desktop.so.bak
sudo mv -v ./libgluegen-rt.so ./libgluegen-rt.so.bak
sudo mv -v ./libnativewindow_awt.so ./libnativewindow_awt.so.bak
sudo mv -v ./libnativewindow_x11.so ./libnativewindow_x11.so.bak
#
# In matlabroot/toolbox/local/classpath I additionally renamed
# 3p_jogl.jcp
# to be shure it is not added back to classpath in case it is regenerated.
cd $MATLABROOT/toolbox/local/classpath
sudo mv -v ./3p_jogl.jcp ./3p_jogl.jcp.bak
printf "
###################
#   PTB install & jogl error fix installed
###################
#
# Must manually apply the following edits w/in your Matlab installation
# To determine path to your \$MATLABROOT, fire up Matlab, then enter
#       matlabroot
# in the command window.
#
###################
# Change the classpath.txt and librarypath.txt files in \$MATLABROOT/toolbox/local.
#
# In classpath.txt find and comment or delete the following two lines (~line 450):
#   \$MATLABROOT/java/jarext/gluegen-rt.jar
#   \$MATLABROOT/java/jarext/jogl-all.jar
# and add instead:
#   /usr/share/java/jogl2.jar
#   /usr/share/java/gluegen2-rt.jar
#
# In librarypath.txt add at the end of the file (incl. a newline):
#   /usr/lib/jni
###################
"

  1 Comment

Great work! This completely solved the problem! I've removed the PsychToolbox-specific steps in the accepted answer to generalize the procedure.

Sign in to comment.


Answer by Kaiyuh
on 30 Mar 2018
Edited by Kaiyuh
on 30 Mar 2018

It is a problem caused by the incompatibility of the graphics card driver, I am not gonna explain what the problem is. The simplest and the best solution for me was:

 1. Open Matlab
 2. Enter "opengl('save','software')"
 3. Restart Matlab

It's done

  3 Comments

Switching to software rendering did not solve all problems in this case (e.g. plots were not working). Please be aware that the described problem is both

  1. Linux specific: An update of the mesa library changed the naming behaviour of the renderer
  2. AMD specific: Only specific AMD graphic cards are affected

It is caused by the really bad way of jogl, the Java OpenGL library, how it tries to find the 3D renderer. The steps above to exchange the MATLAB jogl-library with a fixed version are described above and necessary in this specific case.

And by the way, I did not spend many €€€s for a graphics card just to not use it whenever possible :D

Agreed - Switching to software rendering can side-step the problem for general Matlab use, but many of the plotting capabilities are reduced to uselessness with the lower-quality software OpenGL.

Sign in to comment.


Answer by Thad C
on 2 Apr 2018

Agreed, changing to OpenGL software rendering is not at all a 'solution'.

Couple updates on the current state of the fix for this error:

  • Updates to the functional version of the libjogl2-java package have been accepted into the standard xenial-updates repo (...non-jargony: no longer necessary to add/remove non-standard update source) Updated script here : https://github.com/czuba/sysconfig/raw/master/ubuntuFixJogl_ML2018a.sh
  • Unfortunately these jogl errors persist in Matlab 2018a. Hard to imagine releasing a new version knowing it creates a wall of red error text at startup on the most widely used Linux distro out there...
  • Notably, the "matlab-support" package necessary to fix other persistent matlab errors on Linux is not currently compatible with Matlab 2018a (as of Mar 28, 2018)

  2 Comments

Hi, For some reason your script doesn't recognize Linux Mint 18.3 as Debian Derivative. Though it can be treated as Ubuntu 16.04. Any idea how to fix this?

Royi Avital, see my accepted answer for the sequence of commands without a script. If you're confident you can proceed as on a Debian derivative, just execute the commands.

Sign in to comment.