regression_based_02.c 8.0 KB

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