regression_based.c 4.7 KB

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