regression_based.c 3.5 KB

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