Matlab: Difference between revisions

From HPCwiki
Jump to navigation Jump to search
(Replaced with content provided by Mathworks)
m (Markup improved)
Line 4: Line 4:


This document provides the steps to configure MATLAB to submit jobs to a cluster, retrieve results, and debug errors.
This document provides the steps to configure MATLAB to submit jobs to a cluster, retrieve results, and debug errors.
CONFIGURATION – MATLAB client on the cluster
 
After logging into the cluster, configure MATLAB to run parallel jobs on your cluster by calling the shell script configCluster.sh   This only needs to be called once per version of MATLAB.
=== CONFIGURATION – MATLAB client on the cluster ===
$ module load matlab
After logging into the cluster, configure MATLAB to run parallel jobs on your cluster by calling the shell script '''configCluster.sh'''. (after '''module load matlab''')
$ configCluster.sh
This only needs to be called once per version of MATLAB.
Jobs will now default to the cluster rather than submit to the local machine.
$ module load matlab
INSTALLATION and CONFIGURATION – MATLAB client on the desktop
$ configCluster.sh
Jobs will now default to the cluster rather than submit to the login node.
=== INSTALLATION and CONFIGURATION – MATLAB client on the desktop ===
 
The Anunna MATLAB support package can be found as follows
The Anunna MATLAB support package can be found as follows
Windows: TBD
Windows: TBD
Linux/macOS: TBD
Linux/macOS: TBD


Download the appropriate archive file and start MATLAB.  The archive file should be untarred/unzipped in the location returned by calling
Download the appropriate archive file and start MATLAB.  The archive file should be untarred/unzipped in the location returned by calling
>> userpath
>> userpath
Configure MATLAB to run parallel jobs on your cluster by calling configCluster.  configCluster only needs to be called once per version of MATLAB.
Configure MATLAB to run parallel jobs on your cluster by calling '''configCluster''''''configCluster''' only needs to be called once per version of MATLAB.
>> configCluster
>> configCluster
Submission to the remote cluster requires SSH credentials.  You will be prompted for your ssh username and password or identity file (private key).  The username and location of the private key will be stored in MATLAB for future sessions.
Submission to the remote cluster requires SSH credentials.  You will be prompted for your ssh username and password or identity file (private key).  The username and location of the private key will be stored in MATLAB for future sessions.
Jobs will now default to the cluster rather than submit to the local machine.
Jobs will now default to the cluster rather than submit to the local machine.
NOTE: If you would like to submit to the local machine then run the following command:
NOTE: If you would like to submit to the local machine then run the following command:
>> % Get a handle to the local resources
>> % Get a handle to the local resources
>> c = parcluster('local');
>> c = parcluster('local');
CONFIGURING JOBS
==== CONFIGURING JOBS====
Prior to submitting the job, we can specify various parameters to pass to our jobs, such as queue, e-mail, walltime, etc.  The following is a partial list of parameters.  See AdditionalProperties for the complete list.  Only MemUsage and WallTime are required.  
Prior to submitting the job, we can specify various parameters to pass to our jobs, such as queue, e-mail, walltime, etc.  The following is a partial list of parameters.  See AdditionalProperties for the complete list.  Only MemUsage and WallTime are required.  
<pre>
>> % Get a handle to the cluster
>> % Get a handle to the cluster
>> c = parcluster;
>> c = parcluster;
Line 66: Line 73:
>> % Request there be (for example) 20 GB of local disk space in /tmp
>> % Request there be (for example) 20 GB of local disk space in /tmp
>> c.AdditionalProperties.Tmp = 20g;
>> c.AdditionalProperties.Tmp = 20g;
 
</pre>


Save changes after modifying AdditionalProperties for the above changes to persist between MATLAB sessions.
Save changes after modifying AdditionalProperties for the above changes to persist between MATLAB sessions.
>> c.saveProfile
>> c.saveProfile


To see the values of the current configuration options, display AdditionalProperties.
To see the values of the current configuration options, display AdditionalProperties.


>> % To view current properties
>> % To view current properties
>> c.AdditionalProperties
>> c.AdditionalProperties


