nf_example.f90 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ! StarPU --- Runtime system for heterogeneous multicore architectures.
  2. !
  3. ! Copyright (C) 2015-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. ! Copyright (C) 2015 ONERA
  5. !
  6. ! StarPU is free software; you can redistribute it and/or modify
  7. ! it under the terms of the GNU Lesser General Public License as published by
  8. ! the Free Software Foundation; either version 2.1 of the License, or (at
  9. ! your option) any later version.
  10. !
  11. ! StarPU is distributed in the hope that it will be useful, but
  12. ! WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. !
  15. ! See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. !
  17. ! This is an example of Fortran90 program making use of StarPU.
  18. ! It registers a few matrices for each element of a domain, performs
  19. ! update computations on them, and checks the result.
  20. PROGRAM f90_example
  21. USE nf_types
  22. USE fstarpu_mod
  23. USE nf_compute
  24. USE iso_c_binding
  25. IMPLICIT NONE
  26. TYPE(type_mesh) :: mesh
  27. TYPE(type_numpar),TARGET :: numpar
  28. TYPE(type_mesh_elt),POINTER :: elt => NULL()
  29. INTEGER(KIND=C_INT) :: i,Nelt,res,cpus
  30. INTEGER(KIND=C_INT) :: starpu_maj,starpu_min,starpu_rev
  31. INTEGER(KIND=C_INT) :: it,it_tot
  32. REAL(KIND=C_DOUBLE),TARGET :: flops
  33. TYPE(C_PTR) :: cl_loop_element = C_NULL_PTR ! loop codelet
  34. TYPE(C_PTR) :: cl_copy_element = C_NULL_PTR ! copy codelet
  35. !Initialization with arbitrary data
  36. Nelt = 2
  37. it_tot = 2
  38. numpar%Neq_max = 5
  39. numpar%coeff = 1.0
  40. ALLOCATE(mesh%elt(Nelt))
  41. DO i = 1,Nelt
  42. elt => mesh%elt(i)
  43. elt%Ng = 4
  44. elt%Np = 2
  45. ALLOCATE(elt%ro(numpar%Neq_max,elt%Np))
  46. ALLOCATE(elt%dro(numpar%Neq_max,elt%Np))
  47. ALLOCATE(elt%basis(elt%Np,elt%Ng))
  48. CALL init_element(elt%ro,elt%dro,elt%basis,numpar%Neq_max,elt%Np,elt%Ng,i)
  49. ENDDO
  50. !Initialization of StarPU
  51. res = fstarpu_init(C_NULL_PTR)
  52. IF (res == -19) THEN
  53. STOP 77
  54. END IF
  55. CALL fstarpu_get_version(starpu_maj,starpu_min,starpu_rev)
  56. WRITE(6,'(a,i4,a,i4,a,i4)') "StarPU version: ", starpu_maj , "." , starpu_min , "." , starpu_rev
  57. cpus = fstarpu_cpu_worker_get_count()
  58. IF (cpus == 0) THEN
  59. CALL fstarpu_shutdown()
  60. STOP 77
  61. END IF
  62. cl_loop_element = fstarpu_codelet_allocate()
  63. CALL fstarpu_codelet_add_cpu_func(cl_loop_element, C_FUNLOC(loop_element_cpu_fortran))
  64. CALL fstarpu_codelet_add_buffer(cl_loop_element, FSTARPU_R)
  65. CALL fstarpu_codelet_add_buffer(cl_loop_element, FSTARPU_RW)
  66. CALL fstarpu_codelet_add_buffer(cl_loop_element, FSTARPU_R)
  67. CALL fstarpu_codelet_set_name(cl_loop_element, C_CHAR_"LOOP_ELEMENT"//C_NULL_CHAR)
  68. cl_copy_element = fstarpu_codelet_allocate()
  69. CALL fstarpu_codelet_add_cpu_func(cl_copy_element, C_FUNLOC(copy_element_cpu_fortran))
  70. CALL fstarpu_codelet_add_buffer(cl_copy_element, FSTARPU_RW)
  71. CALL fstarpu_codelet_add_buffer(cl_copy_element, FSTARPU_R)
  72. CALL fstarpu_codelet_set_name(cl_copy_element, C_CHAR_"COPY_ELEMENT"//C_NULL_CHAR)
  73. !Registration of elements
  74. DO i = 1,Nelt
  75. elt => mesh%elt(i)
  76. call fstarpu_matrix_data_register(elt%ro_h, 0, c_loc(elt%ro), numpar%Neq_max, numpar%Neq_max, elt%Np, c_sizeof(elt%ro(1,1)))
  77. call fstarpu_matrix_data_register(elt%dro_h, 0, c_loc(elt%dro), numpar%Neq_max, numpar%Neq_max, elt%Np, c_sizeof(elt%dro(1,1)))
  78. call fstarpu_matrix_data_register(elt%basis_h, 0, c_loc(elt%basis), elt%Np, elt%Np, elt%Ng, c_sizeof(elt%basis(1,1)))
  79. ENDDO
  80. !Compute
  81. DO it = 1,it_tot
  82. ! compute new dro for each element
  83. DO i = 1,Nelt
  84. elt => mesh%elt(i)
  85. flops = elt%Ng * ( (elt%Np * numpar%Neq_max * 2) + 1 + elt%Np * numpar%Neq_max)
  86. CALL fstarpu_insert_task((/ cl_loop_element, &
  87. FSTARPU_VALUE, c_loc(numpar%coeff), FSTARPU_SZ_C_DOUBLE, &
  88. FSTARPU_R, elt%ro_h, &
  89. FSTARPU_RW, elt%dro_h, &
  90. FSTARPU_R, elt%basis_h, &
  91. FSTARPU_FLOPS, c_loc(flops), &
  92. C_NULL_PTR /))
  93. ENDDO
  94. ! sync (if needed by the algorithm)
  95. CALL fstarpu_task_wait_for_all()
  96. ! - - - - -
  97. ! copy dro to ro for each element
  98. DO i = 1,Nelt
  99. elt => mesh%elt(i)
  100. CALL fstarpu_insert_task((/ cl_copy_element, &
  101. FSTARPU_RW, elt%ro_h, &
  102. FSTARPU_R, elt%dro_h, &
  103. C_NULL_PTR /))
  104. ENDDO
  105. ! sync (if needed by the algorithm)
  106. CALL fstarpu_task_wait_for_all()
  107. ENDDO
  108. !Unregistration of elements
  109. DO i = 1,Nelt
  110. elt => mesh%elt(i)
  111. CALL fstarpu_data_unregister(elt%ro_h)
  112. CALL fstarpu_data_unregister(elt%dro_h)
  113. CALL fstarpu_data_unregister(elt%basis_h)
  114. ENDDO
  115. !Terminate StarPU, no task can be submitted after
  116. CALL fstarpu_shutdown()
  117. !Check data with StarPU
  118. WRITE(6,'(a)') " "
  119. WRITE(6,'(a)') " %%%% RESULTS STARPU %%%% "
  120. WRITE(6,'(a)') " "
  121. DO i = 1,Nelt
  122. WRITE(6,'(a,i4,a)') " elt ", i , " ; elt%ro = "
  123. WRITE(6,'(10(1x,F11.2))') mesh%elt(i)%ro
  124. WRITE(6,'(a)') " ------------------------ "
  125. ENDDO
  126. !Same compute without StarPU
  127. DO i = 1,Nelt
  128. elt => mesh%elt(i)
  129. CALL init_element(elt%ro,elt%dro,elt%basis,numpar%Neq_max,elt%Np,elt%Ng,i)
  130. ENDDO
  131. DO it = 1, it_tot
  132. DO i = 1,Nelt
  133. elt => mesh%elt(i)
  134. CALL loop_element_cpu(elt%ro,elt%dro,elt%basis,numpar%coeff,numpar%Neq_max,elt%Ng,elt%Np)
  135. elt%ro = elt%ro + elt%dro
  136. ENDDO
  137. ENDDO
  138. WRITE(6,'(a)') " "
  139. WRITE(6,'(a)') " %%%% RESULTS VERIFICATION %%%% "
  140. WRITE(6,'(a)') " "
  141. DO i = 1,Nelt
  142. WRITE(6,'(a,i4,a)') " elt ", i , " ; elt%ro = "
  143. WRITE(6,'(10(1x,F11.2))') mesh%elt(i)%ro
  144. WRITE(6,'(a)') " ------------------------ "
  145. ENDDO
  146. WRITE(6,'(a)') " "
  147. !Deallocation
  148. CALL fstarpu_codelet_free(cl_loop_element)
  149. CALL fstarpu_codelet_free(cl_copy_element)
  150. DO i = 1,Nelt
  151. elt => mesh%elt(i)
  152. DEALLOCATE(elt%ro)
  153. DEALLOCATE(elt%dro)
  154. DEALLOCATE(elt%basis)
  155. ENDDO
  156. DEALLOCATE(mesh%elt)
  157. END PROGRAM f90_example