nf_sched_ctx.f90 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_"prio"//C_NULL_CHAR
  36. integer(c_int) :: i
  37. integer(c_int), target :: arg_id
  38. integer(c_int), target :: arg_ctx
  39. ! initialize StarPU with default settings
  40. err = fstarpu_init(C_NULL_PTR)
  41. if (err == -19) then
  42. stop 77
  43. end if
  44. ! stop there if no CPU worker available
  45. ncpu = fstarpu_cpu_worker_get_count()
  46. if (ncpu == 0) then
  47. call fstarpu_shutdown()
  48. stop 77
  49. end if
  50. ! actually we really need at least 2 CPU workers such to allocate 2 non overlapping contexts
  51. if (ncpu < 2) then
  52. call fstarpu_shutdown()
  53. stop 77
  54. end if
  55. ! allocate and fill codelet structs
  56. cl1 = fstarpu_codelet_allocate()
  57. call fstarpu_codelet_set_name(cl1, C_CHAR_"sched_ctx_cl1"//C_NULL_CHAR)
  58. call fstarpu_codelet_add_cpu_func(cl1, C_FUNLOC(cl_cpu_func_sched_ctx))
  59. ! allocate and fill codelet structs
  60. cl2 = fstarpu_codelet_allocate()
  61. call fstarpu_codelet_set_name(cl2, C_CHAR_"sched_ctx_cl2"//C_NULL_CHAR)
  62. call fstarpu_codelet_add_cpu_func(cl2, C_FUNLOC(cl_cpu_func_sched_ctx))
  63. ! get the list of CPU worker ids
  64. allocate(procs(ncpu))
  65. err = fstarpu_worker_get_ids_by_type(FSTARPU_CPU_WORKER, procs, ncpu)
  66. ! split the workers in two sets
  67. nprocs1 = ncpu/2;
  68. allocate(procs1(nprocs1))
  69. write(*,*) "procs1:"
  70. do i=1,nprocs1
  71. procs1(i) = procs(i)
  72. write(*,*) i, procs1(i)
  73. end do
  74. nprocs2 = ncpu - nprocs1
  75. allocate(procs2(nprocs2))
  76. write(*,*) "procs2:"
  77. do i=1,nprocs2
  78. procs2(i) = procs(nprocs1+i)
  79. write(*,*) i, procs2(i)
  80. end do
  81. ! create sched context 1 with default policy
  82. ctx1 = fstarpu_sched_ctx_create(procs1, nprocs1, &
  83. C_CHAR_"ctx1"//C_NULL_CHAR, &
  84. (/ c_null_ptr, FSTARPU_SCHED_CTX_POLICY_STRUCT, c_null_ptr, c_null_ptr /) &
  85. )
  86. ! create sched context 2 with policy name
  87. ctx2 = fstarpu_sched_ctx_create(procs2, nprocs2, &
  88. C_CHAR_"ctx2"//C_NULL_CHAR, &
  89. (/ c_null_ptr, FSTARPU_SCHED_CTX_POLICY_NAME, c_loc(ctx2_policy), c_null_ptr /))
  90. ! set inheritor context
  91. call fstarpu_sched_ctx_set_inheritor(ctx2, ctx1);
  92. ! submit a task on context 1
  93. arg_id = 1
  94. arg_ctx = ctx1
  95. call fstarpu_insert_task((/ cl1, &
  96. FSTARPU_VALUE, c_loc(arg_id), FSTARPU_SZ_C_INT, &
  97. FSTARPU_SCHED_CTX, c_loc(arg_ctx), &
  98. C_NULL_PTR /))
  99. ! now submit a task on context 2
  100. arg_id = 2
  101. arg_ctx = ctx2
  102. call fstarpu_insert_task((/ cl2, &
  103. FSTARPU_VALUE, c_loc(arg_id), FSTARPU_SZ_C_INT, &
  104. FSTARPU_SCHED_CTX, c_loc(arg_ctx), &
  105. C_NULL_PTR /))
  106. ! mark submission process as completed on context 2
  107. call fstarpu_sched_ctx_finished_submit(ctx2)
  108. ! now submit a task on context 1 again
  109. arg_id = 1
  110. arg_ctx = ctx1
  111. call fstarpu_insert_task((/ cl1, &
  112. FSTARPU_VALUE, c_loc(arg_id), FSTARPU_SZ_C_INT, &
  113. FSTARPU_SCHED_CTX, c_loc(arg_ctx), &
  114. C_NULL_PTR /))
  115. ! mark submission process as completed on context 1
  116. call fstarpu_sched_ctx_finished_submit(ctx1)
  117. ! wait for completion of all tasks
  118. call fstarpu_task_wait_for_all()
  119. ! show how to add some workers from a context to another
  120. call fstarpu_sched_ctx_add_workers(procs1, nprocs1, ctx2)
  121. ! deallocate both contexts
  122. call fstarpu_sched_ctx_delete(ctx2)
  123. call fstarpu_sched_ctx_delete(ctx1)
  124. deallocate(procs2)
  125. deallocate(procs1)
  126. ! free codelet structure
  127. call fstarpu_codelet_free(cl1)
  128. call fstarpu_codelet_free(cl2)
  129. ! shut StarPU down
  130. call fstarpu_shutdown()
  131. end program nf_sched_ctx