nf_mpi_redux.f90 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 (found ",i4," ; i am ",i4,").")') comm_size, comm_w_rank
  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.ior.FSTARPU_COMMUTE)
  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.2) 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.1) 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)
  87. end if
  88. err = fstarpu_mpi_barrier(comm_world)
  89. tag = 0
  90. if(comm_w_rank.eq.0) then
  91. call fstarpu_variable_data_register(ahdl, 0, c_loc(a),c_sizeof(a))
  92. do i=1,comm_size-1
  93. call fstarpu_variable_data_register(bhdl(i), -1, c_null_ptr,c_sizeof(b(i)))
  94. end do
  95. else
  96. call fstarpu_variable_data_register(ahdl, -1, c_null_ptr,c_sizeof(a))
  97. do i=1,comm_size-1
  98. if (i.eq.comm_w_rank) then
  99. call fstarpu_variable_data_register(bhdl(i), 0, c_loc(b(i)),c_sizeof(b(i)))
  100. else
  101. call fstarpu_variable_data_register(bhdl(i), -1, c_null_ptr,c_sizeof(b(i)))
  102. end if
  103. end do
  104. end if
  105. call fstarpu_mpi_data_register(ahdl, tag, 0)
  106. do i=1,comm_size-1
  107. call fstarpu_mpi_data_register(bhdl(i), tag+i,i)
  108. end do
  109. tag = tag + comm_size
  110. call fstarpu_data_set_reduction_methods(ahdl,task_red_cl,task_ini_cl)
  111. err = fstarpu_mpi_barrier(comm_world)
  112. call fstarpu_fxt_start_profiling()
  113. do w_node=1,comm_size-1
  114. do i=1,work_coef*nworkers
  115. call fstarpu_mpi_task_insert( (/ c_loc(comm_world), &
  116. work_cl, &
  117. task_mode, ahdl, &
  118. FSTARPU_R, bhdl(w_node), &
  119. FSTARPU_EXECUTE_ON_NODE, c_loc(w_node), &
  120. C_NULL_PTR /))
  121. end do
  122. end do
  123. call fstarpu_mpi_redux_data(comm_world, ahdl)
  124. err = fstarpu_mpi_wait_for_all(comm_world)
  125. if(comm_w_rank.eq.0) then
  126. tmp = 0
  127. do w_node=1,comm_size-1
  128. tmp = tmp + 1.0 / (w_node+1.0)
  129. end do
  130. write(*,*) 'computed result ---> ',a, "expected =",&
  131. 1.0 + (comm_size-1.0)*(comm_size)/2.0 + work_coef*nworkers*((comm_size-1.0)*3.0 + tmp)
  132. end if
  133. err = fstarpu_mpi_barrier(comm_world)
  134. call fstarpu_data_unregister(ahdl)
  135. do w_node=1,comm_size-1
  136. call fstarpu_data_unregister(bhdl(w_node))
  137. end do
  138. call fstarpu_codelet_free(work_cl)
  139. end do
  140. call fstarpu_fxt_stop_profiling()
  141. call fstarpu_codelet_free(task_red_cl)
  142. call fstarpu_codelet_free(task_ini_cl)
  143. err = fstarpu_mpi_shutdown()
  144. call fstarpu_shutdown()
  145. deallocate(b, bhdl)
  146. stop 0
  147. contains
  148. recursive subroutine cl_cpu_task (buffers, cl_args) bind(C)
  149. use iso_c_binding ! C interfacing module
  150. use fstarpu_mod ! StarPU interfacing module
  151. implicit none
  152. type(c_ptr), value, intent(in) :: buffers, cl_args ! cl_args is unused
  153. integer(c_int) :: ret, worker_id
  154. integer :: comm_rank
  155. integer, target :: i
  156. real(kind(1.d0)), pointer :: a, b
  157. real(kind(1.d0)) :: old_a
  158. worker_id = fstarpu_worker_get_id()
  159. comm_rank = fstarpu_mpi_world_rank()
  160. call c_f_pointer(fstarpu_variable_get_ptr(buffers, 0), a)
  161. call c_f_pointer(fstarpu_variable_get_ptr(buffers, 1), b)
  162. call nf_sleep(1.d0)
  163. old_a = a
  164. a = old_a + 3.0 + b
  165. write(*,*) "task (c_w_rank:",comm_rank," worker_id:",worker_id,") from ",old_a,"to",a
  166. return
  167. end subroutine cl_cpu_task
  168. recursive subroutine cl_cpu_task_red (buffers, cl_args) bind(C)
  169. use iso_c_binding ! C interfacing module
  170. use fstarpu_mod ! StarPU interfacing module
  171. implicit none
  172. type(c_ptr), value, intent(in) :: buffers, cl_args ! cl_args is unused
  173. integer(c_int) :: ret, worker_id
  174. integer, target :: comm_rank
  175. real(kind(1.d0)), pointer :: as, ad
  176. real(kind(1.d0)) :: old_ad
  177. worker_id = fstarpu_worker_get_id()
  178. comm_rank = fstarpu_mpi_world_rank()
  179. call c_f_pointer(fstarpu_variable_get_ptr(buffers, 0), ad)
  180. call c_f_pointer(fstarpu_variable_get_ptr(buffers, 1), as)
  181. old_ad = ad
  182. ad = ad + as
  183. call nf_sleep(1.d0)
  184. write(*,*) "red_cl (c_w_rank:",comm_rank,"worker_id:",worker_id,")",as, old_ad, ' ---> ',ad
  185. return
  186. end subroutine cl_cpu_task_red
  187. recursive subroutine cl_cpu_task_ini (buffers, cl_args) bind(C)
  188. use iso_c_binding ! C interfacing module
  189. use fstarpu_mod ! StarPU interfacing module
  190. implicit none
  191. type(c_ptr), value, intent(in) :: buffers, cl_args
  192. ! cl_args is unused
  193. integer(c_int) :: ret, worker_id
  194. integer, target :: comm_rank
  195. real(kind(1.d0)), pointer :: a
  196. worker_id = fstarpu_worker_get_id()
  197. comm_rank = fstarpu_mpi_world_rank()
  198. call c_f_pointer(fstarpu_variable_get_ptr(buffers, 0), a)
  199. call nf_sleep(0.5d0)
  200. ! As this codelet is run by each worker in the REDUX mode case
  201. ! this initialization makes salient the number of copies spawned
  202. write(*,*) "ini_cl (c_w_rank:",comm_rank,"worker_id:",worker_id,") set to", comm_rank, "(was",a,")"
  203. a = comm_rank
  204. return
  205. end subroutine cl_cpu_task_ini
  206. subroutine nf_sleep(t)
  207. implicit none
  208. integer :: t_start, t_end, t_rate
  209. real(kind(1.d0)) :: ta, t
  210. call system_clock(t_start)
  211. do
  212. call system_clock(t_end, t_rate)
  213. ta = real(t_end-t_start)/real(t_rate)
  214. if(ta.gt.t) return
  215. end do
  216. end subroutine nf_sleep
  217. end program