Skip to content

Commit

Permalink
script: Use mpiexec
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutH committed Aug 30, 2023
1 parent 1a94011 commit 0a61510
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
2 changes: 1 addition & 1 deletion scripts/DelftBlue commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cd "$directory"
cd C:\Users\Ewout\Documents\GitHub\EMAworkbench\scripts
scp -J eterhoeven@student-linux.tudelft.nl my_model.py test_script.sh eterhoeven@login.delftblue.tudelft.nl:/scratch/eterhoeven/python-test

# Check if files are correctly tranfered
# Check if files are correctly tranfered (Logged in)
ls

# Note: bash scripts (sh) needs LF line endings. Do that in atom.
Expand Down
29 changes: 12 additions & 17 deletions scripts/my_model.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
# my_model.py
from mpi4py import MPI
import numpy as np
import pickle
from mpi4py.futures import MPIPoolExecutor

def my_model(data):
# This could be any function that takes inputs and produces outputs
x, y = data
return x**2 + y**2
result = x**2 + y**2

def main():
# Define the input data
data = np.arange(20).reshape(-1, 2) # Let's say we have 10 sets of (x, y) pairs
# Get the rank of the current MPI process.
rank = MPI.COMM_WORLD.Get_rank()

return rank, result

# The size of COMM_WORLD gives us the number of processes across all nodes
n_cores = MPI.COMM_WORLD.Get_size()
n_jobs = len(data) # Your actual number of jobs
if __name__ == "__main__":
data_list = [(i, j) for i in range(10) for j in range(10)]

if n_jobs > n_cores:
n_jobs = n_cores
with MPIPoolExecutor() as executor:
results = list(executor.map(my_model, data_list))

with MPIPoolExecutor(max_workers=n_jobs) as executor:
# Apply the model to the data
results = list(executor.map(my_model, data))

# Print out the results
print(results)
# Print results along with the rank of the MPI process.
for data, (rank, result) in zip(data_list, results):
print(f"Rank: {rank}, Data: {data}, Result: {result}")

with open('py_test.pickle', 'wb') as handle:
pickle.dump(results, handle, protocol=pickle.HIGHEST_PROTOCOL)
Expand Down
2 changes: 1 addition & 1 deletion scripts/test_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ module load python
module load py-numpy
module load py-mpi4py

python my_model.py > py_test.log
mpiexec -n 10 python -m mpi4py.futures my_model.py > py_test.log

0 comments on commit 0a61510

Please sign in to comment.