diff --git a/scripts/DelftBlue commands.txt b/scripts/DelftBlue commands.txt index e74e5cd82..cceb1cade 100644 --- a/scripts/DelftBlue commands.txt +++ b/scripts/DelftBlue commands.txt @@ -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. diff --git a/scripts/my_model.py b/scripts/my_model.py index 2ab366ba8..6f4e7176c 100644 --- a/scripts/my_model.py +++ b/scripts/my_model.py @@ -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) diff --git a/scripts/test_script.sh b/scripts/test_script.sh index 4756a22b9..4fc0f3f48 100644 --- a/scripts/test_script.sh +++ b/scripts/test_script.sh @@ -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