Unset a value when no longer needed.
Unset a value when no longer needed.
>> % Turn off email notifications  
>> % Turn off email notifications  
>> c.AdditionalProperties.EmailAddress = '';
>> c.AdditionalProperties.EmailAddress = '';
>> c.saveProfile
>> c.saveProfile
INTERACTIVE JOBS - MATLAB client on the cluster
==== INTERACTIVE JOBS - MATLAB client on the cluster ====
To run an interactive pool job on the cluster, continue to use parpool as you’ve done before.
To run an interactive pool job on the cluster, continue to use parpool as you’ve done before.
>> % Get a handle to the cluster
>> % Get a handle to the cluster
>> c = parcluster;
>> c = parcluster;


>> % Open a pool of 64 workers on the cluster
>> % Open a pool of 64 workers on the cluster
>> pool = c.parpool(64);
>> pool = c.parpool(64);


Rather than running local on the local machine, the pool can now run across multiple nodes on the cluster.
Rather than running local on the local machine, the pool can now run across multiple nodes on the cluster.


>> % Run a parfor over 1000 iterations
>> % Run a parfor over 1000 iterations
>> parfor idx = 1:1000
>> parfor idx = 1:1000
      a(idx) = …
      a(idx) = …
  end
    end


Once we’re done with the pool, delete it.
Once we’re done with the pool, delete it.


>> % Delete the pool
>> % Delete the pool
>> pool.delete
>> pool.delete
INDEPENDENT BATCH JOB
==== INDEPENDENT BATCH JOB ====
 
Use the batch command to submit asynchronous jobs to the cluster.  The batch command will return a job object which is used to access the output of the submitted job.  See the MATLAB documentation for more help on batch.
Use the batch command to submit asynchronous jobs to the cluster.  The batch command will return a job object which is used to access the output of the submitted job.  See the MATLAB documentation for more help on batch.
<pre>
>> % Get a handle to the cluster
>> % Get a handle to the cluster
>> c = parcluster;
>> c = parcluster;
Line 117: Line 126:
>> % Delete the job after results are no longer needed
>> % Delete the job after results are no longer needed
>> job.delete
>> job.delete
 
</pre>
To retrieve a list of currently running or completed jobs, call parcluster to retrieve the cluster object.  The cluster object stores an array of jobs that were run, are running, or are queued to run.  This allows us to fetch the results of completed jobs.  Retrieve and view the list of jobs as shown below.
To retrieve a list of currently running or completed jobs, call parcluster to retrieve the cluster object.  The cluster object stores an array of jobs that were run, are running, or are queued to run.  This allows us to fetch the results of completed jobs.  Retrieve and view the list of jobs as shown below.
>> c = parcluster;
>> c = parcluster;
>> jobs = c.Jobs;
>> jobs = c.Jobs;
Once we’ve identified the job we want, we can retrieve the results as we’ve done previously.  
Once we’ve identified the job we want, we can retrieve the results as we’ve done previously.  
fetchOutputs is used to retrieve function output arguments; if calling batch with a script, use load instead.  Data that has been written to files on the cluster needs be retrieved directly from the file system (e.g. via ftp).
fetchOutputs is used to retrieve function output arguments; if calling batch with a script, use load instead.  Data that has been written to files on the cluster needs be retrieved directly from the file system (e.g. via ftp).
To view results of a previously completed job:
To view results of a previously completed job:
>> % Get a handle to the job with ID 2
>> % Get a handle to the job with ID 2
>> job2 = c.Jobs(2);
>> job2 = c.Jobs(2);


NOTE: You can view a list of your jobs, as well as their IDs, using the above c.Jobs command.   
NOTE: You can view a list of your jobs, as well as their IDs, using the above c.Jobs command.   
>> % Fetch results for job with ID 2
>> % Fetch results for job with ID 2
>> job2.fetchOutputs{:}
>> job2.fetchOutputs{:}
PARALLEL BATCH JOB
 
Users can also submit parallel workflows with the batch command.  Let’s use the following example for a parallel job, which is saved as parallel_example.m.   
==== PARALLEL BATCH JOB ====
Users can also submit parallel workflows with the batch command.  Let’s use the following example for a parallel job, which is saved as '''parallel_example.m'''.   
<pre>
function [t, A] = parallel_example(iter)
function [t, A] = parallel_example(iter)
   
   
Line 153: Line 165:
   
   
end
end
 
