MATLAB¶
Overview¶
MATLAB (Matrix Laboratory) is a high-level programming platform and numerical computing environment designed for engineers and scientists. It combines a desktop environment tuned for iterative analysis and design processes with a programming language that expresses matrix and array mathematics directly. MATLAB is widely used across various domains including signal processing, image processing, computer vision, communications, computational finance, control design, robotics, deep learning, and artificial intelligence.
Requirements¶
This guide focuses on using MATLAB's Parallel Computing Toolbox for remote job submission, as it provides the most straightforward method for working with MATLAB on the PERUN cluster.
MATLAB Version Compatibility¶
Local MATLAB installation must match the cluster version for remote job submission. PERUN currently has MATLAB R2025b installed.
Parallel Computing Toolbox¶
Remote job submission requires the Parallel Computing Toolbox.
During Installation: Select "Parallel Computing Toolbox" when installing MATLAB from MathWorks.
After Installation: 1. Open MATLAB 2. Home tab → Add-Ons → Get Add-Ons 3. Search for "Parallel Computing Toolbox" 4. Install
Configuring Parallel Computing Toolbox¶
Download SLURM Plugin¶
- Download the MATLAB Parallel Server plugin for SLURM from GitHub
- Extract the ZIP file to a permanent location (e.g.,
C:\Users\YourName\Documents\MATLAB\slurm-plugin)
Create Cluster Profile¶
- Open MATLAB
- Home tab → Environment section → Parallel → Create and Manage Clusters
- Note: The Cluster Profile Manager may take a moment to load as the module initializes
- Click Add Cluster Profile → Slurm
- Double-click the newly created profile in the Cluster Profiles list on the left to rename it (e.g., "PERUN")
Configure Cluster Profile¶
- Select your cluster profile and click Edit
- Update the following properties:
Properties Tab:
| Property | Value | Description |
|---|---|---|
ClusterMatlabRoot |
/mnt/netapp_home/apps/matlab |
MATLAB installation path on cluster |
JobStorageLocation |
C:\Users\YourName\Documents\MATLAB\jobs |
Local folder for job data |
PluginScriptsLocation |
C:\Users\YourName\Documents\MATLAB\slurm-plugin |
Path to extracted plugin |
AdditionalProperties:
| Property | Value | Type | Description |
|---|---|---|---|
ClusterHost |
login01.perun.tuke.sk |
String | PERUN login node |
Username |
your_username |
String | Your PERUN username |
RemoteJobStorageLocation |
/mnt/data/home/your_username/matlab |
String | Job storage on cluster |
IdentityFile |
C:\Users\YourName\.ssh\id_ed25519 |
String | SSH private key path |
UseIdentityFile |
true |
Logical | Enable SSH key authentication |
IdentityFileHasPassphrase |
false |
Logical | Set true if key has passphrase |
SSHPort |
22 |
Number | SSH port |
AuthenticationMode |
IdentityFile |
String | SSH authentication method |
Partition |
CPU |
String | SLURM partition (use GPU for GPU jobs) |
Time |
01:00:00 |
String | Default job time limit |
CPUsPerTask |
4 |
Number | CPU cores per task |
MPIImplementation |
IntelMPI |
String | Required for parallel jobs |
Important: The MPIImplementation property must be set to IntelMPI for parallel computing to work correctly on PERUN.
Validate Configuration¶
After configuring the cluster profile, validate the connection:
- In the Cluster Profile Manager, select your profile
- Click Validate
- The validation will run through multiple stages:
- ✅ Stage 1: Connection test - Verifies SSH connection to cluster
- ✅ Stage 2: Job submission test - Tests basic job submission
- ✅ Stage 3: Parallel workers test - Verifies worker communication (with
MPIImplementation: IntelMPI) - ⚠️ Stage 4: Interactive pool test - May fail (see note below)
Note: Interactive parpool functionality depends on network configuration and may not work in all cases.
Running Your First Job¶
Simple Test Job¶
% Get cluster object
c = parcluster('PERUN');
% Submit a simple job
job = c.batch(@sqrt, 1, {16});
% Wait for completion
wait(job);
% Fetch and display results
fetchOutputs(job);
Parallel Job Example¶
% Get cluster object
c = parcluster('PERUN');
% Submit job with parallel workers
job = c.batch(@rand, 1, {5, 5}, 'Pool', 4);
% Wait for completion
wait(job);
% Fetch and display results
fetchOutputs(job);
Loading MATLAB Module¶
On PERUN cluster:
# Load MATLAB module
module load matlab/R2025b
# Verify installation
which matlab
matlab -batch "version"
Interactive MATLAB Session¶
Command Line (No GUI)¶
In MATLAB:
With GUI (requires X11 forwarding)¶
Running MATLAB Scripts via SLURM¶
Example 1: Simple Script Execution¶
Create your MATLAB script my_script.m:
% my_script.m
disp('Starting computation...');
A = magic(5);
disp('Matrix A:');
disp(A);
disp('Sum of diagonal:');
disp(sum(diag(A)));
disp('Computation complete!');
Create SLURM job script run_matlab.slurm:
#!/bin/bash
#SBATCH --job-name=matlab_job
#SBATCH --partition=CPU
#SBATCH --time=00:10:00
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=4
#SBATCH --mem=8G
#SBATCH --output=matlab_%j.out
#SBATCH --error=matlab_%j.err
# Load MATLAB module
module load matlab/R2025b
# Run MATLAB script
matlab -batch "run('my_script.m')"
Submit:
Example 2: MATLAB Script with Parameters¶
MATLAB script process_data.m:
function process_data(n, filename)
% Process data with parameter n
fprintf('Processing with n=%d\n', n);
data = rand(n, n);
result = mean(data(:));
fprintf('Mean value: %.4f\n', result);
% Save results
save(filename, 'data', 'result');
fprintf('Results saved to %s\n', filename);
end
SLURM script run_matlab_params.slurm:
#!/bin/bash
#SBATCH --job-name=matlab_params
#SBATCH --partition=CPU
#SBATCH --time=00:15:00
#SBATCH --ntasks=1
#SBATCH --mem=16G
#SBATCH --output=matlab_params_%j.out
module load matlab/R2025b
# Run with parameters
matlab -batch "process_data(1000, 'results.mat')"
Example 3: Parallel MATLAB (using local parpool)¶
MATLAB script parallel_compute.m:
% parallel_compute.m
fprintf('Starting parallel computation...\n');
% Create local parallel pool
pool = parpool('local', str2double(getenv('SLURM_CPUS_PER_TASK')));
% Parallel computation
n = 1000;
results = zeros(n, 1);
parfor i = 1:n
A = rand(100);
results(i) = det(A);
end
fprintf('Computed %d determinants\n', n);
fprintf('Mean: %.4f\n', mean(results));
% Close pool
delete(pool);
fprintf('Done!\n');
SLURM script run_matlab_parallel.slurm:
#!/bin/bash
#SBATCH --job-name=matlab_parallel
#SBATCH --partition=CPU
#SBATCH --time=00:30:00
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=16
#SBATCH --mem=32G
#SBATCH --output=matlab_parallel_%j.out
module load matlab/R2025b
# Run parallel MATLAB script
matlab -batch "run('parallel_compute.m')"
Example 4: GPU Computing¶
MATLAB script gpu_script.m:
% gpu_script.m
fprintf('Running on GPU\n');
% Select GPU device
g = gpuDevice();
fprintf('Using GPU: %s\n', g.Name);
% Create random matrices on GPU
A = gpuArray(rand(1000));
B = A * A';
% Bring result back to CPU
result = gather(B);
% Save results
save('gpu_results.mat', 'result');
fprintf('Computation complete!\n');
SLURM script run_matlab_gpu.slurm:
#!/bin/bash
#SBATCH --job-name=matlab_gpu
#SBATCH --partition=GPU
#SBATCH --gres=gpu:1
#SBATCH --time=00:10:00
#SBATCH --ntasks=1
#SBATCH --mem=16G
#SBATCH --output=matlab_gpu_%j.out
module load matlab/R2025b
# Run GPU MATLAB script
matlab -batch "run('gpu_script.m')"
Submit:
Example 5: Array Job¶
MATLAB script array_task.m:
function array_task(task_id)
% Process task based on task_id
fprintf('Processing task %d\n', task_id);
% Your computation
rng(task_id); % Set random seed
data = rand(500, 500);
result = sum(data(:));
% Save results
filename = sprintf('result_%d.mat', task_id);
save(filename, 'result');
fprintf('Task %d complete, result: %.4f\n', task_id, result);
end
SLURM array job script run_matlab_array.slurm:
#!/bin/bash
#SBATCH --job-name=matlab_array
#SBATCH --partition=CPU
#SBATCH --time=00:10:00
#SBATCH --array=1-10
#SBATCH --ntasks=1
#SBATCH --mem=4G
#SBATCH --output=matlab_array_%A_%a.out
module load matlab/R2025b
# Run with array task ID
matlab -batch "array_task($SLURM_ARRAY_TASK_ID)"
Submit:
Example 6: Long Running Computation¶
MATLAB script long_compute.m:
% long_compute.m
fprintf('Starting long computation...\n');
start_time = tic;
max_time = 3600; % 1 hour
iteration = 0;
while toc(start_time) < max_time
iteration = iteration + 1;
% Your computation
A = rand(1000, 1000);
B = A * A';
if mod(iteration, 10) == 0
fprintf('Iteration %d, elapsed: %.1f min\n', ...
iteration, toc(start_time)/60);
end
end
fprintf('Completed %d iterations in %.1f minutes\n', ...
iteration, toc(start_time)/60);
SLURM script: