Skip to content

Commit b17c2c0

Browse files
committed
feat: add MPI wrappers
1 parent a3d9df3 commit b17c2c0

File tree

5 files changed

+188
-1
lines changed

5 files changed

+188
-1
lines changed

example/use_mpi.f90

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
program mpi_hello_world
2+
!! Use MPI wrappers analogous to Fortran's native parallel features
3+
use parallelism_m, only : mpi_t, init_, finalize_, this_image_, num_images_
4+
implicit none
5+
6+
type(mpi_t) mpi
7+
8+
call init_(mpi)
9+
print *,"Hello from image ",this_image_()," of ",num_images_()
10+
call finalize_(mpi)
11+
end program

fpm.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
name = "sourcery"
2-
version = "4.8.0"
2+
version = "4.8.1"
33
license = "BSD"
44
author = ["Damian Rouson"]
55
maintainer = "[email protected]"
66
copyright = "2020-2024 Sourcery Institute"
77

88
[dependencies]
99
assert = {git = "https://github.com/sourceryinstitute/assert", tag = "1.6.0"}
10+
mpi = "*"
11+
12+
[fortran]
13+
implicit-typing = true
14+
implicit-external = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
submodule(parallelism_m) mpi_parallelism_s
2+
!! Define wrappers for Message Passing Interface (MPI) procedures
3+
use mpi_f08
4+
use iso_fortran_env, only : error_unit
5+
implicit none
6+
7+
contains
8+
9+
module procedure error_stop_mpi_integer
10+
call MPI_Abort(mpi_comm_world, code)
11+
end procedure
12+
13+
module procedure error_stop_mpi_character
14+
write(error_unit,*) code
15+
call MPI_Abort(mpi_comm_world, errorcode=1)
16+
end procedure
17+
18+
module procedure init_mpi
19+
integer ierr
20+
call mpi_init(ierr)
21+
end procedure
22+
23+
module procedure finalize_mpi
24+
call mpi_finalize()
25+
end procedure
26+
27+
module procedure this_image_mpi
28+
integer rank, ierr
29+
call mpi_comm_rank(mpi_comm_world, rank, ierr)
30+
this_image_mpi = rank + 1
31+
end procedure
32+
33+
module procedure num_images_mpi
34+
integer ierr
35+
call mpi_comm_size(mpi_comm_world, num_images_mpi, ierr)
36+
end procedure
37+
38+
end submodule mpi_parallelism_s
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
submodule(parallelism_m) native_parallelism_s
2+
!! Define wrappers for Fortan's native parallel programming model
3+
implicit none
4+
5+
contains
6+
7+
module procedure error_stop_native_integer
8+
error stop code
9+
end procedure
10+
11+
module procedure error_stop_native_character
12+
error stop code
13+
end procedure
14+
15+
module procedure init_native
16+
end procedure
17+
18+
module procedure finalize_native
19+
end procedure
20+
21+
module procedure this_image_native
22+
this_image_native = this_image()
23+
end procedure
24+
25+
module procedure num_images_native
26+
num_images_native = num_images()
27+
end procedure
28+
29+
end submodule native_parallelism_s
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
module parallelism_m
2+
!! Use compile-time polymophism to select wrappers for native or alternative parallel progromming models
3+
implicit none
4+
5+
private
6+
public :: mpi_t ! alternative programming models
7+
8+
public :: error_stop_ ! execute error stop or print stop code, invoke MPI_Finalize, and invoke MPI_Abort
9+
!public :: co_broadcast_ ! call co_broadcast or MPI_Bcast
10+
!public :: co_sum_ ! call co_sum or MPI_Reduce
11+
!public :: co_min_ ! call co_min or MPI_Reduce
12+
!public :: co_max_ ! call co_max or MPI_Reduce
13+
!public :: co_reduce_ ! call co_reduce or MPI_Reduce
14+
public :: init_ ! do nothing or invoke MPI_Init
15+
public :: finalize_ ! do nothing or a invoke MPI_Finalize
16+
public :: num_images_ ! invoke num_images() or call MPI_Comm_Size
17+
!public :: sync_all_ ! execute sync all or invoke MPI_Barrier
18+
!public :: stop_ ! execute stop or print stop code, invoke MPI_Finalize, and then execute stop
19+
public :: this_image_ ! invoke this_image() or call MPI_Comm_Rank
20+
21+
type mpi_t
22+
end type
23+
24+
interface error_stop_
25+
26+
module subroutine error_stop_native_integer(code)
27+
implicit none
28+
integer, intent(in) :: code
29+
end subroutine
30+
31+
module subroutine error_stop_mpi_integer(mpi, code)
32+
implicit none
33+
type(mpi_t) mpi
34+
integer, intent(in) :: code
35+
end subroutine
36+
37+
module subroutine error_stop_native_character(code)
38+
implicit none
39+
character(len=*), intent(in) :: code
40+
end subroutine
41+
42+
module subroutine error_stop_mpi_character(mpi, code)
43+
implicit none
44+
type(mpi_t) mpi
45+
character(len=*), intent(in) :: code
46+
end subroutine
47+
48+
end interface
49+
50+
interface init_
51+
52+
module subroutine init_native()
53+
implicit none
54+
end subroutine
55+
56+
module subroutine init_mpi(mpi)
57+
implicit none
58+
type(mpi_t) mpi
59+
end subroutine
60+
61+
end interface
62+
63+
interface finalize_
64+
65+
module subroutine finalize_native()
66+
implicit none
67+
end subroutine
68+
69+
module subroutine finalize_mpi(mpi)
70+
implicit none
71+
type(mpi_t) mpi
72+
end subroutine
73+
74+
end interface
75+
76+
interface this_image_
77+
78+
integer module function this_image_native()
79+
implicit none
80+
end function
81+
82+
integer module function this_image_mpi(mpi)
83+
implicit none
84+
type(mpi_t) mpi
85+
end function
86+
87+
end interface
88+
89+
interface num_images_
90+
91+
integer module function num_images_native()
92+
implicit none
93+
end function
94+
95+
integer module function num_images_mpi(mpi)
96+
implicit none
97+
type(mpi_t) mpi
98+
end function
99+
100+
end interface
101+
102+
! ...
103+
104+
end module parallelism_m

0 commit comments

Comments
 (0)