regression_based_02.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012,2016 Inria
  4. * Copyright (C) 2010-2015,2017 Université de Bordeaux
  5. * Copyright (C) 2010-2013,2015,2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*
  19. * This examplifies how to get task execution profiling from the application.
  20. */
  21. #include <starpu.h>
  22. #include <assert.h>
  23. #include <starpu_scheduler.h>
  24. #include <unistd.h>
  25. #include "../helper.h"
  26. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  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. //static unsigned niter = 500;
  35. int ret;
  36. #ifdef STARPU_USE_CUDA
  37. static void memset_cuda(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. cudaMemsetAsync(ptr, 42, n * sizeof(*ptr), starpu_cuda_get_local_stream());
  44. }
  45. #endif
  46. #ifdef STARPU_USE_OPENCL
  47. extern void memset_opencl(void *buffers[], void *args);
  48. #endif
  49. void memset_cpu(void *descr[], void *arg)
  50. {
  51. (void)arg;
  52. STARPU_SKIP_IF_VALGRIND;
  53. int *ptr = (int *)STARPU_VECTOR_GET_PTR(descr[0]);
  54. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  55. usleep(1000);
  56. for (int i=0; i<n ; i++)
  57. {
  58. ptr[0] += i;
  59. }
  60. }
  61. static struct starpu_perfmodel model =
  62. {
  63. .type = STARPU_REGRESSION_BASED,
  64. .symbol = "memset_regression_based"
  65. };
  66. static struct starpu_perfmodel nl_model =
  67. {
  68. .type = STARPU_NL_REGRESSION_BASED,
  69. .symbol = "non_linear_memset_regression_based"
  70. };
  71. static struct starpu_codelet memset_cl =
  72. {
  73. #ifdef STARPU_USE_CUDA
  74. .cuda_funcs = {memset_cuda},
  75. .cuda_flags = {STARPU_CUDA_ASYNC},
  76. #endif
  77. #ifdef STARPU_USE_OPENCL
  78. .opencl_funcs = {memset_opencl},
  79. .opencl_flags = {STARPU_OPENCL_ASYNC},
  80. #endif
  81. .cpu_funcs = {memset_cpu},
  82. .cpu_funcs_name = {"memset_cpu"},
  83. .model = &model,
  84. .nbuffers = 1,
  85. .modes = {STARPU_W}
  86. };
  87. static struct starpu_codelet nl_memset_cl =
  88. {
  89. #ifdef STARPU_USE_CUDA
  90. .cuda_funcs = {memset_cuda},
  91. .cuda_flags = {STARPU_CUDA_ASYNC},
  92. #endif
  93. #ifdef STARPU_USE_OPENCL
  94. .opencl_funcs = {memset_opencl},
  95. .opencl_flags = {STARPU_OPENCL_ASYNC},
  96. #endif
  97. .cpu_funcs = {memset_cpu},
  98. .cpu_funcs_name = {"memset_cpu"},
  99. .model = &nl_model,
  100. .nbuffers = 1,
  101. .modes = {STARPU_W}
  102. };
  103. static void test_memset(int nelems, struct starpu_codelet *codelet)
  104. {
  105. int nloops = 100;
  106. int loop;
  107. starpu_data_handle_t handle;
  108. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, nelems, sizeof(int));
  109. for (loop = 0; loop < nloops; loop++)
  110. {
  111. struct starpu_task *task = starpu_task_create();
  112. task->cl = codelet;
  113. task->handles[0] = handle;
  114. int ret = starpu_task_submit(task);
  115. if (ret == -ENODEV)
  116. exit(STARPU_TEST_SKIPPED);
  117. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  118. }
  119. starpu_data_unregister(handle);
  120. }
  121. static void compare_performance(int size, struct starpu_codelet *codelet, struct starpu_task *task)
  122. {
  123. unsigned i;
  124. int niter = 100;
  125. starpu_data_handle_t handle;
  126. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  127. struct starpu_task **tasks = (struct starpu_task **) malloc(niter*sizeof(struct starpu_task *));
  128. assert(tasks);
  129. for (i = 0; i < niter; i++)
  130. {
  131. struct starpu_task *task = starpu_task_create();
  132. task->cl = codelet;
  133. task->handles[0] = handle;
  134. task->synchronous = 1;
  135. /* We will destroy the task structure by hand so that we can
  136. * query the profiling info before the task is destroyed. */
  137. task->destroy = 0;
  138. tasks[i] = task;
  139. ret = starpu_task_submit(task);
  140. if (STARPU_UNLIKELY(ret == -ENODEV))
  141. {
  142. FPRINTF(stderr, "No worker may execute this task\n");
  143. exit(0);
  144. }
  145. }
  146. starpu_data_unregister(handle);
  147. starpu_task_wait_for_all();
  148. double length_sum = 0.0;
  149. for (i = 0; i < niter; i++)
  150. {
  151. struct starpu_task *task = tasks[i];
  152. struct starpu_profiling_task_info *info = task->profiling_info;
  153. /* How long was the task execution ? */
  154. length_sum += starpu_timing_timespec_delay_us(&info->start_time, &info->end_time);
  155. /* We don't need the task structure anymore */
  156. starpu_task_destroy(task);
  157. }
  158. /* Display the occupancy of all workers during the test */
  159. unsigned worker;
  160. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  161. {
  162. struct starpu_profiling_worker_info worker_info;
  163. ret = starpu_profiling_worker_get_info(worker, &worker_info);
  164. STARPU_ASSERT(!ret);
  165. char workername[128];
  166. starpu_worker_get_name(worker, workername, sizeof(workername));
  167. unsigned nimpl;
  168. FPRINTF(stdout, "\n Worker :%s ::::::::::\n\n", workername);
  169. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  170. {
  171. FPRINTF(stdout, "Expected time for %d on %s (impl %u): %f, Measured time: %f\n",
  172. size, workername, nimpl,starpu_task_expected_length(task, starpu_worker_get_perf_archtype(worker, task->sched_ctx), nimpl), ((length_sum)/niter));
  173. }
  174. }
  175. }
  176. #ifdef STARPU_USE_OPENCL
  177. struct starpu_opencl_program opencl_program;
  178. #endif
  179. int main(int argc, char **argv)
  180. {
  181. /* Enable profiling */
  182. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  183. struct starpu_conf conf;
  184. starpu_data_handle_t handle;
  185. int ret;
  186. starpu_conf_init(&conf);
  187. conf.sched_policy_name = "eager";
  188. conf.calibrate = 2;
  189. ret = starpu_initialize(&conf, &argc, &argv);
  190. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  191. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  192. #ifdef STARPU_USE_OPENCL
  193. ret = starpu_opencl_load_opencl_from_file("tests/perfmodels/opencl_memset_kernel.cl",
  194. &opencl_program, NULL);
  195. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  196. #endif
  197. int size;
  198. for (size = STARTlin; size < END; size *= 2)
  199. {
  200. /* Use a linear regression */
  201. test_memset(size, &memset_cl);
  202. }
  203. for (size = START; size < END; size *= 2)
  204. {
  205. /* Use a non-linear regression */
  206. test_memset(size, &nl_memset_cl);
  207. }
  208. ret = starpu_task_wait_for_all();
  209. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  210. starpu_shutdown();
  211. /* Test Phase */
  212. starpu_conf_init(&conf);
  213. conf.sched_policy_name = "eager";
  214. conf.calibrate = 0;
  215. ret = starpu_initialize(&conf, &argc, &argv);
  216. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  217. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  218. /* Now create a dummy task just to estimate its duration according to the regression */
  219. size = 1234567;
  220. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  221. struct starpu_task *task = starpu_task_create();
  222. task->cl = &memset_cl;
  223. task->handles[0] = handle;
  224. task->destroy = 0;
  225. FPRINTF(stdout, "\n ////linear regression results////\n");
  226. compare_performance(size, &memset_cl,task);
  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. #ifdef STARPU_USE_OPENCL
  233. ret = starpu_opencl_unload_opencl(&opencl_program);
  234. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  235. #endif
  236. starpu_shutdown();
  237. return 0;
  238. }