prefetch_data_on_node.c 3.3 KB

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