Documentation:Matlab

I don't think there's a need to tell you what Matlab is. But you should know that it has very good (as in: it's easy and it works) GPU support. Here, you'll discover how you can take advantage of the Electra cluster without having to learn CUDA.

This documentation is valid for

Submitting a matlab job to Electra
Here is a sample PBS script to submit a matlab script called comparecuda.m:

# module load matlab/R2011b cd $PBS_O_WORKDIR echo "running on: " hostname echo "--" matlab -nodisplay -nojvm -nodesktop -singleCompThread -r comparecuda
 * 1) !/bin/bash -l
 * 1) PBS -l select=1:ncpus=8
 * 2) PBS -l walltime=01:00:00
 * 3) PBS -q electest
 * 4) PBS -N matlab_compare
 * 5) PBS -j oe

In order to run matlab with the script above, place it in a file, say "matlab_test.job", and submit it to the batch system by:

qsub [options] matlab_test.job

Using GPUs made easy
Some standard functions are overloaded to work on GPU arrays. A notable example is arrayfun. Here is a function we will pass as an argument to arrayfun:

% file: amxbpc.m function r = amxbpc(a, b, c) r = (a .* b) + c;

Define standard MATLAB matrices h_a, h_b and h_c. Usually, we apply a function with

h_r=arrayfun(@amxbpc, h_a, h_b, h_c);

To execute the function on the GPU, first copy the variables to the GPU memory with

d_a = gpuArray(h_a)

and then apply the funcion with

d_r=arrayfun(@amxbpc, d_a, d_b, d_c);

copy back the result with

h_r= gather(d_r);

Hereafter an exaple comparing the timings of the same operation on CPU and on GPU:

% file: comparecuda.m fprintf('GPU init '); tic d_in  = gpuArray(rand(2))/100 + 0.995; toc for j=10:1:13 i = 2^j; fprintf('%d without CUDA ', i); h_a = ones(i)*3; % i x i matrix h_b  = rand(i)/100 + 0.995; h_c = rand(i)/50 - 0.01; tic h_r= arrayfun(@amxbpc, h_a, h_b, h_c); toc fprintf('%d with CUDA ', i); tic d_b  = gpuArray(h_b); d_c = gpuArray(h_c); d_a = gpuArray(h_a); d_r= arrayfun(@amxbpc, d_a, d_b, d_c); h_r= gather(d_r); toc end

The results of the comparison on Electra are

GPU Initialization time: 3.248233 seconds



MEX files
You may be interested in reading the SSH section as a preliminary to this one.

Since MATLAB is not installed on Electra's frontend, you have to use interactive mode on a node to compile MEX files. Run the following command on the frontend:

qsub -q electest -I

Once you're logged into the node, type:

cd /scratch/${USER}             # home directory is read-only! module load matlab/R2011b module load gcc/4.3.4           # preferred by this MATLAB version matlab

and then do your MEX thing.

According to the version of MATLAB used, "mex" may refuse the "//"-style comments in your C/C++ files. You can remove them seamlessly with a script such as:

[jbernard@electra jbernard]$ cat RemoveBarBarComments.sh for FILE in $(ls *.c); do    echo "--> FILE : ${FILE}" NEW_FILE=${FILE%.c}_new.c    sed 's|//||g' ${FILE} > ${NEW_FILE} ls -sal ${NEW_FILE} echo done
 * 1) !/bin/bash
 * 1) This is for a ".c" extension, adapt to your needs

MatlabMPI
It is to be noted that "rsh" is not supported on our clusters, you should use "ssh", otherwise you'll get messages such as:

Failed to add the host to the list of known hosts (/home/jmenu/.ssh/known_hosts)

You should thus adapt "MatMPI_Comm_settings.m" around line 43 in your own environment to contain:

machine_db_settings.remote_launch = ' ssh '; ... ... ... machine_db_settings.remote_flags = ' -n ';

This documentation is a first attempt and is subject to enhancements!