regression_based.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011,2012,2014 Inria
  4. * Copyright (C) 2011-2016 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 START 1024
  26. #ifdef STARPU_QUICK_CHECK
  27. #define END 1048576
  28. #else
  29. #define END 16777216
  30. #endif
  31. unsigned int usecs=100;
  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. void memset_cpu(void *descr[], void *arg)
  46. {
  47. (void)arg;
  48. STARPU_SKIP_IF_VALGRIND;
  49. int *ptr = (int *)STARPU_VECTOR_GET_PTR(descr[0]);
  50. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  51. //boucle for
  52. for (int i=0; i<n*100; i++)
  53. {
  54. }
  55. }
  56. static struct starpu_perfmodel model =
  57. {
  58. .type = STARPU_REGRESSION_BASED,
  59. .symbol = "memset_regression_based"
  60. };
  61. static struct starpu_perfmodel nl_model =
  62. {
  63. .type = STARPU_NL_REGRESSION_BASED,
  64. .symbol = "non_linear_memset_regression_based"
  65. };
  66. static struct starpu_codelet memset_cl =
  67. {
  68. #ifdef STARPU_USE_CUDA
  69. .cuda_funcs = {memset_cuda},
  70. .cuda_flags = {STARPU_CUDA_ASYNC},
  71. #endif
  72. #ifdef STARPU_USE_OPENCL
  73. .opencl_funcs = {memset_opencl},
  74. .opencl_flags = {STARPU_OPENCL_ASYNC},
  75. #endif
  76. .cpu_funcs = {memset_cpu},
  77. .cpu_funcs_name = {"memset_cpu"},
  78. .model = &model,
  79. .nbuffers = 1,
  80. .modes = {STARPU_W}
  81. };
  82. static struct starpu_codelet nl_memset_cl =
  83. {
  84. #ifdef STARPU_USE_CUDA
  85. .cuda_funcs = {memset_cuda},
  86. .cuda_flags = {STARPU_CUDA_ASYNC},
  87. #endif
  88. #ifdef STARPU_USE_OPENCL
  89. .opencl_funcs = {memset_opencl},
  90. .opencl_flags = {STARPU_OPENCL_ASYNC},
  91. #endif
  92. .cpu_funcs = {memset_cpu},
  93. .cpu_funcs_name = {"memset_cpu"},
  94. .model = &nl_model,
  95. .nbuffers = 1,
  96. .modes = {STARPU_W}
  97. };
  98. static void test_memset(int nelems, struct starpu_codelet *codelet)
  99. {
  100. int nloops = 100;
  101. int loop;
  102. starpu_data_handle_t handle;
  103. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, nelems, sizeof(int));
  104. for (loop = 0; loop < nloops; loop++)
  105. {
  106. struct starpu_task *task = starpu_task_create();
  107. task->cl = codelet;
  108. task->handles[0] = handle;
  109. int ret = starpu_task_submit(task);
  110. if (ret == -ENODEV)
  111. exit(STARPU_TEST_SKIPPED);
  112. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  113. }
  114. starpu_data_unregister(handle);
  115. }
  116. static void show_task_perfs(int size, struct starpu_task *task)
  117. {
  118. unsigned workerid;
  119. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  120. {
  121. char name[32];
  122. starpu_worker_get_name(workerid, name, sizeof(name));
  123. unsigned nimpl;
  124. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  125. {
  126. FPRINTF(stdout, "Expected time for %d on %s (impl %u):\t%f\n",
  127. size, name, nimpl, starpu_task_expected_length(task, starpu_worker_get_perf_archtype(workerid, task->sched_ctx), nimpl));
  128. }
  129. }
  130. }
  131. #ifdef STARPU_USE_OPENCL
  132. struct starpu_opencl_program opencl_program;
  133. #endif
  134. int main(int argc, char **argv)
  135. {
  136. struct starpu_conf conf;
  137. starpu_data_handle_t handle;
  138. int ret;
  139. starpu_conf_init(&conf);
  140. conf.sched_policy_name = "eager";
  141. conf.calibrate = 2;
  142. ret = starpu_initialize(&conf, &argc, &argv);
  143. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  144. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  145. #ifdef STARPU_USE_OPENCL
  146. ret = starpu_opencl_load_opencl_from_file("tests/perfmodels/opencl_memset_kernel.cl",
  147. &opencl_program, NULL);
  148. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  149. #endif
  150. int size;
  151. for (size = START; size < END; size *= 2)
  152. {
  153. /* Use a linear regression */
  154. test_memset(size, &memset_cl);
  155. /* Use a non-linear regression */
  156. test_memset(size, &nl_memset_cl);
  157. }
  158. ret = starpu_task_wait_for_all();
  159. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  160. starpu_shutdown();
  161. /* Test Phase */
  162. starpu_conf_init(&conf);
  163. conf.sched_policy_name = "eager";
  164. conf.calibrate = 0;
  165. ret = starpu_initialize(&conf, &argc, &argv);
  166. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  167. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  168. /* Now create a dummy task just to estimate its duration according to the regression */
  169. size = 1000;
  170. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  171. struct starpu_task *task = starpu_task_create();
  172. task->cl = &memset_cl;
  173. task->handles[0] = handle;
  174. task->destroy = 0;
  175. FPRINTF(stdout, "linear regression results\n");
  176. show_task_perfs(size, task);
  177. task->cl = &nl_memset_cl;
  178. FPRINTF(stdout, "non linear regression results\n");
  179. show_task_perfs(size, task);
  180. starpu_task_destroy(task);
  181. starpu_data_unregister(handle);
  182. #ifdef STARPU_USE_OPENCL
  183. ret = starpu_opencl_unload_opencl(&opencl_program);
  184. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  185. #endif
  186. starpu_shutdown();
  187. return EXIT_SUCCESS;
  188. }