nf_vector.f90 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. allocate(va(5))
  31. va = (/ (i,i=1,5) /)
  32. allocate(vb(7))
  33. vb = (/ (i,i=1,7) /)
  34. ! initialize StarPU with default settings
  35. err = fstarpu_init(C_NULL_PTR)
  36. if (err == -19) then
  37. stop 77
  38. end if
  39. ! stop there if no CPU worker available
  40. ncpu = fstarpu_cpu_worker_get_count()
  41. if (ncpu == 0) then
  42. call fstarpu_shutdown()
  43. stop 77
  44. end if
  45. ! allocate an empty perfmodel structure
  46. perfmodel_vec = fstarpu_perfmodel_allocate()
  47. ! set the perfmodel symbol
  48. call fstarpu_perfmodel_set_symbol(perfmodel_vec, C_CHAR_"my_vec_sym"//C_NULL_CHAR)
  49. ! set the perfmodel type
  50. call fstarpu_perfmodel_set_type(perfmodel_vec, FSTARPU_HISTORY_BASED)
  51. ! allocate an empty codelet structure
  52. cl_vec = fstarpu_codelet_allocate()
  53. ! set the codelet name
  54. call fstarpu_codelet_set_name(cl_vec, C_CHAR_"my_vec_codelet"//C_NULL_CHAR)
  55. ! set the codelet perfmodel
  56. call fstarpu_codelet_set_model(cl_vec, perfmodel_vec)
  57. ! add a CPU implementation function to the codelet
  58. call fstarpu_codelet_add_cpu_func(cl_vec, C_FUNLOC(cl_cpu_func_vec))
  59. ! optionally set 'where' field to CPU only
  60. call fstarpu_codelet_set_where(cl_vec, FSTARPU_CPU)
  61. ! set 'type' field to SEQ (for demonstration purpose)
  62. call fstarpu_codelet_set_type(cl_vec, FSTARPU_SEQ)
  63. ! set 'max_parallelism' field to 1 (for demonstration purpose)
  64. call fstarpu_codelet_set_max_parallelism(cl_vec, 1)
  65. ! add a Read-only mode data buffer to the codelet
  66. call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_R)
  67. ! add a Read-Write mode data buffer to the codelet
  68. call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_RW.ior.FSTARPU_LOCALITY)
  69. ! register 'va', a vector of real(8) elements
  70. call fstarpu_vector_data_register(dh_va, 0, c_loc(va), 1+ubound(va,1)-lbound(va,1), c_sizeof(va(lbound(va,1))))
  71. ! register 'vb', a vector of integer elements
  72. call fstarpu_vector_data_register(dh_vb, 0, c_loc(vb), 1+ubound(vb,1)-lbound(vb,1), c_sizeof(vb(lbound(vb,1))))
  73. ! insert a task with codelet cl_vec, and vectors 'va' and 'vb'
  74. !
  75. ! Note: The array argument must follow the layout:
  76. ! (/
  77. ! <codelet_ptr>,
  78. ! [<argument_type> [<argument_value(s)],]
  79. ! . . .
  80. ! C_NULL_PTR
  81. ! )/
  82. call fstarpu_insert_task((/ cl_vec, &
  83. FSTARPU_R, dh_va, &
  84. FSTARPU_RW.ior.FSTARPU_LOCALITY, dh_vb, &
  85. FSTARPU_EXECUTE_WHERE, FSTARPU_CPU, & ! for illustration, not required here
  86. C_NULL_PTR /))
  87. ! wait for task completion
  88. call fstarpu_task_wait_for_all()
  89. ! unregister 'va'
  90. call fstarpu_data_unregister(dh_va)
  91. ! unregister 'vb'
  92. call fstarpu_data_unregister(dh_vb)
  93. ! free codelet structure
  94. call fstarpu_codelet_free(cl_vec)
  95. ! shut StarPU down
  96. call fstarpu_shutdown()
  97. ! free perfmodel structure (must be called after fstarpu_shutdown)
  98. call fstarpu_perfmodel_free(perfmodel_vec)
  99. deallocate(vb)
  100. deallocate(va)
  101. end program nf_vector