parallel_independent_homogeneous_tasks_data.c 4.7 KB

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