parallel_dependent_homogeneous_tasks_data.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2016-2020 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 homogeneous execution time and share data to some extent */
  21. #define TIME 0.010
  22. #ifdef STARPU_QUICK_CHECK
  23. #define TASK_COEFFICIENT 5
  24. #define DATA_COEFFICIENT 5.5
  25. #define MARGIN 0.15
  26. #else
  27. #define TASK_COEFFICIENT 10
  28. #define DATA_COEFFICIENT 10.5
  29. #define MARGIN 0.05
  30. #endif
  31. #define SECONDS_SCALE_COEFFICIENT_TIMING_NOW 1000000
  32. #define NB_FLOAT 4000000
  33. void wait_homogeneous(void *descr[], void *_args)
  34. {
  35. (void)descr;
  36. (void)_args;
  37. starpu_sleep(TIME);
  38. }
  39. double cost_function(struct starpu_task *t, struct starpu_perfmodel_arch *a, unsigned i)
  40. {
  41. (void)t; (void)a; (void)i;
  42. return TIME * 1000000;
  43. }
  44. static struct starpu_perfmodel perf_model =
  45. {
  46. .type = STARPU_PER_ARCH,
  47. .arch_cost_function = cost_function,
  48. };
  49. static struct starpu_codelet cl =
  50. {
  51. .cpu_funcs = { wait_homogeneous },
  52. .cuda_funcs = { wait_homogeneous },
  53. .opencl_funcs = { wait_homogeneous },
  54. .cpu_funcs_name = { "wait_homogeneous" },
  55. .nbuffers = 1,
  56. .modes = {STARPU_RW},
  57. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  58. .model = &perf_model,
  59. };
  60. int main(int argc, char *argv[])
  61. {
  62. int ret;
  63. ret = starpu_initialize(NULL, &argc, &argv);
  64. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  65. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  66. unsigned nb_tasks, nb_data, nb_workers;
  67. double begin_time, end_time, time_m, time_s, speed_up, expected_speed_up, percentage_expected_speed_up;
  68. bool check, check_sup;
  69. 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);
  70. nb_tasks = nb_workers*TASK_COEFFICIENT*DATA_COEFFICIENT;
  71. nb_data = nb_workers*DATA_COEFFICIENT;
  72. /* We consider a vector of float that is initialized just as any of C
  73. * data */
  74. float *vector[nb_data];
  75. starpu_data_handle_t vector_handle[nb_data];
  76. unsigned i,j;
  77. for (j = 0; j < nb_data; j++)
  78. {
  79. vector[j] = malloc(NB_FLOAT * sizeof(float));
  80. #ifndef STARPU_SIMGRID
  81. for (i = 0; i < NB_FLOAT; i++)
  82. vector[j][i] = (i+1.0f);
  83. #endif
  84. /* Tell StaPU to associate the "vector" vector with the "vector_handle"
  85. * identifier. When a task needs to access a piece of data, it should
  86. * refer to the handle that is associated to it.
  87. * In the case of the "vector" data interface:
  88. * - the first argument of the registration method is a pointer to the
  89. * handle that should describe the data
  90. * - the second argument is the memory node where the data (ie. "vector")
  91. * resides initially: STARPU_MAIN_RAM stands for an address in main memory, as
  92. * opposed to an adress on a GPU for instance.
  93. * - the third argument is the adress of the vector in RAM
  94. * - the fourth argument is the number of elements in the vector
  95. * - the fifth argument is the size of each element.
  96. */
  97. starpu_vector_data_register(&vector_handle[j], STARPU_MAIN_RAM, (uintptr_t)vector[j], NB_FLOAT, sizeof(vector[0][0]));
  98. }
  99. begin_time = starpu_timing_now();
  100. /*execution des tasks*/
  101. for (i=0; i<nb_tasks; i++)
  102. starpu_task_insert(&cl, STARPU_RW, vector_handle[i%nb_data], 0);
  103. for (i=0; i<nb_data; i++)
  104. starpu_data_wont_use(vector_handle[i]);
  105. starpu_task_wait_for_all();
  106. end_time = starpu_timing_now();
  107. for (j = 0; j < nb_data; j++)
  108. starpu_data_unregister(vector_handle[j]);
  109. /*on determine si le temps mesure est satisfaisant ou pas*/
  110. time_m = (end_time - begin_time)/SECONDS_SCALE_COEFFICIENT_TIMING_NOW; //pour ramener en secondes
  111. time_s = nb_tasks * TIME;
  112. speed_up = time_s/time_m;
  113. expected_speed_up = nb_workers;
  114. percentage_expected_speed_up = 100 * (speed_up/expected_speed_up);
  115. check = speed_up >= ((1 - MARGIN) * expected_speed_up);
  116. check_sup = speed_up <= ((1 + MARGIN) * expected_speed_up);
  117. FPRINTF(stderr, "measured time = %f seconds\n", time_m);
  118. FPRINTF(stderr, "sequential time = %f seconds\n", time_s);
  119. FPRINTF(stderr, "speed up = %f\n", speed_up);
  120. FPRINTF(stderr, "number of workers = %u\n", nb_workers);
  121. FPRINTF(stderr, "number of tasks = %u\n", nb_tasks);
  122. FPRINTF(stderr, "expected speed up = %f\n", expected_speed_up);
  123. FPRINTF(stderr, "percentage of expected speed up %.2f%%\n", percentage_expected_speed_up);
  124. starpu_shutdown();
  125. for (j = 0; j < nb_data; j++)
  126. free(vector[j]);
  127. //test reussi ou test echoue
  128. if (check && check_sup)
  129. {
  130. return EXIT_SUCCESS;
  131. }
  132. else
  133. {
  134. return EXIT_FAILURE;
  135. }
  136. }