regression_based.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Université de Bordeaux 1
  4. * Copyright (C) 2011 Télécom-SudParis
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <config.h>
  18. #if STARPU_HAVE_VALGRIND_H
  19. #include <valgrind/valgrind.h>
  20. #endif
  21. #include <starpu.h>
  22. #include "../helper.h"
  23. #ifdef STARPU_USE_CUDA
  24. static void memset_cuda(void *descr[], void *arg)
  25. {
  26. STARPU_SKIP_IF_VALGRIND;
  27. int *ptr = (int *)STARPU_VECTOR_GET_PTR(descr[0]);
  28. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  29. cudaMemset(ptr, 42, n);
  30. cudaThreadSynchronize();
  31. }
  32. #endif
  33. static void memset_cpu(void *descr[], void *arg)
  34. {
  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. memset(ptr, 42, n);
  39. }
  40. static struct starpu_perfmodel model =
  41. {
  42. .type = STARPU_REGRESSION_BASED,
  43. .symbol = "memset_regression_based"
  44. };
  45. static struct starpu_perfmodel nl_model =
  46. {
  47. .type = STARPU_NL_REGRESSION_BASED,
  48. .symbol = "non_linear_memset_regression_based"
  49. };
  50. static struct starpu_codelet memset_cl =
  51. {
  52. .where = STARPU_CUDA|STARPU_CPU,
  53. #ifdef STARPU_USE_CUDA
  54. .cuda_funcs = {memset_cuda, NULL},
  55. #endif
  56. .cpu_funcs = {memset_cpu, NULL},
  57. .model = &model,
  58. .nbuffers = 1,
  59. .modes = {STARPU_W}
  60. };
  61. static struct starpu_codelet nl_memset_cl =
  62. {
  63. .where = STARPU_CUDA|STARPU_CPU,
  64. #ifdef STARPU_USE_CUDA
  65. .cuda_funcs = {memset_cuda, NULL},
  66. #endif
  67. .cpu_funcs = {memset_cpu, NULL},
  68. .model = &nl_model,
  69. .nbuffers = 1,
  70. .modes = {STARPU_W}
  71. };
  72. static void test_memset(int nelems, struct starpu_codelet *codelet)
  73. {
  74. int nloops = 100;
  75. int loop;
  76. starpu_data_handle_t handle;
  77. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, nelems, sizeof(int));
  78. for (loop = 0; loop < nloops; loop++)
  79. {
  80. struct starpu_task *task = starpu_task_create();
  81. task->cl = codelet;
  82. task->handles[0] = handle;
  83. int ret = starpu_task_submit(task);
  84. if (ret == -ENODEV)
  85. exit(STARPU_TEST_SKIPPED);
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  87. }
  88. starpu_data_unregister(handle);
  89. }
  90. static void show_task_perfs(int size, struct starpu_task *task)
  91. {
  92. unsigned workerid;
  93. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  94. {
  95. char name[16];
  96. starpu_worker_get_name(workerid, name, sizeof(name));
  97. unsigned nimpl;
  98. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  99. {
  100. FPRINTF(stdout, "Expected time for %d on %s:\t%f\n",
  101. size, name, starpu_task_expected_length(task, starpu_worker_get_perf_archtype(workerid), nimpl));
  102. }
  103. }
  104. }
  105. int main(int argc, char **argv)
  106. {
  107. struct starpu_conf conf;
  108. starpu_data_handle_t handle;
  109. int ret;
  110. starpu_conf_init(&conf);
  111. conf.sched_policy_name = "eager";
  112. conf.calibrate = 2;
  113. ret = starpu_init(&conf);
  114. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  115. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  116. int size;
  117. for (size = 1024; size < 16777216; size *= 2)
  118. {
  119. /* Use a linear regression */
  120. test_memset(size, &memset_cl);
  121. /* Use a non-linear regression */
  122. test_memset(size, &nl_memset_cl);
  123. }
  124. ret = starpu_task_wait_for_all();
  125. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  126. /* Now create a dummy task just to estimate its duration according to the regression */
  127. size = 12345;
  128. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  129. struct starpu_task *task = starpu_task_create();
  130. task->cl = &memset_cl;
  131. task->handles[0] = handle;
  132. show_task_perfs(size, task);
  133. task->cl = &nl_memset_cl;
  134. show_task_perfs(size, task);
  135. starpu_task_destroy(task);
  136. starpu_data_unregister(handle);
  137. starpu_shutdown();
  138. return EXIT_SUCCESS;
  139. }