MATLAB

From FarmShare

(Difference between revisions)
Jump to: navigation, search
 
(33 intermediate revisions not shown)
Line 1: Line 1:
-
== getting list of available matlab versions ==
+
[https://www.mathworks.com/products/matlab.html MATLAB] is a multi-paradigm numerical computing environment and programming language.
-
You can run module spider matlab to get a list of available matlab versions
+
== Getting Started  ==
 +
 
 +
Run <code>module avail</code> to get a list of all software modules, or <code>module spider</code> to get a list of available versions of MATLAB.
<source lang="sh">
<source lang="sh">
-
$ module spider matlab
+
module spider matlab      
-
  --------------------------------------------------------------------------------------------------------------------
+
--------------------------------------------------------------------------------------------------------------------------------
   matlab:
   matlab:
-
  --------------------------------------------------------------------------------------------------------------------
+
--------------------------------------------------------------------------------------------------------------------------------
     Description:
     Description:
-
       MATLAB and toolboxes
+
       A multi-paradigm numerical computing environment and programming language.
     Versions:
     Versions:
-
         matlab/r2012b
+
         matlab/r2016b
-
         matlab/r2013a
+
         matlab/r2017a
-
 
+
-
  --------------------------------------------------------------------------------------------------------------------
+
-
  To find detailed information about matlab please enter the full name.
+
-
  For example:
+
-
 
+
-
    $ module spider matlab/r2013a
+
-
  --------------------------------------------------------------------------------------------------------------------
+
-
 
+
 +
--------------------------------------------------------------------------------------------------------------------------------
</source>
</source>
-
 
+
Use <code>module load</code> to set up your environment, and then run <code>matlab</code> to start an interactive session.
-
==first steps==
+
-
 
+
-
Per [[FarmShare software]] try something like:
+
<source lang="sh">
<source lang="sh">
-
module avail
 
module load matlab
module load matlab
 +
matlab
</source>
</source>
-
If you're running MATLAB for the first time, it'll try to write some stuff into $HOME/.matlab so make sure you have your [[AFS]] tokens or else MATLAB will crash with an undecipherable error message.
+
If you have not configured a [https://srcc.stanford.edu/farmshare2/connecting remote display] MATLAB will start in text-mode, but the MATLAB desktop is also supported.
-
 
+
-
As of 9/22/2013 this works on the corn.stanford.edu cluster but NOT on the myth.stanford.edu cluster.
+
-
 
+
-
==interactive MATLAB==
+
-
 
+
-
 
+
-
[[Matlab-interactive]]
+
-
 
+
-
[[Matlab-parallel]]
+
-
 
+
-
 
+
-
== example single matlab file run via qsub  ==
+
-
 
+
-
Here's our helloworld.m:
+
-
<source lang="m">disp('Hello World');
+
-
</source>
+
-
Here's a command to run that non-interactively:
+
-
 
+
-
  matlab -nodesktop < helloworld.m
+
-
 
+
-
We want to run this same command via the job scheduling system. Let's write a job script (save this as matlab_example.script)
+
<source lang="sh">
<source lang="sh">
-
#!/bin/bash
+
ssh -X sunetid@rice.stanford.edu
-
 
+
-
#$ -N matlab_example
+
-
#$ -m bes
+
-
#$ -M chekh@stanford.edu
+
-
 
+
module load matlab
module load matlab
-
matlab -nodesktop < /farmshare/software/examples/matlab/helloworld.m
+
matlab &
</source>
</source>
-
Submit the script:
+
== Running MATLAB in Batch Mode ==
-
  qsub matlab_example.script
+
If you have prepared MATLAB code as a program (<code>.m</code>) file you can run it non-interactively.
-
Look at the job status:
+
<source lang="sh">matlab -nodesktop < program.m</source>
-
  qstat
+
You can use this method to submit a MATLAB job to a compute node, and you can even include MATLAB code in-line in an <code>sbatch</code> script.
-
You should get output file like matlab_example.oXXXXX
+
<source lang=sh>
-
<source lang="sh">Warning: No display specified.  You will not be able to display graphics on the screen.
+
-
Warning: No window system found.  Java option 'MWT' ignored
+
-
 
+
-
                            < M A T L A B (R) >
+
-
                  Copyright 1984-2011 The MathWorks, Inc.
+
-
                    R2011b (7.13.0.564) 64-bit (glnxa64)
+
-
                              August 13, 2011
+
-
 
+
-
+
-
To get started, type one of these: helpwin, helpdesk, or demo.
+
-
For product information, visit www.mathworks.com.
+
-
+
-
>> Hello World
+
-
</source>
+
-
 
+
-
== example second matlab file run via qsub  ==
+
-
 
+
-
Now we want to make sure we avoid AFS.
+
-
 
+
-
Here's our helloworld.m:
+
-
<source lang="m">disp('Hello World');
+
-
</source>
+
-
Here's a command to run that non-interactively:
+
-
 
+
-
  matlab -nodesktop < helloworld.m
+
-
 
+
-
We want to run this same command via the job scheduling system. Let's write a job script.
+
-
<source lang="sh">
+
#!/bin/bash
#!/bin/bash
-
 
-
#$ -N matlab_example
 
-
#$ -m bes
 
-
#$ -M chekh@stanford.edu
 
module load matlab
module load matlab
-
matlab -nodesktop < /farmshare/software/examples/matlab/helloworld.m
+
matlab -nodesktop << EOF
-
</source>
+
   % MATLAB code
-
 
+
-
Submit the script:
+
-
 
+
-
   cd /farmshare/user_data/$USER
+
-
  qsub -cwd matlab_example.script
+
-
 
+
-
Look at the job status:
+
-
 
+
-
  qstat
+
-
 
+
-
You should get output file like matlab_example.oXXXXX in your current directory
+
-
<source lang="sh">Warning: No display specified.  You will not be able to display graphics on the screen.
+
-
Warning: No window system found.  Java option 'MWT' ignored
+
-
 
+
-
                            < M A T L A B (R) >
+
-
                  Copyright 1984-2011 The MathWorks, Inc.
+
-
                    R2011b (7.13.0.564) 64-bit (glnxa64)
+
-
                              August 13, 2011
+
-
 
+
-
+
-
To get started, type one of these: helpwin, helpdesk, or demo.
+
-
For product information, visit www.mathworks.com.
+
-
+
-
>> Hello World
+
-
</source>
+
-
 
+
-
=PCT=
+
-
We have the Parallel Computing Toolbox, you can use that to parallelize your job across multiple cores in a single machine.
+
-
 
+
-
Here's how to write a job using MDCS: http://docs.uabgrid.uab.edu/wiki/MatLab_CLI#Parallel_MATLAB
+
-
 
+
-
You can use the "maxNumCompThreads" command (deprecated) to see how many parallel threads you can run. I get "24" on barley, or "8" on corn.
+
-
 
+
-
== simple PCT run  on corn ==
+
-
 
+
-
<source lang="sh">matlab -nodesktop -r 'maxNumCompThreads'
+
-
 
+
-
>> matlabpool ( 'open', 'local', 8)
+
-
Starting matlabpool using the 'local' configuration ... connected to 8 labs.
+
-
>>
+
-
>> matlabpool size
+
-
 
+
-
ans =
+
-
 
+
-
    8
+
-
 
+
-
>> feature('numCores')
+
-
 
+
-
ans =
+
-
 
+
-
    8
+
-
 
+
-
 
+
-
Then use 'parfor' instead of 'for'.
+
-
</source>
+
-
matlabpool of size 0 and size 1 are effectively the same, except the latter uses a PCT toolbox license.
+
-
 
+
-
Here are some training slides and example code that I copied from http://www.osc.edu/~samsi/sc11edu/
+
-
 
+
-
*http://stanford.edu/~chekh/matlab.tar
+
-
 
+
-
 
+
-
== matlab with matlabpools on the barley ==
+
-
 
+
-
A good way to speed up your matlab code on barley is to submit your matlab job with the shm parallel environment.  The parallel environment is specified with -pe shm 4 below.  The number you specify after shm will be the number of cores allocated to your job (all from the same host).  In this case we are asking for 4 cores, which then become workers in a matlab pool.
+
-
 
+
-
There is a problem that frequently occurs with matlab if you run many jobs which all use matlab pools.  Matlab is naive and does not handle having multiple instances running at the same time.  To allow many matlabpools to be run simulataneously, use the following code to setup your pool.  You may substitute any number between 1 and 12 for the number of cores (12 being the max matlab will let us use with current license).
+
-
 
+
-
 
+
-
<source lang="m">
+
-
#!/bin/bash
+
-
 
+
-
#$ -N matlabfft
+
-
#$ -S /bin/bash
+
-
#$ -pe shm 4
+
-
#$ -l mem_free=500M
+
-
#$ -cwd
+
-
 
+
-
hostname
+
-
 
+
-
echo ""
+
-
echo ""
+
-
echo ""
+
-
 
+
-
module load matlab
+
-
 
+
-
matlab -nodesktop <<EOF
+
-
cluster = parcluster('local')
+
-
tmpdirforpool = tempname
+
-
mkdir(tmpdirforpool)
+
-
cluster.JobStorageLocation = tmpdirforpool
+
-
 
+
-
msg = sprintf('setting matlabpool to %s', getenv('NSLOTS'))
+
-
cluster.NumWorkers = str2num(getenv('NSLOTS'))
+
-
 
+
-
matlabpool(cluster)
+
-
 
+
-
cluster
+
-
 
+
-
a = ones(1000,1000)
+
-
 
+
-
disp('begin running parfor')
+
-
parfor i = 1:10000
+
-
  b=ones./a
+
-
end
+
-
 
+
-
disp('done running parfor')
+
-
 
+
-
matlabpool close
+
EOF
EOF
</source>
</source>
 +
== Using MDCS ==
 +
MATLAB Distributed Computing Server is supported, and you can submit jobs using up to 127 workers. To set up the cluster, load the <code>matlab</code> module, start MATLAB, and run <code>configCluster</code>. This only needs to be done once.
-
= tomlab =
+
Once the cluster has been configured, commands like <code>parcluster</code> should use cluster (rather than local) workers by default. You can get a handle to the cluster using the <code>parcluster</code> command, submit jobs using <code>batch</code> (which returns a handle to the job), and query job state and results using <code>State</code> and <code>fetchOuputs</code>.
-
We have a TOMLAB license.  To start it up:
+
-
# launch matlab r2013a
+
-
# cd to /farmshare/software/non-free/MATLAB-R2013a/tomlab
+
-
# startup
+
-
 
+
-
 
+
-
example session:
+
-
 
+
-
<source lang="m">
+
-
$ module load matlab
+
-
$ matlab -nodesktop
+
-
Warning: No display specified.  You will not be able to display graphics on the screen.
+
-
Warning: No window system found.  Java option 'MWT' ignored.
+
-
 
+
-
                                                < M A T L A B (R) >
+
-
                                      Copyright 1984-2013 The MathWorks, Inc.
+
-
                                        R2013a (8.1.0.604) 64-bit (glnxa64)
+
-
                                                February 15, 2013
+
-
 
+
-
No window system found. Java option 'MWT' ignored.
+
-
+
-
To get started, type one of these: helpwin, helpdesk, or demo.
+
-
For product information, visit www.mathworks.com.
+
-
+
-
>> cd /farmshare/software/non-free/MATLAB-R2013a/tomlab
+
-
>> startup
+
-
The TOMLAB v7.9 directory is /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab
+
-
Found the base path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/base
+
-
Found the cgo path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/cgo
+
-
Found the testprob path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/testprob
+
-
Found the examples path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/examples
+
-
Found the optim path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/optim
+
-
+
-
Warning - optimization toolbox drop-in replacements
+
-
Remove tomlab\optim from path to disable
+
-
+
-
Found the mex path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/mex
+
-
Found the splines path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/splines
+
-
Found the quickguide path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/quickguide
+
-
Found the MAD path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/mad
+
-
Found the lib path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/lib
+
-
Found the usersguide path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/usersguide
+
-
Found the ampl path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/ampl
+
-
Found the common path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/common
+
-
Found the finance path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/finance
+
-
Found the modellib path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/modellib
+
-
MAD successfully installed
+
-
Found the PROPT path as /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/propt
+
-
Found the TOMSYM path as /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/tomsym
+
-
Found the modellib path as  /srv/zfs01/software/non-free/MATLAB-R2013a/tomlab/modellib
+
-
+
-
Run tomlablic to display license information
+
-
Done with setting TOMLAB paths
+
-
>> cd quickguide
+
-
>> nllsQG 
+
-
===== * * * =================================================================== * * *
+
-
TOMLAB - Stanford University Ac. department  602044. Valid to 2100-01-01
+
-
=====================================================================================
+
-
Problem: ---  1: Gisela                        f_k    103.648860373704110316
+
-
                                              f(x_0)    103.652176276833472457
+
-
 
+
-
Solver: clsSolve.  EXIT=0.  INFORM=1.
+
-
Fletcher-Xu Hybrid Method. Modified Gauss-Newton - BFGS
+
-
Optimal solution found   
+
-
Iteration points are close
+
-
 
+
-
ResEv  126 JacEv  118 Iter  11
+
-
CPU time: 0.480000 sec. Elapsed time: 0.932415 sec.
+
-
>>
+
 +
<source lang="matlab">
 +
c = parcluster;
 +
j = c.batch(@pwd, 1, {}, 'Pool', 4);
 +
j.State
 +
j.fetchOutputs{:}
</source>
</source>
 +
For a more detailed walkthrough see [[Media:Getting_Started_with_Serial_and_Parallel_MATLAB.pdf|Getting Started with Serial and Parallel MATLAB]].
-
Search the farmshare-discuss archives for posts about&nbsp;[https://mailman.stanford.edu/mailman/swish?query=Matlab&submit=Search+farmshare-discuss+%21&listname=farmshare-discuss&metaname=swishdefault&sort=unixdate Matlab].
+
Please note that, while multi-node jobs are supported, FarmShare does not have a fast interconnect, so code that requires a lot of communication or synchronization between workers may not fully realize any expected improvement in performance. You may want to restrict your MDCS jobs to a single node, or to 15 (or fewer) workers, when possible.

Latest revision as of 16:46, 15 February 2018

MATLAB is a multi-paradigm numerical computing environment and programming language.

Getting Started

Run module avail to get a list of all software modules, or module spider to get a list of available versions of MATLAB.

module spider matlab       

--------------------------------------------------------------------------------------------------------------------------------
  matlab:
--------------------------------------------------------------------------------------------------------------------------------
    Description:
      A multi-paradigm numerical computing environment and programming language.

     Versions:
        matlab/r2016b
        matlab/r2017a

--------------------------------------------------------------------------------------------------------------------------------

Use module load to set up your environment, and then run matlab to start an interactive session.

module load matlab
matlab

If you have not configured a remote display MATLAB will start in text-mode, but the MATLAB desktop is also supported.

ssh -X sunetid@rice.stanford.edu
module load matlab
matlab &

Running MATLAB in Batch Mode

If you have prepared MATLAB code as a program (.m) file you can run it non-interactively.

matlab -nodesktop < program.m

You can use this method to submit a MATLAB job to a compute node, and you can even include MATLAB code in-line in an sbatch script.

#!/bin/bash

module load matlab
matlab -nodesktop << EOF
  % MATLAB code
EOF

Using MDCS

MATLAB Distributed Computing Server is supported, and you can submit jobs using up to 127 workers. To set up the cluster, load the matlab module, start MATLAB, and run configCluster. This only needs to be done once.

Once the cluster has been configured, commands like parcluster should use cluster (rather than local) workers by default. You can get a handle to the cluster using the parcluster command, submit jobs using batch (which returns a handle to the job), and query job state and results using State and fetchOuputs.

c = parcluster;
j = c.batch(@pwd, 1, {}, 'Pool', 4);
j.State
j.fetchOutputs{:}

For a more detailed walkthrough see Getting Started with Serial and Parallel MATLAB.

Please note that, while multi-node jobs are supported, FarmShare does not have a fast interconnect, so code that requires a lot of communication or synchronization between workers may not fully realize any expected improvement in performance. You may want to restrict your MDCS jobs to a single node, or to 15 (or fewer) workers, when possible.

Personal tools
Toolbox
LANGUAGES