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 "../common/helper.h"
  24. #define N 1000
  25. #define VECTORSIZE 1024
  26. starpu_data_handle 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 starpu_access_mode select_random_mode(void)
  39. {
  40. int r = rand();
  41. switch (r % 3) {
  42. case 0:
  43. return STARPU_R;
  44. case 1:
  45. return STARPU_RW;
  46. //return STARPU_W;
  47. case 2:
  48. return STARPU_RW;
  49. };
  50. return STARPU_RW;
  51. }
  52. static starpu_codelet cl = {
  53. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  54. .cpu_func = codelet_null,
  55. .cuda_func = codelet_null,
  56. .opencl_func = codelet_null,
  57. .nbuffers = 1
  58. };
  59. int main(int argc, char **argv)
  60. {
  61. int ret;
  62. ret = starpu_init(NULL);
  63. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  64. starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  65. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  66. unsigned nworker = starpu_worker_get_count();
  67. unsigned iter, worker;
  68. for (iter = 0; iter < N; iter++)
  69. {
  70. for (worker = 0; worker < nworker; worker++)
  71. {
  72. /* synchronous prefetch */
  73. unsigned node = starpu_worker_get_memory_node(worker);
  74. ret = starpu_data_prefetch_on_node(v_handle, node, 0);
  75. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_prefetch_on_node");
  76. /* execute a task */
  77. struct starpu_task *task = starpu_task_create();
  78. task->cl = &cl;
  79. task->buffers[0].handle = v_handle;
  80. task->buffers[0].mode = select_random_mode();
  81. task->synchronous = 1;
  82. int ret = starpu_task_submit(task);
  83. if (ret == -ENODEV) goto enodev;
  84. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  85. }
  86. }
  87. for (iter = 0; iter < N; iter++)
  88. {
  89. for (worker = 0; worker < nworker; worker++)
  90. {
  91. /* asynchronous prefetch */
  92. unsigned node = starpu_worker_get_memory_node(worker);
  93. ret = starpu_data_prefetch_on_node(v_handle, node, 1);
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_prefetch_on_node");
  95. /* execute a task */
  96. struct starpu_task *task = starpu_task_create();
  97. task->cl = &cl;
  98. task->buffers[0].handle = v_handle;
  99. task->buffers[0].mode = select_random_mode();
  100. task->callback_func = callback;
  101. task->callback_arg = (void*)(uintptr_t) starpu_worker_get_memory_node((worker+1)%nworker);
  102. task->synchronous = 0;
  103. int ret = starpu_task_submit(task);
  104. if (ret == -ENODEV) goto enodev;
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  106. }
  107. }
  108. ret = starpu_task_wait_for_all();
  109. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  110. starpu_data_unregister(v_handle);
  111. starpu_free(v);
  112. starpu_shutdown();
  113. return 0;
  114. enodev:
  115. starpu_free(v);
  116. fprintf(stderr, "WARNING: No one can execute this task\n");
  117. /* yes, we do not perform the computation but we did detect that no one
  118. * could perform the kernel, so this is not an error from StarPU */
  119. starpu_shutdown();
  120. return 77;
  121. }