450_native_fortran_support.doxy 9.5 KB

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