parallel_independent_heterogeneous_tasks_data.c 5.4 KB

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