regression_based_memset.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2011 Télécom-SudParis
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <starpu_scheduler.h>
  19. #include "../helper.h"
  20. /*
  21. * Benchmark memset with a linear and non-linear regression
  22. */
  23. #define STARTlin 1024
  24. #define START 1024
  25. #ifdef STARPU_QUICK_CHECK
  26. #define END 1048576
  27. #else
  28. #define END 16777216
  29. #endif
  30. #ifdef STARPU_USE_CUDA
  31. static void memset_cuda(void *descr[], void *arg)
  32. {
  33. (void)arg;
  34. STARPU_SKIP_IF_VALGRIND;
  35. unsigned *ptr = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  36. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  37. cudaMemsetAsync(ptr, 42, n * sizeof(*ptr), starpu_cuda_get_local_stream());
  38. }
  39. #endif
  40. #ifdef STARPU_USE_OPENCL
  41. extern void memset_opencl(void *buffers[], void *args);
  42. #endif
  43. void memset0_cpu(void *descr[], void *arg)
  44. {
  45. (void)arg;
  46. STARPU_SKIP_IF_VALGRIND;
  47. unsigned *ptr = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  48. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  49. unsigned i;
  50. for (i = 0; i < n; i++)
  51. ptr[i] = 42;
  52. }
  53. void memset_cpu(void *descr[], void *arg)
  54. {
  55. (void)arg;
  56. STARPU_SKIP_IF_VALGRIND;
  57. unsigned *ptr = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  58. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  59. starpu_usleep(10);
  60. memset(ptr, 42, n * sizeof(*ptr));
  61. }
  62. static struct starpu_perfmodel model =
  63. {
  64. .type = STARPU_REGRESSION_BASED,
  65. .symbol = "memset_regression_based"
  66. };
  67. static struct starpu_perfmodel nl_model =
  68. {
  69. .type = STARPU_NL_REGRESSION_BASED,
  70. .symbol = "non_linear_memset_regression_based"
  71. };
  72. static struct starpu_codelet memset_cl =
  73. {
  74. #ifdef STARPU_USE_CUDA
  75. .cuda_funcs = {memset_cuda},
  76. .cuda_flags = {STARPU_CUDA_ASYNC},
  77. #endif
  78. #ifdef STARPU_USE_OPENCL
  79. .opencl_funcs = {memset_opencl},
  80. .opencl_flags = {STARPU_OPENCL_ASYNC},
  81. #endif
  82. .cpu_funcs = {memset0_cpu, memset_cpu},
  83. .cpu_funcs_name = {"memset0_cpu", "memset_cpu"},
  84. .model = &model,
  85. .nbuffers = 1,
  86. .modes = {STARPU_W}
  87. };
  88. static struct starpu_codelet nl_memset_cl =
  89. {
  90. #ifdef STARPU_USE_CUDA
  91. .cuda_funcs = {memset_cuda},
  92. .cuda_flags = {STARPU_CUDA_ASYNC},
  93. #endif
  94. #ifdef STARPU_USE_OPENCL
  95. .opencl_funcs = {memset_opencl},
  96. .opencl_flags = {STARPU_OPENCL_ASYNC},
  97. #endif
  98. .cpu_funcs = {memset0_cpu, memset_cpu},
  99. .cpu_funcs_name = {"memset0_cpu", "memset_cpu"},
  100. .model = &nl_model,
  101. .nbuffers = 1,
  102. .modes = {STARPU_W}
  103. };
  104. static void test_memset(int nelems, struct starpu_codelet *codelet)
  105. {
  106. int nloops = 100;
  107. int loop;
  108. starpu_data_handle_t handle;
  109. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, nelems, sizeof(int));
  110. for (loop = 0; loop < nloops; loop++)
  111. {
  112. struct starpu_task *task = starpu_task_create();
  113. task->cl = codelet;
  114. task->handles[0] = handle;
  115. int ret = starpu_task_submit(task);
  116. if (ret == -ENODEV)
  117. exit(STARPU_TEST_SKIPPED);
  118. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  119. }
  120. starpu_data_unregister(handle);
  121. }
  122. static void show_task_perfs(int size, struct starpu_task *task)
  123. {
  124. unsigned workerid;
  125. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  126. {
  127. char name[32];
  128. starpu_worker_get_name(workerid, name, sizeof(name));
  129. unsigned nimpl;
  130. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  131. {
  132. FPRINTF(stdout, "Expected time for %d on %s (impl %u):\t%f\n",
  133. size, name, nimpl, starpu_task_expected_length(task, starpu_worker_get_perf_archtype(workerid, task->sched_ctx), nimpl));
  134. }
  135. }
  136. }
  137. #ifdef STARPU_USE_OPENCL
  138. struct starpu_opencl_program opencl_program;
  139. #endif
  140. int main(int argc, char **argv)
  141. {
  142. struct starpu_conf conf;
  143. starpu_data_handle_t handle;
  144. int ret;
  145. starpu_conf_init(&conf);
  146. conf.sched_policy_name = "dmda";
  147. conf.calibrate = 2;
  148. ret = starpu_initialize(&conf, &argc, &argv);
  149. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  150. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  151. #ifdef STARPU_USE_OPENCL
  152. ret = starpu_opencl_load_opencl_from_file("tests/perfmodels/opencl_memset_kernel.cl",
  153. &opencl_program, NULL);
  154. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  155. #endif
  156. int size;
  157. for (size = STARTlin; size < END; size *= 2)
  158. {
  159. /* Use a linear regression */
  160. test_memset(size, &memset_cl);
  161. }
  162. for (size = START; size < END; size *= 2)
  163. {
  164. /* Use a non-linear regression */
  165. test_memset(size, &nl_memset_cl);
  166. }
  167. ret = starpu_task_wait_for_all();
  168. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  169. /* Now create a dummy task just to estimate its duration according to the regression */
  170. size = 12345;
  171. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  172. struct starpu_task *task = starpu_task_create();
  173. task->cl = &memset_cl;
  174. task->handles[0] = handle;
  175. task->destroy = 0;
  176. show_task_perfs(size, task);
  177. task->cl = &nl_memset_cl;
  178. show_task_perfs(size, task);
  179. starpu_task_destroy(task);
  180. starpu_data_unregister(handle);
  181. #ifdef STARPU_USE_OPENCL
  182. ret = starpu_opencl_unload_opencl(&opencl_program);
  183. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  184. #endif
  185. starpu_shutdown();
  186. return EXIT_SUCCESS;
  187. }