Vous regardez une version antérieure (v. /display/rc/MATLAB) de cette page.

afficher les différences afficher l'historique de la page

« Afficher la version précédente Vous regardez la version actuelle de cette page. (v. 2) afficher la version suivante »

MATLAB is a very powerful tool used for the creation, analysis, and visualization of data. The MATLAB language is an easy-to-learn, high-level programming language. This documentation will not cover how to install or use the basics of MATLAB; there are many tutorials online that can teach you MATLAB. Here is MATLAB's official interactive, online tutorial and a link to their documentation. This document covers parallelization of your MATLAB code, i.e., breaking up long processes into chunks that can be processed at the same time. In MATLAB these chunks that process code in parallel are called workers. These workers can be spread out across multiple nodes and cores to utilize the Research Computing resources. This mean you can get your results much faster. To do this we will be using MATLAB's Parallel Computing Toolbox.

Getting Your MATLAB Code Ready

Parfor loop

MATLAB'S Parallel Computing Toolbox comes with the parfor loop construction. It splits the work done in the for loop among workers to complete the work in parallel. To use simply replace the for loop in your code with parfor, however, there are some rules:

  1. Parfor loops cannot be nested within each other
  2. Parfor loop iterations must be independent of each other, that is, the results of one iteration cannot depend on the results of any other iteration. (No recursion!)
  3. Parfor loop variables must be consecutive, increasing integers. The step used for the loop cannot be any number but 1. To work around this use where iValues is the step you would use in the regular for loop:
    iValues = 0:0.2:1; 
    parfor idx = 1:numel(iValues)
        i = iVal%ues(idx);
         ... 
    end
  4. You cannot break out of a parfor loop early, that means no break or return statements in your parfor loop. If this creates an issue with your loop try parfeval.

Example Code

Using Parameters: If you use parameters in your program you must start the code with a function declaration. This function must have the same name as the MATLAB file.

function [a]=matlab_example(nloop, jobid)
% Example function to demonstrate parfor loop and parallel code
% nloop: number of iterations
% jobid: Slurm job id used to save output to specific file
if ischar(nloop) %checking data type of first input
    nloop=str2num(nloop);
end
%preallocate output array in a nloop x 1 matrix
a=zeros(nloop, 1);
% TIME CONSUMING LOOP
tic; %beginning of timing the loop
parfor i=1:nloop
    a(i)=IntensiveFunction();
end
time=toc %end of timing the loop
%save the output to its own file
save(['example_' num2str(jobid) '.out'], 'time', 'a', '-ascii')
end
function result=IntensiveFunction()
% Computation intensive calculation
    result = max(abs(eig(rand(500))));
end

Click here to download matlab_example.m.

Running Your Code

Using One Core and a Slurm Array

This example is not running code in parallel, but if you want to test your parfor loop or just run your matlab code multiple times in a row, this is useful.

#!/bin/bash -l
#NOTE the -l flag!
#SBATCH -J matlab_example_multiple #Name of the job
#Standard out and Standard error output files %A is the job id and %a is the array index
#SBATCH -o matlab_example_multiple_%A_%a.out
#SBATCH -e matlab_example_multiple_%A_%a.err
# To send emails and notify when done
#SBATCH --mail-user abc1234@rit.edu
#SBATCH --mail-type=END
#SBATCH -t 1:00:00 # Request 1 hour MAX for the job
#Run on tier3
#SBATCH -p tier3
#SBATCH -mem=2000M #memory requirement of jobs in MB
#SBATCH --array=1-10 #create array of 10 to run code 10 times
#Load my environment
module load matlab
#Create the Job and Array ID to to pass into the function
slurmArrayID="${SLURM_ARRAY_JOB_ID}%{SLURM_ARRAY_TASK_ID}"
export slurm ArrayID
#Run the file with no graphical display
matlab -nodisplay -nosplash -singleCompThread -r "matlab_example(10, ${slurmArrayID});exit;"

Click here to download this example.

Running in Parallel on One Node

Now this is our first real taste of parallelization. You will notice in this example the word parpoolThis function creates the workers that process the chunks created by parfor. 

#!/bin/bash -l
#NOTE the -l flag!
#SBATCH -J matlab_example_multiple #Name of the job
#Standard out and Standard error output files %A is the job id and %a is the array index
#SBATCH -o matlab_example_multiple_%A_%a.out
#SBATCH -e matlab_example_multiple_%A_%a.err
# To send emails and notify when done
#SBATCH --mail-user abc1234@rit.edu
#SBATCH --mail-type=END
#SBATCH -t 1:00:00 # Request 1 hour MAX for the job
#Run on tier3
#SBATCH -p tier3
#SBATCH -mem=2000M #memory requirement of jobs in MB
#SBATCH --nodes=1    #Number of nodes to run on
#SBATCH --ntasks-per-node=20 #Number of workers -1 to create on each node
#Load my environment
module load matlab
# Run the file with no graphical display

# Create a pool of workers on the 'local' SPORC cluster. 

# It must be one less than the ntasks-per-nodes because one of those tasks run the 

# matlab instance itself and cannot be a worker

matlab -nodisplay -nosplash -singleCompThread -r \
 "parpool('local', 19);matlab_example(10,${SLURM_JOB_ID});exit;"

Download this example.

This page is still a work in progress and not complete. Coming soon - more parallel code and expected output.


  • Aucune étiquette