regression_based.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. *
  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. #include <starpu.h>
  19. #include "../helper.h"
  20. #ifdef STARPU_USE_CUDA
  21. static void memset_cuda(void *descr[], void *arg)
  22. {
  23. STARPU_SKIP_IF_VALGRIND;
  24. int *ptr = (int *)STARPU_VECTOR_GET_PTR(descr[0]);
  25. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  26. cudaMemset(ptr, 42, n);
  27. cudaThreadSynchronize();
  28. }
  29. #endif
  30. static void memset_cpu(void *descr[], void *arg)
  31. {
  32. STARPU_SKIP_IF_VALGRIND;
  33. int *ptr = (int *)STARPU_VECTOR_GET_PTR(descr[0]);
  34. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  35. memset(ptr, 42, n * sizeof(*ptr));
  36. }
  37. static struct starpu_perfmodel model =
  38. {
  39. .type = STARPU_REGRESSION_BASED,
  40. .symbol = "memset_regression_based"
  41. };
  42. static struct starpu_perfmodel nl_model =
  43. {
  44. .type = STARPU_NL_REGRESSION_BASED,
  45. .symbol = "non_linear_memset_regression_based"
  46. };
  47. static struct starpu_codelet memset_cl =
  48. {
  49. .where = STARPU_CUDA|STARPU_CPU,
  50. #ifdef STARPU_USE_CUDA
  51. .cuda_funcs = {memset_cuda, NULL},
  52. #endif
  53. .cpu_funcs = {memset_cpu, NULL},
  54. .model = &model,
  55. .nbuffers = 1,
  56. .modes = {STARPU_W}
  57. };
  58. static struct starpu_codelet nl_memset_cl =
  59. {
  60. .where = STARPU_CUDA|STARPU_CPU,
  61. #ifdef STARPU_USE_CUDA
  62. .cuda_funcs = {memset_cuda, NULL},
  63. #endif
  64. .cpu_funcs = {memset_cpu, NULL},
  65. .model = &nl_model,
  66. .nbuffers = 1,
  67. .modes = {STARPU_W}
  68. };
  69. static void test_memset(int nelems, struct starpu_codelet *codelet)
  70. {
  71. int nloops = 100;
  72. int loop;
  73. starpu_data_handle_t handle;
  74. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, nelems, sizeof(int));
  75. for (loop = 0; loop < nloops; loop++)
  76. {
  77. struct starpu_task *task = starpu_task_create();
  78. task->cl = codelet;
  79. task->handles[0] = handle;
  80. int ret = starpu_task_submit(task);
  81. if (ret == -ENODEV)
  82. exit(STARPU_TEST_SKIPPED);
  83. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  84. }
  85. starpu_data_unregister(handle);
  86. }
  87. static void show_task_perfs(int size, struct starpu_task *task)
  88. {
  89. unsigned workerid;
  90. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  91. {
  92. char name[16];
  93. starpu_worker_get_name(workerid, name, sizeof(name));
  94. unsigned nimpl;
  95. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  96. {
  97. FPRINTF(stdout, "Expected time for %d on %s:\t%f\n",
  98. size, name, starpu_task_expected_length(task, starpu_worker_get_perf_archtype(workerid), nimpl));
  99. }
  100. }
  101. }
  102. int main(int argc, char **argv)
  103. {
  104. struct starpu_conf conf;
  105. starpu_data_handle_t handle;
  106. int ret;
  107. starpu_conf_init(&conf);
  108. conf.sched_policy_name = "eager";
  109. conf.calibrate = 2;
  110. ret = starpu_init(&conf);
  111. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  112. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  113. int size;
  114. for (size = 1024; size < 16777216; size *= 2)
  115. {
  116. /* Use a linear regression */
  117. test_memset(size, &memset_cl);
  118. /* Use a non-linear regression */
  119. test_memset(size, &nl_memset_cl);
  120. }
  121. ret = starpu_task_wait_for_all();
  122. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  123. /* Now create a dummy task just to estimate its duration according to the regression */
  124. size = 12345;
  125. starpu_vector_data_register(&handle, -1, (uintptr_t)NULL, size, sizeof(int));
  126. struct starpu_task *task = starpu_task_create();
  127. task->cl = &memset_cl;
  128. task->handles[0] = handle;
  129. task->destroy = 0;
  130. show_task_perfs(size, task);
  131. task->cl = &nl_memset_cl;
  132. show_task_perfs(size, task);
  133. starpu_task_destroy(task);
  134. starpu_data_unregister(handle);
  135. starpu_shutdown();
  136. return EXIT_SUCCESS;
  137. }