# MATLAB X-Forwarding, Command Line, and PBS Scripts

Recently it has been noticed that MATLAB bench suite fails when forwarding (by "fails" I mean the applications dies horribly) OpenGL commands. This is not a problem unique to MATLAB, but rather to a range of applications, however there is a simple workaround. My experience is that this a fairly recent problem too, so I shake my fist in futility at certain video card manufacturers who harming scientific research, dammit.

Anyway, the workaround is fairly simple - just run indirect rendering instead of direct, and transmit the OpenGL to the X-server instead, which then calls the hardware. It's not as fast, but it does work.

So for example:

$ module load matlab

$ LIBGL_ALWAYS_INDIRECT=1 matlab

The MATLAB GUI will start on the remote screen, and you can run 'bench' without further problems.

A further option however is to include this directly in a MATLAB environment module with the line:

`set-alias xmatlab "source /usr/local/matlab/scripts/xmatlab.sh"`

The actual xmatlab.sh script is just:

#!/bin/sh

LIBGL_ALWAYS_INDIRECT=1 matlab

So the actual user would simply run xmatlab. It's a few less keystrokes which adds up if you have several hundred users on your system. Also, it's *neat*.

$ module load matlab

$ xmatlab

On a completely different tangent, some users prefer to run their MATLAB entirely on the command line. This, of course, means that they don't get the pretty graphics but you're just into crunching the numbers right here, right now, this can be a good choice.

$ module load matlab

$ matlab -nodisplay -nojvm

Finally, if you wish to run matlab as a PBS job, the following template, the from Pennsylvania State University is an example.

The file matlab.pbs

#!/bin/sh

#PBS -l nodes=1:ppn=1

#PBS -l walltime=00:10:00

module load matlab

cd $PBS_O_WORKDIR

matlab -nojvm -nodisplay -nosplash

The matlab script for a simple polar plot, polar-plot.m:

angle = 0:.1*pi:3*pi;

radius = exp(angle/20);

polar(angle,radius),...

title('An Example Polar Plot'),...

grid

print -deps polar-plot.ps;

quit;

Launch with `ghostscript polar-plot.ps`

.

- lev_lafayette's blog
- Log in to post comments

## Comments

## There has been a couple of

There has been a couple of requests to elaborate the issue concerning LIBGL_ALWAYS_INDIRECT=1. This followed a similar problem with the view program for mrtrix. This is a superb Australian-made, scientific application in its early days, but with an active community. From its website:

The problem - and solution for mrview to operate as expected is the same as the Matlab solution, i.e., add in the environment module and with a minimum script.

set-alias xmrview "source /usr/local/mrtrix/scripts/xmrview.sh"

#!/bin/sh

LIBGL_ALWAYS_INDIRECT=1 matlab

Indirect rendering means that OpenGL commands are transmitted to the X-server. Direct rendering means that application accesses the hardware directly. This is usually faster. However OpenGL is designed to work over a network (such as, ssh -X connections), and these indirect network connections are better.