regression_based_03.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011,2012,2014 Inria
  4. * Copyright (C) 2011-2016,2019 Université de Bordeaux
  5. * Copyright (C) 2011-2017 CNRS
  6. * Copyright (C) 2011 Télécom-SudParis
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu.h>
  20. #include <starpu_scheduler.h>
  21. #include "../helper.h"
  22. /*
  23. * A multi-implementation benchmark with dmda scheduler
  24. * we aim to test the energy model with the different size of gamma
  25. * for large size of gamma, dmda choose the second implementation which consumes less energy
  26. * otherwise, it choose the first implementtaion which minimizes the execution time
  27. */
  28. #define STARTlin 1048576
  29. #define START 1024
  30. #ifdef STARPU_QUICK_CHECK
  31. #define END 1048576
  32. #else
  33. #define END 16777216
  34. #endif
  35. int ret;
  36. //1er implémentation avec un delai initial (100 us)
  37. void memset0_cpu(void *descr[], void *arg)
  38. {
  39. (void)arg;
  40. STARPU_SKIP_IF_VALGRIND;
  41. int *ptr = (int *)STARPU_VECTOR_GET_PTR(descr[0]);
  42. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  43. int i;
  44. usleep(100);
  45. for (i=0; i<n ; i++)
  46. {
  47. ptr[0] += i;
  48. }
  49. }
  50. //deuxième implémentation sans delai initial usleep() et fait 2.5 plus de tours de boucles
  51. void memset_cpu(void *descr[], void *arg)
  52. {
  53. (void)arg;
  54. STARPU_SKIP_IF_VALGRIND;
  55. int *ptr = (int *)STARPU_VECTOR_GET_PTR(descr[0]);
  56. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  57. int i;
  58. for (i=0; i<6.5*n ; i++)
  59. {
  60. ptr[0] += i;
  61. }
  62. }
  63. //fonction pour mesurer l'energie
  64. double energy_function(struct starpu_task *task, struct starpu_perfmodel_arch *arch, unsigned nimpl)
  65. {
  66. double energy;
  67. int factor;
  68. if (nimpl == 0)
  69. factor = 10;
  70. else
  71. factor = 1;
  72. energy=starpu_task_expected_length(task, arch, nimpl)*factor;
  73. return energy;
  74. }
  75. static struct starpu_perfmodel model =
  76. {
  77. .type = STARPU_REGRESSION_BASED,
  78. .symbol = "memset_regression_based"
  79. };
  80. static struct starpu_perfmodel nl_model =
  81. {
  82. .type = STARPU_NL_REGRESSION_BASED,
  83. .symbol = "non_linear_memset_regression_based"
  84. };
  85. static struct starpu_perfmodel nl_energy_model=
  86. {
  87. .type = STARPU_PER_ARCH,
  88. .symbol = "non_linear_energy_model",
  89. .arch_cost_function={energy_function},
  90. };
  91. static struct starpu_codelet memset_cl =
  92. {
  93. .cpu_funcs = {memset0_cpu, memset_cpu},
  94. .cpu_funcs_name = {"memset0_cpu", "memset_cpu"},
  95. .model = &model,
  96. .nbuffers = 1,
  97. .modes = {STARPU_W}
  98. };
  99. static struct starpu_codelet nl_memset_cl =
  100. {
  101. .cpu_funcs = {memset0_cpu, memset_cpu},
  102. .cpu_funcs_name = {"memset0_cpu", "memset_cpu"},
  103. .model = &nl_model,
  104. .energy_model = &nl_energy_model,
  105. .nbuffers = 1,
  106. .modes = {STARPU_W}
  107. };
  108. static void test_memset(int nelems, struct starpu_codelet *codelet)
  109. {
  110. int nloops = 100;
  111. int loop;
  112. starpu_data_handle_t handle;
  113. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, nelems, sizeof(int));
  114. for (loop = 0; loop < nloops; loop++)
  115. {
  116. struct starpu_task *task = starpu_task_create();
  117. task->cl = codelet;
  118. task->handles[0] = handle;
  119. int ret = starpu_task_submit(task);
  120. if (ret == -ENODEV)
  121. exit(STARPU_TEST_SKIPPED);
  122. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  123. }
  124. starpu_data_unregister(handle);
  125. }
  126. static void compare_performance(int size, struct starpu_codelet *codelet, struct starpu_task *task)
  127. {
  128. unsigned i;
  129. int niter = 100;
  130. starpu_data_handle_t handle;
  131. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  132. struct starpu_task **tasks = (struct starpu_task **) malloc(niter*sizeof(struct starpu_task *));
  133. assert(tasks);
  134. for (i = 0; i < niter; i++)
  135. {
  136. //fabriquer la tache
  137. struct starpu_task *task = starpu_task_create();
  138. task->cl = codelet;
  139. task->handles[0] = handle;
  140. task->synchronous = 1;
  141. /* We will destroy the task structure by hand so that we can
  142. * query the profiling info before the task is destroyed. */
  143. task->destroy = 0;
  144. tasks[i] = task;
  145. //soumettre la tache
  146. ret = starpu_task_submit(task);
  147. if (STARPU_UNLIKELY(ret == -ENODEV))
  148. {
  149. FPRINTF(stderr, "No worker may execute this task\n");
  150. exit(0);
  151. }
  152. }
  153. starpu_data_unregister(handle);
  154. starpu_task_wait_for_all();
  155. double length_sum = 0.0;
  156. for (i = 0; i < niter; i++)
  157. {
  158. struct starpu_task *task = tasks[i];
  159. struct starpu_profiling_task_info *info = task->profiling_info;
  160. /* How long was the task execution ? */
  161. length_sum += starpu_timing_timespec_delay_us(&info->start_time, &info->end_time);
  162. /* We don't need the task structure anymore */
  163. starpu_task_destroy(task);
  164. }
  165. /* Display the occupancy of all workers during the test */
  166. unsigned worker;
  167. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  168. {
  169. struct starpu_profiling_worker_info worker_info;
  170. ret = starpu_profiling_worker_get_info(worker, &worker_info);
  171. STARPU_ASSERT(!ret);
  172. char workername[128];
  173. starpu_worker_get_name(worker, workername, sizeof(workername));
  174. unsigned nimpl;
  175. if (starpu_worker_get_type(worker)==STARPU_CPU_WORKER)
  176. {
  177. FPRINTF(stdout, "\n Worker :%s ::::::::::\n\n", workername);
  178. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  179. {
  180. FPRINTF(stdout, "Expected time for %d on %s (impl %u): %f, Measured time: %f, Expected energy: %f\n",
  181. size, workername, nimpl,starpu_task_expected_length(task, starpu_worker_get_perf_archtype(worker, task->sched_ctx), nimpl), ((length_sum)/niter),
  182. starpu_task_expected_energy(task, starpu_worker_get_perf_archtype(worker, task->sched_ctx), nimpl));
  183. }
  184. }
  185. }
  186. }
  187. int main(int argc, char **argv)
  188. {
  189. /* Enable profiling */
  190. starpu_profiling_status_set(1);
  191. struct starpu_conf conf;
  192. starpu_data_handle_t handle;
  193. int ret;
  194. starpu_conf_init(&conf);
  195. conf.sched_policy_name = "dmda";
  196. conf.calibrate = 2;
  197. ret = starpu_initialize(&conf, &argc, &argv);
  198. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  199. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  200. int size;
  201. for (size = STARTlin; size < END; size *= 2)
  202. {
  203. /* Use a linear regression */
  204. test_memset(size, &memset_cl);
  205. }
  206. for (size = START; size < END; size *= 2)
  207. {
  208. /* Use a non-linear regression */
  209. test_memset(size, &nl_memset_cl);
  210. }
  211. ret = starpu_task_wait_for_all();
  212. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  213. starpu_shutdown();
  214. /* Test Phase */
  215. starpu_conf_init(&conf);
  216. conf.sched_policy_name = "dmda";
  217. conf.calibrate = 0;
  218. ret = starpu_initialize(&conf, &argc, &argv);
  219. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  220. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  221. /* Now create a dummy task just to estimate its duration according to the regression */
  222. size = 1234567;
  223. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  224. struct starpu_task *task = starpu_task_create();
  225. task->cl = &memset_cl;
  226. task->handles[0] = handle;
  227. task->destroy = 0;
  228. task->cl = &nl_memset_cl;
  229. FPRINTF(stdout, "\n ////non linear regression results////\n");
  230. compare_performance(size, &nl_memset_cl,task);
  231. starpu_task_destroy(task);
  232. starpu_data_unregister(handle);
  233. starpu_shutdown();
  234. return EXIT_SUCCESS;
  235. }