nf_sched_ctx.f90 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. ! create sched context 1 with default policy, by giving a NULL policy name
  83. ctx1 = fstarpu_sched_ctx_create(procs1, nprocs1, &
  84. C_CHAR_"ctx1"//C_NULL_CHAR, &
  85. (/ FSTARPU_SCHED_CTX_POLICY_NAME, c_null_ptr, c_null_ptr /) &
  86. )
  87. ! create sched context 2 with a user selected policy name
  88. ctx2 = fstarpu_sched_ctx_create(procs2, nprocs2, &
  89. C_CHAR_"ctx2"//C_NULL_CHAR, &
  90. (/ FSTARPU_SCHED_CTX_POLICY_NAME, c_loc(ctx2_policy), c_null_ptr /))
  91. ! set inheritor context
  92. call fstarpu_sched_ctx_set_inheritor(ctx2, ctx1);
  93. call fstarpu_sched_ctx_display_workers(ctx1)
  94. call fstarpu_sched_ctx_display_workers(ctx2)
  95. do i = 1, n
  96. ! submit a task on context 1
  97. arg_id = 1*1000 + i
  98. arg_ctx = ctx1
  99. call fstarpu_insert_task((/ cl1, &
  100. FSTARPU_VALUE, c_loc(arg_id), FSTARPU_SZ_C_INT, &
  101. FSTARPU_SCHED_CTX, c_loc(arg_ctx), &
  102. C_NULL_PTR /))
  103. end do
  104. do i = 1, n
  105. ! now submit a task on context 2
  106. arg_id = 2*1000 + i
  107. arg_ctx = ctx2
  108. call fstarpu_insert_task((/ cl2, &
  109. FSTARPU_VALUE, c_loc(arg_id), FSTARPU_SZ_C_INT, &
  110. FSTARPU_SCHED_CTX, c_loc(arg_ctx), &
  111. C_NULL_PTR /))
  112. end do
  113. ! mark submission process as completed on context 2
  114. call fstarpu_sched_ctx_finished_submit(ctx2)
  115. do i = 1, n
  116. ! now submit a task on context 1 again
  117. arg_id = 1*10000 + i
  118. arg_ctx = ctx1
  119. call fstarpu_insert_task((/ cl1, &
  120. FSTARPU_VALUE, c_loc(arg_id), FSTARPU_SZ_C_INT, &
  121. FSTARPU_SCHED_CTX, c_loc(arg_ctx), &
  122. C_NULL_PTR /))
  123. end do
  124. ! mark submission process as completed on context 1
  125. call fstarpu_sched_ctx_finished_submit(ctx1)
  126. ! wait for completion of all tasks
  127. call fstarpu_task_wait_for_all()
  128. ! show how to add some workers from a context to another
  129. call fstarpu_sched_ctx_add_workers(procs1, nprocs1, ctx2)
  130. ! deallocate both contexts
  131. call fstarpu_sched_ctx_delete(ctx2)
  132. call fstarpu_sched_ctx_delete(ctx1)
  133. deallocate(procs2)
  134. deallocate(procs1)
  135. ! free codelet structure
  136. call fstarpu_codelet_free(cl1)
  137. call fstarpu_codelet_free(cl2)
  138. ! shut StarPU down
  139. call fstarpu_shutdown()
  140. end program nf_sched_ctx