regression_based_03.c 7.3 KB

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