Matlab use cases: Difference between revisions

From Cheaha
Jump to navigation Jump to search
(Strated Use case Mark Bolding.)
 
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Some use cases describing how to offload MATLAB computation tot he cluster.
Some use cases describing how to offload MATLAB computation to the cluster.
== Use Matlab on a compute node with a scheduled batch script ==
A shared cluster environment like Cheaha uses a job scheduler to run tasks on the cluster to provide optimal resource sharing among users. Cheaha uses a job scheduling system call Slurm to schedule and manage jobs. A user needs to tell Slurm about resource requirements (e.g. CPU, memory) so that it can schedule jobs effectively. These resource requirements along with actual application code can be specified in a single file commonly referred as 'Job Script/File'. Following is a simple job script that prints job number and hostname.


== Use MATLAB on a compute node via VNC ==


This entails running a MATLAB client (GUI) via VNC on a compute node on [[Cheaha]].
Following is a sample script to generate Fibonacci series from 1-10 and save the output
<pre>
%% Job Test on Cheaha with SLURM  - Test1Fib.m
% Submit Job with matlabSubmit.sbatch
% Output Files out.mat
% Create Fibonacci Series upto 10


This is a typical use case in the following scenarios
% clear all
# Computation needs to be offloaded to a compute node on the cluster to free the user's desktop for development
% close all
# Computation is too large to be performed on the user's desktop and needs more resources
 
limit = 10        % Fibonacci series from 1 to limit
c(1)=0;          % first element
c(2)=1;          % second element
 
for i=3:limit
      c(i)=c(i-1)+c(i-2);
end
c                  % Displays the output on screen
save('out1.mat')  % Saves the workspace to mat file
exit
</pre>
 
Following is the matlabSubmit.sbatch
Please edit the email address and the processor cores/memory requirement according to your jobs
<pre>
#!/bin/bash
#
#SBATCH --job-name=matTest
#SBATCH --output=res.txt
#SBATCH --ntasks=1
#SBATCH --partition=express
#SBATCH --time=10:00
#SBATCH --mem-per-cpu=100
#SBATCH --mail-type=FAIL
#SBATCH --mail-user=YOUR_EMAIL_ADDRESS
 
module load rc/matlab/R2016b
srun matlab -nodesktop -nodisplay -r Test1Fib
srun sleep 60                       


The vnc session needs to be setup in two stages"
1. Login to cheaha using terminal
<pre>
$ ssh USERID@cheaha.uabgrid.uab.edu
</pre>
</pre>


2. Start VNC server on Cheaha
The above job can be submitted by
<pre>
<pre>
$ vncserver
$ sbatch matlabSubmit.sbatch
New 'cheaha.uabgrid.uab.edu:6 (USERID)' desktop is cheaha.uabgrid.uab.edu:6
Submitted batch job 359705
</pre>
</pre>
Cheaha will start the vncserver with an available port number. In the above case ":6"


3. Open a  new terminal window on the user desktop and connect using port forwarding using the following
You can check the job status using squeue command
<pre>
<pre>
$ ssh -L port:localhost:port USERID@cheaha.uabgrid.uab.edu
$ squeue -u $USER
359705  express    matTest tanthony  R      0:03      1 c0005
</pre>
</pre>


where the port number specified in the above command is obtained from
This shows that the job is running on compute node c0005
<pre>  port = 5900 + port number on which the vncserver is running </pre>
The working directory should have two new files res.txt and out1.mat
 
== Use Matlab on a compute node via VNC ==
 
This use case creates an ordinary Matlab session, just like you would on your desktop, however, instead of running MATLAB you your desktop, you run MATLAB on a compute node of the [[Cheaha|cluster]] and access it's GUI via your cluster desktop.


In the above case
This is useful in situations where your personal desktop or laptop is not powerful enough to run your Matlab code or you simply want to offload a long-running computation so you can free your desktop or laptop for other activities.
 
=== Connect to Your Cluster Desktop ===
The first step is to connect to an existing cluster desktop session via VNC.  Please follow the steps for [[Setting_Up_VNC_ Session|setting up a VNC session]] to create your cluster desktop and connect to it.
 
=== Open a Terminal ===
The second step is to open a terminal window on your cluster desktop.  Once you are connected to your cluster desktop, start the Terminal application from the cluster desktop menu Applications->Accessories->Terminal.
 
