regression_based_01.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * Dans ce benchmark:
  20. - calibrer le modèle linéaire seulement pour des grandes tailles STARTlin 1048576
  21. - séparer la boucle test_memset en deux boucles:
  22. *linéaire: démarrer à partir de 1 048 576
  23. *non linéaire: conserver le démarrage à 1024
  24. */
  25. #include <starpu.h>
  26. #include <assert.h>
  27. #include <starpu_scheduler.h>
  28. #include <unistd.h>
  29. #include "../helper.h"
  30. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  31. #define STARTlin 1048576
  32. #define START 1024
  33. #ifdef STARPU_QUICK_CHECK
  34. #define END 1048576
  35. #else
  36. #define END 16777216
  37. #endif
  38. int ret;
  39. void memset_cpu(void *descr[], void *arg)
  40. {
  41. (void)arg;
  42. STARPU_SKIP_IF_VALGRIND;
  43. int *ptr = (int *)STARPU_VECTOR_GET_PTR(descr[0]);
  44. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  45. usleep(1000);
  46. int i;
  47. for (i=0; i<n ; i++)
  48. {
  49. ptr[0] += i;
  50. }
  51. }
  52. static struct starpu_perfmodel model =
  53. {
  54. .type = STARPU_REGRESSION_BASED,
  55. .symbol = "memset_regression_based"
  56. };
  57. static struct starpu_perfmodel nl_model =
  58. {
  59. .type = STARPU_NL_REGRESSION_BASED,
  60. .symbol = "non_linear_memset_regression_based"
  61. };
  62. static struct starpu_codelet memset_cl =
  63. {
  64. .cpu_funcs = {memset_cpu},
  65. .cpu_funcs_name = {"memset_cpu"},
  66. .model = &model,
  67. .nbuffers = 1,
  68. .modes = {STARPU_W}
  69. };
  70. static struct starpu_codelet nl_memset_cl =
  71. {
  72. .cpu_funcs = {memset_cpu},
  73. .cpu_funcs_name = {"memset_cpu"},
  74. .model = &nl_model,
  75. .nbuffers = 1,
  76. .modes = {STARPU_W}
  77. };
  78. static void test_memset(int nelems, struct starpu_codelet *codelet)
  79. {
  80. int nloops = 100;
  81. int loop;
  82. starpu_data_handle_t handle;
  83. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, nelems, sizeof(int));
  84. for (loop = 0; loop < nloops; loop++)
  85. {
  86. struct starpu_task *task = starpu_task_create();
  87. task->cl = codelet;
  88. task->handles[0] = handle;
  89. int ret = starpu_task_submit(task);
  90. if (ret == -ENODEV)
  91. exit(STARPU_TEST_SKIPPED);
  92. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  93. }
  94. starpu_data_unregister(handle);
  95. }
  96. static void compare_performance(int size, struct starpu_codelet *codelet, struct starpu_task *task)
  97. {
  98. unsigned i;
  99. int niter = 100;
  100. starpu_data_handle_t handle;
  101. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  102. struct starpu_task **tasks = (struct starpu_task **) malloc(niter*sizeof(struct starpu_task *));
  103. assert(tasks);
  104. for (i = 0; i < niter; i++)
  105. {
  106. struct starpu_task *task = starpu_task_create();
  107. task->cl = codelet;
  108. task->handles[0] = handle;
  109. /* create a synchronous task: any call to starpu_task_submit will block
  110. * until it is terminated */
  111. task->synchronous = 1;
  112. /* We will destroy the task structure by hand so that we can
  113. * query the profiling info before the task is destroyed. */
  114. task->destroy = 0;
  115. tasks[i] = task;
  116. ret = starpu_task_submit(task);
  117. if (STARPU_UNLIKELY(ret == -ENODEV))
  118. {
  119. FPRINTF(stderr, "No worker may execute this task\n");
  120. exit(0);
  121. }
  122. }
  123. starpu_data_unregister(handle);
  124. starpu_task_wait_for_all();
  125. double length_sum = 0.0;
  126. for (i = 0; i < niter; i++)
  127. {
  128. struct starpu_task *task = tasks[i];
  129. struct starpu_profiling_task_info *info = task->profiling_info;
  130. /* How long was the task execution ? */
  131. length_sum += starpu_timing_timespec_delay_us(&info->start_time, &info->end_time);
  132. /* We don't need the task structure anymore */
  133. starpu_task_destroy(task);
  134. }
  135. /* Display the occupancy of all workers during the test */
  136. unsigned worker;
  137. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  138. {
  139. struct starpu_profiling_worker_info worker_info;
  140. ret = starpu_profiling_worker_get_info(worker, &worker_info);
  141. STARPU_ASSERT(!ret);
  142. char workername[128];
  143. starpu_worker_get_name(worker, workername, sizeof(workername));
  144. unsigned nimpl;
  145. if (starpu_worker_get_type(worker)==STARPU_CPU_WORKER)
  146. {
  147. FPRINTF(stdout, "\n Worker :%s ::::::::::\n\n", workername);
  148. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  149. {
  150. FPRINTF(stdout, "Expected time for %d on %s (impl %u): %f, Measured time: %f\n",
  151. size, workername, nimpl,starpu_task_expected_length(task, starpu_worker_get_perf_archtype(worker, task->sched_ctx), nimpl), ((length_sum)/niter));
  152. }
  153. }
  154. }
  155. }
  156. int main(int argc, char **argv)
  157. {
  158. /* Enable profiling */
  159. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  160. struct starpu_conf conf;
  161. starpu_data_handle_t handle;
  162. int ret;
  163. starpu_conf_init(&conf);
  164. conf.sched_policy_name = "eager";
  165. conf.calibrate = 2;
  166. ret = starpu_initialize(&conf, &argc, &argv);
  167. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  168. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  169. int size;
  170. for (size = STARTlin; size < END; size *= 2)
  171. {
  172. /* Use a linear regression */
  173. test_memset(size, &memset_cl);
  174. }
  175. for (size = START; size < END; size *= 2)
  176. {
  177. /* Use a non-linear regression */
  178. test_memset(size, &nl_memset_cl);
  179. }
  180. ret = starpu_task_wait_for_all();
  181. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  182. starpu_shutdown();
  183. /* Test Phase */
  184. starpu_conf_init(&conf);
  185. conf.sched_policy_name = "eager";
  186. conf.calibrate = 0;
  187. ret = starpu_initialize(&conf, &argc, &argv);
  188. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  189. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  190. /* Now create a dummy task just to estimate its duration according to the regression */
  191. size = 1234567;
  192. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  193. struct starpu_task *task = starpu_task_create();
  194. task->cl = &memset_cl;
  195. task->handles[0] = handle;
  196. task->destroy = 0;
  197. FPRINTF(stdout, "\n ////linear regression results////\n");
  198. compare_performance(size, &memset_cl,task);
  199. task->cl = &nl_memset_cl;
  200. FPRINTF(stdout, "\n ////non linear regression results////\n");
  201. compare_performance(size, &nl_memset_cl,task);
  202. starpu_task_destroy(task);
  203. starpu_data_unregister(handle);
  204. starpu_shutdown();
  205. return 0;
  206. }