heteroprio_test.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015 INRIA
  4. * Copyright (C) 2016, 2017 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. /*
  18. * This is an example making use of the heteroprio scheduler, it shows how
  19. * priorities are taken into account.
  20. */
  21. #include <starpu.h>
  22. #include <schedulers/starpu_heteroprio.h>
  23. #include <unistd.h>
  24. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  25. void initSchedulerCallback(unsigned sched_ctx)
  26. {
  27. // CPU uses 3 buckets
  28. #ifdef STARPU_USE_CPU
  29. if (starpu_cpu_worker_get_count())
  30. {
  31. starpu_heteroprio_set_nb_prios(0, STARPU_CPU_IDX, 3);
  32. // It uses direct mapping idx => idx
  33. unsigned idx;
  34. for(idx = 0; idx < 3; ++idx)
  35. {
  36. starpu_heteroprio_set_mapping(sched_ctx, STARPU_CPU_IDX, idx, idx);
  37. starpu_heteroprio_set_faster_arch(sched_ctx, STARPU_CPU_IDX, idx);
  38. }
  39. }
  40. #endif
  41. #ifdef STARPU_USE_OPENCL
  42. // OpenCL is enabled and uses 2 buckets
  43. starpu_heteroprio_set_nb_prios(sched_ctx, STARPU_OPENCL_IDX, 2);
  44. // OpenCL will first look to priority 2
  45. int prio2 = starpu_cpu_worker_get_count() ? 2 : 1;
  46. starpu_heteroprio_set_mapping(sched_ctx, STARPU_OPENCL_IDX, 0, prio2);
  47. // For this bucket OpenCL is the fastest
  48. starpu_heteroprio_set_faster_arch(sched_ctx, STARPU_OPENCL_IDX, prio2);
  49. // And CPU is 4 times slower
  50. #ifdef STARPU_USE_CPU
  51. starpu_heteroprio_set_arch_slow_factor(sched_ctx, STARPU_CPU_IDX, 2, 4.0f);
  52. #endif
  53. int prio1 = starpu_cpu_worker_get_count() ? 1 : 0;
  54. starpu_heteroprio_set_mapping(sched_ctx, STARPU_OPENCL_IDX, 1, prio1);
  55. // We let the CPU as the fastest and tell that OpenCL is 1.7 times slower
  56. starpu_heteroprio_set_arch_slow_factor(sched_ctx, STARPU_OPENCL_IDX, prio1, 1.7f);
  57. #endif
  58. }
  59. void callback_a_cpu(void *buffers[], void *cl_arg)
  60. {
  61. usleep(100000);
  62. FPRINTF(stderr, "[COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stderr);
  63. }
  64. void callback_b_cpu(void *buffers[], void *cl_arg)
  65. {
  66. usleep(100000);
  67. FPRINTF(stderr, "[COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stderr);
  68. }
  69. void callback_c_cpu(void *buffers[], void *cl_arg)
  70. {
  71. usleep(100000);
  72. FPRINTF(stderr, "[COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stderr);
  73. }
  74. #ifdef STARPU_USE_OPENCL
  75. void callback_a_opencl(void *buffers[], void *cl_arg)
  76. {
  77. usleep(100000);
  78. FPRINTF(stderr, "[COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stderr);
  79. }
  80. void callback_b_opencl(void *buffers[], void *cl_arg)
  81. {
  82. usleep(100000);
  83. FPRINTF(stderr, "[COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stderr);
  84. }
  85. void callback_c_opencl(void *buffers[], void *cl_arg)
  86. {
  87. usleep(100000);
  88. FPRINTF(stderr, "[COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stderr);
  89. }
  90. #endif
  91. int main(int argc, char** argv)
  92. {
  93. int ret;
  94. struct starpu_conf conf;
  95. int ncpus, nopencls;
  96. ret = starpu_conf_init(&conf);
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_conf_init");
  98. assert(ret == 0);
  99. conf.sched_policy_name = "heteroprio";
  100. #warning "heteroprio needs update with new synchro scheme"
  101. return 77;
  102. conf.sched_policy_init = &initSchedulerCallback;
  103. ret = starpu_init(&conf);
  104. if (ret == -ENODEV)
  105. return 77;
  106. ncpus = starpu_cpu_worker_get_count();
  107. nopencls = starpu_opencl_worker_get_count();
  108. FPRINTF(stderr, "Worker = %u\n", starpu_worker_get_count());
  109. FPRINTF(stderr, "Worker CPU = %d\n", ncpus);
  110. FPRINTF(stderr, "Worker OpenCL = %d\n", nopencls);
  111. if (ncpus + nopencls == 0)
  112. {
  113. FPRINTF(stderr, "Needs at least one CPU or OpenCL device\n");
  114. starpu_shutdown();
  115. return 77;
  116. }
  117. struct starpu_codelet codeleteA;
  118. {
  119. memset(&codeleteA, 0, sizeof(codeleteA));
  120. codeleteA.nbuffers = 2;
  121. codeleteA.modes[0] = STARPU_RW;
  122. codeleteA.modes[1] = STARPU_RW;
  123. codeleteA.name = "codeleteA";
  124. #ifdef STARPU_USE_CPU
  125. codeleteA.cpu_funcs[0] = callback_a_cpu;
  126. #endif
  127. #ifdef STARPU_USE_OPENCL
  128. codeleteA.opencl_funcs[0] = callback_a_opencl;
  129. #endif
  130. }
  131. struct starpu_codelet codeleteB;
  132. {
  133. memset(&codeleteB, 0, sizeof(codeleteB));
  134. codeleteB.nbuffers = 2;
  135. codeleteB.modes[0] = STARPU_RW;
  136. codeleteB.modes[1] = STARPU_RW;
  137. codeleteB.name = "codeleteB";
  138. codeleteB.cpu_funcs[0] = callback_b_cpu;
  139. #ifdef STARPU_USE_OPENCL
  140. codeleteB.opencl_funcs[0] = callback_b_opencl;
  141. #endif
  142. }
  143. struct starpu_codelet codeleteC;
  144. {
  145. memset(&codeleteC, 0, sizeof(codeleteC));
  146. codeleteC.nbuffers = 2;
  147. codeleteC.modes[0] = STARPU_RW;
  148. codeleteC.modes[1] = STARPU_RW;
  149. codeleteC.name = "codeleteC";
  150. codeleteC.cpu_funcs[0] = callback_c_cpu;
  151. #ifdef STARPU_USE_OPENCL
  152. codeleteC.opencl_funcs[0] = callback_c_opencl;
  153. #endif
  154. }
  155. const int nbHandles = 10;
  156. FPRINTF(stderr, "Nb handles = %d\n", nbHandles);
  157. starpu_data_handle_t handles[nbHandles];
  158. memset(handles, 0, sizeof(handles[0])*nbHandles);
  159. int dataA[nbHandles];
  160. int idx;
  161. for(idx = 0; idx < nbHandles; ++idx)
  162. {
  163. dataA[idx] = idx;
  164. }
  165. int idxHandle;
  166. for(idxHandle = 0; idxHandle < nbHandles; ++idxHandle)
  167. {
  168. starpu_variable_data_register(&handles[idxHandle], 0, (uintptr_t)&dataA[idxHandle], sizeof(dataA[idxHandle]));
  169. }
  170. const int nbTasks = 4;
  171. FPRINTF(stderr, "Submit %d tasks \n", nbTasks);
  172. int prio2 = starpu_cpu_worker_get_count() ? 2 : 1;
  173. int idxTask;
  174. for(idxTask = 0; idxTask < nbTasks; ++idxTask)
  175. {
  176. starpu_insert_task(&codeleteA,
  177. STARPU_PRIORITY, 0,
  178. (STARPU_RW), handles[(idxTask*2)%nbHandles],
  179. (STARPU_RW), handles[(idxTask*3+1)%nbHandles],
  180. 0);
  181. starpu_insert_task(&codeleteB,
  182. STARPU_PRIORITY, 1,
  183. (STARPU_RW), handles[(idxTask*2 +1 )%nbHandles],
  184. (STARPU_RW), handles[(idxTask*2)%nbHandles],
  185. 0);
  186. starpu_insert_task(&codeleteC,
  187. STARPU_PRIORITY, prio2,
  188. (STARPU_RW), handles[(idxTask)%nbHandles],
  189. (STARPU_RW), handles[(idxTask*idxTask)%nbHandles],
  190. 0);
  191. }
  192. FPRINTF(stderr, "Wait task\n");
  193. starpu_task_wait_for_all();
  194. FPRINTF(stderr, "Release data\n");
  195. for(idxHandle = 0 ; idxHandle < nbHandles ; ++idxHandle)
  196. {
  197. starpu_data_unregister(handles[idxHandle]);
  198. }
  199. FPRINTF(stderr, "Shutdown\n");
  200. starpu_shutdown();
  201. return 0;
  202. }