regression_based.c 5.6 KB

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