nf_vector.f90 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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) :: 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. ! add a Read-only mode data buffer to the codelet
  62. call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_R)
  63. ! add a Read-Write mode data buffer to the codelet
  64. call fstarpu_codelet_add_buffer(cl_vec, FSTARPU_RW.ior.FSTARPU_LOCALITY)
  65. ! register 'va', a vector of real(8) elements
  66. call fstarpu_vector_data_register(dh_va, 0, c_loc(va), 1+ubound(va,1)-lbound(va,1), c_sizeof(va(lbound(va,1))))
  67. ! register 'vb', a vector of integer elements
  68. call fstarpu_vector_data_register(dh_vb, 0, c_loc(vb), 1+ubound(vb,1)-lbound(vb,1), c_sizeof(vb(lbound(vb,1))))
  69. ! insert a task with codelet cl_vec, and vectors 'va' and 'vb'
  70. !
  71. ! Note: The array argument must follow the layout:
  72. ! (/
  73. ! <codelet_ptr>,
  74. ! [<argument_type> [<argument_value(s)],]
  75. ! . . .
  76. ! C_NULL_PTR
  77. ! )/
  78. call fstarpu_insert_task((/ cl_vec, FSTARPU_R, dh_va, FSTARPU_RW.ior.FSTARPU_LOCALITY, dh_vb, C_NULL_PTR /))
  79. ! wait for task completion
  80. call fstarpu_task_wait_for_all()
  81. ! unregister 'va'
  82. call fstarpu_data_unregister(dh_va)
  83. ! unregister 'vb'
  84. call fstarpu_data_unregister(dh_vb)
  85. ! free codelet structure
  86. call fstarpu_codelet_free(cl_vec)
  87. ! shut StarPU down
  88. call fstarpu_shutdown()
  89. ! free perfmodel structure (must be called after fstarpu_shutdown)
  90. call fstarpu_perfmodel_free(perfmodel_vec)
  91. deallocate(vb)
  92. deallocate(va)
  93. end program nf_vector