parallel_redux_homogeneous_tasks_data.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 homogeneous 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 SECONDS_SCALE_COEFFICIENT_TIMING_NOW 1000000
  31. #define NB_FLOAT 4000000
  32. void wait_homogeneous(void *descr[], void *_args)
  33. {
  34. (void)descr;
  35. (void)_args;
  36. starpu_sleep(TIME);
  37. }
  38. double cost_function(struct starpu_task *t, struct starpu_perfmodel_arch *a, unsigned i)
  39. {
  40. (void)t; (void)a; (void)i;
  41. return TIME * 1000000;
  42. }
  43. static struct starpu_perfmodel perf_model =
  44. {
  45. .type = STARPU_PER_ARCH,
  46. .arch_cost_function = cost_function,
  47. };
  48. static struct starpu_codelet cl =
  49. {
  50. .cpu_funcs = { wait_homogeneous },
  51. .cuda_funcs = { wait_homogeneous },
  52. .opencl_funcs = { wait_homogeneous },
  53. .cpu_funcs_name = { "wait_homogeneous" },
  54. .nbuffers = 1,
  55. .modes = {STARPU_REDUX},
  56. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  57. .model = &perf_model,
  58. .name = "cl",
  59. };
  60. static struct starpu_perfmodel perf_model_init =
  61. {
  62. .type = STARPU_PER_ARCH,
  63. .arch_cost_function = cost_function,
  64. };
  65. static struct starpu_codelet cl_init =
  66. {
  67. .cpu_funcs = { wait_homogeneous },
  68. .cuda_funcs = { wait_homogeneous },
  69. .opencl_funcs = { wait_homogeneous },
  70. .cpu_funcs_name = { "wait_homogeneous" },
  71. .nbuffers = 1,
  72. .modes = {STARPU_RW},
  73. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  74. .model = &perf_model_init,
  75. .name = "init",
  76. };
  77. static struct starpu_perfmodel perf_model_redux =
  78. {
  79. .type = STARPU_PER_ARCH,
  80. .arch_cost_function = cost_function,
  81. };
  82. static struct starpu_codelet cl_redux =
  83. {
  84. .cpu_funcs = { wait_homogeneous },
  85. .cuda_funcs = { wait_homogeneous },
  86. .opencl_funcs = { wait_homogeneous },
  87. .cpu_funcs_name = { "wait_homogeneous" },
  88. .nbuffers = 2,
  89. .modes = {STARPU_RW, STARPU_R},
  90. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  91. .model = &perf_model_redux,
  92. .name = "redux",
  93. };
  94. int main(int argc, char *argv[])
  95. {
  96. int ret;
  97. ret = starpu_initialize(NULL, &argc, &argv);
  98. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  100. unsigned nb_tasks, nb_workers;
  101. double begin_time, end_time, time_m, time_s, speed_up, expected_speed_up, percentage_expected_speed_up;
  102. bool check, check_sup;
  103. nb_workers = starpu_worker_get_count_by_type(STARPU_CPU_WORKER) + starpu_worker_get_count_by_type(STARPU_CUDA_WORKER) + starpu_worker_get_count_by_type(STARPU_OPENCL_WORKER);
  104. nb_tasks = nb_workers*TASK_COEFFICIENT;
  105. /* We consider a vector of float that is initialized just as any of C
  106. * data */
  107. float *vector;
  108. starpu_data_handle_t vector_handle;
  109. unsigned i;
  110. vector = calloc(NB_FLOAT, sizeof(float));
  111. #ifndef STARPU_SIMGRID
  112. for (i = 0; i < NB_FLOAT; i++)
  113. vector[i] = (i+1.0f);
  114. #endif
  115. /* Tell StaPU to associate the "vector" vector with the "vector_handle"
  116. * identifier. When a task needs to access a piece of data, it should
  117. * refer to the handle that is associated to it.
  118. * In the case of the "vector" data interface:
  119. * - the first argument of the registration method is a pointer to the
  120. * handle that should describe the data
  121. * - the second argument is the memory node where the data (ie. "vector")
  122. * resides initially: STARPU_MAIN_RAM stands for an address in main memory, as
  123. * opposed to an adress on a GPU for instance.
  124. * - the third argument is the adress of the vector in RAM
  125. * - the fourth argument is the number of elements in the vector
  126. * - the fifth argument is the size of each element.
  127. */
  128. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, NB_FLOAT, sizeof(vector[0]));
  129. starpu_data_set_reduction_methods(vector_handle, &cl_redux, &cl_init);
  130. begin_time = starpu_timing_now();
  131. /*execution des tasks*/
  132. for (i=0; i<nb_tasks; i++)
  133. starpu_task_insert(&cl, STARPU_REDUX, vector_handle, 0);
  134. starpu_data_wont_use(vector_handle);
  135. starpu_task_wait_for_all();
  136. end_time = starpu_timing_now();
  137. starpu_data_unregister(vector_handle);
  138. /*on determine si le temps mesure est satisfaisant ou pas*/
  139. time_m = (end_time - begin_time)/SECONDS_SCALE_COEFFICIENT_TIMING_NOW; //pour ramener en secondes
  140. time_s = nb_tasks * TIME;
  141. speed_up = time_s/time_m;
  142. expected_speed_up = nb_workers;
  143. percentage_expected_speed_up = 100 * (speed_up/expected_speed_up);
  144. check = speed_up >= ((1 - MARGIN) * expected_speed_up);
  145. check_sup = speed_up <= ((1 + MARGIN) * expected_speed_up);
  146. printf("measured time = %f seconds\nsequential time = %f seconds\nspeed up = %f\nnumber of workers = %u\nnumber of tasks = %u\nexpected speed up = %f\npercentage of expected speed up %.2f%%\n", time_m, time_s, speed_up, nb_workers, nb_tasks, expected_speed_up, percentage_expected_speed_up);
  147. starpu_shutdown();
  148. free(vector);
  149. //test reussi ou test echoue
  150. if (check && check_sup)
  151. {
  152. return EXIT_SUCCESS;
  153. }
  154. else
  155. {
  156. return EXIT_FAILURE;
  157. }
  158. }