sched_ctx.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2014 Université de Bordeaux
  4. * Copyright (C) 2010-2014, 2016, 2017 CNRS
  5. * Copyright (C) 2017 Inria
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <stdlib.h>
  20. #ifdef STARPU_QUICK_CHECK
  21. #define NTASKS 64
  22. #else
  23. #define NTASKS 1000
  24. #endif
  25. int tasks_executed = 0;
  26. int ctx1_tasks_executed = 0;
  27. int ctx2_tasks_executed = 0;
  28. static void sched_ctx_cpu_func(void *descr[], void *arg)
  29. {
  30. (void)descr;
  31. (void)arg;
  32. (void)STARPU_ATOMIC_ADD(&tasks_executed,1);
  33. (void)STARPU_ATOMIC_ADD(&ctx1_tasks_executed,1);
  34. }
  35. static void sched_ctx2_cpu_func(void *descr[], void *arg)
  36. {
  37. (void)descr;
  38. (void)arg;
  39. (void)STARPU_ATOMIC_ADD(&tasks_executed,1);
  40. (void)STARPU_ATOMIC_ADD(&ctx2_tasks_executed,1);
  41. }
  42. static void sched_ctx2_cuda_func(void *descr[], void *arg)
  43. {
  44. (void)descr;
  45. (void)arg;
  46. (void)STARPU_ATOMIC_ADD(&tasks_executed,1);
  47. (void)STARPU_ATOMIC_ADD(&ctx2_tasks_executed,1);
  48. }
  49. static struct starpu_codelet sched_ctx_codelet1 =
  50. {
  51. .cpu_funcs = {sched_ctx_cpu_func},
  52. .model = NULL,
  53. .nbuffers = 0,
  54. .name = "sched_ctx"
  55. };
  56. static struct starpu_codelet sched_ctx_codelet2 =
  57. {
  58. .cpu_funcs = {sched_ctx2_cpu_func},
  59. .cuda_funcs = {sched_ctx2_cuda_func},
  60. .model = NULL,
  61. .nbuffers = 0,
  62. .name = "sched_ctx"
  63. };
  64. int main(void)
  65. {
  66. int ntasks = NTASKS;
  67. int ret;
  68. unsigned ncuda = 0;
  69. int nprocs1 = 0;
  70. int nprocs2 = 0;
  71. int procs1[STARPU_NMAXWORKERS], procs2[STARPU_NMAXWORKERS];
  72. char *sched;
  73. ret = starpu_init(NULL);
  74. if (ret == -ENODEV)
  75. return 77;
  76. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  77. #ifdef STARPU_USE_CPU
  78. nprocs1 = starpu_cpu_worker_get_count();
  79. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs1, nprocs1);
  80. #endif
  81. // if there is no cpu, skip
  82. if (nprocs1 == 0) goto enodev;
  83. #ifdef STARPU_USE_CUDA
  84. ncuda = nprocs2 = starpu_cuda_worker_get_count();
  85. starpu_worker_get_ids_by_type(STARPU_CUDA_WORKER, procs2, nprocs2);
  86. #endif
  87. if (nprocs2 == 0)
  88. {
  89. nprocs2 = 1;
  90. procs2[0] = procs1[0];
  91. }
  92. /*create contexts however you want*/
  93. sched = getenv("STARPU_SCHED");
  94. unsigned sched_ctx1 = starpu_sched_ctx_create(procs1, nprocs1, "ctx1", STARPU_SCHED_CTX_POLICY_NAME, sched?sched:"eager", 0);
  95. unsigned sched_ctx2 = starpu_sched_ctx_create(procs2, nprocs2, "ctx2", STARPU_SCHED_CTX_POLICY_NAME, sched?sched:"eager", 0);
  96. /*indicate what to do with the resources when context 2 finishes (it depends on your application)*/
  97. starpu_sched_ctx_set_inheritor(sched_ctx2, sched_ctx1);
  98. starpu_sched_ctx_display_workers(sched_ctx2, stderr);
  99. int i;
  100. for (i = 0; i < ntasks/2; i++)
  101. {
  102. struct starpu_task *task = starpu_task_create();
  103. task->cl = &sched_ctx_codelet1;
  104. task->cl_arg = NULL;
  105. /*submit tasks to context*/
  106. ret = starpu_task_submit_to_ctx(task,sched_ctx1);
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  108. }
  109. /* tell starpu when you finished submitting tasks to this context
  110. in order to allow moving resources from this context to the inheritor one
  111. when its corresponding tasks finished executing */
  112. starpu_sched_ctx_finished_submit(sched_ctx1);
  113. /* task with no cuda impl submitted to a ctx with gpus only */
  114. struct starpu_task *task2 = starpu_task_create();
  115. task2->cl = &sched_ctx_codelet1;
  116. task2->cl_arg = NULL;
  117. /*submit tasks to context*/
  118. ret = starpu_task_submit_to_ctx(task2,sched_ctx2);
  119. if (ncuda == 0)
  120. {
  121. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  122. }
  123. else
  124. {
  125. STARPU_ASSERT_MSG(ret == -ENODEV, "submit task should ret enodev when the ctx does not have the PUs needed by the task");
  126. }
  127. for (i = 0; i < ntasks/2; i++)
  128. {
  129. struct starpu_task *task = starpu_task_create();
  130. task->cl = &sched_ctx_codelet2;
  131. task->cl_arg = NULL;
  132. ret = starpu_task_submit_to_ctx(task,sched_ctx2);
  133. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  134. }
  135. starpu_sched_ctx_finished_submit(sched_ctx2);
  136. /* wait for all tasks at the end*/
  137. starpu_task_wait_for_all();
  138. starpu_sched_ctx_add_workers(procs1, nprocs1, sched_ctx2);
  139. starpu_sched_ctx_delete(sched_ctx1);
  140. starpu_sched_ctx_delete(sched_ctx2);
  141. printf("tasks executed %d out of %d\n", tasks_executed, ntasks+1);
  142. printf("tasks executed on ctx1: %d\n", ctx1_tasks_executed);
  143. printf("tasks executed on ctx2: %d\n", ctx2_tasks_executed);
  144. enodev:
  145. starpu_shutdown();
  146. return nprocs1 == 0 ? 77 : 0;
  147. }