<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.nimbios.org/index.php?action=history&amp;feed=atom&amp;title=Rocky_MATLAB_Prime_Array</id>
	<title>Rocky MATLAB Prime Array - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.nimbios.org/index.php?action=history&amp;feed=atom&amp;title=Rocky_MATLAB_Prime_Array"/>
	<link rel="alternate" type="text/html" href="https://wiki.nimbios.org/index.php?title=Rocky_MATLAB_Prime_Array&amp;action=history"/>
	<updated>2026-04-05T06:11:37Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.2</generator>
	<entry>
		<id>https://wiki.nimbios.org/index.php?title=Rocky_MATLAB_Prime_Array&amp;diff=216&amp;oldid=prev</id>
		<title>Jstratt7: Created page with &quot;= 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...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.nimbios.org/index.php?title=Rocky_MATLAB_Prime_Array&amp;diff=216&amp;oldid=prev"/>
		<updated>2023-04-29T02:13:08Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= 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...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Job Array =&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Batch File =&lt;br /&gt;
&lt;br /&gt;
There are three differences when turning this into a job array.  &lt;br /&gt;
&lt;br /&gt;
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).&lt;br /&gt;
&lt;br /&gt;
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 [https://slurm.schedmd.com/sbatch.html#SECTION_%3CB%3Efilename-pattern%3C/B%3E this link]&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''matlab_prime_array.run'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
&lt;br /&gt;
#SBATCH --job-name=MATLAB_PRIME&lt;br /&gt;
#SBATCH --output=logs/matlab_prime_array_%A_%a.out&lt;br /&gt;
#SBATCH --array=0-99&lt;br /&gt;
&lt;br /&gt;
module load MATLAB/2022b &lt;br /&gt;
&lt;br /&gt;
matlab -nojvm -batch &amp;quot;task_id = ${SLURM_ARRAY_TASK_ID}; run('prime_array.m');&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= matlab Code =&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''prime_array.m'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
chunksize = 10000;&lt;br /&gt;
&lt;br /&gt;
start = task_id * chunksize;&lt;br /&gt;
stop = start + chunksize;&lt;br /&gt;
&lt;br /&gt;
for i = start : stop&lt;br /&gt;
    if isprime(i)&lt;br /&gt;
        fprintf(&amp;quot;%d\n&amp;quot;, i)&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Running Job =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ pwd&lt;br /&gt;
/home/test_user/projects/matlab/prime_array/&lt;br /&gt;
&lt;br /&gt;
$ ls&lt;br /&gt;
logs  prime_array.m  matlab_prime_array.run&lt;br /&gt;
&lt;br /&gt;
$ sbatch matlab_prime_array.run &lt;br /&gt;
Submitted batch job 5771&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here we can see the job is queued with all 100 jobs.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ squeue&lt;br /&gt;
            JOBID   PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)&lt;br /&gt;
      5771_[0-99] compute_all MATLAB_P test_use PD       0:00      1 (Resources)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ squeue&lt;br /&gt;
             JOBID   PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)&lt;br /&gt;
      5771_[23-99] compute_all MATLAB_P test_use PD       0:00      1 (Resources)&lt;br /&gt;
           5771_22 compute_all MATLAB_P test_use  R       0:00      1 rocky2&lt;br /&gt;
           5771_20 compute_all MATLAB_P test_use  R       0:01      1 rocky2&lt;br /&gt;
           5771_21 compute_all MATLAB_P test_use  R       0:01      1 rocky2&lt;br /&gt;
            5771_3 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
            5771_4 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
            5771_5 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
            5771_6 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
            5771_7 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
            5771_8 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
            5771_9 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
           5771_10 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
           5771_11 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
           5771_12 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
           5771_13 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
           5771_14 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
           5771_15 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
           5771_16 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
           5771_17 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
           5771_18 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
           5771_19 compute_all MATLAB_P test_use  R       0:03      1 rocky2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ ls logs/*.out | sort -V &lt;br /&gt;
logs/matlab_prime_array_5771-0.out&lt;br /&gt;
logs/matlab_prime_array_5771-1.out&lt;br /&gt;
logs/matlab_prime_array_5771-2.out&lt;br /&gt;
logs/matlab_prime_array_5771-3.out&lt;br /&gt;
logs/matlab_prime_array_5771-4.out&lt;br /&gt;
logs/matlab_prime_array_5771-5.out&lt;br /&gt;
logs/matlab_prime_array_5771-6.out&lt;br /&gt;
logs/matlab_prime_array_5771-7.out&lt;br /&gt;
logs/matlab_prime_array_5771-8.out&lt;br /&gt;
logs/matlab_prime_array_5771-9.out&lt;br /&gt;
logs/matlab_prime_array_5771-10.out&lt;br /&gt;
logs/matlab_prime_array_5771-11.out&lt;br /&gt;
[truncated]&lt;br /&gt;
logs/matlab_prime_array_5771-90.out&lt;br /&gt;
logs/matlab_prime_array_5771-91.out&lt;br /&gt;
logs/matlab_prime_array_5771-92.out&lt;br /&gt;
logs/matlab_prime_array_5771-93.out&lt;br /&gt;
logs/matlab_prime_array_5771-94.out&lt;br /&gt;
logs/matlab_prime_array_5771-95.out&lt;br /&gt;
logs/matlab_prime_array_5771-96.out&lt;br /&gt;
logs/matlab_prime_array_5771-97.out&lt;br /&gt;
logs/matlab_prime_array_5771-98.out&lt;br /&gt;
logs/matlab_prime_array_5771-99.out&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We can see the results in order by using the cat command and then sorting the results.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ cat logs/matlab_prime_array_5771-*.out | sort -V&lt;br /&gt;
&lt;br /&gt;
3&lt;br /&gt;
5&lt;br /&gt;
7&lt;br /&gt;
11&lt;br /&gt;
13&lt;br /&gt;
17&lt;br /&gt;
19&lt;br /&gt;
23&lt;br /&gt;
29&lt;br /&gt;
31&lt;br /&gt;
37&lt;br /&gt;
41&lt;br /&gt;
43&lt;br /&gt;
47&lt;br /&gt;
53&lt;br /&gt;
59&lt;br /&gt;
61&lt;br /&gt;
[truncated]&lt;br /&gt;
999863&lt;br /&gt;
999883&lt;br /&gt;
999907&lt;br /&gt;
999917&lt;br /&gt;
999931&lt;br /&gt;
999953&lt;br /&gt;
999959&lt;br /&gt;
999961&lt;br /&gt;
999979&lt;br /&gt;
999983&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We can also use the wc command to count how many primes were found.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ cat logs/matlab_prime_array_5771-* | wc -l&lt;br /&gt;
78498&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jstratt7</name></author>
	</entry>
</feed>