regression_based.c 4.9 KB

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