regression_based.c 5.1 KB

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