parallel_redux_heterogeneous_tasks_data.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2016,2017 CNRS
  4. * Copyright (C) 2016,2017 Université de Bordeaux
  5. * Copyright (C) 2016 Bérangère Subervie
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <stdbool.h>
  19. #include <starpu.h>
  20. #include "../helper.h"
  21. /* Run a series of tasks with heterogeneous execution time and redux data */
  22. #define TIME 0.010
  23. #ifdef STARPU_QUICK_CHECK
  24. #define TASK_COEFFICIENT 20
  25. #define MARGIN 0.20
  26. #else
  27. #define TASK_COEFFICIENT 100
  28. #define MARGIN 0.10
  29. #endif
  30. #define TIME_CUDA_COEFFICIENT 10
  31. #define TIME_OPENCL_COEFFICIENT 5
  32. #define SECONDS_SCALE_COEFFICIENT_TIMING_NOW 1000000
  33. #define NB_FLOAT 400000
  34. void wait_CPU(void *descr[], void *_args)
  35. {
  36. (void)descr;
  37. (void)_args;
  38. starpu_sleep(TIME);
  39. }
  40. void wait_CUDA(void *descr[], void *_args)
  41. {
  42. (void)descr;
  43. (void)_args;
  44. starpu_sleep(TIME/TIME_CUDA_COEFFICIENT);
  45. }
  46. void wait_OPENCL(void *descr[], void *_args)
  47. {
  48. (void)descr;
  49. (void)_args;
  50. starpu_sleep(TIME/TIME_OPENCL_COEFFICIENT);
  51. }
  52. double cost_function(struct starpu_task *t, struct starpu_perfmodel_arch *a, unsigned i)
  53. {
  54. (void) t; (void) i;
  55. STARPU_ASSERT(a->ndevices == 1);
  56. if (a->devices[0].type == STARPU_CPU_WORKER)
  57. {
  58. STARPU_ASSERT(a->devices[0].ncores == 1);
  59. return TIME * 1000000;
  60. }
  61. else if (a->devices[0].type == STARPU_CUDA_WORKER)
  62. {
  63. return TIME/TIME_CUDA_COEFFICIENT * 1000000;
  64. }
  65. else if (a->devices[0].type == STARPU_OPENCL_WORKER)
  66. {
  67. return TIME/TIME_OPENCL_COEFFICIENT * 1000000;
  68. }
  69. STARPU_ASSERT(0);
  70. return 0.0;
  71. }
  72. static struct starpu_perfmodel perf_model =
  73. {
  74. .type = STARPU_PER_ARCH,
  75. .arch_cost_function = cost_function,
  76. };
  77. static struct starpu_codelet cl =
  78. {
  79. .cpu_funcs = { wait_CPU },
  80. .cuda_funcs = { wait_CUDA },
  81. .opencl_funcs = { wait_OPENCL },
  82. .cpu_funcs_name = { "wait_CPU" },
  83. .nbuffers = 1,
  84. .modes = {STARPU_REDUX},
  85. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  86. .model = &perf_model,
  87. .name = "cl",
  88. };
  89. static struct starpu_perfmodel perf_model_init =
  90. {
  91. .type = STARPU_PER_ARCH,
  92. .arch_cost_function = cost_function,
  93. };
  94. static struct starpu_codelet cl_init =
  95. {
  96. .cpu_funcs = { wait_CPU },
  97. .cuda_funcs = { wait_CUDA },
  98. .opencl_funcs = { wait_OPENCL },
  99. .cpu_funcs_name = { "wait_CPU" },
  100. .nbuffers = 1,
  101. .modes = {STARPU_RW},
  102. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  103. .model = &perf_model_init,
  104. .name = "init",
  105. };
  106. static struct starpu_perfmodel perf_model_redux =
  107. {
  108. .type = STARPU_PER_ARCH,
  109. .arch_cost_function = cost_function,
  110. };
  111. static struct starpu_codelet cl_redux =
  112. {
  113. .cpu_funcs = { wait_CPU },
  114. .cuda_funcs = { wait_CUDA },
  115. .opencl_funcs = { wait_OPENCL },
  116. .cpu_funcs_name = { "wait_CPU" },
  117. .nbuffers = 2,
  118. .modes = {STARPU_RW, STARPU_R},
  119. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  120. .model = &perf_model_redux,
  121. .name = "redux",
  122. };
  123. int main(int argc, char *argv[])
  124. {
  125. int ret;
  126. ret = starpu_initialize(NULL, &argc, &argv);
  127. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  128. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  129. unsigned nb_tasks, nb_workers_CPU, nb_workers_CUDA, nb_workers_OPENCL;
  130. double begin_time, end_time, time_m, time_s, speed_up, expected_speed_up, percentage_expected_speed_up;
  131. bool check, check_sup;
  132. nb_workers_CPU = starpu_worker_get_count_by_type(STARPU_CPU_WORKER);
  133. nb_workers_CUDA = starpu_worker_get_count_by_type(STARPU_CUDA_WORKER);
  134. nb_workers_OPENCL = starpu_worker_get_count_by_type(STARPU_OPENCL_WORKER);
  135. nb_tasks = (nb_workers_CPU + nb_workers_CUDA + nb_workers_OPENCL)*TASK_COEFFICIENT;
  136. /* We consider a vector of float that is initialized just as any of C
  137. * data */
  138. float *vector;
  139. starpu_data_handle_t vector_handle;
  140. unsigned i;
  141. vector = calloc(NB_FLOAT, sizeof(float));
  142. #ifndef STARPU_SIMGRID
  143. for (i = 0; i < NB_FLOAT; i++)
  144. vector[i] = (i+1.0f);
  145. #endif
  146. /* Tell StaPU to associate the "vector" vector with the "vector_handle"
  147. * identifier. When a task needs to access a piece of data, it should
  148. * refer to the handle that is associated to it.
  149. * In the case of the "vector" data interface:
  150. * - the first argument of the registration method is a pointer to the
  151. * handle that should describe the data
  152. * - the second argument is the memory node where the data (ie. "vector")
  153. * resides initially: STARPU_MAIN_RAM stands for an address in main memory, as
  154. * opposed to an adress on a GPU for instance.
  155. * - the third argument is the adress of the vector in RAM
  156. * - the fourth argument is the number of elements in the vector
  157. * - the fifth argument is the size of each element.
  158. */
  159. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, NB_FLOAT, sizeof(vector[0]));
  160. starpu_data_set_reduction_methods(vector_handle, &cl_redux, &cl_init);
  161. begin_time = starpu_timing_now();
  162. /*execution des tasks*/
  163. for (i=0; i<nb_tasks; i++)
  164. starpu_task_insert(&cl, STARPU_REDUX, vector_handle, 0);
  165. starpu_data_wont_use(vector_handle);
  166. starpu_task_wait_for_all();
  167. end_time = starpu_timing_now();
  168. starpu_data_unregister(vector_handle);
  169. /*on determine si le temps mesure est satisfaisant ou pas*/
  170. time_m = (end_time - begin_time)/SECONDS_SCALE_COEFFICIENT_TIMING_NOW; //pour ramener en secondes
  171. time_s = nb_tasks * TIME;
  172. speed_up = time_s/time_m;
  173. expected_speed_up = nb_workers_CPU + TIME_CUDA_COEFFICIENT*nb_workers_CUDA + TIME_OPENCL_COEFFICIENT*nb_workers_OPENCL;
  174. percentage_expected_speed_up = 100 * (speed_up/expected_speed_up);
  175. check = speed_up >= ((1 - MARGIN) * expected_speed_up);
  176. check_sup = speed_up <= ((1 + MARGIN) * expected_speed_up);
  177. printf("measured time = %f seconds\nsequential time = %f seconds\nspeed up = %f\nnumber of workers CPU = %u\nnumber of workers CUDA = %u\nnumber of workers OPENCL = %u\nnumber of tasks = %u\nexpected speed up = %f\npercentage of expected speed up %.2f%%\n", time_m, time_s, speed_up, nb_workers_CPU, nb_workers_CUDA, nb_workers_OPENCL, nb_tasks, expected_speed_up, percentage_expected_speed_up);
  178. starpu_shutdown();
  179. free(vector);
  180. //test reussi ou test echoue
  181. if (check && check_sup)
  182. {
  183. return EXIT_SUCCESS;
  184. }
  185. else
  186. {
  187. return EXIT_FAILURE;
  188. }
  189. }