450_native_fortran_support.doxy 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014-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. /*! \page NativeFortranSupport The StarPU Native Fortran Support
  17. StarPU provides the necessary routines and support to natively access
  18. most of its functionalities from Fortran 2008+ codes.
  19. All symbols (functions, constants) are defined in <c>fstarpu_mod.f90</c>.
  20. Every symbol of the Native Fortran support API is prefixed by
  21. <c>fstarpu_</c>.
  22. Note: Mixing uses of <c>fstarpu_</c> and <c>starpu_</c>
  23. symbols in the same Fortran code has unspecified behaviour.
  24. See \ref NFAPIMIX for a discussion about valid and unspecified
  25. combinations.
  26. \section NFImplementation Implementation Details and Specificities
  27. \subsection NFPrerequisites Prerequisites
  28. The Native Fortran support relies on Fortran 2008 specific constructs,
  29. as well as on the support of interoperability of assumed-shape arrays
  30. introduced as part of Fortran's Technical Specification ISO/IEC TS 29113:2012,
  31. for which no equivalent are available in previous versions of the
  32. standard. It has currently been tested successfully with GNU GFortran 4.9,
  33. GFortran 5.x, GFortran 6.x and the Intel Fortran Compiler >= 2016. It is known
  34. not to work with GNU GFortran < 4.9, Intel Fortran Compiler < 2016.
  35. See Section \ref NFOldFortran on information on how to write StarPU
  36. Fortran code with older compilers.
  37. \subsection NFConfiguration Configuration
  38. The Native Fortran API is enabled and its companion
  39. <c>fstarpu_mod.f90</c> Fortran module source file is installed
  40. by default when a Fortran compiler is found, unless the detected Fortran
  41. compiler is known not to support the requirements for the Native Fortran
  42. API. The support can be disabled through the \c configure option \ref
  43. disable-fortran "--disable-fortran". Conditional compiled source codes
  44. may check for the availability of the Native Fortran Support by testing
  45. whether the preprocessor macro <c>STARPU_HAVE_FC</c> is defined or not.
  46. \subsection NFExamples Examples
  47. Several examples using the Native Fortran API are provided in
  48. StarPU's <c>examples/native_fortran/</c> examples directory, to showcase
  49. the Fortran flavor of various basic and more advanced StarPU features.
  50. \subsection NFAppCompile Compiling a Native Fortran Application
  51. The Fortran module <c>fstarpu_mod.f90</c> installed in StarPU's
  52. <c>include/</c> directory provides all the necessary API definitions. It
  53. must be compiled with the same compiler (same vendor, same version) as
  54. the application itself, and the resulting <c>fstarpu_mod.o</c> object
  55. file must linked with the application executable.
  56. Each example provided in StarPU's <c>examples/native_fortran/</c>
  57. examples directory comes with its own dedicated Makefile for out-of-tree
  58. build. Such example Makefiles may be used as starting points for
  59. building application codes with StarPU.
  60. \section NFIdioms Fortran Translation for Common StarPU API Idioms
  61. All these examples assume that the standard Fortran module <c>iso_c_binding</c>
  62. is in use.
  63. - Specifying a <c>NULL</c> pointer
  64. \code{.f90}
  65. type(c_ptr) :: my_ptr ! variable to store the pointer
  66. ! [...]
  67. my_ptr = C_NULL_PTR ! assign standard constant for NULL ptr
  68. \endcode
  69. - Obtaining a pointer to some object:
  70. \code{.f90}
  71. real(8), dimension(:), allocatable, target :: va
  72. type(c_ptr) :: p_va ! variable to store a pointer to array va
  73. ! [...]
  74. p_va = c_loc(va)
  75. \endcode
  76. - Obtaining a pointer to some subroutine:
  77. \code{.f90}
  78. ! pointed routine definition
  79. recursive subroutine myfunc () bind(C)
  80. ! [...]
  81. type(c_funptr) :: p_fun ! variable to store the routine pointer
  82. ! [...]
  83. p_fun = c_funloc(my_func)
  84. \endcode
  85. - Obtaining the size of some object:
  86. \code{.f90}
  87. real(8) :: a
  88. integer(c_size_t) :: sz_a ! variable to store the size of a
  89. ! [...]
  90. sz_a = c_sizeof(a)
  91. \endcode
  92. - Obtaining the length of an array dimension:
  93. \code{.f90}
  94. real(8), dimension(:,:), allocatable, target :: vb
  95. intger(c_int) :: ln_vb_1 ! variable to store the length of vb's dimension 1
  96. intger(c_int) :: ln_vb_2 ! variable to store the length of vb's dimension 2
  97. ! [...]
  98. ln_vb_1 = 1+ubound(vb,1)-lbound(vb,1) ! get length of dimension 1 of vb
  99. ln_vb_2 = 1+ubound(vb,2)-lbound(vb,2) ! get length of dimension 2 of vb
  100. \endcode
  101. - Specifying a string constant:
  102. \code{.f90}
  103. type(c_ptr) :: my_cl ! a StarPU codelet
  104. ! [...]
  105. ! set the name of a codelet to string 'my_codele't:
  106. call fstarpu_codelet_set_name(my_cl, C_CHAR_"my_codelet"//C_NULL_CHAR)
  107. ! note: using the C_CHAR_ prefix and the //C_NULL_CHAR concatenation at the end ensures
  108. ! that the string constant is properly '\0' terminated, and compatible with StarPU's
  109. ! internal C routines
  110. !
  111. ! note: plain Fortran string constants are not '\0' terminated, and as such, must not be
  112. ! passed to StarPU routines.
  113. \endcode
  114. - Combining multiple flag constants with a bitwise 'or':
  115. \code{.f90}
  116. type(c_ptr) :: my_cl ! a pointer for the codelet structure
  117. ! [...]
  118. ! add a managed buffer to a codelet, specifying both the Read/Write access mode and the Locality hint
  119. call fstarpu_codelet_add_buffer(my_cl, FSTARPU_RW.ior.FSTARPU_LOCALITY)
  120. \endcode
  121. \section NFInitExit Uses, Initialization and Shutdown
  122. The snippet below show an example of minimal StarPU code using the
  123. Native Fortran support. The program should <c>use</c> the standard
  124. module <c>iso_c_binding</c> as well as StarPU's <c>fstarpu_mod</c>. The
  125. StarPU runtime engine is initialized with a call to function
  126. <c>fstarpu_init</c>, which returns an integer status of 0 if successful
  127. or non-0 otherwise. Eventually, a call to <c>fstarpu_shutdown</c> ends
  128. the runtime engine and frees all internal StarPU data structures.
  129. \snippet nf_initexit.f90 To be included. You should update doxygen if you see this text.
  130. \section NFInsertTask Fortran Flavor of StarPU's Variadic Insert_task
  131. Fortran does not have a construction similar to C variadic functions on which
  132. starpu_insert_task() relies at the time of this writing. However, Fortran's variable
  133. length arrays of <c>c_ptr</c> elements enable to emulate much of the
  134. convenience of C's variadic functions. This is the approach retained for
  135. implementing <c>fstarpu_insert_task</c>.
  136. The general syntax for using <c>fstarpu_insert_task</c> is as follows:
  137. \code{.f90}
  138. call fstarpu_insert_task((/ <codelet ptr> &
  139. [, <access mode flags>, <data handle>]* &
  140. [, <argument type constant>, <argument>]* &
  141. , C_NULL_PTR /))
  142. \endcode
  143. There is thus a unique array argument <c>(/ ... /)</c> passed to
  144. <c>fstarpu_insert_task</c> which itself contains the task settings.
  145. Each element of the array must be of type <c>type(c_ptr)</c>.
  146. The last element of the array must be <c>C_NULL_PTR</c>.
  147. Example extracted from nf_vector.f90:
  148. \code{.f90}
  149. call fstarpu_insert_task((/ cl_vec, & ! codelet
  150. FSTARPU_R, dh_va, & ! a first data handle
  151. FSTARPU_RW.ior.FSTARPU_LOCALITY, dh_vb, & ! a second data handle
  152. C_NULL_PTR /)) ! no more args
  153. \endcode
  154. \section NFStructs Functions and Subroutines Expecting Data Structures Arguments
  155. Several StarPU structures that are expected to be passed to the C API,
  156. are replaced by function/subroutine wrapper sets to allocate, set fields
  157. and free such structure. This strategy has been prefered over defining
  158. native Fortran equivalent of such structures using Fortran's derived
  159. types, to avoid potential layout mismatch between C and Fortran StarPU
  160. data structures. Examples of such data structures wrappers include
  161. <c>fstarpu_conf_allocate</c> and alike, <c>fstarpu_codelet_allocate</c>
  162. and alike, <c>fstarpu_data_filter_allocate</c> and alike.
  163. Here is an example of allocating, filling and deallocating a codelet
  164. structure:
  165. \code{.f90}
  166. ! a pointer for the codelet structure
  167. type(c_ptr) :: cl_vec
  168. ! [...]
  169. ! allocate an empty codelet structure
  170. cl_vec = fstarpu_codelet_allocate()
  171. ! add a CPU implementation function to the codelet
  172. call fstarpu_codelet_add_cpu_func(cl_vec, C_FUNLOC(cl_cpu_func_vec))
  173. ! set the codelet name
  174. call fstarpu_codelet_set_name(cl_vec, C_CHAR_"my_vec_codelet"//C_NULL_CHAR)
  175. ! add a Read-only mode data buffer to the codelet
  176. call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_R)
  177. ! add a Read-Write mode data buffer to the codelet
  178. call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_RW.ior.FSTARPU_LOCALITY)
  179. ! [...]
  180. ! free codelet structure
  181. call fstarpu_codelet_free(cl_vec)
  182. \endcode
  183. \section NFNotes Additional Notes about the Native Fortran Support
  184. \subsection NFOldFortran Using StarPU with Older Fortran Compilers
  185. When using older compilers, Fortran applications may still interoperate
  186. with StarPU using C marshalling functions as exemplified in StarPU's
  187. <c>examples/fortran/</c> and <c>examples/fortran90/</c> example
  188. directories, though the process will be less convenient.
  189. Basically, the main FORTRAN code calls some C wrapper functions to
  190. submit tasks to StarPU. Then, when StarPU starts a task, another C
  191. wrapper function calls the FORTRAN routine for the task.
  192. Note that this marshalled FORTRAN support remains available even
  193. when specifying \c configure option \ref disable-fortran "--disable-fortran"
  194. (which only disables StarPU's native Fortran layer).
  195. \subsection NFAPIMIX Valid API Mixes and Language Mixes
  196. Mixing uses of
  197. <c>fstarpu_</c> and <c>starpu_</c> symbols in the same
  198. Fortran code has unspecified behaviour. Using <c>fstarpu_</c>
  199. symbols in C code has unspecified behaviour.
  200. For multi-language applications using both C and Fortran source files:
  201. - C source files must use <c>starpu_</c> symbols exclusively
  202. - Fortran sources must uniformly use either <c>fstarpu_</c> symbols
  203. exclusively, or <c>starpu_</c> symbols exclusively. Every other
  204. combination has unspecified behaviour.
  205. */