prefetch_data_on_node.c 4.5 KB

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