-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
32: add various ways to calculate pi
serial, disbatch, mpi4py + sbatch examples
- Loading branch information
Showing
9 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,25 @@ Activities where participants all actively work to foster an environment which e | |
|
||
|
||
|
||
## Environment management | ||
|
||
|
||
## What you'll need | ||
|
||
- Remote access to the cluster via terminal | ||
- on 'FI' wifi network: `ssh username@rusty` | ||
- or... `ssh -p 61022 [email protected]`, `ssh rusty` | ||
- or... `https://jupyter.flatironinstitute.org` | ||
- Way to edit files on cluster | ||
- terminal `emacs/vi/nano/ed` | ||
- or... remote edit via `vscode/emacs/vi/sshfs` | ||
- or... `https://jupyter.flatironinstitute.org` | ||
|
||
|
||
## | ||
|
||
|
||
|
||
## SciWare Survey [TODO] | ||
<!-- <center> --> | ||
<!-- <img width="50%" src="./qr.png"> --> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import os | ||
import numpy as np | ||
|
||
pivals = [] | ||
for fname in os.listdir('logs'): | ||
with open(os.path.join('logs', fname), 'r') as f: | ||
pivals.append(float(f.read())) | ||
|
||
pi = np.array(pivals).mean() | ||
print(f'{pi:.16f}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
|
||
module -q reset | ||
module load python | ||
if [[ ! -d "venv" ]]; then | ||
python -m venv venv --system-site-packages | ||
fi | ||
mkdir -p logs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#DISBATCH PREFIX seed=$DISBATCH_REPEAT_INDEX ; ( cd ~/mc_pi ; source load_env.sh ; python pi.py 100000 $seed ) &> logs/pi_s${seed}.log | ||
#DISBATCH REPEAT 256 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module -q reset | ||
module load openmpi python python-mpi | ||
source venv/bin/activate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import numpy as np | ||
from mpi4py import MPI | ||
import sys | ||
|
||
N = int(float(sys.argv[1])) | ||
comm = MPI.COMM_WORLD | ||
rank = comm.Get_rank() | ||
np.random.seed(rank) | ||
|
||
hits = 0 | ||
for i in range(N): | ||
hits += int(np.linalg.norm(np.random.uniform(low=-1.0, high=1.0, size=2)) <= 1.0) | ||
|
||
hits_arr = comm.gather(hits, root=0) | ||
if rank == 0: | ||
pi = 4 * np.array(hits_arr).mean() / N | ||
print(f'{pi:.16f}', np.abs(100 * (1 - pi/np.pi))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/bash | ||
#SBATCH -o mpi_pi.log | ||
#SBATCH -e mpi_pi.err | ||
#SBATCH -N 2 | ||
#SBATCH -c 1 | ||
#SBATCH -n 256 | ||
#SBATCH -p scc | ||
#SBATCH -C rome | ||
#SBATCH -t 1:00 | ||
|
||
source load_env.sh | ||
mpirun python mpi_pi.py 100000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import numpy as np | ||
import sys | ||
|
||
N = int(float(sys.argv[1])) | ||
seed = int(sys.argv[2]) | ||
np.random.seed(seed) | ||
|
||
hits = 0 | ||
for i in range(N): | ||
hits += int(np.linalg.norm(np.random.uniform(low=-1, high=1.0, size=2)) <= 1.0) | ||
|
||
pi = 4 * hits / N | ||
print(f'{pi:.16f}') |