parallel_redux_heterogeneous_tasks_data.c 6.4 KB

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