-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.f90
102 lines (78 loc) · 2.6 KB
/
main.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
!> Fortran 90 demo of Python embedding
!!
!! To compile:
!! mpif90 -ldl main.f90 execute_py.o
!!
program exec_py
implicit none
include 'mpif.h'
interface
function init_python_interpreter(python_so) bind(C)
use iso_c_binding, only: c_int, c_char
integer(c_int) :: init_python_interpreter !< Return value
character(kind=c_char), intent(in) :: python_so(*) !< Python lib path
end function
function init_python_interpreter_from_env(env_python_so) bind(C)
use iso_c_binding, only: c_int, c_char
integer(c_int) :: init_python_interpreter_from_env !< Return value
character(kind=c_char), intent(in) :: env_python_so(*) !< Environment variable containing Python lib path
end function
function execute_python_file(python_script) bind(C)
use iso_c_binding, only: c_int, c_char
integer(c_int) :: execute_python_file !< Return value
character(kind=c_char), intent(in) :: python_script(*) !< Python script name
end function
function close_python_interpreter() bind(C)
use iso_c_binding, only: c_int, c_char
integer(c_int) :: close_python_interpreter !< Return value
end function
end interface
!
! Main program
!
integer :: narg
integer :: ierr, mrank, msize
character(len=256) :: python_so, python_script
narg = command_argument_count()
if(narg /= 2) then
print*, " Requires 2 args: Python shared lib location + script"
stop
endif
call get_command_argument(1, python_so)
call get_command_argument(2, python_script)
! MPI part
call mpi_init(ierr)
call mpi_comm_size(MPI_COMM_WORLD, msize, ierr)
call mpi_comm_rank(MPI_COMM_WORLD, mrank, ierr)
if(mrank == 0) then
print*, " Python lib: ", trim(python_so)
print*, " Python script: ", trim(python_script)
endif
print *, " I am rank ", mrank, " out of ", msize
!launch python
!ierr = init_python_interpreter(f2c_string(python_so))
ierr = init_python_interpreter_from_env(f2c_string(python_so))
!execute script
ierr = execute_python_file(f2c_string(python_script))
call mpi_barrier(MPI_COMM_WORLD,ierr)
!in between calls
print *, " Call between: I am rank ", mrank, " out of ", msize
call mpi_barrier(MPI_COMM_WORLD,ierr)
!execute script again
ierr = execute_python_file(f2c_string(python_script))
call mpi_barrier(MPI_COMM_WORLD,ierr)
!close python
ierr = close_python_interpreter()
call mpi_finalize(ierr)
!
! Helper function
!
contains
function f2c_string(f_str)
use iso_c_binding, only: c_char, c_null_char
implicit none
character(len=*), intent(in) :: f_str
character(kind=c_char, len=255) :: f2c_string
f2c_string = trim(f_str)//c_null_char
end function
end program