nf_vector.f90 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ! StarPU --- Runtime system for heterogeneous multicore architectures.
  2. !
  3. ! Copyright (C) 2017 CNRS
  4. ! Copyright (C) 2016 Inria
  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. program nf_vector
  18. use iso_c_binding ! C interfacing module
  19. use fstarpu_mod ! StarPU interfacing module
  20. use nf_codelets
  21. implicit none
  22. real(8), dimension(:), allocatable, target :: va
  23. integer, dimension(:), allocatable, target :: vb
  24. integer :: i
  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 codelet structure
  46. cl_vec = fstarpu_codelet_allocate()
  47. ! set the codelet name
  48. call fstarpu_codelet_set_name(cl_vec, C_CHAR_"my_vec_codelet"//C_NULL_CHAR)
  49. ! add a CPU implementation function to the codelet
  50. call fstarpu_codelet_add_cpu_func(cl_vec, C_FUNLOC(cl_cpu_func_vec))
  51. ! optionally set 'where' field to CPU only
  52. call fstarpu_codelet_set_where(cl_vec, FSTARPU_CPU)
  53. ! add a Read-only mode data buffer to the codelet
  54. call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_R)
  55. ! add a Read-Write mode data buffer to the codelet
  56. call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_RW.ior.FSTARPU_LOCALITY)
  57. ! register 'va', a vector of real(8) elements
  58. call fstarpu_vector_data_register(dh_va, 0, c_loc(va), 1+ubound(va,1)-lbound(va,1), c_sizeof(va(lbound(va,1))))
  59. ! register 'vb', a vector of integer elements
  60. call fstarpu_vector_data_register(dh_vb, 0, c_loc(vb), 1+ubound(vb,1)-lbound(vb,1), c_sizeof(vb(lbound(vb,1))))
  61. ! insert a task with codelet cl_vec, and vectors 'va' and 'vb'
  62. !
  63. ! Note: The array argument must follow the layout:
  64. ! (/
  65. ! <codelet_ptr>,
  66. ! [<argument_type> [<argument_value(s)],]
  67. ! . . .
  68. ! C_NULL_PTR
  69. ! )/
  70. call fstarpu_insert_task((/ cl_vec, FSTARPU_R, dh_va, FSTARPU_RW.ior.FSTARPU_LOCALITY, dh_vb, C_NULL_PTR /))
  71. ! wait for task completion
  72. call fstarpu_task_wait_for_all()
  73. ! unregister 'va'
  74. call fstarpu_data_unregister(dh_va)
  75. ! unregister 'vb'
  76. call fstarpu_data_unregister(dh_vb)
  77. ! free codelet structure
  78. call fstarpu_codelet_free(cl_vec)
  79. ! shut StarPU down
  80. call fstarpu_shutdown()
  81. deallocate(vb)
  82. deallocate(va)
  83. end program nf_vector