fstarpu_mod.f90 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. ! StarPU --- Runtime system for heterogeneous multicore architectures.
  2. !
  3. ! Copyright (C) 2016 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. module fstarpu_mod
  16. use iso_c_binding
  17. implicit none
  18. ! Note: Constants truly are intptr_t, but are declared as c_ptr to be
  19. ! readily usable in c_ptr arrays to mimic variadic functions.
  20. ! A side effect, though, is that such constants cannot be logically
  21. ! 'or'-ed.
  22. type(c_ptr), bind(C) :: FSTARPU_R
  23. type(c_ptr), bind(C) :: FSTARPU_W
  24. type(c_ptr), bind(C) :: FSTARPU_RW
  25. type(c_ptr), bind(C) :: FSTARPU_SCRATCH
  26. type(c_ptr), bind(C) :: FSTARPU_REDUX
  27. type(c_ptr), bind(C) :: FSTARPU_DATA
  28. type(c_ptr), bind(C) :: FSTARPU_VALUE
  29. type(c_ptr), bind(C) :: FSTARPU_SZ_INT4
  30. type(c_ptr), bind(C) :: FSTARPU_SZ_INT8
  31. type(c_ptr), bind(C) :: FSTARPU_SZ_REAL4
  32. type(c_ptr), bind(C) :: FSTARPU_SZ_REAL8
  33. interface
  34. ! == starpu.h ==
  35. ! starpu_conf_init: see fstarpu_conf_allocate
  36. function fstarpu_conf_allocate () bind(C)
  37. use iso_c_binding, only: c_ptr
  38. type(c_ptr) :: fstarpu_conf_allocate
  39. end function fstarpu_conf_allocate
  40. subroutine fstarpu_conf_free (cl) bind(C)
  41. use iso_c_binding, only: c_ptr
  42. type(c_ptr), value, intent(in) :: cl
  43. end subroutine fstarpu_conf_free
  44. subroutine fstarpu_conf_set_sched_policy_name (conf, policy_name) bind(C)
  45. use iso_c_binding, only: c_ptr, c_char
  46. type(c_ptr), value, intent(in) :: conf
  47. character(c_char), intent(in) :: policy_name
  48. end subroutine fstarpu_conf_set_sched_policy_name
  49. subroutine fstarpu_conf_set_min_prio (conf, min_prio) bind(C)
  50. use iso_c_binding, only: c_ptr, c_int
  51. type(c_ptr), value, intent(in) :: conf
  52. integer(c_int), value, intent(in) :: min_prio
  53. end subroutine fstarpu_conf_set_min_prio
  54. subroutine fstarpu_conf_set_max_prio (conf, max_prio) bind(C)
  55. use iso_c_binding, only: c_ptr, c_int
  56. type(c_ptr), value, intent(in) :: conf
  57. integer(c_int), value, intent(in) :: max_prio
  58. end subroutine fstarpu_conf_set_max_prio
  59. subroutine fstarpu_conf_set_ncpu (conf, ncpu) bind(C)
  60. use iso_c_binding, only: c_ptr, c_int
  61. type(c_ptr), value, intent(in) :: conf
  62. integer(c_int), value, intent(in) :: ncpu
  63. end subroutine fstarpu_conf_set_ncpu
  64. subroutine fstarpu_conf_set_ncuda (conf, ncuda) bind(C)
  65. use iso_c_binding, only: c_ptr, c_int
  66. type(c_ptr), value, intent(in) :: conf
  67. integer(c_int), value, intent(in) :: ncuda
  68. end subroutine fstarpu_conf_set_ncuda
  69. subroutine fstarpu_conf_set_nopencl (conf, nopencl) bind(C)
  70. use iso_c_binding, only: c_ptr, c_int
  71. type(c_ptr), value, intent(in) :: conf
  72. integer(c_int), value, intent(in) :: nopencl
  73. end subroutine fstarpu_conf_set_nopencl
  74. subroutine fstarpu_conf_set_nmic (conf, nmic) bind(C)
  75. use iso_c_binding, only: c_ptr, c_int
  76. type(c_ptr), value, intent(in) :: conf
  77. integer(c_int), value, intent(in) :: nmic
  78. end subroutine fstarpu_conf_set_nmic
  79. subroutine fstarpu_conf_set_nscc (conf, nscc) bind(C)
  80. use iso_c_binding, only: c_ptr, c_int
  81. type(c_ptr), value, intent(in) :: conf
  82. integer(c_int), value, intent(in) :: nscc
  83. end subroutine fstarpu_conf_set_nscc
  84. ! starpu_init: see fstarpu_init
  85. ! starpu_initialize: see fstarpu_init
  86. subroutine fstarpu_pause() bind(C,name="starpu_pause")
  87. end subroutine fstarpu_pause
  88. subroutine fstarpu_resume() bind(C,name="starpu_resume")
  89. end subroutine fstarpu_resume
  90. subroutine fstarpu_shutdown () bind(C,name="starpu_shutdown")
  91. end subroutine fstarpu_shutdown
  92. ! starpu_topology_print
  93. subroutine fstarpu_asynchronous_copy_disabled() bind(C,name="starpu_asynchronous_copy_disabled")
  94. end subroutine fstarpu_asynchronous_copy_disabled
  95. subroutine fstarpu_asynchronous_cuda_copy_disabled() bind(C,name="starpu_asynchronous_cuda_copy_disabled")
  96. end subroutine fstarpu_asynchronous_cuda_copy_disabled
  97. subroutine fstarpu_asynchronous_opencl_copy_disabled() bind(C,name="starpu_asynchronous_opencl_copy_disabled")
  98. end subroutine fstarpu_asynchronous_opencl_copy_disabled
  99. subroutine fstarpu_asynchronous_mic_copy_disabled() bind(C,name="starpu_asynchronous_mic_copy_disabled")
  100. end subroutine fstarpu_asynchronous_mic_copy_disabled
  101. subroutine fstarpu_display_stats() bind(C,name="starpu_display_stats")
  102. end subroutine fstarpu_display_stats
  103. subroutine fstarpu_get_version(major,minor,release) bind(C,name="starpu_get_version")
  104. use iso_c_binding, only: c_int
  105. integer(c_int), intent(out) :: major,minor,release
  106. end subroutine fstarpu_get_version
  107. function fstarpu_cpu_worker_get_count() bind(C,name="starpu_cpu_worker_get_count")
  108. use iso_c_binding, only: c_int
  109. integer(c_int) :: fstarpu_cpu_worker_get_count
  110. end function fstarpu_cpu_worker_get_count
  111. ! == starpu_task.h ==
  112. ! starpu_tag_declare_deps
  113. ! starpu_tag_declare_deps_array
  114. ! starpu_task_declare_deps_array
  115. ! starpu_tag_wait
  116. ! starpu_tag_wait_array
  117. ! starpu_tag_notify_from_apps
  118. ! starpu_tag_restart
  119. ! starpu_tag_remove
  120. ! starpu_task_init
  121. ! starpu_task_clean
  122. ! starpu_task_create
  123. ! starpu_task_destroy
  124. ! starpu_task_submit
  125. ! starpu_task_submit_to_ctx
  126. ! starpu_task_finished
  127. ! starpu_task_wait
  128. subroutine fstarpu_task_wait_for_all () bind(C,name="starpu_task_wait_for_all")
  129. end subroutine fstarpu_task_wait_for_all
  130. ! starpu_task_wait_for_n_submitted
  131. ! starpu_task_wait_for_all_in_ctx
  132. ! starpu_task_wait_for_n_submitted_in_ctx
  133. ! starpu_task_wait_for_no_ready
  134. ! starpu_task_nready
  135. ! starpu_task_nsubmitted
  136. ! starpu_codelet_init
  137. ! starpu_codelet_display_stats
  138. ! starpu_task_get_current
  139. ! starpu_parallel_task_barrier_init
  140. ! starpu_parallel_task_barrier_init_n
  141. ! starpu_task_dup
  142. ! starpu_task_set_implementation
  143. ! starpu_task_get_implementation
  144. ! --
  145. function fstarpu_codelet_allocate () bind(C)
  146. use iso_c_binding, only: c_ptr
  147. type(c_ptr) :: fstarpu_codelet_allocate
  148. end function fstarpu_codelet_allocate
  149. subroutine fstarpu_codelet_free (cl) bind(C)
  150. use iso_c_binding, only: c_ptr
  151. type(c_ptr), value, intent(in) :: cl
  152. end subroutine fstarpu_codelet_free
  153. subroutine fstarpu_codelet_set_name (cl, cl_name) bind(C)
  154. use iso_c_binding, only: c_ptr, c_char
  155. type(c_ptr), value, intent(in) :: cl
  156. character(c_char), intent(in) :: cl_name
  157. end subroutine fstarpu_codelet_set_name
  158. subroutine fstarpu_codelet_add_cpu_func (cl, f_ptr) bind(C)
  159. use iso_c_binding, only: c_ptr, c_funptr
  160. type(c_ptr), value, intent(in) :: cl
  161. type(c_funptr), value, intent(in) :: f_ptr
  162. end subroutine fstarpu_codelet_add_cpu_func
  163. subroutine fstarpu_codelet_add_cuda_func (cl, f_ptr) bind(C)
  164. use iso_c_binding, only: c_ptr, c_funptr
  165. type(c_ptr), value, intent(in) :: cl
  166. type(c_funptr), value, intent(in) :: f_ptr
  167. end subroutine fstarpu_codelet_add_cuda_func
  168. subroutine fstarpu_codelet_add_opencl_func (cl, f_ptr) bind(C)
  169. use iso_c_binding, only: c_ptr, c_funptr
  170. type(c_ptr), value, intent(in) :: cl
  171. type(c_funptr), value, intent(in) :: f_ptr
  172. end subroutine fstarpu_codelet_add_opencl_func
  173. subroutine fstarpu_codelet_add_mic_func (cl, f_ptr) bind(C)
  174. use iso_c_binding, only: c_ptr, c_funptr
  175. type(c_ptr), value, intent(in) :: cl
  176. type(c_funptr), value, intent(in) :: f_ptr
  177. end subroutine fstarpu_codelet_add_mic_func
  178. subroutine fstarpu_codelet_add_scc_func (cl, f_ptr) bind(C)
  179. use iso_c_binding, only: c_ptr, c_funptr
  180. type(c_ptr), value, intent(in) :: cl
  181. type(c_funptr), value, intent(in) :: f_ptr
  182. end subroutine fstarpu_codelet_add_scc_func
  183. subroutine fstarpu_codelet_add_buffer (cl, mode) bind(C)
  184. use iso_c_binding, only: c_ptr
  185. type(c_ptr), value, intent(in) :: cl
  186. type(c_ptr), value, intent(in) :: mode ! C function expects an intptr_t
  187. end subroutine fstarpu_codelet_add_buffer
  188. function fstarpu_vector_data_register(vector, nx, elt_size, ram) bind(C)
  189. use iso_c_binding, only: c_ptr, c_int, c_size_t
  190. type(c_ptr) :: fstarpu_vector_data_register
  191. type(c_ptr), value, intent(in) :: vector
  192. integer(c_int), value, intent(in) :: nx
  193. integer(c_size_t), value, intent(in) :: elt_size
  194. integer(c_int), value, intent(in) :: ram
  195. end function fstarpu_vector_data_register
  196. function fstarpu_vector_get_ptr(buffers, i) bind(C)
  197. use iso_c_binding, only: c_ptr, c_int
  198. type(c_ptr) :: fstarpu_vector_get_ptr
  199. type(c_ptr), value, intent(in) :: buffers
  200. integer(c_int), value, intent(in) :: i
  201. end function fstarpu_vector_get_ptr
  202. function fstarpu_vector_get_nx(buffers, i) bind(C)
  203. use iso_c_binding, only: c_ptr, c_int
  204. integer(c_int) :: fstarpu_vector_get_nx
  205. type(c_ptr), value, intent(in) :: buffers
  206. integer(c_int), value, intent(in) :: i
  207. end function fstarpu_vector_get_nx
  208. function fstarpu_matrix_data_register(matrix, ldy, ny, nx, elt_size, ram) bind(C)
  209. use iso_c_binding, only: c_ptr, c_int, c_size_t
  210. type(c_ptr) :: fstarpu_matrix_data_register
  211. type(c_ptr), value, intent(in) :: matrix
  212. integer(c_int), value, intent(in) :: ldy
  213. integer(c_int), value, intent(in) :: ny
  214. integer(c_int), value, intent(in) :: nx
  215. integer(c_size_t), value, intent(in) :: elt_size
  216. integer(c_int), value, intent(in) :: ram
  217. end function fstarpu_matrix_data_register
  218. function fstarpu_matrix_get_ptr(buffers, i) bind(C)
  219. use iso_c_binding, only: c_ptr, c_int
  220. type(c_ptr) :: fstarpu_matrix_get_ptr
  221. type(c_ptr), value, intent(in) :: buffers
  222. integer(c_int), value, intent(in) :: i
  223. end function fstarpu_matrix_get_ptr
  224. function fstarpu_matrix_get_ld(buffers, i) bind(C)
  225. use iso_c_binding, only: c_ptr, c_int
  226. integer(c_int) :: fstarpu_matrix_get_ld
  227. type(c_ptr), value, intent(in) :: buffers
  228. integer(c_int), value, intent(in) :: i
  229. end function fstarpu_matrix_get_ld
  230. function fstarpu_matrix_get_ny(buffers, i) bind(C)
  231. use iso_c_binding, only: c_ptr, c_int
  232. integer(c_int) :: fstarpu_matrix_get_ny
  233. type(c_ptr), value, intent(in) :: buffers
  234. integer(c_int), value, intent(in) :: i
  235. end function fstarpu_matrix_get_ny
  236. function fstarpu_matrix_get_nx(buffers, i) bind(C)
  237. use iso_c_binding, only: c_ptr, c_int
  238. integer(c_int) :: fstarpu_matrix_get_nx
  239. type(c_ptr), value, intent(in) :: buffers
  240. integer(c_int), value, intent(in) :: i
  241. end function fstarpu_matrix_get_nx
  242. subroutine fstarpu_data_unregister (dh) bind(C,name="starpu_data_unregister")
  243. use iso_c_binding, only: c_ptr
  244. type(c_ptr), value, intent(in) :: dh
  245. end subroutine fstarpu_data_unregister
  246. subroutine fstarpu_insert_task(arglist) bind(C)
  247. use iso_c_binding, only: c_ptr
  248. type(c_ptr), dimension(:), intent(in) :: arglist
  249. end subroutine fstarpu_insert_task
  250. subroutine fstarpu_unpack_arg(cl_arg,bufferlist) bind(C)
  251. use iso_c_binding, only: c_ptr
  252. type(c_ptr), value, intent(in) :: cl_arg
  253. type(c_ptr), dimension(:), intent(in) :: bufferlist
  254. end subroutine fstarpu_unpack_arg
  255. end interface
  256. contains
  257. function ip_to_p(i) bind(C)
  258. use iso_c_binding, only: c_ptr,c_intptr_t,C_NULL_PTR
  259. type(c_ptr) :: ip_to_p
  260. integer(c_intptr_t), value, intent(in) :: i
  261. ip_to_p = transfer(i,C_NULL_PTR)
  262. end function ip_to_p
  263. function sz_to_p(sz) bind(C)
  264. use iso_c_binding, only: c_ptr,c_size_t,c_intptr_t
  265. type(c_ptr) :: sz_to_p
  266. integer(c_size_t), value, intent(in) :: sz
  267. sz_to_p = ip_to_p(int(sz,kind=c_intptr_t))
  268. end function sz_to_p
  269. function fstarpu_init (conf) bind(C)
  270. use iso_c_binding
  271. integer(c_int) :: fstarpu_init
  272. type(c_ptr), value, intent(in) :: conf
  273. integer(4) :: FSTARPU_SZ_INT4_dummy
  274. integer(8) :: FSTARPU_SZ_INT8_dummy
  275. real(4) :: FSTARPU_SZ_REAL4_dummy
  276. real(8) :: FSTARPU_SZ_REAL8_dummy
  277. ! Note: Referencing global C constants from Fortran has
  278. ! been found unreliable on some architectures, notably
  279. ! on Darwin. The get_integer/get_pointer_constant
  280. ! scheme is a workaround to that issue.
  281. interface
  282. ! These functions are not exported to the end user
  283. function fstarpu_get_constant(s) bind(C)
  284. use iso_c_binding, only: c_ptr,c_char
  285. type(c_ptr) :: fstarpu_get_constant ! C function returns an intptr_t
  286. character(kind=c_char) :: s
  287. end function fstarpu_get_constant
  288. function fstarpu_init_internal (conf) bind(C,name="starpu_init")
  289. use iso_c_binding, only: c_ptr,c_int
  290. integer(c_int) :: fstarpu_init_internal
  291. type(c_ptr), value :: conf
  292. end function fstarpu_init_internal
  293. end interface
  294. ! Initialize Fortran constants from C peers
  295. FSTARPU_R = fstarpu_get_constant(C_CHAR_"FSTARPU_R"//C_NULL_CHAR)
  296. FSTARPU_W = fstarpu_get_constant(C_CHAR_"FSTARPU_W"//C_NULL_CHAR)
  297. FSTARPU_RW = fstarpu_get_constant(C_CHAR_"FSTARPU_RW"//C_NULL_CHAR)
  298. FSTARPU_SCRATCH = fstarpu_get_constant(C_CHAR_"FSTARPU_SCRATCH"//C_NULL_CHAR)
  299. FSTARPU_REDUX = fstarpu_get_constant(C_CHAR_"FSTARPU_REDUX"//C_NULL_CHAR)
  300. FSTARPU_DATA = fstarpu_get_constant(C_CHAR_"FSTARPU_DATA"//C_NULL_CHAR)
  301. FSTARPU_VALUE = fstarpu_get_constant(C_CHAR_"FSTARPU_VALUE"//C_NULL_CHAR)
  302. ! Initialize size constants as 'c_ptr'
  303. FSTARPU_SZ_INT4 = sz_to_p(c_sizeof(FSTARPU_SZ_INT4_dummy))
  304. FSTARPU_SZ_INT8 = sz_to_p(c_sizeof(FSTARPU_SZ_INT8_dummy))
  305. FSTARPU_SZ_REAL4 = sz_to_p(c_sizeof(FSTARPU_SZ_REAL4_dummy))
  306. FSTARPU_SZ_REAL8 = sz_to_p(c_sizeof(FSTARPU_SZ_REAL8_dummy))
  307. ! Initialize StarPU
  308. if (c_associated(conf)) then
  309. fstarpu_init = fstarpu_init_internal(conf)
  310. else
  311. fstarpu_init = fstarpu_init_internal(C_NULL_PTR)
  312. end if
  313. end function fstarpu_init
  314. function fstarpu_csizet_to_cptr(i) bind(C)
  315. use iso_c_binding
  316. type(c_ptr) :: fstarpu_csizet_to_cptr
  317. integer(c_size_t) :: i
  318. fstarpu_csizet_to_cptr = transfer(int(i,kind=c_intptr_t),C_NULL_PTR)
  319. end function fstarpu_csizet_to_cptr
  320. function fstarpu_int_to_cptr(i) bind(C)
  321. use iso_c_binding
  322. type(c_ptr) :: fstarpu_int_to_cptr
  323. integer :: i
  324. fstarpu_int_to_cptr = transfer(int(i,kind=c_intptr_t),C_NULL_PTR)
  325. end function fstarpu_int_to_cptr
  326. end module fstarpu_mod