heteroprio_test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015 INRIA
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <schedulers/starpu_heteroprio.h>
  18. #include <unistd.h>
  19. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  20. void initSchedulerCallback()
  21. {
  22. // CPU uses 3 buckets
  23. starpu_heteroprio_set_nb_prios(0, STARPU_CPU_IDX, 3);
  24. // It uses direct mapping idx => idx
  25. unsigned idx;
  26. for(idx = 0; idx < 3; ++idx)
  27. {
  28. starpu_heteroprio_set_mapping(0, STARPU_CPU_IDX, idx, idx);
  29. starpu_heteroprio_set_faster_arch(0, STARPU_CPU_IDX, idx);
  30. }
  31. #ifdef STARPU_USE_OPENCL
  32. // OpenCL is enabled and uses 2 buckets
  33. starpu_heteroprio_set_nb_prios(0, STARPU_OPENCL_IDX, 2);
  34. // OpenCL will first look to priority 2
  35. starpu_heteroprio_set_mapping(0, STARPU_OPENCL_IDX, 0, 2);
  36. // For this bucket OpenCL is the fastest
  37. starpu_heteroprio_set_faster_arch(0, STARPU_OPENCL_IDX, 2);
  38. // And CPU is 4 times slower
  39. starpu_heteroprio_set_arch_slow_factor(0, STARPU_CPU_IDX, 2, 4.0f);
  40. starpu_heteroprio_set_mapping(0, STARPU_OPENCL_IDX, 1, 1);
  41. // We let the CPU as the fastest and tell that OpenCL is 1.7 times slower
  42. starpu_heteroprio_set_arch_slow_factor(0, STARPU_OPENCL_IDX, 1, 1.7f);
  43. #endif
  44. }
  45. void callback_a_cpu(void *buffers[], void *cl_arg)
  46. {
  47. usleep(100000);
  48. FPRINTF(stderr, "[COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stdout);
  49. }
  50. void callback_b_cpu(void *buffers[], void *cl_arg)
  51. {
  52. usleep(100000);
  53. FPRINTF(stderr, "[COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stdout);
  54. }
  55. void callback_c_cpu(void *buffers[], void *cl_arg)
  56. {
  57. usleep(100000);
  58. FPRINTF(stderr, "[COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stdout);
  59. }
  60. #ifdef STARPU_USE_OPENCL
  61. void callback_a_opencl(void *buffers[], void *cl_arg)
  62. {
  63. usleep(100000);
  64. FPRINTF(stderr, "[COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stdout);
  65. }
  66. void callback_b_opencl(void *buffers[], void *cl_arg)
  67. {
  68. usleep(100000);
  69. FPRINTF(stderr, "[COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stdout);
  70. }
  71. void callback_c_opencl(void *buffers[], void *cl_arg)
  72. {
  73. usleep(100000);
  74. FPRINTF(stderr, "[COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stdout);
  75. }
  76. #endif
  77. int main(int argc, char** argv)
  78. {
  79. unsigned ret;
  80. struct starpu_conf conf;
  81. ret = starpu_conf_init(&conf);
  82. assert(ret == 0);
  83. conf.sched_policy_name = "heteroprio";
  84. conf.sched_policy_init = &initSchedulerCallback;
  85. ret = starpu_init(&conf);
  86. assert(ret == 0);
  87. starpu_pause();
  88. FPRINTF(stderr, "Worker = %d\n", starpu_worker_get_count());
  89. FPRINTF(stderr, "Worker CPU = %d\n", starpu_cpu_worker_get_count());
  90. #ifdef STARPU_USE_OPENCL
  91. FPRINTF(stderr, "Worker OpenCL = %d\n", starpu_opencl_worker_get_count());
  92. #endif
  93. struct starpu_codelet codeleteA;
  94. {
  95. memset(&codeleteA, 0, sizeof(codeleteA));
  96. codeleteA.nbuffers = 2;
  97. codeleteA.modes[0] = STARPU_RW;
  98. codeleteA.modes[1] = STARPU_RW;
  99. codeleteA.name = "codeleteA";
  100. codeleteA.where = STARPU_CPU;
  101. codeleteA.cpu_funcs[0] = callback_a_cpu;
  102. #ifdef STARPU_USE_OPENCL
  103. codeleteA.where |= STARPU_OPENCL;
  104. codeleteA.opencl_funcs[0] = callback_a_opencl;
  105. #endif
  106. }
  107. struct starpu_codelet codeleteB;
  108. {
  109. memset(&codeleteB, 0, sizeof(codeleteB));
  110. codeleteB.nbuffers = 2;
  111. codeleteB.modes[0] = STARPU_RW;
  112. codeleteB.modes[1] = STARPU_RW;
  113. codeleteB.name = "codeleteB";
  114. codeleteB.where = STARPU_CPU;
  115. codeleteB.cpu_funcs[0] = callback_b_cpu;
  116. #ifdef STARPU_USE_OPENCL
  117. codeleteB.where |= STARPU_OPENCL;
  118. codeleteB.opencl_funcs[0] = callback_b_opencl;
  119. #endif
  120. }
  121. struct starpu_codelet codeleteC;
  122. {
  123. memset(&codeleteC, 0, sizeof(codeleteC));
  124. codeleteC.nbuffers = 2;
  125. codeleteC.modes[0] = STARPU_RW;
  126. codeleteC.modes[1] = STARPU_RW;
  127. codeleteC.name = "codeleteC";
  128. codeleteC.where = STARPU_CPU;
  129. codeleteC.cpu_funcs[0] = callback_c_cpu;
  130. #ifdef STARPU_USE_OPENCL
  131. codeleteC.where |= STARPU_OPENCL;
  132. codeleteC.opencl_funcs[0] = callback_c_opencl;
  133. #endif
  134. }
  135. const int nbHandles = 10;
  136. FPRINTF(stderr, "Nb handles = %d\n", nbHandles);
  137. starpu_data_handle_t handles[nbHandles];
  138. memset(handles, 0, sizeof(handles[0])*nbHandles);
  139. int dataA[nbHandles];
  140. int idx;
  141. for(idx = 0; idx < nbHandles; ++idx){
  142. dataA[idx] = idx;
  143. }
  144. int idxHandle;
  145. for(idxHandle = 0; idxHandle < nbHandles; ++idxHandle){
  146. starpu_variable_data_register(&handles[idxHandle], 0, (uintptr_t)&dataA[idxHandle], sizeof(dataA[idxHandle]));
  147. }
  148. const int nbTasks = 40;
  149. FPRINTF(stderr, "Submit %d tasks \n", nbTasks);
  150. starpu_resume();
  151. int idxTask;
  152. for(idxTask = 0; idxTask < nbTasks; ++idxTask)
  153. {
  154. starpu_insert_task(&codeleteA,
  155. STARPU_PRIORITY, 0,
  156. (STARPU_RW), handles[(idxTask*2)%nbHandles],
  157. (STARPU_RW), handles[(idxTask*3+1)%nbHandles],
  158. 0);
  159. starpu_insert_task(&codeleteB,
  160. STARPU_PRIORITY, 1,
  161. (STARPU_RW), handles[(idxTask*2 +1 )%nbHandles],
  162. (STARPU_RW), handles[(idxTask*2)%nbHandles],
  163. 0);
  164. starpu_insert_task(&codeleteC,
  165. STARPU_PRIORITY, 2,
  166. (STARPU_RW), handles[(idxTask)%nbHandles],
  167. (STARPU_RW), handles[(idxTask*idxTask)%nbHandles],
  168. 0);
  169. }
  170. FPRINTF(stderr, "Wait task\n");
  171. starpu_task_wait_for_all();
  172. FPRINTF(stderr, "Release data\n");
  173. for(idxHandle = 0 ; idxHandle < nbHandles ; ++idxHandle)
  174. {
  175. starpu_data_unregister(handles[idxHandle]);
  176. }
  177. FPRINTF(stderr, "Shutdown\n");
  178. starpu_shutdown();
  179. return 0;
  180. }