Skip to content

Commit 9e29d66

Browse files
committed
Updated parallel Mathematica examples
1 parent 4426627 commit 9e29d66

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

Parallel_Computing/Mathematica/README.md

+38-8
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,57 @@ Program computes PI with parallel Monte-Carlo method.
44

55
### Contents:
66

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.
99

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:
1144

1245
```bash
1346
#!/bin/bash
1447
#SBATCH -J parallel_pi
1548
#SBATCH -o parallel_pi.out
1649
#SBATCH -e parallel_pi.err
17-
#SBATCH -p shared
50+
#SBATCH -p test
1851
#SBATCH -N 1
1952
#SBATCH -c 8
2053
#SBATCH -t 0-00:30
2154
#SBATCH --mem=4000
2255

2356
# 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
2658

2759
# Run program
2860
srun -n 1 -c 8 math -script parallel_pi.m
@@ -31,8 +63,6 @@ srun -n 1 -c 8 math -script parallel_pi.m
3163
### Example Usage:
3264

3365
```bash
34-
source new-modules.sh
35-
module load mathematica/11.1.1-fasrc01
3666
sbatch run.sbatch
3767
```
3868

Parallel_Computing/Mathematica/run.sbtach

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
#SBATCH -J parallel_pi
33
#SBATCH -o parallel_pi.out
44
#SBATCH -e parallel_pi.err
5-
#SBATCH -p shared
5+
#SBATCH -p test
66
#SBATCH -N 1
77
#SBATCH -c 8
88
#SBATCH -t 0-00:30
99
#SBATCH --mem=4000
1010

1111
# Load required software modules
12-
module load centos6/0.0.1-fasrc01
13-
module load mathematica/11.1.1-fasrc01
12+
module load mathematica/13.3.0-fasrc01
1413

1514
# Run program
1615
srun -n 1 -c 8 math -script parallel_pi.m

0 commit comments

Comments
 (0)