prefetch_data_on_node.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include <stdlib.h>
  21. #include "../helper.h"
  22. /*
  23. * Try calling starpu_data_prefetch_on_node before running a task there
  24. */
  25. #ifdef STARPU_QUICK_CHECK
  26. #define N 10
  27. #elif !defined(STARPU_LONG_CHECK)
  28. #define N 100
  29. #else
  30. #define N 1000
  31. #endif
  32. #define VECTORSIZE 1024
  33. starpu_data_handle_t v_handle;
  34. static unsigned *v;
  35. static
  36. void callback(void *arg)
  37. {
  38. unsigned node = (unsigned)(uintptr_t) arg;
  39. starpu_data_prefetch_on_node(v_handle, node, 1);
  40. }
  41. void codelet_null(void *descr[], void *_args)
  42. {
  43. (void)descr;
  44. (void)_args;
  45. }
  46. static struct starpu_codelet cl_r =
  47. {
  48. .cpu_funcs = {codelet_null},
  49. .cuda_funcs = {codelet_null},
  50. .opencl_funcs = {codelet_null},
  51. .cpu_funcs_name = {"codelet_null"},
  52. .nbuffers = 1,
  53. .modes = {STARPU_R}
  54. };
  55. static struct starpu_codelet cl_w =
  56. {
  57. .cpu_funcs = {codelet_null},
  58. .cuda_funcs = {codelet_null},
  59. .opencl_funcs = {codelet_null},
  60. .cpu_funcs_name = {"codelet_null"},
  61. .nbuffers = 1,
  62. .modes = {STARPU_W}
  63. };
  64. static struct starpu_codelet cl_rw =
  65. {
  66. .cpu_funcs = {codelet_null},
  67. .cuda_funcs = {codelet_null},
  68. .opencl_funcs = {codelet_null},
  69. .cpu_funcs_name = {"codelet_null"},
  70. .nbuffers = 1,
  71. .modes = {STARPU_RW}
  72. };
  73. static struct starpu_codelet *select_codelet_with_random_mode(void)
  74. {
  75. int r = rand();
  76. switch (r % 3)
  77. {
  78. case 0:
  79. return &cl_r;
  80. case 1:
  81. return &cl_w;
  82. case 2:
  83. return &cl_rw;
  84. };
  85. return &cl_rw;
  86. }
  87. int main(int argc, char **argv)
  88. {
  89. int ret;
  90. ret = starpu_initialize(NULL, &argc, &argv);
  91. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  92. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  93. starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  94. starpu_vector_data_register(&v_handle, STARPU_MAIN_RAM, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  95. unsigned nworker = starpu_worker_get_count();
  96. unsigned iter, worker;
  97. for (iter = 0; iter < N; iter++)
  98. {
  99. for (worker = 0; worker < nworker; worker++)
  100. {
  101. /* synchronous prefetch */
  102. unsigned node = starpu_worker_get_memory_node(worker);
  103. ret = starpu_data_prefetch_on_node(v_handle, node, 0);
  104. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_prefetch_on_node");
  105. /* execute a task */
  106. struct starpu_task *task = starpu_task_create();
  107. task->handles[0] = v_handle;
  108. task->cl = select_codelet_with_random_mode();
  109. task->synchronous = 1;
  110. task->execute_on_a_specific_worker = 1;
  111. task->workerid = worker;
  112. ret = starpu_task_submit(task);
  113. if (ret == -ENODEV) goto enodev;
  114. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  115. }
  116. }
  117. for (iter = 0; iter < N; iter++)
  118. {
  119. for (worker = 0; worker < nworker; worker++)
  120. {
  121. /* asynchronous prefetch */
  122. unsigned node = starpu_worker_get_memory_node(worker);
  123. ret = starpu_data_prefetch_on_node(v_handle, node, 1);
  124. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_prefetch_on_node");
  125. /* execute a task */
  126. struct starpu_task *task = starpu_task_create();
  127. task->handles[0] = v_handle;
  128. task->cl = select_codelet_with_random_mode();
  129. task->callback_func = callback;
  130. task->callback_arg = (void*)(uintptr_t) starpu_worker_get_memory_node((worker+1)%nworker);
  131. task->execute_on_a_specific_worker = 1;
  132. task->workerid = worker;
  133. task->synchronous = 0;
  134. ret = starpu_task_submit(task);
  135. if (ret == -ENODEV) goto enodev;
  136. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  137. }
  138. }
  139. ret = starpu_task_wait_for_all();
  140. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  141. starpu_data_unregister(v_handle);
  142. starpu_free(v);
  143. starpu_shutdown();
  144. return EXIT_SUCCESS;
  145. enodev:
  146. starpu_free(v);
  147. fprintf(stderr, "WARNING: No one can execute this task\n");
  148. /* yes, we do not perform the computation but we did detect that no one
  149. * could perform the kernel, so this is not an error from StarPU */
  150. starpu_shutdown();
  151. return STARPU_TEST_SKIPPED;
  152. }