Skip to content

Commit

Permalink
32: add various ways to calculate pi
Browse files Browse the repository at this point in the history
serial, disbatch, mpi4py + sbatch examples
  • Loading branch information
blackwer committed Apr 11, 2024
1 parent bae5908 commit c2f2f1c
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 32_IntroToHPC/assets/square-circle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions 32_IntroToHPC/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"> -->
Expand Down
10 changes: 10 additions & 0 deletions 32_IntroToHPC/mc_pi/average_logs.py
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}')
8 changes: 8 additions & 0 deletions 32_IntroToHPC/mc_pi/create_env.sh
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
2 changes: 2 additions & 0 deletions 32_IntroToHPC/mc_pi/disbatch_taskfile
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
3 changes: 3 additions & 0 deletions 32_IntroToHPC/mc_pi/load_env.sh
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
17 changes: 17 additions & 0 deletions 32_IntroToHPC/mc_pi/mpi_pi.py
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)))
12 changes: 12 additions & 0 deletions 32_IntroToHPC/mc_pi/mpi_pi.sbatch
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
13 changes: 13 additions & 0 deletions 32_IntroToHPC/mc_pi/pi.py
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}')

0 comments on commit c2f2f1c

Please sign in to comment.