@@ -4,25 +4,57 @@ Program computes PI with parallel Monte-Carlo method.
4
4
5
5
### Contents:
6
6
7
- * parallel_pi.m: Mathematica source code
8
- * run.sbatch: Btach-job submission script to send the job to the queue.
7
+ * ` parallel_pi.m ` : Mathematica source code
8
+ * ` run.sbatch ` : Btach-job submission script to send the job to the queue.
9
9
10
- #### Example Batch-Job Submission Script:
10
+ ### Source code:
11
+
12
+ ``` mathematica
13
+ (* Compute PI in via parallel Monte-Carlo method *)
14
+ tstart = AbsoluteTime[];
15
+ Print [ "Parallel calculation of PI via Monte-Carlo method." ];
16
+
17
+ nproc = 8;
18
+ LaunchKernels[nproc];
19
+
20
+ Print[ " Number of kernels: ", nproc];
21
+
22
+ n = 10^8;
23
+ m = n / nproc;
24
+ Print [ " Total number of hits: ", n ];
25
+ Print [ " Number of hits per core: ", m ];
26
+
27
+ acceptpoint[j_] := Total[Table[ 1 - Floor[ Sqrt[ (Random[])^2 + (Random[])^2 ] ], {i,1,m} ] ];
28
+ DistributeDefinitions[n,m,acceptpoint];
29
+ t1 = AbsoluteTime[];
30
+ hits = ParallelTable[acceptpoint[j], {j,1,nproc} ];
31
+ t2 = AbsoluteTime[];
32
+ tt = t2 - t1;
33
+ hits = Total[ hits ];
34
+ pi = hits / ( nproc * m ) * 4.0;
35
+ Print [ " Computed PI = ", pi ];
36
+ Print [ " Time in parallel calculation: ", tt ] ;
37
+ tend = AbsoluteTime[];
38
+ ttotal = tend - tstart;
39
+ Print [ " Total time: ", ttotal ];
40
+ Quit [ ]
41
+ ```
42
+
43
+ ### Example Batch-Job Submission Script:
11
44
12
45
``` bash
13
46
#! /bin/bash
14
47
# SBATCH -J parallel_pi
15
48
# SBATCH -o parallel_pi.out
16
49
# SBATCH -e parallel_pi.err
17
- # SBATCH -p shared
50
+ # SBATCH -p test
18
51
# SBATCH -N 1
19
52
# SBATCH -c 8
20
53
# SBATCH -t 0-00:30
21
54
# SBATCH --mem=4000
22
55
23
56
# Load required software modules
24
- source new-modules.sh
25
- module load mathematica/11.1.1-fasrc01
57
+ module load mathematica/13.3.0-fasrc01
26
58
27
59
# Run program
28
60
srun -n 1 -c 8 math -script parallel_pi.m
@@ -31,8 +63,6 @@ srun -n 1 -c 8 math -script parallel_pi.m
31
63
### Example Usage:
32
64
33
65
``` bash
34
- source new-modules.sh
35
- module load mathematica/11.1.1-fasrc01
36
66
sbatch run.sbatch
37
67
```
38
68
0 commit comments