Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/matlab #92

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions docs/software/maths/matlab.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,47 +79,54 @@ You have two ways to proceed:
matlab -nodisplay -nosplash -r inputfile -logfile outputfile.out
```

The following script uses one full socket in a [compute node of Aion](/systems/aion/compute/).

```bash
#!/bin/bash -l
#SBATCH -J MATLAB
###SBATCH -A <project_name>
#SBATCH --ntasks-per-node 1
#SBATCH -c 1
#!/usr/bin/bash --login
#SBATCH --job-name=MATLAB_job
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=1
#SBATCH --cpus-per-task=16
#SBATCH --partition=batch
#SBATCH --qos=normal
#SBATCH --time=00:30:00
#SBATCH -p batch

# Uncomment if you use a non-default account to run your jobs
##SBATCH --account=<project_name>

# Load the module MATLAB
module purge
module load math/MATLAB

# second form with CLI options '-r <input>' and '-logfile <output>.out'
srun -c $SLURM_CPUS_PER_TASK matlab -nodisplay -r my_matlab_script -logfile output.out
srun matlab -nodisplay -r my_matlab_script -logfile output.out

# example for if you need to have a input parameters for the computations
# matlab_script_serial_file(x,y,z)
srun matlab -nodisplay -r my_matlab_script(2,2,1)' -logfile output.out

# safeguard (!) afterwards
rm -rf $HOME/.matlab
rm -rf $HOME/java*
# Cleaunp MATLAB autogenerated files from your home directory
rm -rf ${HOME}/.matlab
rm -rf ${HOME}/java*
```

In matlab, you can create a parallel pool of thread workers on the local computing node by using the [parpool](https://mathworks.com/help/parallel-computing/parallel.threadpool.html) function.
After you create the pool, parallel pool features, such as [`parfor`](https://mathworks.com/help/parallel-computing/parallel-for-loops-parfor.html) or [`parfeval`](https://fr.mathworks.com/help/matlab/ref/parfeval.html?searchHighlight=parfeval&s_tid=srchtitle_parfeval_1), run on the workers. With the ThreadPool object, you can interact with the parallel pool.
Most matlab scripts cannot take advantage of more that one core (`--cpus-per-task=1`). If you want to use more than one core in your computations, use a parallel pool of threads.

A parallel pool of threads allows the allocation of threads form the local computing node to independent workers that then use the thread to evaluate functions in parallel. Create a parallel pool on using the [parpool](https://mathworks.com/help/parallel-computing/parallel.threadpool.html) function. After you create the pool, parallel pool features, such as [`parfor`](https://mathworks.com/help/parallel-computing/parallel-for-loops-parfor.html) or [`parfeval`](https://fr.mathworks.com/help/matlab/ref/parfeval.html?searchHighlight=parfeval&s_tid=srchtitle_parfeval_1), run computations on independent workers in parallel. With the ThreadPool object, you can interact with the parallel pool.


!!! example

```bash
# example for MATLAB ParFor (matlab_script_parallel_file.m)
parpool('local', str2num(getenv('SLURM_CPUS_PER_TASK'))) % set the default cores
p = parpool('local', str2num(getenv('SLURM_CPUS_PER_TASK'))) % set the default cores
%as number of threads
tic
n = 50;
A = 50;
a = zeros(1,n);
parfor i = 1:n
a(i) = max(abs(eig(rand(A))));
a(i) = max(abs(eig(rand(A))));
end
toc
delete(gcp); % you have to delete the parallel region after the work is done
Expand All @@ -128,9 +135,7 @@ After you create the pool, parallel pool features, such as [`parfor`](https://ma

## Additional information

To know more information about MATLAB tutorial and documentation,
please refer to [MATLAB tutorial](https://nl.mathworks.com/academia/books.html).
To know more information about MATLAB tutorial and documentation, please refer to [MATLAB tutorial](https://nl.mathworks.com/academia/books.html).

!!! tip
If you find some issues with the instructions above,
please report it to us using [support ticket](https://hpc.uni.lu/support).
If you find some issues with the instructions above, please report it to us using [support ticket](https://hpc.uni.lu/support).
Loading