Rocky MATLAB Prime Array

From NIMBioS
Revision as of 02:13, 29 April 2023 by Jstratt7 (talk | contribs) (Created page with "= Job Array = Job arrays allow you to run the same code many times with a different task id. The task id can then be used to determine which subset of your data to process. This strategy breaks your large job up into multiple smaller jobs that not only execute more quickly but can run concurrently. In the example of discovering prime numbers, lets say we want to discover all the primes in the first 1 million numbers. We could just create code that goes from 1 to 1000...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Job Array

Job arrays allow you to run the same code many times with a different task id. The task id can then be used to determine which subset of your data to process. This strategy breaks your large job up into multiple smaller jobs that not only execute more quickly but can run concurrently.

In the example of discovering prime numbers, lets say we want to discover all the primes in the first 1 million numbers. We could just create code that goes from 1 to 1000000. But if we use a job array, we could create 100 jobs that each search 10000 numbers.


Batch File

There are three differences when turning this into a job array.

First, we've added a SBATCH parameter to define not only how many jobs but the range of task ids to produce. In our example, we're making the range 0 to 99 (we could have also done 1-100).

Secondly, for the log file pattern, we're using %A and %a instead of %j. These are patterns specific to job arrays. You can read more about the file patterns at this link

Lastly, we pass the environment variable $SLURM_ARRAY_TASK_ID as a parameter to our code. We will need to read in this parameter and use it to determine what data to process. We know from our array definition that it will be a number from 0 to 99.


matlab_prime_array.run

#!/bin/bash

#SBATCH --job-name=MATLAB_PRIME
#SBATCH --output=logs/matlab_prime_array_%A_%a.out
#SBATCH --array=0-99

module load MATLAB/2022b 

matlab -nojvm -batch "task_id = ${SLURM_ARRAY_TASK_ID}; run('prime_array.m');"


matlab Code

In the matlab code, we'll need determine the MIN and MAX values to search. As long as we know our CHUNKSIZE, we should be able to calculate those values using the task id being passed in as a parameter. This way, each execution of the code will process different chunks of numbers.


prime_array.m

chunksize = 10000;

start = task_id * chunksize;
stop = start + chunksize;

for i = start : stop
    if isprime(i)
        fprintf("%d\n", i)
    end
end


Running Job

$ pwd
/home/test_user/projects/matlab/prime_array/

$ ls
logs  prime_array.m  matlab_prime_array.run

$ sbatch matlab_prime_array.run 
Submitted batch job 5771


Here we can see the job is queued with all 100 jobs.

$ squeue
            JOBID   PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
      5771_[0-99] compute_all MATLAB_P test_use PD       0:00      1 (Resources)


Here we can see the jobs are beginning to run. Two of the jobs have completed, 20 of them are currently running, and the rest are still queued.

$ squeue
             JOBID   PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
      5771_[23-99] compute_all MATLAB_P test_use PD       0:00      1 (Resources)
           5771_22 compute_all MATLAB_P test_use  R       0:00      1 rocky2
           5771_20 compute_all MATLAB_P test_use  R       0:01      1 rocky2
           5771_21 compute_all MATLAB_P test_use  R       0:01      1 rocky2
            5771_3 compute_all MATLAB_P test_use  R       0:03      1 rocky2
            5771_4 compute_all MATLAB_P test_use  R       0:03      1 rocky2
            5771_5 compute_all MATLAB_P test_use  R       0:03      1 rocky2
            5771_6 compute_all MATLAB_P test_use  R       0:03      1 rocky2
            5771_7 compute_all MATLAB_P test_use  R       0:03      1 rocky2
            5771_8 compute_all MATLAB_P test_use  R       0:03      1 rocky2
            5771_9 compute_all MATLAB_P test_use  R       0:03      1 rocky2
           5771_10 compute_all MATLAB_P test_use  R       0:03      1 rocky2
           5771_11 compute_all MATLAB_P test_use  R       0:03      1 rocky2
           5771_12 compute_all MATLAB_P test_use  R       0:03      1 rocky2
           5771_13 compute_all MATLAB_P test_use  R       0:03      1 rocky2
           5771_14 compute_all MATLAB_P test_use  R       0:03      1 rocky2
           5771_15 compute_all MATLAB_P test_use  R       0:03      1 rocky2
           5771_16 compute_all MATLAB_P test_use  R       0:03      1 rocky2
           5771_17 compute_all MATLAB_P test_use  R       0:03      1 rocky2
           5771_18 compute_all MATLAB_P test_use  R       0:03      1 rocky2
           5771_19 compute_all MATLAB_P test_use  R       0:03      1 rocky2


Once jobs are no longer listed in the queue, we see there is a log file for every task in the job array. Each one contains the prime numbers in their respective chunk.

$ ls logs/*.out | sort -V 
logs/matlab_prime_array_5771-0.out
logs/matlab_prime_array_5771-1.out
logs/matlab_prime_array_5771-2.out
logs/matlab_prime_array_5771-3.out
logs/matlab_prime_array_5771-4.out
logs/matlab_prime_array_5771-5.out
logs/matlab_prime_array_5771-6.out
logs/matlab_prime_array_5771-7.out
logs/matlab_prime_array_5771-8.out
logs/matlab_prime_array_5771-9.out
logs/matlab_prime_array_5771-10.out
logs/matlab_prime_array_5771-11.out
[truncated]
logs/matlab_prime_array_5771-90.out
logs/matlab_prime_array_5771-91.out
logs/matlab_prime_array_5771-92.out
logs/matlab_prime_array_5771-93.out
logs/matlab_prime_array_5771-94.out
logs/matlab_prime_array_5771-95.out
logs/matlab_prime_array_5771-96.out
logs/matlab_prime_array_5771-97.out
logs/matlab_prime_array_5771-98.out
logs/matlab_prime_array_5771-99.out


We can see the results in order by using the cat command and then sorting the results.

$ cat logs/matlab_prime_array_5771-*.out | sort -V

3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
[truncated]
999863
999883
999907
999917
999931
999953
999959
999961
999979
999983


We can also use the wc command to count how many primes were found.

$ cat logs/matlab_prime_array_5771-* | wc -l
78498