</pre>
This time when we use the batch command, to run a parallel job, we’ll also specify a MATLAB Pool.     
This time when we use the batch command, to run a parallel job, we’ll also specify a MATLAB Pool.     
<pre>
>> % Get a handle to the cluster
>> % Get a handle to the cluster
>> c = parcluster;
>> c = parcluster;
Line 169: Line 182:
ans =  
ans =  
8.8872
8.8872
The job ran in 8.89 seconds using four workers.  Note that these jobs will always request N+1 CPU cores, since one worker is required to manage the batch job and pool of workers.  For example, a job that needs eight workers will consume nine CPU cores.
</pre>
The job ran in 8.89 seconds using four workers.  Note that these jobs will always request N+1 CPU cores, since one worker is required to manage the batch job and pool of workers.  For example, a job that needs eight workers will consume nine CPU cores.
We’ll run the same simulation but increase the Pool size.  This time, to retrieve the results later, we’ll keep track of the job ID.
We’ll run the same simulation but increase the Pool size.  This time, to retrieve the results later, we’ll keep track of the job ID.
NOTE: For some applications, there will be a diminishing return when allocating too many workers, as the overhead may exceed computation time.     
NOTE: For some applications, there will be a diminishing return when allocating too many workers, as the overhead may exceed computation time.     
<pre>
>> % Get a handle to the cluster
>> % Get a handle to the cluster
>> c = parcluster;
>> c = parcluster;
Line 185: Line 202:
>> % Clear job from workspace (as though we quit MATLAB)
>> % Clear job from workspace (as though we quit MATLAB)
>> clear job
>> clear job
</pre>
Once we have a handle to the cluster, we’ll call the findJob method to search for the job with the specified job ID.   
Once we have a handle to the cluster, we’ll call the findJob method to search for the job with the specified job ID.   
<pre>
>> % Get a handle to the cluster
>> % Get a handle to the cluster
>> c = parcluster;
>> c = parcluster;
Line 200: Line 219:
ans =  
ans =  
4.7270
4.7270
</pre>
The job now runs in 4.73 seconds using eight workers.  Run code with different number of workers to determine the ideal number to use.
The job now runs in 4.73 seconds using eight workers.  Run code with different number of workers to determine the ideal number to use.
Alternatively, to retrieve job results via a graphical user interface, use the Job Monitor (Parallel > Monitor Jobs).
Alternatively, to retrieve job results via a graphical user interface, use the Job Monitor (Parallel > Monitor Jobs).
   
   
DEBUGGING
==== DEBUGGING ====
If a serial job produces an error, call the getDebugLog method to view the error log file.  When submitting independent jobs, with multiple tasks, specify the task number.   
If a serial job produces an error, call the getDebugLog method to view the error log file.  When submitting independent jobs, with multiple tasks, specify the task number.   
>> c.getDebugLog(job.Tasks(3))
>> c.getDebugLog(job.Tasks(3))
For Pool jobs, only specify the job object.
For Pool jobs, only specify the job object.
>> c.getDebugLog(job)
>> c.getDebugLog(job)
When troubleshooting a job, the cluster admin may request the scheduler ID of the job.  This can be derived by calling schedID
When troubleshooting a job, the cluster admin may request the scheduler ID of the job.  This can be derived by calling schedID
<pre>
>> schedID(job)
>> schedID(job)
ans =  
ans =  
25539
25539
TO LEARN MORE
</pre>
==== TO LEARN MORE ====
To learn more about the MATLAB Parallel Computing Toolbox, check out these resources:
To learn more about the MATLAB Parallel Computing Toolbox, check out these resources:
• Parallel Computing Coding Examples
[https://www.mathworks.com/help/parallel-computing/examples.html|Parallel Computing Coding Examples]
• Parallel Computing Documentation
[http://www.mathworks.com/help/distcomp/index.html|Parallel Computing Documentation]
• Parallel Computing Overview
[http://www.mathworks.com/products/parallel-computing/index.html|Parallel Computing Overview]
• Parallel Computing Tutorials
[http://www.mathworks.com/products/parallel-computing/tutorials.html|Parallel Computing Tutorials]
• Parallel Computing Videos
[http://www.mathworks.com/products/parallel-computing/videos.html|Parallel Computing Videos]
• Parallel Computing Webinars
[http://www.mathworks.com/products/parallel-computing/webinars.html|Parallel Computing Webinars]

Revision as of 09:44, 9 May 2022

MATLAB is a non-free calculation language owned by Mathworks. The HPC has this installed as part of the WUR academic license, and so this is only available to WUR users.

Getting Started with Parallel Computing using MATLAB on the Anunna HPC Cluster

This document provides the steps to configure MATLAB to submit jobs to a cluster, retrieve results, and debug errors.

CONFIGURATION – MATLAB client on the cluster

After logging into the cluster, configure MATLAB to run parallel jobs on your cluster by calling the shell script configCluster.sh. (after module load matlab) This only needs to be called once per version of MATLAB.

$ module load matlab
$ configCluster.sh

Jobs will now default to the cluster rather than submit to the login node.

INSTALLATION and CONFIGURATION – MATLAB client on the desktop

The Anunna MATLAB support package can be found as follows

Windows: TBD

Linux/macOS: TBD

Download the appropriate archive file and start MATLAB. The archive file should be untarred/unzipped in the location returned by calling

>> userpath

Configure MATLAB to run parallel jobs on your cluster by calling configCluster. configCluster only needs to be called once per version of MATLAB.

>> configCluster

Submission to the remote cluster requires SSH credentials. You will be prompted for your ssh username and password or identity file (private key). The username and location of the private key will be stored in MATLAB for future sessions. Jobs will now default to the cluster rather than submit to the local machine. NOTE: If you would like to submit to the local machine then run the following command:

>> % Get a handle to the local resources
>> c = parcluster('local');

CONFIGURING JOBS

Prior to submitting the job, we can specify various parameters to pass to our jobs, such as queue, e-mail, walltime, etc. The following is a partial list of parameters. See AdditionalProperties for the complete list. Only MemUsage and WallTime are required.

>> % Get a handle to the cluster
>> c = parcluster;
[REQUIRED]

>> % Specify memory to use for MATLAB jobs, per core (default: 4gb)
>> c.AdditionalProperties.MemUsage = '6gb';

>> % Specify the walltime (e.g., 5 hours)
>> c.AdditionalProperties.WallTime = '05:00:00';

[OPTIONAL]

>> % Specify an account to use for MATLAB jobs
>> c.AdditionalProperties.AccountName = 'account-name';

>> % Assign a comment to the job
>> c.AdditionalProperties.Comment = 'a-comment';

>> % Request a specific GPU flavor (e.g., V100)
>> c.AdditionalProperties.Constraint = 'V100';

>> % Specify e-mail address to receive notifications about your job
>> c.AdditionalProperties.EmailAddress = 'user-id@wur.nl';

>> % Specify number of GPUs
>> c.AdditionalProperties.GpusPerNode = 1;

>> % Specify a QoS (default: std)
>> c.AdditionalProperties.QoS = 'the-qos';

>> % Specify a queue to use for MATLAB jobs				
>> c.AdditionalProperties.QueueName = 'queue-name';

>> % Require exclusive nodes
>> c.AdditionalProperties.RequireExclusiveNode = true;

>> % Specify a reservation
>> c.AdditionalProperties.Reservation = 'a-reservation';

>> % Request there be (for example) 20 GB of local disk space in /tmp
>> c.AdditionalProperties.Tmp = 20g;

Save changes after modifying AdditionalProperties for the above changes to persist between MATLAB sessions.

>> c.saveProfile

To see the values of the current configuration options, display AdditionalProperties.

>> % To view current properties
>> c.AdditionalProperties

Unset a value when no longer needed.

>> % Turn off email notifications 
>> c.AdditionalProperties.EmailAddress = ;
>> c.saveProfile

INTERACTIVE JOBS - MATLAB client on the cluster

To run an interactive pool job on the cluster, continue to use parpool as you’ve done before.

>> % Get a handle to the cluster
>> c = parcluster;
>> % Open a pool of 64 workers on the cluster
>> pool = c.parpool(64);

Rather than running local on the local machine, the pool can now run across multiple nodes on the cluster.

>> % Run a parfor over 1000 iterations
>> parfor idx = 1:1000
      a(idx) = …
   end

Once we’re done with the pool, delete it.

>> % Delete the pool
>> pool.delete

INDEPENDENT BATCH JOB

Use the batch command to submit asynchronous jobs to the cluster. The batch command will return a job object which is used to access the output of the submitted job. See the MATLAB documentation for more help on batch.

>> % Get a handle to the cluster
>> c = parcluster;

>> % Submit job to query where MATLAB is running on the cluster
>> job = c.batch(@pwd, 1, {}, …
       'CurrentFolder','.', 'AutoAddClientPath',false);

>> % Query job for state
>> job.State

>> % If state is finished, fetch the results
>> job.fetchOutputs{:}


>> % Delete the job after results are no longer needed
>> job.delete

To retrieve a list of currently running or completed jobs, call parcluster to retrieve the cluster object. The cluster object stores an array of jobs that were run, are running, or are queued to run. This allows us to fetch the results of completed jobs. Retrieve and view the list of jobs as shown below.

>> c = parcluster;
>> jobs = c.Jobs;

Once we’ve identified the job we want, we can retrieve the results as we’ve done previously. fetchOutputs is used to retrieve function output arguments; if calling batch with a script, use load instead. Data that has been written to files on the cluster needs be retrieved directly from the file system (e.g. via ftp).

To view results of a previously completed job:

>> % Get a handle to the job with ID 2
>> job2 = c.Jobs(2);

NOTE: You can view a list of your jobs, as well as their IDs, using the above c.Jobs command.

>> % Fetch results for job with ID 2
>> job2.fetchOutputs{:}

PARALLEL BATCH JOB

Users can also submit parallel workflows with the batch command. Let’s use the following example for a parallel job, which is saved as parallel_example.m.

function [t, A] = parallel_example(iter)
 
if nargin==0
    iter = 8;
end
 
disp('Start sim')
 
t0 = tic;
parfor idx = 1:iter
    A(idx) = idx;
    pause(2)
    idx
end
t = toc(t0);
 
disp('Sim completed')
 
save RESULTS A
 
end

This time when we use the batch command, to run a parallel job, we’ll also specify a MATLAB Pool.

>> % Get a handle to the cluster
>> c = parcluster;

>> % Submit a batch pool job using 4 workers for 16 simulations
>> job = c.batch(@parallel_example, 1, {16}, 'Pool',4, …
       'CurrentFolder','.', 'AutoAddClientPath',false);

>> % View current job status
>> job.State

>> % Fetch the results after a finished state is retrieved
>> job.fetchOutputs{:}
ans = 
	8.8872

The job ran in 8.89 seconds using four workers. Note that these jobs will always request N+1 CPU cores, since one worker is required to manage the batch job and pool of workers. For example, a job that needs eight workers will consume nine CPU cores.

We’ll run the same simulation but increase the Pool size. This time, to retrieve the results later, we’ll keep track of the job ID.

NOTE: For some applications, there will be a diminishing return when allocating too many workers, as the overhead may exceed computation time.

>> % Get a handle to the cluster
>> c = parcluster;

>> % Submit a batch pool job using 8 workers for 16 simulations
>> job = c.batch(@parallel_example, 1, {16}, 'Pool', 8, …
       'CurrentFolder','.', 'AutoAddClientPath',false);

>> % Get the job ID
>> id = job.ID
id =
	4
>> % Clear job from workspace (as though we quit MATLAB)
>> clear job

Once we have a handle to the cluster, we’ll call the findJob method to search for the job with the specified job ID.

>> % Get a handle to the cluster
>> c = parcluster;

>> % Find the old job
>> job = c.findJob('ID', 4);

>> % Retrieve the state of the job
>> job.State
ans = 
finished
>> % Fetch the results
>> job.fetchOutputs{:};
ans = 
4.7270

The job now runs in 4.73 seconds using eight workers. Run code with different number of workers to determine the ideal number to use. Alternatively, to retrieve job results via a graphical user interface, use the Job Monitor (Parallel > Monitor Jobs).

DEBUGGING

If a serial job produces an error, call the getDebugLog method to view the error log file. When submitting independent jobs, with multiple tasks, specify the task number.

>> c.getDebugLog(job.Tasks(3))

For Pool jobs, only specify the job object.

>> c.getDebugLog(job)

When troubleshooting a job, the cluster admin may request the scheduler ID of the job. This can be derived by calling schedID

>> schedID(job)
ans = 
25539

TO LEARN MORE

To learn more about the MATLAB Parallel Computing Toolbox, check out these resources: • Computing Coding ExamplesComputing DocumentationComputing OverviewComputing TutorialsComputing VideosComputing Webinars