prefetch_data_on_node.c 4.5 KB

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