heteroprio_test.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/heteroprio.h>
  18. #include <unistd.h>
  19. void initSchedulerCallback(){
  20. // CPU uses 3 buckets
  21. starpu_heteroprio_set_nb_prios(0, FSTARPU_CPU_IDX, 3);
  22. // It uses direct mapping idx => idx
  23. unsigned idx;
  24. for(idx = 0; idx < 3; ++idx){
  25. starpu_heteroprio_set_mapping(0, FSTARPU_CPU_IDX, idx, idx);
  26. starpu_heteroprio_set_faster_arch(0, FSTARPU_CPU_IDX, idx);
  27. }
  28. #ifdef STARPU_USE_OPENCL
  29. // OpenCL is enabled and uses 2 buckets
  30. starpu_heteroprio_set_nb_prios(0, FSTARPU_OPENCL_IDX, 2);
  31. // OpenCL will first look to priority 2
  32. starpu_heteroprio_set_mapping(0, FSTARPU_OPENCL_IDX, 0, 2);
  33. // For this bucket OpenCL is the fastest
  34. starpu_heteroprio_set_faster_arch(0, FSTARPU_OPENCL_IDX, 2);
  35. // And CPU is 4 times slower
  36. starpu_heteroprio_set_arch_slow_factor(0, FSTARPU_CPU_IDX, 2, 4.0f);
  37. starpu_heteroprio_set_mapping(0, FSTARPU_OPENCL_IDX, 1, 1);
  38. // We let the CPU as the fastest and tell that OpenCL is 1.7 times slower
  39. starpu_heteroprio_set_arch_slow_factor(0, FSTARPU_OPENCL_IDX, 1, 1.7f);
  40. #endif
  41. }
  42. void callback_a_cpu(void *buffers[], void *cl_arg){
  43. usleep(100000);
  44. printf("COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stdout);
  45. }
  46. void callback_b_cpu(void *buffers[], void *cl_arg){
  47. usleep(100000);
  48. printf("COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stdout);
  49. }
  50. void callback_c_cpu(void *buffers[], void *cl_arg){
  51. usleep(100000);
  52. printf("COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stdout);
  53. }
  54. #ifdef STARPU_USE_OPENCL
  55. void callback_a_opencl(void *buffers[], void *cl_arg){
  56. usleep(100000);
  57. printf("COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stdout);
  58. }
  59. void callback_b_opencl(void *buffers[], void *cl_arg){
  60. usleep(100000);
  61. printf("COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stdout);
  62. }
  63. void callback_c_opencl(void *buffers[], void *cl_arg){
  64. usleep(100000);
  65. printf("COMMUTE_LOG] callback %s\n", __FUNCTION__); fflush(stdout);
  66. }
  67. #endif
  68. int main(int argc, char** argv){
  69. unsigned ret;
  70. struct starpu_conf conf;
  71. ret = starpu_conf_init(&conf);
  72. assert(ret == 0);
  73. conf.sched_policy_name = "heteroprio";
  74. conf.sched_policy_init = &initSchedulerCallback;
  75. ret = starpu_init(&conf);
  76. assert(ret == 0);
  77. starpu_pause();
  78. printf("Worker = %d\n", starpu_worker_get_count());
  79. printf("Worker CPU = %d\n", starpu_cpu_worker_get_count());
  80. #ifdef STARPU_USE_OPENCL
  81. printf("Worker OpenCL = %d\n", starpu_cpu_worker_get_count());
  82. #endif
  83. struct starpu_codelet codeleteA;
  84. {
  85. memset(&codeleteA, 0, sizeof(codeleteA));
  86. codeleteA.nbuffers = 2;
  87. codeleteA.modes[0] = STARPU_RW;
  88. codeleteA.modes[1] = STARPU_RW;
  89. codeleteA.name = "codeleteA";
  90. codeleteA.where = STARPU_CPU;
  91. codeleteA.cpu_funcs[0] = callback_a_cpu;
  92. #ifdef STARPU_USE_OPENCL
  93. codeleteA.where |= STARPU_OPENCL;
  94. codeleteA.cpu_funcs[0] = callback_a_opencl;
  95. #endif
  96. }
  97. struct starpu_codelet codeleteB;
  98. {
  99. memset(&codeleteB, 0, sizeof(codeleteB));
  100. codeleteB.nbuffers = 2;
  101. codeleteB.modes[0] = STARPU_RW;
  102. codeleteB.modes[1] = STARPU_RW;
  103. codeleteB.name = "codeleteB";
  104. codeleteB.where = STARPU_CPU;
  105. codeleteB.cpu_funcs[0] = callback_b_cpu;
  106. #ifdef STARPU_USE_OPENCL
  107. codeleteB.where |= STARPU_OPENCL;
  108. codeleteB.cpu_funcs[0] = callback_b_opencl;
  109. #endif
  110. }
  111. struct starpu_codelet codeleteC;
  112. {
  113. memset(&codeleteC, 0, sizeof(codeleteC));
  114. codeleteC.nbuffers = 2;
  115. codeleteC.modes[0] = STARPU_RW;
  116. codeleteC.modes[1] = STARPU_RW;
  117. codeleteC.name = "codeleteC";
  118. codeleteC.where = STARPU_CPU;
  119. codeleteC.cpu_funcs[0] = callback_c_cpu;
  120. #ifdef STARPU_USE_OPENCL
  121. codeleteC.where |= STARPU_OPENCL;
  122. codeleteC.cpu_funcs[0] = callback_c_opencl;
  123. #endif
  124. }
  125. const int nbHandles = 10;
  126. printf("Nb handles = %d\n", nbHandles);
  127. starpu_data_handle_t handles[nbHandles];
  128. memset(handles, 0, sizeof(handles[0])*nbHandles);
  129. int dataA[nbHandles];
  130. int idx;
  131. for(idx = 0; idx < nbHandles; ++idx){
  132. dataA[idx] = idx;
  133. }
  134. int idxHandle;
  135. for(idxHandle = 0; idxHandle < nbHandles; ++idxHandle){
  136. starpu_variable_data_register(&handles[idxHandle], 0, (uintptr_t)&dataA[idxHandle], sizeof(dataA[idxHandle]));
  137. }
  138. const int nbTasks = 40;
  139. printf("Submit %d tasks \n", nbTasks);
  140. starpu_resume();
  141. int idxTask;
  142. for(idxTask = 0; idxTask < nbTasks; ++idxTask){
  143. starpu_insert_task(&codeleteA,
  144. STARPU_PRIORITY, 0,
  145. (STARPU_RW), handles[(idxTask*2)%nbHandles],
  146. (STARPU_RW), handles[(idxTask*3+1)%nbHandles],
  147. 0);
  148. starpu_insert_task(&codeleteB,
  149. STARPU_PRIORITY, 1,
  150. (STARPU_RW), handles[(idxTask*2 +1 )%nbHandles],
  151. (STARPU_RW), handles[(idxTask*2)%nbHandles],
  152. 0);
  153. starpu_insert_task(&codeleteC,
  154. STARPU_PRIORITY, 2,
  155. (STARPU_RW), handles[(idxTask)%nbHandles],
  156. (STARPU_RW), handles[(idxTask*idxTask)%nbHandles],
  157. 0);
  158. }
  159. printf("Wait task\n");
  160. starpu_task_wait_for_all();
  161. starpu_pause();
  162. printf("Release data\n");
  163. for(idxHandle = 0 ; idxHandle < nbHandles ; ++idxHandle){
  164. starpu_data_unregister(handles[idxHandle]);
  165. }
  166. printf("Shutdown\n");
  167. starpu_resume();
  168. starpu_shutdown();
  169. return 0;
  170. }