| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 | /* * This file is part of the StarPU Handbook. * Copyright (C) 2014, 2016 INRIA * See the file version.doxy for copying conditions. *//*! \page NativeFortranSupport The StarPU Native Fortran SupportStarPU provides the necessary routines and support to natively accessmost of its functionalities from Fortran 2008+ codes.All symbols (functions, constants) are defined in <c>fstarpu_mod.f90</c>.Every symbol of the Native Fortran support API is prefixed by<c>fstarpu_</c>. Note: Mixing uses of <c>fstarpu_</c> and <c>starpu_</c>symbols in the same Fortran code has unspecified behaviour.See \ref APIMIX for a discussion about valid and unspecifiedcombinations.\section Implementation Implementation Details and Specificities\subsection Prerequisites PrerequisitesThe Native Fortran support relies on Fortran 2008 specific constructs,as well as on the support of interoperability of assumed-shape arraysintroduced as part of Fortran's Technical Specification ISO/IEC TS 29113:2012,for which no equivalent are available in previous versions of thestandard. It has currently been tested successfully with GNU GFortran 4.9,GFortran 5.x, GFortran 6.x and the Intel Fortran Compiler >= 2016. It is knownnot to work with GNU GFortran < 4.9, Intel Fortran Compiler < 2016.See Section \ref OldFortran on information on how to write StarPUFortran code with older compilers.\subsection Configuration ConfigurationThe Native Fortran API is enabled and its companion<c>fstarpu_mod.f90</c> Fortran module source file is installedby default when a Fortran compiler is found, unless the detected Fortrancompiler is known not to support the requirements for the Native FortranAPI. The support can be disabled through the configure option \refdisable-fortran "--disable-fortran". Conditional compiled source codesmay check for the availability of the Native Fortran Support by testingwhether the preprocessor macro <c>STARPU_HAVE_FC</c> is defined or not.\subsection Examples ExamplesSeveral examples using the Native Fortran API are provided inStarPU's <c>examples/native_fortran/</c> examples directory, to showcasethe Fortran flavor of various basic and more advanced StarPU features.\subsection AppCompile Compiling a Native Fortran ApplicationThe Fortran module <c>fstarpu_mod.f90</c> installed in StarPU's<c>include/</c> directory provides all the necessary API definitions. Itmust be compiled with the same compiler (same vendor, same version) asthe application itself, and the resulting <c>fstarpu_mod.o</c> objectfile must linked with the application executable.Each example provided in StarPU's <c>examples/native_fortran/</c>examples directory comes with its own dedicated Makefile for out-of-treebuild. Such example Makefiles may be used as starting points forbuilding application codes with StarPU.\section Idioms Fortran Translation for Common StarPU API IdiomsAll these examples assume that the standard Fortran module <c>iso_c_binding</c>is in use.- Specifying a <c>NULL</c> pointer\code{.f90}        type(c_ptr) :: my_ptr  ! variable to store the pointer        ! [...]        my_ptr = C_NULL_PTR    ! assign standard constant for NULL ptr\endcode- Obtaining a pointer to some object:\code{.f90}        real(8), dimension(:), allocatable, target :: va        type(c_ptr) :: p_va  ! variable to store a pointer to array va        ! [...]        p_va = c_loc(va)\endcode- Obtaining a pointer to some subroutine:\code{.f90}        ! pointed routine definition        recursive subroutine myfunc () bind(C)        ! [...]        type(c_funptr) :: p_fun  ! variable to store the routine pointer        ! [...]        p_fun = c_funloc(my_func)\endcode- Obtaining the size of some object:\code{.f90}        real(8) :: a        integer(c_size_t) :: sz_a  ! variable to store the size of a        ! [...]        sz_a = c_sizeof(a)\endcode- Obtaining the length of an array dimension:\code{.f90}        real(8), dimension(:,:), allocatable, target :: vb        intger(c_int) :: ln_vb_1  ! variable to store the length of vb's dimension 1        intger(c_int) :: ln_vb_2  ! variable to store the length of vb's dimension 2        ! [...]        ln_vb_1 = 1+ubound(vb,1)-lbound(vb,1)  ! get length of dimension 1 of vb        ln_vb_2 = 1+ubound(vb,2)-lbound(vb,2)  ! get length of dimension 2 of vb\endcode- Specifying a string constant:\code{.f90}        type(c_ptr) :: my_cl  ! a StarPU codelet        ! [...]        ! set the name of a codelet to string 'my_codele't:        call fstarpu_codelet_set_name(my_cl, C_CHAR_"my_codelet"//C_NULL_CHAR)        ! note: using the C_CHAR_ prefix and the //C_NULL_CHAR concatenation at the end ensures        ! that the string constant is properly '\0' terminated, and compatible with StarPU's        ! internal C routines        !        ! note: plain Fortran string constants are not '\0' terminated, and as such, must not be        ! passed to StarPU routines.\endcode- Combining multiple flag constants with a bitwise 'or':\code{.f90}        type(c_ptr) :: my_cl  ! a pointer for the codelet structure        ! [...]        ! add a managed buffer to a codelet, specifying both the Read/Write access mode and the Locality hint        call fstarpu_codelet_add_buffer(my_cl, FSTARPU_RW.ior.FSTARPU_LOCALITY)\endcode\section InitExit Uses, Initialization and ShutdownThe snippet below show an example of minimal StarPU code using theNative Fortran support. The program should <c>use</c> the standardmodule <c>iso_c_binding</c> as well as StarPU's <c>fstarpu_mod</c>. TheStarPU runtime engine is initialized with a call to function<c>fstarpu_init</c>, which returns an integer status of 0 if successfulor non-0 otherwise. Eventually, a call to <c>fstarpu_shutdown</c> endsthe runtime engine and frees all internal StarPU data structures.\snippet nf_initexit.f90 To be included. You should update doxygen if you see this text.\section InsertTask Fortran Flavor of StarPU's Variadic Insert_taskFortran does not have a construction similar to C variadic functions on which<c>starpu_insert_task</c> relies at the time of this writing. However, Fortran's variablelength arrays of <c>c_ptr</c> elements enable to emulate much of theconvenience of C's variadic functions. This is the approach retained forimplementing <c>fstarpu_insert_task</c>.The general syntax for using <c>fstarpu_insert_task</c> is as follows:\code{.f90}        call fstarpu_insert_task((/ <codelet ptr>       &            [, <access mode flags>, <data handle>]*     &            [, <argument type constant>, <argument>]*   &            , C_NULL_PTR /))\endcodeThere is thus a unique array argument <c>(/ ... /)</c> passed to<c>fstarpu_insert_task</c> which itself contains the task settings.Each element of the array must be of type <c>type(c_ptr)</c>.The last element of the array must be <c>C_NULL_PTR</c>.Example extracted from nf_vector.f90:\code{.f90}        call fstarpu_insert_task((/ cl_vec,          &    ! codelet            FSTARPU_R, dh_va,                        &    ! a first data handle            FSTARPU_RW.ior.FSTARPU_LOCALITY, dh_vb,  &    ! a second data handle            C_NULL_PTR /))                                ! no more args\endcode\section Structs Functions and Subroutines Expecting Data Structures ArgumentsSeveral StarPU structures that are expected to be passed to the C API,are replaced by function/subroutine wrapper sets to allocate, set fieldsand free such structure. This strategy has been prefered over definingnative Fortran equivalent of such structures using Fortran's derivedtypes, to avoid potential layout mismatch between C and Fortran StarPUdata structures. Examples of such data structures wrappers include<c>fstarpu_conf_allocate</c> and alike, <c>fstarpu_codelet_allocate</c>and alike, <c>fstarpu_data_filter_allocate</c> and alike.Here is an example of allocating, filling and deallocating a codeletstructure:\code{.f90}        ! a pointer for the codelet structure        type(c_ptr) :: cl_vec        ! [...]        ! allocate an empty codelet structure        cl_vec = fstarpu_codelet_allocate()        ! add a CPU implementation function to the codelet        call fstarpu_codelet_add_cpu_func(cl_vec, C_FUNLOC(cl_cpu_func_vec))        ! set the codelet name        call fstarpu_codelet_set_name(cl_vec, C_CHAR_"my_vec_codelet"//C_NULL_CHAR)        ! add a Read-only mode data buffer to the codelet        call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_R)        ! add a Read-Write mode data buffer to the codelet        call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_RW.ior.FSTARPU_LOCALITY)        ! [...]        ! free codelet structure        call fstarpu_codelet_free(cl_vec)\endcode\section Notes Additional Notes about the Native Fortran Support\subsection OldFortran Using StarPU with Older Fortran CompilersWhen using older compilers, Fortran applications may still interoperatewith StarPU using C marshalling functions as exemplified in StarPU's<c>examples/fortran/</c> and <c>examples/fortran90/</c> exampledirectories, though the process will be less convenient.Basically, the main FORTRAN code calls some C wrapper functions tosubmit tasks to StarPU. Then, when StarPU starts a task, another Cwrapper function calls the FORTRAN routine for the task.Note that this marshalled FORTRAN support remains available evenwhen specifying configure option \ref disable-fortran "--disable-fortran"(which only disables StarPU's native Fortran layer).\subsection APIMIX Valid API Mixes and Language MixesMixing uses of<c>fstarpu_</c> and <c>starpu_</c> symbols in the sameFortran code has unspecified behaviour. Using <c>fstarpu_</c>symbols in C code has unspecified behaviour.For multi-language applications using both C and Fortran source files:- C source files must use <c>starpu_</c> symbols exclusively- Fortran sources must uniformly use either <c>fstarpu_</c> symbolsexclusively, or <c>starpu_</c> symbols exclusively. Every othercombination has unspecified behaviour.*/
 |