nf_sched_ctx.f90 6.2 KB

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