nf_matrix.f90 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. program nf_matrix
  2. use iso_c_binding ! C interfacing module
  3. use fstarpu_mod ! StarPU interfacing module
  4. use nf_codelets
  5. implicit none
  6. real(8), dimension(:,:), allocatable, target :: ma
  7. integer, dimension(:,:), allocatable, target :: mb
  8. integer :: i,j
  9. type(c_ptr) :: cl_mat ! a pointer for the codelet structure
  10. type(c_ptr) :: dh_ma ! a pointer for the 'ma' vector data handle
  11. type(c_ptr) :: dh_mb ! a pointer for the 'mb' vector data handle
  12. integer(c_int) :: err ! return status for fstarpu_init
  13. integer(c_int) :: ncpu ! number of cpus workers
  14. allocate(ma(5,6))
  15. do i=1,5
  16. do j=1,6
  17. ma(i,j) = (i*10)+j
  18. end do
  19. end do
  20. allocate(mb(7,8))
  21. do i=1,7
  22. do j=1,8
  23. mb(i,j) = (i*10)+j
  24. end do
  25. end do
  26. ! initialize StarPU with default settings
  27. err = fstarpu_init(C_NULL_PTR)
  28. if (err == -19) then
  29. stop 77
  30. end if
  31. ! stop there if no CPU worker available
  32. ncpu = fstarpu_cpu_worker_get_count()
  33. if (ncpu == 0) then
  34. call fstarpu_shutdown()
  35. stop 77
  36. end if
  37. ! allocate an empty codelet structure
  38. cl_mat = fstarpu_codelet_allocate()
  39. ! set the codelet name
  40. call fstarpu_codelet_set_name(cl_mat, C_CHAR_"my_mat_codelet"//C_NULL_CHAR)
  41. ! add a CPU implementation function to the codelet
  42. call fstarpu_codelet_add_cpu_func(cl_mat, C_FUNLOC(cl_cpu_func_mat))
  43. ! add a Read-only mode data buffer to the codelet
  44. call fstarpu_codelet_add_buffer(cl_mat, FSTARPU_R)
  45. ! add a Read-Write mode data buffer to the codelet
  46. call fstarpu_codelet_add_buffer(cl_mat, FSTARPU_RW)
  47. ! register 'ma', a vector of real(8) elements
  48. dh_ma = fstarpu_matrix_data_register(c_loc(ma), 5, 5, 6, c_sizeof(ma(1,1)), 0)
  49. ! register 'mb', a vector of integer elements
  50. dh_mb = fstarpu_matrix_data_register(c_loc(mb), 7, 7, 8, c_sizeof(mb(1,1)), 0)
  51. ! insert a task with codelet cl_mat, and vectors 'ma' and 'mb'
  52. !
  53. ! Note: The array argument must follow the layout:
  54. ! (/
  55. ! <codelet_ptr>,
  56. ! [<argument_type> [<argument_value(s)],]
  57. ! . . .
  58. ! C_NULL_PTR
  59. ! )/
  60. call fstarpu_insert_task((/ cl_mat, FSTARPU_R, dh_ma, FSTARPU_RW, dh_mb, C_NULL_PTR /))
  61. ! wait for task completion
  62. call fstarpu_task_wait_for_all()
  63. ! unregister 'ma'
  64. call fstarpu_data_unregister(dh_ma)
  65. ! unregister 'mb'
  66. call fstarpu_data_unregister(dh_mb)
  67. ! free codelet structure
  68. call fstarpu_codelet_free(cl_mat)
  69. ! shut StarPU down
  70. call fstarpu_shutdown()
  71. deallocate(mb)
  72. deallocate(ma)
  73. end program nf_matrix