parallel_independent_heterogeneous_tasks.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 independent tasks with heterogeneous execution time */
  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. void wait_CPU(void *descr[], void *_args)
  33. {
  34. (void)descr;
  35. (void)_args;
  36. starpu_sleep(TIME);
  37. }
  38. void wait_CUDA(void *descr[], void *_args)
  39. {
  40. (void)descr;
  41. (void)_args;
  42. starpu_sleep(TIME/TIME_CUDA_COEFFICIENT);
  43. }
  44. void wait_OPENCL(void *descr[], void *_args)
  45. {
  46. (void)descr;
  47. (void)_args;
  48. starpu_sleep(TIME/TIME_OPENCL_COEFFICIENT);
  49. }
  50. double cost_function(struct starpu_task *t, struct starpu_perfmodel_arch *a, unsigned i)
  51. {
  52. (void) t; (void) i;
  53. STARPU_ASSERT(a->ndevices == 1);
  54. if (a->devices[0].type == STARPU_CPU_WORKER)
  55. {
  56. STARPU_ASSERT(a->devices[0].ncores == 1);
  57. return TIME * 1000000;
  58. }
  59. else if (a->devices[0].type == STARPU_CUDA_WORKER)
  60. {
  61. return TIME/TIME_CUDA_COEFFICIENT * 1000000;
  62. }
  63. else if (a->devices[0].type == STARPU_OPENCL_WORKER)
  64. {
  65. return TIME/TIME_OPENCL_COEFFICIENT * 1000000;
  66. }
  67. STARPU_ASSERT(0);
  68. return 0.0;
  69. }
  70. static struct starpu_perfmodel perf_model =
  71. {
  72. .type = STARPU_PER_ARCH,
  73. .arch_cost_function = cost_function,
  74. };
  75. static struct starpu_codelet cl =
  76. {
  77. .cpu_funcs = { wait_CPU },
  78. .cuda_funcs = { wait_CUDA },
  79. .opencl_funcs = { wait_OPENCL },
  80. .cpu_funcs_name = { "wait_CPU" },
  81. .nbuffers = 0,
  82. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  83. .model = &perf_model,
  84. };
  85. int main(int argc, char *argv[])
  86. {
  87. int ret;
  88. ret = starpu_initialize(NULL, &argc, &argv);
  89. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  90. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  91. unsigned nb_tasks, nb_workers_CPU, nb_workers_CUDA, nb_workers_OPENCL, i;
  92. double begin_time, end_time, time_m, time_s, speed_up, expected_speed_up, percentage_expected_speed_up;
  93. bool check, check_sup;
  94. nb_workers_CPU = starpu_worker_get_count_by_type(STARPU_CPU_WORKER);
  95. nb_workers_CUDA = starpu_worker_get_count_by_type(STARPU_CUDA_WORKER);
  96. nb_workers_OPENCL = starpu_worker_get_count_by_type(STARPU_OPENCL_WORKER);
  97. nb_tasks = (nb_workers_CPU + nb_workers_CUDA + nb_workers_OPENCL)*TASK_COEFFICIENT;
  98. begin_time = starpu_timing_now();
  99. /*execution des tasks*/
  100. for (i=0; i<nb_tasks; i++)
  101. {
  102. starpu_task_insert(&cl,0);
  103. }
  104. starpu_task_wait_for_all();
  105. end_time = starpu_timing_now();
  106. /*on determine si le temps mesure est satisfaisant ou pas*/
  107. time_m = (end_time - begin_time)/SECONDS_SCALE_COEFFICIENT_TIMING_NOW; //pour ramener en secondes
  108. time_s = nb_tasks * TIME;
  109. speed_up = time_s/time_m;
  110. expected_speed_up = nb_workers_CPU + TIME_CUDA_COEFFICIENT*nb_workers_CUDA + TIME_OPENCL_COEFFICIENT*nb_workers_OPENCL;
  111. percentage_expected_speed_up = 100 * (speed_up/expected_speed_up);
  112. check = speed_up >= (1 - MARGIN) * expected_speed_up;
  113. check_sup = speed_up <= (1 + MARGIN) * expected_speed_up;
  114. 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);
  115. starpu_shutdown();
  116. //test reussi ou test echoue
  117. if (check && check_sup)
  118. {
  119. return EXIT_SUCCESS;
  120. }
  121. else
  122. {
  123. return EXIT_FAILURE;
  124. }
  125. }