nf_sched_ctx.f90 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ! StarPU --- Runtime system for heterogeneous multicore architectures.
  2. !
  3. ! Copyright (C) 2016 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. program nf_sched_ctx
  16. use iso_c_binding ! C interfacing module
  17. use fstarpu_mod ! StarPU interfacing module
  18. use nf_sched_ctx_cl
  19. implicit none
  20. type(c_ptr) :: cl1 ! a pointer for a codelet structure
  21. type(c_ptr) :: cl2 ! a pointer for another codelet structure
  22. integer(c_int) :: err ! return status for fstarpu_init
  23. integer(c_int) :: ncpu ! number of cpus workers
  24. ! list of cpu worker ids
  25. integer(c_int), dimension(:), allocatable :: procs
  26. ! sub-list of cpu worker ids for sched context 1
  27. integer(c_int) :: nprocs1
  28. integer(c_int), dimension(:), allocatable :: procs1
  29. integer(c_int) :: ctx1
  30. ! sub-list of cpu worker ids for sched context 2
  31. integer(c_int) :: nprocs2
  32. integer(c_int), dimension(:), allocatable :: procs2
  33. integer(c_int) :: ctx2
  34. ! needed to be able to call c_loc on it, to get a ptr to the string
  35. character(kind=c_char,len=6), target :: ctx2_policy = C_CHAR_"eager"//C_NULL_CHAR
  36. integer(c_int),parameter :: n = 20
  37. integer(c_int) :: i
  38. integer(c_int), target :: arg_id
  39. integer(c_int), target :: arg_ctx
  40. ! initialize StarPU with default settings
  41. err = fstarpu_init(C_NULL_PTR)
  42. if (err == -19) then
  43. stop 77
  44. end if
  45. ! stop there if no CPU worker available
  46. ncpu = fstarpu_cpu_worker_get_count()
  47. if (ncpu == 0) then
  48. call fstarpu_shutdown()
  49. stop 77
  50. end if
  51. ! actually we really need at least 2 CPU workers such to allocate 2 non overlapping contexts
  52. if (ncpu < 2) then
  53. call fstarpu_shutdown()
  54. stop 77
  55. end if
  56. ! allocate and fill codelet structs
  57. cl1 = fstarpu_codelet_allocate()
  58. call fstarpu_codelet_set_name(cl1, C_CHAR_"sched_ctx_cl1"//C_NULL_CHAR)
  59. call fstarpu_codelet_add_cpu_func(cl1, C_FUNLOC(cl_cpu_func_sched_ctx))
  60. ! allocate and fill codelet structs
  61. cl2 = fstarpu_codelet_allocate()
  62. call fstarpu_codelet_set_name(cl2, C_CHAR_"sched_ctx_cl2"//C_NULL_CHAR)
  63. call fstarpu_codelet_add_cpu_func(cl2, C_FUNLOC(cl_cpu_func_sched_ctx))
  64. ! get the list of CPU worker ids
  65. allocate(procs(ncpu))
  66. err = fstarpu_worker_get_ids_by_type(FSTARPU_CPU_WORKER, procs, ncpu)
  67. ! split the workers in two sets
  68. nprocs1 = ncpu/2;
  69. allocate(procs1(nprocs1))
  70. write(*,*) "procs1:"
  71. do i=1,nprocs1
  72. procs1(i) = procs(i)
  73. write(*,*) i, procs1(i)
  74. end do
  75. nprocs2 = ncpu - nprocs1
  76. allocate(procs2(nprocs2))
  77. write(*,*) "procs2:"
  78. do i=1,nprocs2
  79. procs2(i) = procs(nprocs1+i)
  80. write(*,*) i, procs2(i)
  81. end do
  82. deallocate(procs)
  83. ! create sched context 1 with default policy, by giving a NULL policy name
  84. ctx1 = fstarpu_sched_ctx_create(procs1, nprocs1, &
  85. C_CHAR_"ctx1"//C_NULL_CHAR, &
  86. (/ FSTARPU_SCHED_CTX_POLICY_NAME, c_null_ptr, c_null_ptr /) &
  87. )
  88. ! create sched context 2 with a user selected policy name
  89. ctx2 = fstarpu_sched_ctx_create(procs2, nprocs2, &
  90. C_CHAR_"ctx2"//C_NULL_CHAR, &
  91. (/ FSTARPU_SCHED_CTX_POLICY_NAME, c_loc(ctx2_policy), c_null_ptr /))
  92. ! set inheritor context
  93. call fstarpu_sched_ctx_set_inheritor(ctx2, ctx1);
  94. call fstarpu_sched_ctx_display_workers(ctx1)
  95. call fstarpu_sched_ctx_display_workers(ctx2)
  96. do i = 1, n
  97. ! submit a task on context 1
  98. arg_id = 1*1000 + i
  99. arg_ctx = ctx1
  100. call fstarpu_insert_task((/ cl1, &
  101. FSTARPU_VALUE, c_loc(arg_id), FSTARPU_SZ_C_INT, &
  102. FSTARPU_SCHED_CTX, c_loc(arg_ctx), &
  103. C_NULL_PTR /))
  104. end do
  105. do i = 1, n
  106. ! now submit a task on context 2
  107. arg_id = 2*1000 + i
  108. arg_ctx = ctx2
  109. call fstarpu_insert_task((/ cl2, &
  110. FSTARPU_VALUE, c_loc(arg_id), FSTARPU_SZ_C_INT, &
  111. FSTARPU_SCHED_CTX, c_loc(arg_ctx), &
  112. C_NULL_PTR /))
  113. end do
  114. ! mark submission process as completed on context 2
  115. call fstarpu_sched_ctx_finished_submit(ctx2)
  116. do i = 1, n
  117. ! now submit a task on context 1 again
  118. arg_id = 1*10000 + i
  119. arg_ctx = ctx1
  120. call fstarpu_insert_task((/ cl1, &
  121. FSTARPU_VALUE, c_loc(arg_id), FSTARPU_SZ_C_INT, &
  122. FSTARPU_SCHED_CTX, c_loc(arg_ctx), &
  123. C_NULL_PTR /))
  124. end do
  125. ! mark submission process as completed on context 1
  126. call fstarpu_sched_ctx_finished_submit(ctx1)
  127. ! wait for completion of all tasks
  128. call fstarpu_task_wait_for_all()
  129. ! show how to add some workers from a context to another
  130. call fstarpu_sched_ctx_add_workers(procs1, nprocs1, ctx2)
  131. ! deallocate both contexts
  132. call fstarpu_sched_ctx_delete(ctx2)
  133. call fstarpu_sched_ctx_delete(ctx1)
  134. deallocate(procs2)
  135. deallocate(procs1)
  136. ! free codelet structure
  137. call fstarpu_codelet_free(cl1)
  138. call fstarpu_codelet_free(cl2)
  139. ! shut StarPU down
  140. call fstarpu_shutdown()
  141. end program nf_sched_ctx