prefetch_data_on_node.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 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. #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 struct starpu_codelet cl_r =
  39. {
  40. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  41. .cpu_funcs = {codelet_null, NULL},
  42. .cuda_funcs = {codelet_null, NULL},
  43. .opencl_funcs = {codelet_null, NULL},
  44. .nbuffers = 1,
  45. .modes = {STARPU_R}
  46. };
  47. static struct starpu_codelet cl_w =
  48. {
  49. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  50. .cpu_funcs = {codelet_null, NULL},
  51. .cuda_funcs = {codelet_null, NULL},
  52. .opencl_funcs = {codelet_null, NULL},
  53. .nbuffers = 1,
  54. .modes = {STARPU_W}
  55. };
  56. static struct starpu_codelet cl_rw =
  57. {
  58. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  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. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  84. starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  85. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  86. unsigned nworker = starpu_worker_get_count();
  87. unsigned iter, worker;
  88. for (iter = 0; iter < N; iter++)
  89. {
  90. for (worker = 0; worker < nworker; worker++)
  91. {
  92. /* synchronous prefetch */
  93. unsigned node = starpu_worker_get_memory_node(worker);
  94. ret = starpu_data_prefetch_on_node(v_handle, node, 0);
  95. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_prefetch_on_node");
  96. /* execute a task */
  97. struct starpu_task *task = starpu_task_create();
  98. task->handles[0] = v_handle;
  99. task->cl = select_codelet_with_random_mode();
  100. task->synchronous = 1;
  101. int ret = starpu_task_submit(task);
  102. if (ret == -ENODEV) goto enodev;
  103. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  104. }
  105. }
  106. for (iter = 0; iter < N; iter++)
  107. {
  108. for (worker = 0; worker < nworker; worker++)
  109. {
  110. /* asynchronous prefetch */
  111. unsigned node = starpu_worker_get_memory_node(worker);
  112. ret = starpu_data_prefetch_on_node(v_handle, node, 1);
  113. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_prefetch_on_node");
  114. /* execute a task */
  115. struct starpu_task *task = starpu_task_create();
  116. task->handles[0] = v_handle;
  117. task->cl = select_codelet_with_random_mode();
  118. task->callback_func = callback;
  119. task->callback_arg = (void*)(uintptr_t) starpu_worker_get_memory_node((worker+1)%nworker);
  120. task->synchronous = 0;
  121. int ret = starpu_task_submit(task);
  122. if (ret == -ENODEV) goto enodev;
  123. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  124. }
  125. }
  126. ret = starpu_task_wait_for_all();
  127. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  128. starpu_data_unregister(v_handle);
  129. starpu_free(v);
  130. starpu_shutdown();
  131. return EXIT_SUCCESS;
  132. enodev:
  133. starpu_free(v);
  134. fprintf(stderr, "WARNING: No one can execute this task\n");
  135. /* yes, we do not perform the computation but we did detect that no one
  136. * could perform the kernel, so this is not an error from StarPU */
  137. starpu_shutdown();
  138. return STARPU_TEST_SKIPPED;
  139. }