regression_based.c 3.9 KB

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