regression_based_03.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 () function
  45. usleep(100);
  46. for (i=0; i<n ; i++)
  47. {
  48. ptr[0] += i;
  49. }
  50. }
  51. //deuxième implémentation sans delai initial usleep() et fait 2.5 plus de tours de boucles
  52. void memset_cpu(void *descr[], void *arg)
  53. {
  54. (void)arg;
  55. STARPU_SKIP_IF_VALGRIND;
  56. int *ptr = (int *)STARPU_VECTOR_GET_PTR(descr[0]);
  57. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  58. int i;
  59. for (i=0; i<6.5*n ; i++)
  60. {
  61. ptr[0] += i;
  62. }
  63. }
  64. //fonction pour mesurer l'energie
  65. double energy_function(struct starpu_task *task, struct starpu_perfmodel_arch *arch, unsigned nimpl)
  66. {
  67. double energy;
  68. int factor;
  69. if (nimpl == 0)
  70. factor = 10;
  71. else
  72. factor = 1;
  73. energy=starpu_task_expected_length(task, arch, nimpl)*factor;
  74. return energy;
  75. }
  76. static struct starpu_perfmodel model =
  77. {
  78. .type = STARPU_REGRESSION_BASED,
  79. .symbol = "memset_regression_based"
  80. };
  81. static struct starpu_perfmodel nl_model =
  82. {
  83. .type = STARPU_NL_REGRESSION_BASED,
  84. .symbol = "non_linear_memset_regression_based"
  85. };
  86. static struct starpu_perfmodel nl_energy_model=
  87. {
  88. .type = STARPU_PER_ARCH,
  89. .symbol = "non_linear_energy_model",
  90. .arch_cost_function={energy_function},
  91. };
  92. static struct starpu_codelet memset_cl =
  93. {
  94. .cpu_funcs = {memset0_cpu, memset_cpu},
  95. .cpu_funcs_name = {"memset0_cpu", "memset_cpu"},
  96. .model = &model,
  97. .nbuffers = 1,
  98. .modes = {STARPU_W}
  99. };
  100. static struct starpu_codelet nl_memset_cl =
  101. {
  102. .cpu_funcs = {memset0_cpu, memset_cpu},
  103. .cpu_funcs_name = {"memset0_cpu", "memset_cpu"},
  104. .model = &nl_model,
  105. .energy_model = &nl_energy_model,
  106. .nbuffers = 1,
  107. .modes = {STARPU_W}
  108. };
  109. static void test_memset(int nelems, struct starpu_codelet *codelet)
  110. {
  111. int nloops = 100;
  112. int loop;
  113. starpu_data_handle_t handle;
  114. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, nelems, sizeof(int));
  115. for (loop = 0; loop < nloops; loop++)
  116. {
  117. struct starpu_task *task = starpu_task_create();
  118. task->cl = codelet;
  119. task->handles[0] = handle;
  120. int ret = starpu_task_submit(task);
  121. if (ret == -ENODEV)
  122. exit(STARPU_TEST_SKIPPED);
  123. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  124. }
  125. starpu_data_unregister(handle);
  126. }
  127. static void compare_performance(int size, struct starpu_codelet *codelet, struct starpu_task *task)
  128. {
  129. unsigned i;
  130. int niter = 100;
  131. starpu_data_handle_t handle;
  132. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  133. struct starpu_task **tasks = (struct starpu_task **) malloc(niter*sizeof(struct starpu_task *));
  134. assert(tasks);
  135. for (i = 0; i < niter; i++)
  136. {
  137. //fabriquer la tache
  138. struct starpu_task *task = starpu_task_create();
  139. task->cl = codelet;
  140. task->handles[0] = handle;
  141. task->synchronous = 1;
  142. /* We will destroy the task structure by hand so that we can
  143. * query the profiling info before the task is destroyed. */
  144. task->destroy = 0;
  145. tasks[i] = task;
  146. //soumettre la tache
  147. ret = starpu_task_submit(task);
  148. if (STARPU_UNLIKELY(ret == -ENODEV))
  149. {
  150. FPRINTF(stderr, "No worker may execute this task\n");
  151. exit(0);
  152. }
  153. }
  154. starpu_data_unregister(handle);
  155. starpu_task_wait_for_all();
  156. double length_sum = 0.0;
  157. for (i = 0; i < niter; i++)
  158. {
  159. struct starpu_task *task = tasks[i];
  160. struct starpu_profiling_task_info *info = task->profiling_info;
  161. /* How long was the task execution ? */
  162. length_sum += starpu_timing_timespec_delay_us(&info->start_time, &info->end_time);
  163. /* We don't need the task structure anymore */
  164. starpu_task_destroy(task);
  165. }
  166. /* Display the occupancy of all workers during the test */
  167. unsigned worker;
  168. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  169. {
  170. struct starpu_profiling_worker_info worker_info;
  171. ret = starpu_profiling_worker_get_info(worker, &worker_info);
  172. STARPU_ASSERT(!ret);
  173. char workername[128];
  174. starpu_worker_get_name(worker, workername, sizeof(workername));
  175. unsigned nimpl;
  176. if (starpu_worker_get_type(worker)==STARPU_CPU_WORKER)
  177. {
  178. FPRINTF(stdout, "\n Worker :%s ::::::::::\n\n", workername);
  179. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  180. {
  181. FPRINTF(stdout, "Expected time for %d on %s (impl %u): %f, Measured time: %f, Expected energy: %f\n",
  182. size, workername, nimpl,starpu_task_expected_length(task, starpu_worker_get_perf_archtype(worker, task->sched_ctx), nimpl), ((length_sum)/niter),
  183. starpu_task_expected_energy(task, starpu_worker_get_perf_archtype(worker, task->sched_ctx), nimpl));
  184. }
  185. }
  186. }
  187. }
  188. int main(int argc, char **argv)
  189. {
  190. /* Enable profiling */
  191. starpu_profiling_status_set(1);
  192. struct starpu_conf conf;
  193. starpu_data_handle_t handle;
  194. int ret;
  195. starpu_conf_init(&conf);
  196. conf.sched_policy_name = "dmda";
  197. conf.calibrate = 2;
  198. ret = starpu_initialize(&conf, &argc, &argv);
  199. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  200. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  201. int size;
  202. for (size = STARTlin; size < END; size *= 2)
  203. {
  204. /* Use a linear regression */
  205. test_memset(size, &memset_cl);
  206. }
  207. for (size = START; size < END; size *= 2)
  208. {
  209. /* Use a non-linear regression */
  210. test_memset(size, &nl_memset_cl);
  211. }
  212. ret = starpu_task_wait_for_all();
  213. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  214. starpu_shutdown();
  215. /* Test Phase */
  216. starpu_conf_init(&conf);
  217. conf.sched_policy_name = "dmda";
  218. conf.calibrate = 0;
  219. ret = starpu_initialize(&conf, &argc, &argv);
  220. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  221. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  222. /* Now create a dummy task just to estimate its duration according to the regression */
  223. size = 1234567;
  224. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  225. struct starpu_task *task = starpu_task_create();
  226. task->cl = &memset_cl;
  227. task->handles[0] = handle;
  228. task->destroy = 0;
  229. task->cl = &nl_memset_cl;
  230. FPRINTF(stdout, "\n ////non linear regression results////\n");
  231. compare_performance(size, &nl_memset_cl,task);
  232. starpu_task_destroy(task);
  233. starpu_data_unregister(handle);
  234. starpu_shutdown();
  235. return EXIT_SUCCESS;
  236. }