prefetch_data_on_node.c 4.3 KB

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