| 65 | MATLAB supports multithreaded computation for a number of functions and expressions that are combinations of element-wise functions. |
| 66 | These functions automatically execute on multiple threads if data size is large enough. |
| 67 | Note that on Cypress, in default, MATLAB runs with a single threads, and you have to explicitly specify the number of threads in your code. |
| 68 | For example, |
| 69 | {{{#!matlab |
| 70 | % Matlab Test Code "FuncTest.m" |
| 71 | % |
| 72 | LASTN = maxNumCompThreads(str2num(getenv('SLURM_JOB_CPUS_PER_NODE'))); |
| 73 | nth = maxNumCompThreads; |
| 74 | fprintf('Number of Threads = %d.\n',nth); |
66 | | === Running MATLAB with Automatic Offloading === |
| 76 | N=2^(14); |
| 77 | A = randn(N); |
| 78 | st = cputime; |
| 79 | tic; |
| 80 | B = sin(A); |
| 81 | realT = toc; |
| 82 | cpuT = cputime -st; |
| 83 | fprintf('Real Time = %f(sec)\n',realT); |
| 84 | fprintf('CPU Time = %f(sec)\n',cpuT); |
| 85 | fprintf('Speedup Factor = %f\n',cpuT / realT); |
| 86 | }}} |
| 87 | |
| 88 | In above code, the line, |
| 89 | {{{#!matlab |
| 90 | LASTN = maxNumCompThreads(str2num(getenv('SLURM_JOB_CPUS_PER_NODE'))); |
| 91 | }}} |
| 92 | defines the number of threads. |
| 93 | The 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 | |
| 103 | module load matlab |
| 104 | matlab -nodesktop -nodisplay -nosplash -r "FuncTest; exit;" |
| 105 | }}} |
| 106 | The number of cores per process (task) is set by '''--cpus-per-task=10'''. |
| 107 | This value goes to '''SLURM_JOB_CPUS_PER_NODE''' and you can use it to determine the number of threads used in the code. |