prefetch_data_on_node.c 4.6 KB

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