prefetch_data_on_node.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 =
  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. int main(int argc, char **argv)
  62. {
  63. int ret;
  64. ret = starpu_init(NULL);
  65. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  66. starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  67. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  68. unsigned nworker = starpu_worker_get_count();
  69. unsigned iter, worker;
  70. for (iter = 0; iter < N; iter++)
  71. {
  72. for (worker = 0; worker < nworker; worker++)
  73. {
  74. /* synchronous prefetch */
  75. unsigned node = starpu_worker_get_memory_node(worker);
  76. ret = starpu_data_prefetch_on_node(v_handle, node, 0);
  77. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_prefetch_on_node");
  78. /* execute a task */
  79. struct starpu_task *task = starpu_task_create();
  80. task->cl = &cl;
  81. task->buffers[0].handle = v_handle;
  82. task->buffers[0].mode = select_random_mode();
  83. task->synchronous = 1;
  84. int ret = starpu_task_submit(task);
  85. if (ret == -ENODEV) goto enodev;
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  87. }
  88. }
  89. for (iter = 0; iter < N; iter++)
  90. {
  91. for (worker = 0; worker < nworker; worker++)
  92. {
  93. /* asynchronous prefetch */
  94. unsigned node = starpu_worker_get_memory_node(worker);
  95. ret = starpu_data_prefetch_on_node(v_handle, node, 1);
  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->cl = &cl;
  100. task->buffers[0].handle = v_handle;
  101. task->buffers[0].mode = select_random_mode();
  102. task->callback_func = callback;
  103. task->callback_arg = (void*)(uintptr_t) starpu_worker_get_memory_node((worker+1)%nworker);
  104. task->synchronous = 0;
  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. ret = starpu_task_wait_for_all();
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  112. starpu_data_unregister(v_handle);
  113. starpu_free(v);
  114. starpu_shutdown();
  115. return EXIT_SUCCESS;
  116. enodev:
  117. starpu_free(v);
  118. fprintf(stderr, "WARNING: No one can execute this task\n");
  119. /* yes, we do not perform the computation but we did detect that no one
  120. * could perform the kernel, so this is not an error from StarPU */
  121. starpu_shutdown();
  122. return STARPU_TEST_SKIPPED;
  123. }