MatLab DCS Examples

From Cheaha
Jump to navigation Jump to search


Attention: Research Computing Documentation has Moved
https://docs.rc.uab.edu/


Please use the new documentation url https://docs.rc.uab.edu/ for all Research Computing documentation needs.


As a result of this move, we have deprecated use of this wiki for documentation. We are providing read-only access to the content to facilitate migration of bookmarks and to serve as an historical record. All content updates should be made at the new documentation site. The original wiki will not receive further updates.

Thank you,

The Research Computing Team

Examples

Additional examples and submit scripts available in the  Workshop 2011 demo  section.

Parfor

This example uses two files:

  • myWave.m - a script with a parfor loop generates a wave form
  • rParforWave.m - the submission script

The job will use 4 total slots/ MATLAB workers on the cluster (3 workers plus the master worker process).

  1. Create the myWave.m script containing this code
    parfor i=1:1024
    
      A(i) = sin(i*2*pi/1024);
    
    end
    
  2. Next create the rParforWave.m script making sure to change the YOUREMAIL string to a working email address. The following submit script is used in case of Matlab R 2011b or older.
  3. % Submit script for Matlab R2010b, 2011a, and 2011b
    % Always set these variables
    email           = 'YOUREMAIL';
    email_opt       = 'eas';      % qsub email options
    s_rt            = '00:05:00'; % soft wall time
    h_rt            = '00:07:00'; % hard wall time
    vf              = '1G';       % Amount of memory need per task
    min_cpu_slots   = 2;          % Min number of cpu slots needed for the job
    max_cpu_slots   = 3;          % Max number of cpu slots needed for the job
    
    % Configure the scheduler - Do NOT modify these
    sge_options = ['-l vf=', vf, ',h_rt=', h_rt, ',s_rt=', s_rt, ' -m ', email_opt, ' -M ', email];
    SGEClusterInfo.setExtraParameter(sge_options);
    sched = findResource();
    % End of scheduler configuration
    
    % start of user specific commands
    job = batch('myWave', 'matlabpool', max_cpu_slots, 'FileDependencies', {'myWave.m'});
    
    % The following commands can be run once the job is submitted to view the results
    % >> waitForState(job)
    % >> load(job, 'A')
    % >> plot(A)
    
    % Once the job is complete, permanently remove its data
    % >> destroy(job)
    
  4. Matlab R2012 a uses the following submit script
  5. % Submit script for Matlab R2012a
    % Always set these variables
    email           = 'YOUREMAIL';
    email_opt       = 'eas';      % qsub email options
    h_rt            = '00:07:00'; % hard wall time
    vf              = '1G';       % Amount of memory need per task
    min_cpu_slots   = 2;          % Min number of cpu slots needed for the job
    max_cpu_slots   = 3;          % Max number of cpu slots needed for the job
    
    % Configure the scheduler - Do NOT modify these
    sge_options = ['-l vf=', vf, ',h_rt=', h_rt, ', ' -m ', email_opt, ' -M ', email];
    SGEClusterInfo.setExtraParameter(sge_options);
    
    cluster = findResource();
    % End of scheduler configuration
    
    % start of user specific commands
    job = batch('myWave', 'matlabpool', max_cpu_slots, 'FileDependencies', {'myWave.m'});
    
    % The following commands can be run once the job is submitted to view the results
    % >> waitForState(job)
    % >> load(job, 'A')
    % >> plot(A)
    
    % Once the job is complete, permanently remove its data
    % >> delete(job)
    
  6. Select cheaha as the parallel configuration by clicking Parallel -> Select Configuration -> cheaha in the main MATLAB window
  7. Run the rParforWave.m code by opening the script in the MATLAB editor and clicking the green run arrow
  8. After several seconds you should see output similar to the following in the MATLAB Command Window
    • The job 294773 is the job number assigned by the Cheaha scheduler
    • The Job1 is the job name and number used by MATLAB to reference the job. In most cases, this is the number that you'll use to interact with MATLAB to load the results, clean up the job, etc...
      Your job 294773 ("Job1") has been submitted
      
  9. Now that the job has been submitted, instruct MATLAB to wait for the job to complete using waitForState. MATLAB will show as 'Busy' until the job completes, at which time the >> prompt will appear. You can verify that the job is complete by running the job.State function call. (waitForState is not available in R2012a. Use Job Monitor to check for state of job on the cluster)
    >> waitForState(job)   % not available on R2012a
    >> job.State
    
    ans =
    
    finished
    
  10. Now that the job has completed, to view the results first use the load function to load the workspace variable A from our batch job:
    >> load(job, 'A')
    
  11. Next, display the plot
    >> plot(A)
    

    RParforWave-plot.png

  12. Once you are done with the job, make sure to run the destroy the job to clean up the space used on the cluster
    >> job.destroy
    

In the case of longer running jobs, you probably don't want to tie up your MATLAB client by using waitForState.

This will allow you to perform other tasks in MATLAB or exit entirely without having to wait for your job to complete.

MATLAB provides a function to load a previously submitted job back into the workspace, findJob.

In the example above, our MATLAB job name was Job1 and from that we can deduce that the MATLAB job number was 1. It can be loaded as follows (make sure the correct Parallel configuration is selected):

>> sched = findResource();
>> job = findJob(sched, 'ID', 1);
>> job.State

ans =

finished

It is important to clean up after the job using destroy to free up hard disk space on both your desktop and the cluster. Any output that is to be saved should be copied to another location on your Desktop prior to running destroy.

>> findJob(sched, 'ID', 1)
>> job.destroy