regression_based_01.c 8.0 KB

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