regression_based_03.c 8.6 KB

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