nf_vector.f90 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ! StarPU --- Runtime system for heterogeneous multicore architectures.
  2. !
  3. ! Copyright (C) 2016-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. !
  5. ! StarPU is free software; you can redistribute it and/or modify
  6. ! it under the terms of the GNU Lesser General Public License as published by
  7. ! the Free Software Foundation; either version 2.1 of the License, or (at
  8. ! your option) any later version.
  9. !
  10. ! StarPU is distributed in the hope that it will be useful, but
  11. ! WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. !
  14. ! See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. !
  16. program nf_vector
  17. use iso_c_binding ! C interfacing module
  18. use fstarpu_mod ! StarPU interfacing module
  19. use nf_codelets
  20. implicit none
  21. real(8), dimension(:), allocatable, target :: va
  22. integer, dimension(:), allocatable, target :: vb
  23. integer :: i
  24. type(c_ptr) :: perfmodel_vec ! a pointer for the perfmodel structure
  25. type(c_ptr) :: cl_vec ! a pointer for the codelet structure
  26. type(c_ptr) :: dh_va ! a pointer for the 'va' vector data handle
  27. type(c_ptr) :: dh_vb ! a pointer for the 'vb' vector data handle
  28. integer(c_int) :: err ! return status for fstarpu_init
  29. integer(c_int) :: ncpu ! number of cpus workers
  30. integer(c_int) :: bool_ret
  31. allocate(va(5))
  32. va = (/ (i,i=1,5) /)
  33. allocate(vb(7))
  34. vb = (/ (i,i=1,7) /)
  35. ! initialize StarPU with default settings
  36. err = fstarpu_init(C_NULL_PTR)
  37. if (err == -19) then
  38. stop 77
  39. end if
  40. ! stop there if no CPU worker available
  41. ncpu = fstarpu_cpu_worker_get_count()
  42. if (ncpu == 0) then
  43. call fstarpu_shutdown()
  44. stop 77
  45. end if
  46. ! illustrate use of pause/resume/is_paused
  47. bool_ret = fstarpu_is_paused()
  48. if (bool_ret /= 0) then
  49. stop 1
  50. end if
  51. call fstarpu_pause
  52. bool_ret = fstarpu_is_paused()
  53. if (bool_ret == 0) then
  54. stop 1
  55. end if
  56. call fstarpu_resume
  57. bool_ret = fstarpu_is_paused()
  58. if (bool_ret /= 0) then
  59. stop 1
  60. end if
  61. ! allocate an empty perfmodel structure
  62. perfmodel_vec = fstarpu_perfmodel_allocate()
  63. ! set the perfmodel symbol
  64. call fstarpu_perfmodel_set_symbol(perfmodel_vec, C_CHAR_"my_vec_sym"//C_NULL_CHAR)
  65. ! set the perfmodel type
  66. call fstarpu_perfmodel_set_type(perfmodel_vec, FSTARPU_HISTORY_BASED)
  67. ! allocate an empty codelet structure
  68. cl_vec = fstarpu_codelet_allocate()
  69. ! set the codelet name
  70. call fstarpu_codelet_set_name(cl_vec, C_CHAR_"my_vec_codelet"//C_NULL_CHAR)
  71. ! set the codelet perfmodel
  72. call fstarpu_codelet_set_model(cl_vec, perfmodel_vec)
  73. ! add a CPU implementation function to the codelet
  74. call fstarpu_codelet_add_cpu_func(cl_vec, C_FUNLOC(cl_cpu_func_vec))
  75. ! optionally set 'where' field to CPU only
  76. call fstarpu_codelet_set_where(cl_vec, FSTARPU_CPU)
  77. ! set 'type' field to SEQ (for demonstration purpose)
  78. call fstarpu_codelet_set_type(cl_vec, FSTARPU_SEQ)
  79. ! set 'max_parallelism' field to 1 (for demonstration purpose)
  80. call fstarpu_codelet_set_max_parallelism(cl_vec, 1)
  81. ! add a Read-only mode data buffer to the codelet
  82. call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_R)
  83. ! add a Read-Write mode data buffer to the codelet
  84. call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_RW.ior.FSTARPU_LOCALITY)
  85. ! register 'va', a vector of real(8) elements
  86. call fstarpu_vector_data_register(dh_va, 0, c_loc(va), 1+ubound(va,1)-lbound(va,1), c_sizeof(va(lbound(va,1))))
  87. ! register 'vb', a vector of integer elements
  88. call fstarpu_vector_data_register(dh_vb, 0, c_loc(vb), 1+ubound(vb,1)-lbound(vb,1), c_sizeof(vb(lbound(vb,1))))
  89. ! insert a task with codelet cl_vec, and vectors 'va' and 'vb'
  90. !
  91. ! Note: The array argument must follow the layout:
  92. ! (/
  93. ! <codelet_ptr>,
  94. ! [<argument_type> [<argument_value(s)],]
  95. ! . . .
  96. ! C_NULL_PTR
  97. ! )/
  98. call fstarpu_insert_task((/ cl_vec, &
  99. FSTARPU_R, dh_va, &
  100. FSTARPU_RW.ior.FSTARPU_LOCALITY, dh_vb, &
  101. FSTARPU_EXECUTE_WHERE, FSTARPU_CPU, & ! for illustration, not required here
  102. C_NULL_PTR /))
  103. ! wait for task completion
  104. call fstarpu_task_wait_for_all()
  105. ! unregister 'va'
  106. call fstarpu_data_unregister(dh_va)
  107. ! unregister 'vb'
  108. call fstarpu_data_unregister(dh_vb)
  109. ! free codelet structure
  110. call fstarpu_codelet_free(cl_vec)
  111. ! shut StarPU down
  112. call fstarpu_shutdown()
  113. ! free perfmodel structure (must be called after fstarpu_shutdown)
  114. call fstarpu_perfmodel_free(perfmodel_vec)
  115. deallocate(vb)
  116. deallocate(va)
  117. end program nf_vector