MATLAB: Hardware OpenGL Failure on Ubuntu 16.04.3 – Can not initialize shared resources

driversgraphicslinuxMATLABopenglUbuntu

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)"

Best 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 libjogl2 library (Updated: no longer requires bleeding-edge version):
# Add libjogl2-java
sudo apt update -y
sudo apt install -y libjogl2-java
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.