nested_sched_ctxs.c 5.9 KB

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