nf_example.f90 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ! StarPU --- Runtime system for heterogeneous multicore architectures.
  2. !
  3. ! Copyright (C) 2017 CNRS
  4. ! Copyright (C) 2015,2016 Inria
  5. ! Copyright (C) 2016 Université de Bordeaux
  6. ! Copyright (C) 2015 ONERA
  7. !
  8. ! StarPU is free software; you can redistribute it and/or modify
  9. ! it under the terms of the GNU Lesser General Public License as published by
  10. ! the Free Software Foundation; either version 2.1 of the License, or (at
  11. ! your option) any later version.
  12. !
  13. ! StarPU is distributed in the hope that it will be useful, but
  14. ! WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. !
  17. ! See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. !
  19. ! This is an example of Fortran90 program making use of StarPU.
  20. ! It registers a few matrices for each element of a domain, performs
  21. ! update computations on them, and checks the result.
  22. PROGRAM f90_example
  23. USE nf_types
  24. USE fstarpu_mod
  25. USE nf_compute
  26. USE iso_c_binding
  27. IMPLICIT NONE
  28. TYPE(type_mesh) :: mesh
  29. TYPE(type_numpar),TARGET :: numpar
  30. TYPE(type_mesh_elt),POINTER :: elt => NULL()
  31. INTEGER(KIND=C_INT) :: i,Nelt,res,cpus
  32. INTEGER(KIND=C_INT) :: starpu_maj,starpu_min,starpu_rev
  33. INTEGER(KIND=C_INT) :: neq,ng,nb,it,it_tot
  34. REAL(KIND=C_DOUBLE) :: r, coeff2
  35. TYPE(C_PTR) :: cl_loop_element = C_NULL_PTR ! loop codelet
  36. TYPE(C_PTR) :: cl_copy_element = C_NULL_PTR ! copy codelet
  37. !Initialization with arbitrary data
  38. Nelt = 2
  39. it_tot = 2
  40. numpar%Neq_max = 5
  41. numpar%coeff = 1.0
  42. ALLOCATE(mesh%elt(Nelt))
  43. DO i = 1,Nelt
  44. elt => mesh%elt(i)
  45. elt%Ng = 4
  46. elt%Np = 2
  47. ALLOCATE(elt%ro(numpar%Neq_max,elt%Np))
  48. ALLOCATE(elt%dro(numpar%Neq_max,elt%Np))
  49. ALLOCATE(elt%basis(elt%Np,elt%Ng))
  50. CALL init_element(elt%ro,elt%dro,elt%basis,numpar%Neq_max,elt%Np,elt%Ng,i)
  51. ENDDO
  52. !Initialization of StarPU
  53. res = fstarpu_init(C_NULL_PTR)
  54. IF (res == -19) THEN
  55. STOP 77
  56. END IF
  57. CALL fstarpu_get_version(starpu_maj,starpu_min,starpu_rev)
  58. WRITE(6,'(a,i4,a,i4,a,i4)') "StarPU version: ", starpu_maj , "." , starpu_min , "." , starpu_rev
  59. cpus = fstarpu_cpu_worker_get_count()
  60. IF (cpus == 0) THEN
  61. CALL fstarpu_shutdown()
  62. STOP 77
  63. END IF
  64. cl_loop_element = fstarpu_codelet_allocate()
  65. CALL fstarpu_codelet_add_cpu_func(cl_loop_element, C_FUNLOC(loop_element_cpu_fortran))
  66. CALL fstarpu_codelet_add_buffer(cl_loop_element, FSTARPU_R)
  67. CALL fstarpu_codelet_add_buffer(cl_loop_element, FSTARPU_RW)
  68. CALL fstarpu_codelet_add_buffer(cl_loop_element, FSTARPU_R)
  69. CALL fstarpu_codelet_set_name(cl_loop_element, C_CHAR_"LOOP_ELEMENT"//C_NULL_CHAR)
  70. cl_copy_element = fstarpu_codelet_allocate()
  71. CALL fstarpu_codelet_add_cpu_func(cl_copy_element, C_FUNLOC(copy_element_cpu_fortran))
  72. CALL fstarpu_codelet_add_buffer(cl_copy_element, FSTARPU_RW)
  73. CALL fstarpu_codelet_add_buffer(cl_copy_element, FSTARPU_R)
  74. CALL fstarpu_codelet_set_name(cl_copy_element, C_CHAR_"COPY_ELEMENT"//C_NULL_CHAR)
  75. !Registration of elements
  76. DO i = 1,Nelt
  77. elt => mesh%elt(i)
  78. 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)))
  79. 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)))
  80. 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)))
  81. ENDDO
  82. !Compute
  83. DO it = 1,it_tot
  84. ! compute new dro for each element
  85. DO i = 1,Nelt
  86. elt => mesh%elt(i)
  87. CALL fstarpu_insert_task((/ cl_loop_element, &
  88. FSTARPU_VALUE, c_loc(numpar%coeff), FSTARPU_SZ_C_DOUBLE, &
  89. FSTARPU_R, elt%ro_h, &
  90. FSTARPU_RW, elt%dro_h, &
  91. FSTARPU_R, elt%basis_h, &
  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