Skip to content

Commit

Permalink
Adding MPI material
Browse files Browse the repository at this point in the history
  • Loading branch information
stammler committed Dec 1, 2023
1 parent 9577ba1 commit 4398a6a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
8 changes: 8 additions & 0 deletions presentations/2023_11_17_mpi/README.txt
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions presentations/2023_11_17_mpi/demo/array/parent.py
Original file line number Diff line number Diff line change
@@ -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()
23 changes: 23 additions & 0 deletions presentations/2023_11_17_mpi/demo/array/worker.f
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
18 changes: 18 additions & 0 deletions presentations/2023_11_17_mpi/demo/simple/parent.py
Original file line number Diff line number Diff line change
@@ -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()
32 changes: 32 additions & 0 deletions presentations/2023_11_17_mpi/demo/simple/worker.f
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4398a6a

Please sign in to comment.