regression_based.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #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 memset_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. memset(ptr, 42, n * sizeof(*ptr));
  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. #ifdef STARPU_USE_CUDA
  65. .cuda_funcs = {memset_cuda},
  66. .cuda_flags = {STARPU_CUDA_ASYNC},
  67. #endif
  68. #ifdef STARPU_USE_OPENCL
  69. .opencl_funcs = {memset_opencl},
  70. .opencl_flags = {STARPU_OPENCL_ASYNC},
  71. #endif
  72. .cpu_funcs = {memset_cpu},
  73. .cpu_funcs_name = {"memset_cpu"},
  74. .model = &model,
  75. .nbuffers = 1,
  76. .modes = {STARPU_W}
  77. };
  78. static struct starpu_codelet nl_memset_cl =
  79. {
  80. #ifdef STARPU_USE_CUDA
  81. .cuda_funcs = {memset_cuda},
  82. .cuda_flags = {STARPU_CUDA_ASYNC},
  83. #endif
  84. #ifdef STARPU_USE_OPENCL
  85. .opencl_funcs = {memset_opencl},
  86. .opencl_flags = {STARPU_OPENCL_ASYNC},
  87. #endif
  88. .cpu_funcs = {memset_cpu},
  89. .cpu_funcs_name = {"memset_cpu"},
  90. .model = &nl_model,
  91. .nbuffers = 1,
  92. .modes = {STARPU_W}
  93. };
  94. static void test_memset(int nelems, struct starpu_codelet *codelet)
  95. {
  96. int nloops = 100;
  97. int loop;
  98. starpu_data_handle_t handle;
  99. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, nelems, sizeof(int));
  100. for (loop = 0; loop < nloops; loop++)
  101. {
  102. struct starpu_task *task = starpu_task_create();
  103. task->cl = codelet;
  104. task->handles[0] = handle;
  105. int ret = starpu_task_submit(task);
  106. if (ret == -ENODEV)
  107. exit(STARPU_TEST_SKIPPED);
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  109. }
  110. starpu_data_unregister(handle);
  111. }
  112. static void show_task_perfs(int size, struct starpu_task *task)
  113. {
  114. unsigned workerid;
  115. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  116. {
  117. char name[32];
  118. starpu_worker_get_name(workerid, name, sizeof(name));
  119. unsigned nimpl;
  120. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  121. {
  122. FPRINTF(stdout, "Expected time for %d on %s (impl %u):\t%f\n",
  123. size, name, nimpl, starpu_task_expected_length(task, starpu_worker_get_perf_archtype(workerid, task->sched_ctx), nimpl));
  124. }
  125. }
  126. }
  127. #ifdef STARPU_USE_OPENCL
  128. struct starpu_opencl_program opencl_program;
  129. #endif
  130. int main(int argc, char **argv)
  131. {
  132. struct starpu_conf conf;
  133. starpu_data_handle_t handle;
  134. int ret;
  135. starpu_conf_init(&conf);
  136. conf.sched_policy_name = "eager";
  137. conf.calibrate = 2;
  138. ret = starpu_initialize(&conf, &argc, &argv);
  139. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  140. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  141. #ifdef STARPU_USE_OPENCL
  142. ret = starpu_opencl_load_opencl_from_file("tests/perfmodels/opencl_memset_kernel.cl",
  143. &opencl_program, NULL);
  144. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  145. #endif
  146. int size;
  147. for (size = START; size < END; size *= 2)
  148. {
  149. /* Use a linear regression */
  150. test_memset(size, &memset_cl);
  151. /* Use a non-linear regression */
  152. test_memset(size, &nl_memset_cl);
  153. }
  154. ret = starpu_task_wait_for_all();
  155. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  156. /* Now create a dummy task just to estimate its duration according to the regression */
  157. size = 12345;
  158. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  159. struct starpu_task *task = starpu_task_create();
  160. task->cl = &memset_cl;
  161. task->handles[0] = handle;
  162. task->destroy = 0;
  163. show_task_perfs(size, task);
  164. task->cl = &nl_memset_cl;
  165. show_task_perfs(size, task);
  166. starpu_task_destroy(task);
  167. starpu_data_unregister(handle);
  168. #ifdef STARPU_USE_OPENCL
  169. ret = starpu_opencl_unload_opencl(&opencl_program);
  170. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  171. #endif
  172. starpu_shutdown();
  173. return EXIT_SUCCESS;
  174. }