regression_based.c 3.6 KB

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