Changes between Initial Version and Version 1 of Workshops/IntroToHpc2015/Matlab


Ignore:
Timestamp:
10/12/15 15:56:58 (9 years ago)
Author:
pdejesus
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Workshops/IntroToHpc2015/Matlab

    v1 v1  
     1[[PageOutline]]
     2= MATLAB =
     3MATLAB (matrix laboratory) is a proprietary programming language developed by [http://www.mathworks.com/ MathWorks], MATLAB allows matrix manipulations,
     4plotting of functions and data, implementation of algorithms, and creation of user interfaces.
     5
     6You can run your Matlab codes on Cypress clusters but you can't use GUI(Graphical User Interface) on computing nodes.
     7
     8== Running MATLAB interactively ==
     9Start an interactive session,
     10{{{#!bash
     11[fuji@cypress2 ~]$ idev
     12Requesting 1 node(s)  task(s) to normal queue of defq partition
     131 task(s)/node, 20 cpu(s)/task, 2 MIC device(s)/node
     14Time: 0 (hr) 60 (min).
     15Submitted batch job 47343
     16JOBID=47343 begin on cypress01-063
     17--> Creating interactive terminal session (login) on node cypress01-063.
     18--> You have 0 (hr) 60 (min).
     19Last login: Mon Jun  8 20:18:50 2015 from cypress1.cm.cluster
     20}}}
     21Load the module
     22{{{#!bash
     23[fuji@cypress01-063 ~]$ module load matlab
     24}}}
     25Run MATLAB on the command-line window,
     26{{{#!bash
     27[fuji@cypress01-063 ~]$ matlab
     28MATLAB is selecting SOFTWARE OPENGL rendering.
     29
     30                                                                                         < M A T L A B (R) >
     31                                                                               Copyright 1984-2015 The MathWorks, Inc.
     32                                                                               R2015a (8.5.0.197613) 64-bit (glnxa64)
     33                                                                                          February 12, 2015
     34
     35
     36To get started, type one of these: helpwin, helpdesk, or demo.
     37For product information, visit www.mathworks.com.
     38
     39
     40        Academic License
     41
     42>>
     43}}}
     44You will get to the MATLAB command-line and can run MATLAB code here but no graphics.
     45
     46
     47== Running MATLAB in a batch mode ==
     48You can also submit your MATLAB job to the batch nodes (compute nodes) on Cypress. To do so, please first make sure that the MATLAB module has been loaded, and then launch "matlab" with the "-nodesktop -nodisplay -nosplash" option as shown in the sample SLURM job script below.
     49
     50{{{#!bash
     51#!/bin/bash
     52#SBATCH --qos=normal            # Quality of Service
     53#SBATCH --job-name=matlab     # Job Name
     54#SBATCH --time=24:00:00         # WallTime
     55#SBATCH --nodes=1               # Number of Nodes
     56#SBATCH --ntasks-per-node=1     # Number of tasks (MPI processes)
     57#SBATCH --cpus-per-task=1       # Number of threads per task (OMP threads)
     58
     59module load matlab
     60matlab -nodesktop -nodisplay -nosplash < mymatlabprog.m
     61}}}
     62
     63
     64=== Running MATLAB in Parallel with Multithreads ===
     65MATLAB supports multithreaded computation for a number of functions and expressions that are combinations of element-wise functions.
     66These functions automatically execute on multiple threads if data size is large enough.
     67Note that on Cypress, in default, MATLAB runs with a single threads, and you have to explicitly specify the number of threads in your code.
     68For example,
     69{{{#!matlab
     70% Matlab Test Code "FuncTest.m"
     71%
     72LASTN = maxNumCompThreads(str2num(getenv('SLURM_JOB_CPUS_PER_NODE')));
     73nth = maxNumCompThreads;
     74fprintf('Number of Threads = %d.\n',nth);
     75
     76N=2^(14);
     77A = randn(N);
     78st = cputime;
     79tic;
     80B = sin(A);
     81realT = toc;
     82cpuT = cputime -st;
     83fprintf('Real Time = %f(sec)\n',realT);
     84fprintf('CPU Time = %f(sec)\n',cpuT);
     85fprintf('Ratio = %f\n',cpuT / realT);
     86}}}
     87
     88In above code, the line,
     89{{{#!matlab
     90LASTN = maxNumCompThreads(str2num(getenv('SLURM_JOB_CPUS_PER_NODE')));
     91}}}
     92defines the number of threads.
     93The environmental variable, '''SLURM_JOB_CPUS_PER_NODE''' has the value set in SLURM script, for example,
     94{{{#!bash
     95#!/bin/bash
     96#SBATCH --qos=normal            # Quality of Service
     97#SBATCH --job-name=matlabMT     # Job Name
     98#SBATCH --time=1:00:00          # WallTime
     99#SBATCH --nodes=1               # Number of Nodes
     100#SBATCH --ntasks-per-node=1     # Number of tasks (MPI processes)
     101#SBATCH --cpus-per-task=10      # Number of threads per task (OMP threads)
     102
     103module load matlab
     104matlab -nodesktop -nodisplay -nosplash -r "FuncTest; exit;"
     105}}}
     106The number of cores per process (task) is set by '''--cpus-per-task=10'''.
     107This value goes to '''SLURM_JOB_CPUS_PER_NODE''' and you can use it to determine the number of threads used in the code.
     108
     109
     110==== Explicit parallelism ====
     111The ''parallel computing toolbox'' is available on Cypress.
     112You can use up to 12 workers for shared parallel operations on a single node in the current MATLAB version.
     113Our license does not include MATLAB Distributed Computing Server. Therefore, multi-node parallel operations are not supported.
     114
     115Workers are like independent processes. If you want to use 4 workers, you have to request at least 4 tasks within a node.
     116
     117[[Image(MatlabWorkers.jpeg)]]
     118
     119{{{#!bash
     120#!/bin/bash
     121#SBATCH --qos=normal            # Quality of Service
     122#SBATCH --job-name=matlabPool     # Job Name
     123#SBATCH --time=1:00:00          # WallTime
     124#SBATCH --nodes=1               # Number of Nodes
     125#SBATCH --ntasks-per-node=1     # Number of tasks (MPI processes)
     126#SBATCH --cpus-per-task=4       # Number of threads per task (OMP threads)
     127
     128module load matlab
     129matlab  -nodesktop -nodisplay -nosplash -r "CreateWorker; ParforTest; exit;"
     130}}}
     131
     132''!CreateWorker.m'' is a Matlab code to create workers.
     133{{{#!matlab
     134% Parallel Tool Box Test "CreateWorker.m"
     135%
     136if isempty(getenv('SLURM_JOB_CPUS_PER_NODE'))
     137   nWorker = 1;
     138else
     139   nWorker = min(12,str2num(getenv('SLURM_JOB_CPUS_PER_NODE')));
     140end
     141% Create Workers
     142parpool(nWorker);
     143%
     144}}}
     145
     146''Parfor.m'' is a sample 'parfor' test code,
     147{{{#!matlab
     148% parfor "ParforTest.m"
     149%
     150iter = 10000;
     151sz = 50;
     152a = zeros(1,iter);
     153%
     154fprintf('Computing...\n');
     155tic;
     156parfor i = 1:iter
     157    a(i) = max(svd(randn(sz)));
     158end
     159toc;
     160%
     161poolobj = gcp('nocreate'); % Returns the current pool if one exists. If no pool, do not create new one.
     162if isempty(poolobj)
     163    poolobj = gcp;
     164end
     165fprintf('Number of Workers = %d.\n',poolobj.NumWorkers);
     166%
     167}}}
     168 
     169=== Running MATLAB with Automatic Offload ===
     170Internally MATLAB uses Intel MKL Basic Linear Algebra Subroutines (BLAS) and Linear Algebra package (LAPACK) routines to perform the underlying computations when running on Intel processors.
     171
     172Intel MKL includes Automatic Offload (AO) feature that enables computationally intensive Intel MKL functions to offload partial workload to attached '''Intel Xeon Phi''' coprocessors automatically and transparently.
     173
     174As a result, MATLAB performance can benefit from Intel Xeon Phi coprocessors via the Intel MKL AO feature when problem sizes are large enough to amortize the cost of transferring data to the coprocessors.
     175
     176In SLURM script, make sure that option '''--gres=mic:1''' is set and ''intel-psxe'' module as well as the MATLAB module has been loaded.
     177
     178{{{#!bash
     179#!/bin/bash
     180#SBATCH --qos=normal            # Quality of Service
     181#SBATCH --job-name=matlabAO     # Job Name
     182#SBATCH --time=1:00:00          # WallTime
     183#SBATCH --nodes=1               # Number of Nodes
     184#SBATCH --ntasks-per-node=1     # Number of tasks (MPI processes)
     185#SBATCH --cpus-per-task=1       # Number of threads per task (OMP threads)
     186#SBATCH --gres=mic:1            # Number of Co-Processors
     187
     188module load matlab
     189module load intel-psxe
     190
     191export MKL_MIC_ENABLE=1
     192matlab  -nodesktop -nodisplay -nosplash -r "MatTest; exit;"
     193}}}
     194
     195Note that
     196{{{#!bash
     197export MKL_MIC_ENABLE=1
     198}}}
     199enables Intel MKL Automatic Offload (AO).
     200
     201The sample cose is below:
     202{{{#!matlab
     203%
     204% Matrix test "MatTest.m"
     205%
     206A = rand(10000, 10000);
     207B = rand(10000, 10000);
     208tic;
     209C = A * B;
     210realT = toc;
     211fprintf('Real Time = %f(sec)\n',realT);
     212}}}
     213
     214== Compiled Matlab ==
     215=== Compiling Matlab Scripts using mcc ===
     216Start an interactive session with idev, load the intel-psxe module (if you want to use mkl and multithreading), load the matlab module
     217
     218{{{
     219[tulaneID@cypress1 ~]$ idev
     220Requesting 1 node(s)  task(s) to normal queue of defq partition
     2211 task(s)/node, 20 cpu(s)/task, 2 MIC device(s)/node
     222Time: 0 (hr) 60 (min).
     223Submitted batch job 80102
     224JOBID=80102 begin on cypress01-036
     225--> Creating interactive terminal session (login) on node cypress01-036.
     226--> You have 0 (hr) 60 (min).
     227--> Assigned Host List : /tmp/idev_nodes_file_tulaneID
     228Last login: Tue Sep 22 16:27:39 2015 from cypress1.cm.cluster
     229[tulaneID@cypress01-036 ~]$ module load intel-psxe
     230[tulaneID@cypress01-036 ~]$ module load matlab
     231[tulaneID@cypress01-036 ~]$
     232}}}
     233
     234cd to the directory containing your matlab file and compile using matlabs C compiler {{{mcc -m <your matlab .m file>}}}. If your script is spread over many files you need to specify the directories containing those files {{{mcc -m -I <source directory> <your matlab .m file>}}}. For example, to compile my_script.m which depends on other .m files located in /home/tulaneID/myMatlab I would run
     235{{{
     236mcc -m -I /home/tulaneID/myMatlab my_script.m
     237}}}
     238This will compile files that my_script.m depends upon provided they are in /home/tulaneID/myMatlab
     239
     240=== Executing Compiled scripts ===
     241The above will create a binary executable named my_script. To run the executable as a SLRUM Job, just include the matlab module in your SLURM script. This will provide all the necessary libraries. For example,
     242{{{#!bash
     243#!/bin/bash
     244#SBATCH --qos=normal                    # Quality of Service
     245#SBATCH --job-name=my_script            # Job Name
     246#SBATCH --time=1-0:00:00                # WallTime
     247#SBATCH --nodes=1                       # Number of Nodes
     248#SBATCH --ntasks-per-node=1             # Number of tasks (MPI processes)
     249#SBATCH --cpus-per-task=1               # Number of threads per task (OMP threads)
     250
     251module load matlab
     252
     253./my_script
     254}}}