regression_based_01.c 7.1 KB

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