=== Reserve your Computer ===
The third step is to request a computer on which you can run Matlab.  In your cluster desktop terminal window, submit a resource request to reserve a compute node for running Matlab
<pre>
<pre>
port =5900 + 6 = 5900 </pre>
sinteractive --ntasks=1 --mem-per-cpu=1024 --time=4:00:00 --partition=medium
</pre>
The above resource request asks for a computer with 1 gigabyte of RAM for 4 hours.  You can adjust these values to suit your needs but you should try to make reasonable estimates to ensure that resources are shared fairly.  WARNING: the specified time limit is a hard boundary and can't be changed after you start Matlab.  When this time limit is reached, your Matlab session will be automatically stopped, without warning, by the cluster resource manager.


The command to connect becomes
<pre>
<pre>
$ ssh -L 5906:localhost:5906 USERID@cheaha.uabgrid.uab.edu
sinteractive --ntasks=1 --cpus-per-task=4 --mem-per-cpu=1024 --time=8:00:00 --partition=medium
</pre>
</pre>
The above resource request asks for a computer with 4 cores and 1 gigabyte of RAM/core (total 4GB ram) for 8 hours.


Next connect to the vnc server on the Cheaha
=== Start Matlab ===
On the MAC '''start finder''', then '''cmd+k''' will bring up the "connect to server" window. (Please make sure You have '''"screen sharing"''' turned ON in '''system preferences'''.  
The fourth and final step is to start Matlab on the compute resource you have reserved.  In the same terminal window from which you made the resource request (ie. ran "qlogin"), run the following commands to start matlab.
Input the server address as
<pre>
<pre>vnc://localhost:port </pre>
module load rc/matlab/R2016b
matlab
</pre>


In the above case
Matlab will be running your reserved compute resource and can be controlled interactively via the VNC session.  This Matlab session works just like the Matlab sessions on your personal desktop or laptop. When you are finished using Matlab, simply exit the application as you normally would. Your compute resource will automatically return to the resource pool at the end of the your requested reservation time or you can type "exit" to return it immediately.
<pre>vnc://localhost:5906 </pre>
and click Connect.
It will open a vnc window with a terminal on the cheaha.


To portforward and login to a compute node use the following command from the terminal in the vnc session
{{MATLAB Support}}
<pre>
$ qlogin -l vf=memory,h_rt=hr:min:sec
</pre>


Load the mathworks module and start matlab
[[Category:MATLAB]]
<pre>$ module load mathworks/R2011b
$ matlab
</pre>
MATLAB will now start interactively in the VNC session and is running ont he compute node on Cheaha.

Latest revision as of 19:20, 9 March 2017

Some use cases describing how to offload MATLAB computation to the cluster.

Use Matlab on a compute node with a scheduled batch script

A shared cluster environment like Cheaha uses a job scheduler to run tasks on the cluster to provide optimal resource sharing among users. Cheaha uses a job scheduling system call Slurm to schedule and manage jobs. A user needs to tell Slurm about resource requirements (e.g. CPU, memory) so that it can schedule jobs effectively. These resource requirements along with actual application code can be specified in a single file commonly referred as 'Job Script/File'. Following is a simple job script that prints job number and hostname.


Following is a sample script to generate Fibonacci series from 1-10 and save the output

%% Job Test on Cheaha with SLURM  - Test1Fib.m
% Submit Job with matlabSubmit.sbatch
% Output Files out.mat
% Create Fibonacci Series upto 10

% clear all
% close all

limit = 10        % Fibonacci series from 1 to limit 
c(1)=0;           % first element
c(2)=1;           % second element

for i=3:limit
      c(i)=c(i-1)+c(i-2);
end
c                  % Displays the output on screen
save('out1.mat')   % Saves the workspace to mat file 
exit

Following is the matlabSubmit.sbatch Please edit the email address and the processor cores/memory requirement according to your jobs

#!/bin/bash
#
#SBATCH --job-name=matTest
#SBATCH --output=res.txt
#SBATCH --ntasks=1
#SBATCH --partition=express
#SBATCH --time=10:00
#SBATCH --mem-per-cpu=100
#SBATCH --mail-type=FAIL
#SBATCH --mail-user=YOUR_EMAIL_ADDRESS

module load rc/matlab/R2016b
srun matlab -nodesktop -nodisplay -r Test1Fib
srun sleep 60                        

The above job can be submitted by

$ sbatch matlabSubmit.sbatch 
Submitted batch job 359705

You can check the job status using squeue command

$  squeue -u $USER
359705   express     matTest tanthony  R       0:03      1 c0005

This shows that the job is running on compute node c0005 The working directory should have two new files res.txt and out1.mat

Use Matlab on a compute node via VNC

This use case creates an ordinary Matlab session, just like you would on your desktop, however, instead of running MATLAB you your desktop, you run MATLAB on a compute node of the cluster and access it's GUI via your cluster desktop.

This is useful in situations where your personal desktop or laptop is not powerful enough to run your Matlab code or you simply want to offload a long-running computation so you can free your desktop or laptop for other activities.

Connect to Your Cluster Desktop

The first step is to connect to an existing cluster desktop session via VNC. Please follow the steps for setting up a VNC session to create your cluster desktop and connect to it.

Open a Terminal

The second step is to open a terminal window on your cluster desktop. Once you are connected to your cluster desktop, start the Terminal application from the cluster desktop menu Applications->Accessories->Terminal.

Reserve your Computer

The third step is to request a computer on which you can run Matlab. In your cluster desktop terminal window, submit a resource request to reserve a compute node for running Matlab

sinteractive --ntasks=1 --mem-per-cpu=1024 --time=4:00:00 --partition=medium

The above resource request asks for a computer with 1 gigabyte of RAM for 4 hours. You can adjust these values to suit your needs but you should try to make reasonable estimates to ensure that resources are shared fairly. WARNING: the specified time limit is a hard boundary and can't be changed after you start Matlab. When this time limit is reached, your Matlab session will be automatically stopped, without warning, by the cluster resource manager.

sinteractive --ntasks=1 --cpus-per-task=4 --mem-per-cpu=1024 --time=8:00:00 --partition=medium

The above resource request asks for a computer with 4 cores and 1 gigabyte of RAM/core (total 4GB ram) for 8 hours.

Start Matlab

The fourth and final step is to start Matlab on the compute resource you have reserved. In the same terminal window from which you made the resource request (ie. ran "qlogin"), run the following commands to start matlab.

module load rc/matlab/R2016b 
matlab

Matlab will be running your reserved compute resource and can be controlled interactively via the VNC session. This Matlab session works just like the Matlab sessions on your personal desktop or laptop. When you are finished using Matlab, simply exit the application as you normally would. Your compute resource will automatically return to the resource pool at the end of the your requested reservation time or you can type "exit" to return it immediately.

MATLAB Support / Mailing List

As with any application or computer language, learning to use MATLAB to analyze data or to develop or modify MATLAB applications is an individual responsibility. There is ample application documentation available from the Mathworks website, potential outreach to colleagues who also use MATLAB, and options for consultation with Mathworks. Mathworks also host on-campus training seminars several times a year and provides many on-line learning tutorials.

Installation support for MATLAB at UAB is provided by your local IT support organization and the Docs wiki.

Mathworks Website

Your first and best option for application-specific questions on MATLAB is to refer to the on-line MATLAB documentation. The Mathworks site also provides a a support matrix and an on-line knowledge base.

UAB MATLAB Wiki

The MATLAB page on the Docs wiki is the starting point for installing MATLAB at UAB and, optionally, configuring it to use cluster computing. All users are encouraged to contribute to the MATLAB knowledge in this wiki, especially if you see areas where improvements are needed. Remember, this knowledge base is only as good as the people who contribute to it.

Contributing to the wiki is as easy as clicking the login link on the top-right of the page and signing in with your UAB BlazerID. If you are unsure about making an edit, you can make suggestions for improvement on the page's Discussion tab or discuss the proposed improvement in the MATLAB user group.

UAB MATLAB User Group

At UAB, MATLAB installation support is provided by your local IT support group. Support for application specific questions is available from peers in your research group. We realize that some people are not as familiar with MATLAB as others. For this reason, we have established a MATLAB user forum (mailing list) where users of MATLAB at UAB can help answer each others questions.

This is a network of volunteers sharing their knowledge with peers. You are encouraged to reach out to this community for questions on using MATLAB by

Archives of MATLAB user group discussions are available on-line at https://vo.uabgrid.uab.edu/sympa/arc/matlab-user. You may find your question is already answered in these archives.


UAB MATLAB announce mailing list

To receive information about UAB's MATLAB license and announcements please subscribe to the matlab-annc mailing list by