nf_mpi_redux.f90 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. ! StarPU --- Runtime system for heterogeneous multicore architectures.
  2. !
  3. ! Copyright (C) 2016-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. !
  5. ! StarPU is free software; you can redistribute it and/or modify
  6. ! it under the terms of the GNU Lesser General Public License as published by
  7. ! the Free Software Foundation; either version 2.1 of the License, or (at
  8. ! your option) any later version.
  9. !
  10. ! StarPU is distributed in the hope that it will be useful, but
  11. ! WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. !
  14. ! See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. !
  16. program nf_mpi_redux
  17. use iso_c_binding
  18. use fstarpu_mod
  19. use fstarpu_mpi_mod
  20. implicit none
  21. integer, target :: ret, np, i, j, trial
  22. type(c_ptr) :: work_cl, task_rw_cl,task_red_cl, task_ini_cl
  23. character(kind=c_char,len=*), parameter :: name=C_CHAR_"task"//C_NULL_CHAR
  24. character(kind=c_char,len=*), parameter :: namered=C_CHAR_"task_red"//C_NULL_CHAR
  25. character(kind=c_char,len=*), parameter :: nameini=C_CHAR_"task_ini"//C_NULL_CHAR
  26. real(kind(1.d0)), target :: a,tmp
  27. real(kind(1.d0)), target, allocatable :: b(:)
  28. integer(kind=8) :: tag, err
  29. type(c_ptr) :: ahdl
  30. type(c_ptr), target, allocatable :: bhdl(:)
  31. type(c_ptr) :: task_mode, codelet_mode
  32. integer, target :: comm_world,comm_w_rank, comm_size
  33. integer(c_int), target :: w_node, nworkers, work_coef
  34. call fstarpu_fxt_autostart_profiling(0)
  35. ret = fstarpu_init(c_null_ptr)
  36. ret = fstarpu_mpi_init(1)
  37. comm_world = fstarpu_mpi_world_comm()
  38. comm_w_rank = fstarpu_mpi_world_rank()
  39. comm_size = fstarpu_mpi_world_size()
  40. if (comm_size.lt.2) then
  41. write(*,'(" ")')
  42. write(*,'("This application is meant to run with at least two nodes.")')
  43. stop 2
  44. end if
  45. allocate(b(comm_size-1), bhdl(comm_size-1))
  46. nworkers = fstarpu_worker_get_count()
  47. if (nworkers.lt.1) then
  48. write(*,'(" ")')
  49. write(*,'("This application is meant to run with at least one worker per node.")')
  50. stop 2
  51. end if
  52. ! allocate and reduction codelets
  53. task_red_cl = fstarpu_codelet_allocate()
  54. call fstarpu_codelet_set_name(task_red_cl, namered)
  55. call fstarpu_codelet_add_cpu_func(task_red_cl,C_FUNLOC(cl_cpu_task_red))
  56. call fstarpu_codelet_add_buffer(task_red_cl, FSTARPU_RW)
  57. call fstarpu_codelet_add_buffer(task_red_cl, FSTARPU_R)
  58. task_ini_cl = fstarpu_codelet_allocate()
  59. call fstarpu_codelet_set_name(task_ini_cl, nameini)
  60. call fstarpu_codelet_add_cpu_func(task_ini_cl,C_FUNLOC(cl_cpu_task_ini))
  61. call fstarpu_codelet_add_buffer(task_ini_cl, FSTARPU_W)
  62. work_coef=2
  63. do trial=1,2
  64. if (trial.eq.1) then
  65. write(*,*) "Using STARPU_MPI_REDUX"
  66. codelet_mode = FSTARPU_RW.ior.FSTARPU_COMMUTE
  67. task_mode = FSTARPU_MPI_REDUX
  68. else if (trial.eq.2) then
  69. write(*,*) "Using STARPU_REDUX"
  70. codelet_mode = FSTARPU_REDUX
  71. task_mode = FSTARPU_REDUX
  72. end if
  73. ! allocate and fill codelet structs
  74. work_cl = fstarpu_codelet_allocate()
  75. call fstarpu_codelet_set_name(work_cl, name)
  76. call fstarpu_codelet_add_cpu_func(work_cl, C_FUNLOC(cl_cpu_task))
  77. call fstarpu_codelet_add_buffer(work_cl, codelet_mode)
  78. call fstarpu_codelet_add_buffer(work_cl, FSTARPU_R)
  79. err = fstarpu_mpi_barrier(comm_world)
  80. if(comm_w_rank.eq.0) then
  81. write(*,'(" ")')
  82. a = 1.0
  83. write(*,*) "init a = ", a
  84. else
  85. b(comm_w_rank) = 1.0 / (comm_w_rank + 1.0)
  86. write(*,*) "init b_",comm_w_rank,"=", b(comm_w_rank), " AT ", &
  87. c_loc(bhdl(comm_w_rank)) ! This is not really meaningful
  88. end if
  89. err = fstarpu_mpi_barrier(comm_world)
  90. tag = 0
  91. if(comm_w_rank.eq.0) then
  92. call fstarpu_variable_data_register(ahdl, 0, c_loc(a),c_sizeof(a))
  93. do i=1,comm_size-1
  94. call fstarpu_variable_data_register(bhdl(i), -1, c_null_ptr,c_sizeof(b(i)))
  95. end do
  96. else
  97. call fstarpu_variable_data_register(ahdl, -1, c_null_ptr,c_sizeof(a))
  98. do i=1,comm_size-1
  99. if (i.eq.comm_w_rank) then
  100. call fstarpu_variable_data_register(bhdl(i), 0, c_loc(b(i)),c_sizeof(b(i)))
  101. else
  102. call fstarpu_variable_data_register(bhdl(i), -1, c_null_ptr,c_sizeof(b(i)))
  103. end if
  104. end do
  105. end if
  106. call fstarpu_mpi_data_register(ahdl, tag, 0)
  107. do i=1,comm_size-1
  108. call fstarpu_mpi_data_register(bhdl(i), tag+i,i)
  109. end do
  110. tag = tag + comm_size
  111. call fstarpu_data_set_reduction_methods(ahdl,task_red_cl,task_ini_cl)
  112. err = fstarpu_mpi_barrier(comm_world)
  113. call fstarpu_fxt_start_profiling()
  114. do w_node=1,comm_size-1
  115. do i=1,work_coef*nworkers
  116. call fstarpu_mpi_task_insert( (/ c_loc(comm_world), &
  117. work_cl, &
  118. task_mode, ahdl, &
  119. FSTARPU_R, bhdl(w_node), &
  120. FSTARPU_EXECUTE_ON_NODE, c_loc(w_node), &
  121. C_NULL_PTR /))
  122. end do
  123. end do
  124. call fstarpu_mpi_redux_data(comm_world, ahdl)
  125. err = fstarpu_mpi_wait_for_all(comm_world)
  126. if(comm_w_rank.eq.0) then
  127. tmp = 0
  128. do w_node=1,comm_size-1
  129. tmp = tmp + 1.0 / (w_node+1.0)
  130. end do
  131. write(*,*) 'computed result ---> ',a, "expected =",&
  132. 1.0 + (comm_size-1.0)*(comm_size)/2.0 + work_coef*nworkers*((comm_size-1.0)*3.0 + tmp)
  133. end if
  134. err = fstarpu_mpi_barrier(comm_world)
  135. call fstarpu_data_unregister(ahdl)
  136. do w_node=1,comm_size-1
  137. call fstarpu_data_unregister(bhdl(w_node))
  138. end do
  139. call fstarpu_codelet_free(work_cl)
  140. end do
  141. call fstarpu_fxt_stop_profiling()
  142. call fstarpu_codelet_free(task_red_cl)
  143. call fstarpu_codelet_free(task_ini_cl)
  144. err = fstarpu_mpi_shutdown()
  145. call fstarpu_shutdown()
  146. deallocate(b, bhdl)
  147. stop
  148. contains
  149. recursive subroutine cl_cpu_task (buffers, cl_args) bind(C)
  150. use iso_c_binding ! C interfacing module
  151. use fstarpu_mod ! StarPU interfacing module
  152. implicit none
  153. type(c_ptr), value, intent(in) :: buffers, cl_args ! cl_args is unused
  154. integer(c_int) :: ret, worker_id
  155. integer :: comm_rank
  156. integer, target :: i
  157. real(kind(1.d0)), pointer :: a, b
  158. real(kind(1.d0)) :: old_a
  159. worker_id = fstarpu_worker_get_id()
  160. comm_rank = fstarpu_mpi_world_rank()
  161. call c_f_pointer(fstarpu_variable_get_ptr(buffers, 0), a)
  162. call c_f_pointer(fstarpu_variable_get_ptr(buffers, 1), b)
  163. call nf_sleep(1.d0)
  164. old_a = a
  165. a = old_a + 3.0 + b
  166. write(*,*) "task (c_w_rank:",comm_rank," worker_id:",worker_id,") from ",old_a,"to",a
  167. return
  168. end subroutine cl_cpu_task
  169. recursive subroutine cl_cpu_task_red (buffers, cl_args) bind(C)
  170. use iso_c_binding ! C interfacing module
  171. use fstarpu_mod ! StarPU interfacing module
  172. implicit none
  173. type(c_ptr), value, intent(in) :: buffers, cl_args ! cl_args is unused
  174. integer(c_int) :: ret, worker_id
  175. integer, target :: comm_rank
  176. real(kind(1.d0)), pointer :: as, ad
  177. real(kind(1.d0)) :: old_ad
  178. worker_id = fstarpu_worker_get_id()
  179. comm_rank = fstarpu_mpi_world_rank()
  180. call c_f_pointer(fstarpu_variable_get_ptr(buffers, 0), ad)
  181. call c_f_pointer(fstarpu_variable_get_ptr(buffers, 1), as)
  182. old_ad = ad
  183. ad = ad + as
  184. call nf_sleep(1.d0)
  185. write(*,*) "red_cl (c_w_rank:",comm_rank,"worker_id:",worker_id,")",as, old_ad, ' ---> ',ad
  186. return
  187. end subroutine cl_cpu_task_red
  188. recursive subroutine cl_cpu_task_ini (buffers, cl_args) bind(C)
  189. use iso_c_binding ! C interfacing module
  190. use fstarpu_mod ! StarPU interfacing module
  191. implicit none
  192. type(c_ptr), value, intent(in) :: buffers, cl_args
  193. ! cl_args is unused
  194. integer(c_int) :: ret, worker_id
  195. integer, target :: comm_rank
  196. real(kind(1.d0)), pointer :: a
  197. worker_id = fstarpu_worker_get_id()
  198. comm_rank = fstarpu_mpi_world_rank()
  199. call c_f_pointer(fstarpu_variable_get_ptr(buffers, 0), a)
  200. call nf_sleep(0.5d0)
  201. ! As this codelet is run by each worker in the REDUX mode case
  202. ! this initialization makes salient the number of copies spawned
  203. write(*,*) "ini_cl (c_w_rank:",comm_rank,"worker_id:",worker_id,") set to", comm_rank, "(was",a,")"
  204. a = comm_rank
  205. return
  206. end subroutine cl_cpu_task_ini
  207. subroutine nf_sleep(t)
  208. implicit none
  209. integer :: t_start, t_end, t_rate
  210. real(kind(1.d0)) :: ta, t
  211. call system_clock(t_start)
  212. do
  213. call system_clock(t_end, t_rate)
  214. ta = real(t_end-t_start)/real(t_rate)
  215. if(ta.gt.t) return
  216. end do
  217. end subroutine nf_sleep
  218. end program