prefetch_data_on_node.c 4.2 KB

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