regression_based_multiimpl.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2011 Télécom-SudParis
  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 <starpu.h>
  18. #include <starpu_scheduler.h>
  19. #include "../helper.h"
  20. /*
  21. * A multi-implementation benchmark with dmda scheduler
  22. * we aim to test the dmda behavior when we have two implementations
  23. * dmda choose the implementation which minimises the execution time
  24. */
  25. #define STARTlin 131072
  26. #define START 1024
  27. #ifdef STARPU_QUICK_CHECK
  28. #define END 1048576
  29. #else
  30. #define END 16777216
  31. #endif
  32. // first implementation with an initial delay (100 us)
  33. void memset0_cpu(void *descr[], void *arg)
  34. {
  35. (void)arg;
  36. STARPU_SKIP_IF_VALGRIND;
  37. unsigned *ptr = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  38. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  39. unsigned i;
  40. starpu_usleep(100);
  41. for (i=0; i<n ; i++)
  42. {
  43. ptr[0] += i;
  44. }
  45. }
  46. // second implementation without initial delay but 2.5 more loops
  47. void memset_cpu(void *descr[], void *arg)
  48. {
  49. (void)arg;
  50. STARPU_SKIP_IF_VALGRIND;
  51. unsigned *ptr = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  52. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  53. int i;
  54. for (i=0; i<6.5*n ; i++)
  55. {
  56. ptr[0] += i;
  57. }
  58. }
  59. //fonction pour mesurer l'energie
  60. double energy_function(struct starpu_task *task, struct starpu_perfmodel_arch *arch, unsigned nimpl)
  61. {
  62. double energy;
  63. int factor;
  64. if (nimpl == 0)
  65. factor = 10;
  66. else
  67. factor = 1;
  68. energy=starpu_task_expected_length(task, arch, nimpl)*factor;
  69. return energy;
  70. }
  71. static struct starpu_perfmodel model =
  72. {
  73. .type = STARPU_REGRESSION_BASED,
  74. .symbol = "memset_regression_based"
  75. };
  76. static struct starpu_perfmodel nl_model =
  77. {
  78. .type = STARPU_NL_REGRESSION_BASED,
  79. .symbol = "non_linear_memset_regression_based"
  80. };
  81. static struct starpu_perfmodel nl_energy_model=
  82. {
  83. .type = STARPU_PER_ARCH,
  84. .symbol = "non_linear_energy_model",
  85. .arch_cost_function=energy_function,
  86. };
  87. static struct starpu_codelet memset_cl =
  88. {
  89. .cpu_funcs = {memset0_cpu, memset_cpu},
  90. .cpu_funcs_name = {"memset0_cpu", "memset_cpu"},
  91. .model = &model,
  92. .nbuffers = 1,
  93. .modes = {STARPU_SCRATCH}
  94. };
  95. static struct starpu_codelet nl_memset_cl =
  96. {
  97. .cpu_funcs = {memset0_cpu, memset_cpu},
  98. .cpu_funcs_name = {"memset0_cpu", "memset_cpu"},
  99. .model = &nl_model,
  100. .energy_model = &nl_energy_model,
  101. .nbuffers = 1,
  102. .modes = {STARPU_SCRATCH}
  103. };
  104. static void test_memset(int nelems, struct starpu_codelet *codelet)
  105. {
  106. int nloops = 100;
  107. int loop;
  108. starpu_data_handle_t handle;
  109. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, nelems, sizeof(int));
  110. for (loop = 0; loop < nloops; loop++)
  111. {
  112. struct starpu_task *task = starpu_task_create();
  113. task->cl = codelet;
  114. task->handles[0] = handle;
  115. int ret = starpu_task_submit(task);
  116. if (ret == -ENODEV)
  117. exit(STARPU_TEST_SKIPPED);
  118. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  119. }
  120. starpu_do_schedule();
  121. starpu_data_unregister(handle);
  122. }
  123. static void compare_performance(int size, struct starpu_codelet *codelet, struct starpu_task *compar_task)
  124. {
  125. unsigned i;
  126. unsigned niter = 100;
  127. starpu_data_handle_t handle;
  128. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  129. struct starpu_task *tasks[niter];
  130. for (i = 0; i < niter; i++)
  131. {
  132. struct starpu_task *task = starpu_task_create();
  133. task->cl = codelet;
  134. task->handles[0] = handle;
  135. task->synchronous = 1;
  136. /* We will destroy the task structure by hand so that we can
  137. * query the profiling info before the task is destroyed. */
  138. task->destroy = 0;
  139. tasks[i] = task;
  140. int ret = starpu_task_submit(task);
  141. if (STARPU_UNLIKELY(ret == -ENODEV))
  142. {
  143. FPRINTF(stderr, "No worker may execute this task\n");
  144. exit(0);
  145. }
  146. }
  147. starpu_data_unregister(handle);
  148. starpu_task_wait_for_all();
  149. double length_sum = 0.0;
  150. for (i = 0; i < niter; i++)
  151. {
  152. struct starpu_task *task = tasks[i];
  153. struct starpu_profiling_task_info *info = task->profiling_info;
  154. /* How long was the task execution ? */
  155. length_sum += starpu_timing_timespec_delay_us(&info->start_time, &info->end_time);
  156. /* We don't need the task structure anymore */
  157. starpu_task_destroy(task);
  158. }
  159. /* Display the occupancy of all workers during the test */
  160. unsigned worker;
  161. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  162. {
  163. struct starpu_profiling_worker_info worker_info;
  164. int ret = starpu_profiling_worker_get_info(worker, &worker_info);
  165. STARPU_ASSERT(!ret);
  166. char workername[128];
  167. starpu_worker_get_name(worker, workername, sizeof(workername));
  168. unsigned nimpl;
  169. if (starpu_worker_get_type(worker)==STARPU_CPU_WORKER)
  170. {
  171. FPRINTF(stdout, "\n Worker :%s ::::::::::\n\n", workername);
  172. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  173. {
  174. FPRINTF(stdout, "Expected time for %d on %s (impl %u): %f, Measured time: %f, Expected energy: %f\n",
  175. size, workername, nimpl,starpu_task_expected_length(compar_task, starpu_worker_get_perf_archtype(worker, compar_task->sched_ctx), nimpl), ((length_sum)/niter),
  176. starpu_task_expected_energy(compar_task, starpu_worker_get_perf_archtype(worker, compar_task->sched_ctx), nimpl));
  177. }
  178. }
  179. }
  180. }
  181. int main(int argc, char **argv)
  182. {
  183. /* Enable profiling */
  184. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  185. struct starpu_conf conf;
  186. starpu_data_handle_t handle;
  187. int ret;
  188. starpu_conf_init(&conf);
  189. conf.sched_policy_name = "dmda";
  190. conf.calibrate = 2;
  191. ret = starpu_initialize(&conf, &argc, &argv);
  192. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  193. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  194. int size;
  195. #if 0
  196. for (size = STARTlin; size < END; size *= 2)
  197. {
  198. /* Use a linear regression */
  199. test_memset(size, &memset_cl);
  200. }
  201. #endif
  202. for (size = START; size < END; size *= 2)
  203. {
  204. /* Use a non-linear regression */
  205. test_memset(size, &nl_memset_cl);
  206. }
  207. ret = starpu_task_wait_for_all();
  208. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  209. starpu_shutdown();
  210. /* Test Phase */
  211. starpu_conf_init(&conf);
  212. conf.sched_policy_name = "dmda";
  213. conf.calibrate = 0;
  214. ret = starpu_initialize(&conf, &argc, &argv);
  215. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  216. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  217. /* Now create a dummy task just to estimate its duration according to the regression */
  218. size = 1234567;
  219. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  220. struct starpu_task *task = starpu_task_create();
  221. task->handles[0] = handle;
  222. task->destroy = 0;
  223. FPRINTF(stdout, "\n ////non linear regression results////\n");
  224. task->cl = &nl_memset_cl;
  225. compare_performance(size, &nl_memset_cl, task);
  226. starpu_task_destroy(task);
  227. starpu_data_unregister(handle);
  228. starpu_shutdown();
  229. return EXIT_SUCCESS;
  230. }