nf_vector.f90 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ! StarPU --- Runtime system for heterogeneous multicore architectures.
  2. !
  3. ! Copyright (C) 2016-2020 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) :: cl_vec ! a pointer for the codelet structure
  25. type(c_ptr) :: dh_va ! a pointer for the 'va' vector data handle
  26. type(c_ptr) :: dh_vb ! a pointer for the 'vb' vector data handle
  27. integer(c_int) :: err ! return status for fstarpu_init
  28. integer(c_int) :: ncpu ! number of cpus workers
  29. allocate(va(5))
  30. va = (/ (i,i=1,5) /)
  31. allocate(vb(7))
  32. vb = (/ (i,i=1,7) /)
  33. ! initialize StarPU with default settings
  34. err = fstarpu_init(C_NULL_PTR)
  35. if (err == -19) then
  36. stop 77
  37. end if
  38. ! stop there if no CPU worker available
  39. ncpu = fstarpu_cpu_worker_get_count()
  40. if (ncpu == 0) then
  41. call fstarpu_shutdown()
  42. stop 77
  43. end if
  44. ! allocate an empty codelet structure
  45. cl_vec = fstarpu_codelet_allocate()
  46. ! set the codelet name
  47. call fstarpu_codelet_set_name(cl_vec, C_CHAR_"my_vec_codelet"//C_NULL_CHAR)
  48. ! add a CPU implementation function to the codelet
  49. call fstarpu_codelet_add_cpu_func(cl_vec, C_FUNLOC(cl_cpu_func_vec))
  50. ! optionally set 'where' field to CPU only
  51. call fstarpu_codelet_set_where(cl_vec, FSTARPU_CPU)
  52. ! add a Read-only mode data buffer to the codelet
  53. call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_R)
  54. ! add a Read-Write mode data buffer to the codelet
  55. call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_RW.ior.FSTARPU_LOCALITY)
  56. ! register 'va', a vector of real(8) elements
  57. call fstarpu_vector_data_register(dh_va, 0, c_loc(va), 1+ubound(va,1)-lbound(va,1), c_sizeof(va(lbound(va,1))))
  58. ! register 'vb', a vector of integer elements
  59. call fstarpu_vector_data_register(dh_vb, 0, c_loc(vb), 1+ubound(vb,1)-lbound(vb,1), c_sizeof(vb(lbound(vb,1))))
  60. ! insert a task with codelet cl_vec, and vectors 'va' and 'vb'
  61. !
  62. ! Note: The array argument must follow the layout:
  63. ! (/
  64. ! <codelet_ptr>,
  65. ! [<argument_type> [<argument_value(s)],]
  66. ! . . .
  67. ! C_NULL_PTR
  68. ! )/
  69. call fstarpu_insert_task((/ cl_vec, FSTARPU_R, dh_va, FSTARPU_RW.ior.FSTARPU_LOCALITY, dh_vb, C_NULL_PTR /))
  70. ! wait for task completion
  71. call fstarpu_task_wait_for_all()
  72. ! unregister 'va'
  73. call fstarpu_data_unregister(dh_va)
  74. ! unregister 'vb'
  75. call fstarpu_data_unregister(dh_vb)
  76. ! free codelet structure
  77. call fstarpu_codelet_free(cl_vec)
  78. ! shut StarPU down
  79. call fstarpu_shutdown()
  80. deallocate(vb)
  81. deallocate(va)
  82. end program nf_vector