diff --git a/presentations/2023_11_17_mpi/README.txt b/presentations/2023_11_17_mpi/README.txt new file mode 100644 index 0000000..34a64a6 --- /dev/null +++ b/presentations/2023_11_17_mpi/README.txt @@ -0,0 +1,8 @@ +Requirement: +OpenMPI, mpi4py for parent.py + +Compilation: +mpifort -o worker -fopenmp worker.f + +Execution: +mpiexec -n 1 python parent.py : -n 1 worker diff --git a/presentations/2023_11_17_mpi/demo/array/parent.py b/presentations/2023_11_17_mpi/demo/array/parent.py new file mode 100644 index 0000000..63f343f --- /dev/null +++ b/presentations/2023_11_17_mpi/demo/array/parent.py @@ -0,0 +1,19 @@ +from mpi4py import MPI +import numpy as np + +# MPI initialization +comm = MPI.COMM_WORLD + +# Send array to Fortran code +comm.Send([np.array([[10,11],[12,13]],dtype='i4'), MPI.INT], dest=1, tag=3) + +# Send array to Fortran code +#comm.Send([np.array([[10,11],[12,13]],dtype='i4',order='F'), MPI.INT], dest=1, tag=3) + +#arr[0][0] = 10 +#arr[0][1] = 11 +#arr[1][0] = 12 +#arr[1][1] = 13 + +# MPI Finalize +MPI.Finalize() diff --git a/presentations/2023_11_17_mpi/demo/array/worker.f b/presentations/2023_11_17_mpi/demo/array/worker.f new file mode 100644 index 0000000..8ad8ff3 --- /dev/null +++ b/presentations/2023_11_17_mpi/demo/array/worker.f @@ -0,0 +1,23 @@ + use mpi + use omp_lib + implicit none + + integer ierr,comm,i,j + integer arr(2,2) + real*8 num +! MPI initialization + call MPI_INIT(ierr) + comm = MPI_COMM_WORLD + +! Receive array from Python code + call MPI_RECV(arr,4,MPI_INT,0,3,comm,MPI_STATUS_IGNORE,ierr) + do i=1,2 + do j=1,2 + write(*,*) arr(i,j) + enddo + enddo + +! MPI Finalize + call MPI_FINALIZE(ierr) + return + end diff --git a/presentations/2023_11_17_mpi/demo/presentation.pdf b/presentations/2023_11_17_mpi/demo/presentation.pdf new file mode 100644 index 0000000..e7dddfd Binary files /dev/null and b/presentations/2023_11_17_mpi/demo/presentation.pdf differ diff --git a/presentations/2023_11_17_mpi/demo/simple/parent.py b/presentations/2023_11_17_mpi/demo/simple/parent.py new file mode 100644 index 0000000..b6d86f4 --- /dev/null +++ b/presentations/2023_11_17_mpi/demo/simple/parent.py @@ -0,0 +1,18 @@ +from mpi4py import MPI +import numpy as np + +# MPI initialization +comm = MPI.COMM_WORLD +rank = comm.Get_rank() +print('Python rank: ' + str(rank)) + +# Send integer to Fortran code +comm.Send([np.array([12489],dtype='i4'), MPI.INT], dest=1, tag=1) + +# Receive double from Fortran code +num = np.array([0.],dtype='f8') +comm.Recv([num, MPI.DOUBLE], source=1, tag=2) +print('Python receives: ' + str(num)) + +# MPI Finalize +MPI.Finalize() diff --git a/presentations/2023_11_17_mpi/demo/simple/worker.f b/presentations/2023_11_17_mpi/demo/simple/worker.f new file mode 100644 index 0000000..514e3bb --- /dev/null +++ b/presentations/2023_11_17_mpi/demo/simple/worker.f @@ -0,0 +1,32 @@ + use mpi + use omp_lib + implicit none + + integer ierr,var,comm,rank,i + real*8 num +! MPI initialization + call MPI_INIT(ierr) + comm = MPI_COMM_WORLD + call MPI_COMM_RANK(comm, rank, ierr) + write(*,*) 'Fortran rank: ',rank + +! Receive integer from Python code and print it + call MPI_RECV(var,1,MPI_INT,0,1,comm,MPI_STATUS_IGNORE,ierr) + write(*,*) 'Fortran receives: ',var + +! Send double to Python code + num = 3.d6 + call MPI_SEND(num,1,MPI_DOUBLE_PRECISION,0,2,comm,ierr) + +! OpenMP in Fortran code + call omp_set_num_threads(4) +!$OMP PARALLEL DO + do i=1,4 + write(*,*) 'Fortran OpenMP: ',i + enddo +!$OMP END PARALLEL DO + +! MPI Finalize + call MPI_FINALIZE(ierr) + return + end