prefetch_data_on_node.c 4.6 KB

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