prefetch_data_on_node.c 4.4 KB

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