regression_based.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2016 Université de Bordeaux
  4. * Copyright (C) 2011 Télécom-SudParis
  5. * Copyright (C) 2012 INRIA
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <config.h>
  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. STARPU_SKIP_IF_VALGRIND;
  35. int *ptr = (int *)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 memset_cpu(void *descr[], void *arg)
  44. {
  45. STARPU_SKIP_IF_VALGRIND;
  46. int *ptr = (int *)STARPU_VECTOR_GET_PTR(descr[0]);
  47. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  48. memset(ptr, 42, n * sizeof(*ptr));
  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. #ifdef STARPU_USE_CUDA
  63. .cuda_funcs = {memset_cuda},
  64. .cuda_flags = {STARPU_CUDA_ASYNC},
  65. #endif
  66. #ifdef STARPU_USE_OPENCL
  67. .opencl_funcs = {memset_opencl},
  68. .opencl_flags = {STARPU_OPENCL_ASYNC},
  69. #endif
  70. .cpu_funcs = {memset_cpu},
  71. .cpu_funcs_name = {"memset_cpu"},
  72. .model = &model,
  73. .nbuffers = 1,
  74. .modes = {STARPU_W}
  75. };
  76. static struct starpu_codelet nl_memset_cl =
  77. {
  78. #ifdef STARPU_USE_CUDA
  79. .cuda_funcs = {memset_cuda},
  80. .cuda_flags = {STARPU_CUDA_ASYNC},
  81. #endif
  82. #ifdef STARPU_USE_OPENCL
  83. .opencl_funcs = {memset_opencl},
  84. .opencl_flags = {STARPU_OPENCL_ASYNC},
  85. #endif
  86. .cpu_funcs = {memset_cpu},
  87. .cpu_funcs_name = {"memset_cpu"},
  88. .model = &nl_model,
  89. .nbuffers = 1,
  90. .modes = {STARPU_W}
  91. };
  92. static void test_memset(int nelems, struct starpu_codelet *codelet)
  93. {
  94. int nloops = 100;
  95. int loop;
  96. starpu_data_handle_t handle;
  97. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, nelems, sizeof(int));
  98. for (loop = 0; loop < nloops; loop++)
  99. {
  100. struct starpu_task *task = starpu_task_create();
  101. task->cl = codelet;
  102. task->handles[0] = handle;
  103. int ret = starpu_task_submit(task);
  104. if (ret == -ENODEV)
  105. exit(STARPU_TEST_SKIPPED);
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  107. }
  108. starpu_data_unregister(handle);
  109. }
  110. static void show_task_perfs(int size, struct starpu_task *task)
  111. {
  112. unsigned workerid;
  113. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  114. {
  115. char name[32];
  116. starpu_worker_get_name(workerid, name, sizeof(name));
  117. unsigned nimpl;
  118. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  119. {
  120. FPRINTF(stdout, "Expected time for %d on %s (impl %u):\t%f\n",
  121. size, name, nimpl, starpu_task_expected_length(task, starpu_worker_get_perf_archtype(workerid, task->sched_ctx), nimpl));
  122. }
  123. }
  124. }
  125. #ifdef STARPU_USE_OPENCL
  126. struct starpu_opencl_program opencl_program;
  127. #endif
  128. int main(int argc, char **argv)
  129. {
  130. struct starpu_conf conf;
  131. starpu_data_handle_t handle;
  132. int ret;
  133. starpu_conf_init(&conf);
  134. conf.sched_policy_name = "eager";
  135. conf.calibrate = 2;
  136. ret = starpu_initialize(&conf, &argc, &argv);
  137. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  138. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  139. #ifdef STARPU_USE_OPENCL
  140. ret = starpu_opencl_load_opencl_from_file("tests/perfmodels/opencl_memset_kernel.cl",
  141. &opencl_program, NULL);
  142. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  143. #endif
  144. int size;
  145. for (size = START; size < END; size *= 2)
  146. {
  147. /* Use a linear regression */
  148. test_memset(size, &memset_cl);
  149. /* Use a non-linear regression */
  150. test_memset(size, &nl_memset_cl);
  151. }
  152. ret = starpu_task_wait_for_all();
  153. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  154. /* Now create a dummy task just to estimate its duration according to the regression */
  155. size = 12345;
  156. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  157. struct starpu_task *task = starpu_task_create();
  158. task->cl = &memset_cl;
  159. task->handles[0] = handle;
  160. task->destroy = 0;
  161. show_task_perfs(size, task);
  162. task->cl = &nl_memset_cl;
  163. show_task_perfs(size, task);
  164. starpu_task_destroy(task);
  165. starpu_data_unregister(handle);
  166. #ifdef STARPU_USE_OPENCL
  167. ret = starpu_opencl_unload_opencl(&opencl_program);
  168. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  169. #endif
  170. starpu_shutdown();
  171. return EXIT_SUCCESS;
  172. }