sched_ctx.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010-2014 Centre National de la Recherche Scientifique
  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. OB */
  17. #include <starpu.h>
  18. #ifdef STARPU_QUICK_CHECK
  19. #define NTASKS 64
  20. #else
  21. #define NTASKS 1000
  22. #endif
  23. int tasks_executed = 0;
  24. starpu_pthread_mutex_t mut;
  25. static void sched_ctx_cpu_func(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg STARPU_ATTRIBUTE_UNUSED)
  26. {
  27. starpu_pthread_mutex_lock(&mut);
  28. tasks_executed++;
  29. starpu_pthread_mutex_unlock(&mut);
  30. }
  31. static void sched_ctx_cuda_func(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg STARPU_ATTRIBUTE_UNUSED)
  32. {
  33. }
  34. static struct starpu_codelet sched_ctx_codelet1 =
  35. {
  36. .cpu_funcs = {sched_ctx_cpu_func, NULL},
  37. .cuda_funcs = {NULL},
  38. .opencl_funcs = {NULL},
  39. .model = NULL,
  40. .nbuffers = 0,
  41. .name = "sched_ctx"
  42. };
  43. static struct starpu_codelet sched_ctx_codelet2 =
  44. {
  45. .cpu_funcs = {sched_ctx_cpu_func, NULL},
  46. .cuda_funcs = {sched_ctx_cuda_func, NULL},
  47. .opencl_funcs = {NULL},
  48. .model = NULL,
  49. .nbuffers = 0,
  50. .name = "sched_ctx"
  51. };
  52. int main(int argc, char **argv)
  53. {
  54. int ntasks = NTASKS;
  55. int ret;
  56. unsigned ncuda = 0;
  57. unsigned ncpus = 0;
  58. ret = starpu_init(NULL);
  59. if (ret == -ENODEV)
  60. return 77;
  61. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  62. starpu_pthread_mutex_init(&mut, NULL);
  63. int nprocs1 = 1;
  64. int nprocs2 = 1;
  65. int procs1[STARPU_NMAXWORKERS], procs2[STARPU_NMAXWORKERS];
  66. procs1[0] = 0;
  67. procs2[0] = 0;
  68. #ifdef STARPU_USE_CPU
  69. ncpus = starpu_cpu_worker_get_count();
  70. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs1, ncpus);
  71. nprocs1 = ncpus;
  72. #endif
  73. if (ncpus == 0) goto enodev;
  74. #ifdef STARPU_USE_CUDA
  75. ncuda = starpu_cuda_worker_get_count();
  76. starpu_worker_get_ids_by_type(STARPU_CUDA_WORKER, procs2, ncuda);
  77. nprocs2 = ncuda == 0 ? 1 : ncuda;
  78. #endif
  79. /*create contexts however you want*/
  80. unsigned sched_ctx1 = starpu_sched_ctx_create(procs1, nprocs1, "ctx1", STARPU_SCHED_CTX_POLICY_NAME, "eager", 0);
  81. unsigned sched_ctx2 = starpu_sched_ctx_create(procs2, nprocs2, "ctx2", STARPU_SCHED_CTX_POLICY_NAME, "eager", 0);
  82. /*indicate what to do with the resources when context 2 finishes (it depends on your application)*/
  83. starpu_sched_ctx_set_inheritor(sched_ctx2, sched_ctx1);
  84. int i;
  85. for (i = 0; i < ntasks/2; i++)
  86. {
  87. struct starpu_task *task = starpu_task_create();
  88. task->cl = &sched_ctx_codelet1;
  89. task->cl_arg = NULL;
  90. /*submit tasks to context*/
  91. ret = starpu_task_submit_to_ctx(task,sched_ctx1);
  92. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  93. }
  94. /* tell starpu when you finished submitting tasks to this context
  95. in order to allow moving resources from this context to the inheritor one
  96. when its corresponding tasks finished executing */
  97. starpu_sched_ctx_finished_submit(sched_ctx1);
  98. /* task with no cuda impl submitted to a ctx with gpus only */
  99. struct starpu_task *task2 = starpu_task_create();
  100. task2->cl = &sched_ctx_codelet1;
  101. task2->cl_arg = NULL;
  102. /*submit tasks to context*/
  103. ret = starpu_task_submit_to_ctx(task2,sched_ctx2);
  104. if (ncuda == 0)
  105. {
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  107. }
  108. else
  109. {
  110. STARPU_ASSERT_MSG(ret == -ENODEV, "submit task should ret enodev when the ctx does not have the PUs needed by the task");
  111. }
  112. for (i = 0; i < ntasks/2; i++)
  113. {
  114. struct starpu_task *task = starpu_task_create();
  115. task->cl = &sched_ctx_codelet2;
  116. task->cl_arg = NULL;
  117. ret = starpu_task_submit_to_ctx(task,sched_ctx2);
  118. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  119. }
  120. starpu_sched_ctx_finished_submit(sched_ctx2);
  121. /* wait for all tasks at the end*/
  122. starpu_task_wait_for_all();
  123. starpu_sched_ctx_delete(sched_ctx1);
  124. starpu_sched_ctx_delete(sched_ctx2);
  125. printf("tasks executed %d out of %d\n", tasks_executed, ntasks/2);
  126. enodev:
  127. starpu_shutdown();
  128. return ncpus == 0 ? 77 : 0;
  129. }