nested_sched_ctxs.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2015 Université de Bordeaux
  4. * Copyright (C) 2010-2014, 2016 CNRS
  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. #include <starpu.h>
  18. #include <omp.h>
  19. #ifdef STARPU_QUICK_CHECK
  20. #define NTASKS 64
  21. #else
  22. #define NTASKS 100
  23. #endif
  24. int tasks_executed[2];
  25. int parallel_code(int sched_ctx)
  26. {
  27. int i;
  28. int t = 0;
  29. int *cpuids = NULL;
  30. int ncpuids = 0;
  31. starpu_sched_ctx_get_available_cpuids(sched_ctx, &cpuids, &ncpuids);
  32. // printf("execute task of %d threads \n", ncpuids);
  33. #pragma omp parallel num_threads(ncpuids)
  34. {
  35. starpu_sched_ctx_bind_current_thread_to_cpuid(cpuids[omp_get_thread_num()]);
  36. // printf("cpu = %d ctx%d nth = %d\n", sched_getcpu(), sched_ctx, omp_get_num_threads());
  37. #pragma omp for
  38. for(i = 0; i < NTASKS; i++)
  39. t++;
  40. }
  41. free(cpuids);
  42. return t;
  43. }
  44. static void sched_ctx_func(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  45. {
  46. int w = starpu_worker_get_id();
  47. unsigned sched_ctx = (uintptr_t)arg;
  48. int n = parallel_code(sched_ctx);
  49. //printf("w %d executed %d it \n", w, n);
  50. }
  51. static struct starpu_codelet sched_ctx_codelet =
  52. {
  53. .cpu_funcs = {sched_ctx_func},
  54. .model = NULL,
  55. .nbuffers = 0,
  56. .name = "sched_ctx"
  57. };
  58. int main(int argc, char **argv)
  59. {
  60. tasks_executed[0] = 0;
  61. tasks_executed[1] = 0;
  62. int ntasks = NTASKS;
  63. int ret, j, k;
  64. unsigned ncpus = 0;
  65. ret = starpu_init(NULL);
  66. if (ret == -ENODEV)
  67. return 77;
  68. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  69. int nprocs1 = 1;
  70. int nprocs2 = 1;
  71. int *procs1, *procs2;
  72. #ifdef STARPU_USE_CPU
  73. ncpus = starpu_cpu_worker_get_count();
  74. procs1 = (int*)malloc(ncpus*sizeof(int));
  75. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, procs1, ncpus);
  76. if (ncpus > 1)
  77. {
  78. nprocs1 = ncpus/2;
  79. nprocs2 = nprocs1;
  80. k = 0;
  81. procs2 = (int*)malloc(nprocs2*sizeof(int));
  82. for(j = nprocs1; j < nprocs1+nprocs2; j++)
  83. procs2[k++] = procs1[j];
  84. }
  85. else
  86. {
  87. procs2 = (int*)malloc(nprocs2*sizeof(int));
  88. procs2[0] = procs1[0];
  89. }
  90. #endif
  91. if (ncpus == 0)
  92. {
  93. #ifdef STARPU_USE_CPU
  94. free(procs1);
  95. free(procs2);
  96. #endif
  97. starpu_shutdown();
  98. return 77;
  99. }
  100. /*create contexts however you want*/
  101. unsigned sched_ctx1 = starpu_sched_ctx_create(procs1, nprocs1, "ctx1", STARPU_SCHED_CTX_POLICY_NAME, "eager", 0);
  102. unsigned sched_ctx2 = starpu_sched_ctx_create(procs2, nprocs2, "ctx2", STARPU_SCHED_CTX_POLICY_NAME, "dmda", 0);
  103. /*indicate what to do with the resources when context 2 finishes (it depends on your application)*/
  104. // starpu_sched_ctx_set_inheritor(sched_ctx2, sched_ctx1);
  105. int nprocs3 = nprocs1/2;
  106. int nprocs4 = nprocs1/2;
  107. int nprocs5 = nprocs2/2;
  108. int nprocs6 = nprocs2/2;
  109. int *procs3 = NULL;
  110. int *procs4 = NULL;
  111. int *procs5 = NULL;
  112. int *procs6 = NULL;
  113. if (nprocs3)
  114. procs3 = malloc(nprocs3 * sizeof(*procs3));
  115. if (nprocs4)
  116. procs4 = malloc(nprocs4 * sizeof(*procs4));
  117. if (nprocs5)
  118. procs5 = malloc(nprocs5 * sizeof(*procs5));
  119. if (nprocs6)
  120. procs6 = malloc(nprocs6 * sizeof(*procs6));
  121. k = 0;
  122. for(j = 0; j < nprocs3; j++)
  123. procs3[k++] = procs1[j];
  124. k = 0;
  125. for(j = nprocs3; j < nprocs3+nprocs4; j++)
  126. procs4[k++] = procs1[j];
  127. k = 0;
  128. for(j = 0; j < nprocs5; j++)
  129. procs5[k++] = procs2[j];
  130. k = 0;
  131. for(j = nprocs5; j < nprocs5+nprocs6; j++)
  132. procs6[k++] = procs2[j];
  133. int sched_ctx3 = -1;
  134. int sched_ctx4 = -1;
  135. int sched_ctx5 = -1;
  136. int sched_ctx6 = -1;
  137. if (nprocs3)
  138. sched_ctx3 = starpu_sched_ctx_create(procs3, nprocs3, "ctx3", STARPU_SCHED_CTX_NESTED, sched_ctx1, 0);
  139. if (nprocs4)
  140. sched_ctx4 = starpu_sched_ctx_create(procs4, nprocs4, "ctx4", STARPU_SCHED_CTX_NESTED, sched_ctx1, 0);
  141. if (nprocs5)
  142. sched_ctx5 = starpu_sched_ctx_create(procs5, nprocs5, "ctx5", STARPU_SCHED_CTX_NESTED, sched_ctx2, 0);
  143. if (nprocs6)
  144. sched_ctx6 = starpu_sched_ctx_create(procs6, nprocs6, "ctx6", STARPU_SCHED_CTX_NESTED, sched_ctx2, 0);
  145. int i;
  146. for (i = 0; i < ntasks; i++)
  147. {
  148. struct starpu_task *task = starpu_task_create();
  149. task->cl = &sched_ctx_codelet;
  150. task->cl_arg = (void*)(uintptr_t) sched_ctx1;
  151. task->possibly_parallel = 1;
  152. /*submit tasks to context*/
  153. ret = starpu_task_submit_to_ctx(task,sched_ctx1);
  154. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  155. }
  156. for (i = 0; i < ntasks; i++)
  157. {
  158. struct starpu_task *task = starpu_task_create();
  159. task->cl = &sched_ctx_codelet;
  160. task->cl_arg = (void*)(uintptr_t) sched_ctx2;
  161. task->possibly_parallel = 1;
  162. /*submit tasks to context*/
  163. ret = starpu_task_submit_to_ctx(task,sched_ctx2);
  164. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  165. }
  166. /* tell starpu when you finished submitting tasks to this context
  167. in order to allow moving resources from this context to the inheritor one
  168. when its corresponding tasks finished executing */
  169. /* wait for all tasks at the end*/
  170. starpu_task_wait_for_all();
  171. if (nprocs3)
  172. {
  173. starpu_sched_ctx_delete(sched_ctx3);
  174. free(procs3);
  175. }
  176. if (nprocs4)
  177. {
  178. starpu_sched_ctx_delete(sched_ctx4);
  179. free(procs4);
  180. }
  181. if (nprocs5)
  182. {
  183. starpu_sched_ctx_delete(sched_ctx5);
  184. free(procs5);
  185. }
  186. if (nprocs6)
  187. {
  188. starpu_sched_ctx_delete(sched_ctx6);
  189. free(procs6);
  190. }
  191. starpu_sched_ctx_delete(sched_ctx1);
  192. starpu_sched_ctx_delete(sched_ctx2);
  193. printf("ctx%u: tasks starpu executed %d out of %d\n", sched_ctx1, tasks_executed[0], NTASKS);
  194. printf("ctx%u: tasks starpu executed %d out of %d\n", sched_ctx2, tasks_executed[1], NTASKS);
  195. #ifdef STARPU_USE_CPU
  196. free(procs1);
  197. free(procs2);
  198. #endif
  199. starpu_shutdown();
  200. return 0;
  201. }