prefetch_data_on_node.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. struct starpu_conf conf;
  91. starpu_conf_init(&conf);
  92. conf.nfpga = 0;
  93. ret = starpu_initialize(&conf, &argc, &argv);
  94. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  95. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  96. starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  97. starpu_vector_data_register(&v_handle, STARPU_MAIN_RAM, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  98. unsigned nworker = starpu_worker_get_count();
  99. unsigned iter, worker;
  100. for (iter = 0; iter < N; iter++)
  101. {
  102. for (worker = 0; worker < nworker; worker++)
  103. {
  104. /* synchronous prefetch */
  105. unsigned node = starpu_worker_get_memory_node(worker);
  106. ret = starpu_data_prefetch_on_node(v_handle, node, 0);
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_prefetch_on_node");
  108. /* execute a task */
  109. struct starpu_task *task = starpu_task_create();
  110. task->handles[0] = v_handle;
  111. task->cl = select_codelet_with_random_mode();
  112. task->synchronous = 1;
  113. task->execute_on_a_specific_worker = 1;
  114. task->workerid = worker;
  115. ret = starpu_task_submit(task);
  116. if (ret == -ENODEV) goto enodev;
  117. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  118. }
  119. }
  120. for (iter = 0; iter < N; iter++)
  121. {
  122. for (worker = 0; worker < nworker; worker++)
  123. {
  124. /* asynchronous prefetch */
  125. unsigned node = starpu_worker_get_memory_node(worker);
  126. ret = starpu_data_prefetch_on_node(v_handle, node, 1);
  127. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_prefetch_on_node");
  128. /* execute a task */
  129. struct starpu_task *task = starpu_task_create();
  130. task->handles[0] = v_handle;
  131. task->cl = select_codelet_with_random_mode();
  132. task->callback_func = callback;
  133. task->callback_arg = (void*)(uintptr_t) starpu_worker_get_memory_node((worker+1)%nworker);
  134. task->execute_on_a_specific_worker = 1;
  135. task->workerid = worker;
  136. task->synchronous = 0;
  137. ret = starpu_task_submit(task);
  138. if (ret == -ENODEV) goto enodev;
  139. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  140. }
  141. }
  142. ret = starpu_task_wait_for_all();
  143. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  144. starpu_data_unregister(v_handle);
  145. starpu_free(v);
  146. starpu_shutdown();
  147. return EXIT_SUCCESS;
  148. enodev:
  149. starpu_free(v);
  150. fprintf(stderr, "WARNING: No one can execute this task\n");
  151. /* yes, we do not perform the computation but we did detect that no one
  152. * could perform the kernel, so this is not an error from StarPU */
  153. starpu_shutdown();
  154. return STARPU_TEST_SKIPPED;
  155. }