prefetch_data_on_node.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include <stdlib.h>
  21. #include <pthread.h>
  22. #define N 1000
  23. #define VECTORSIZE 1024
  24. static pthread_mutex_t mutex;
  25. static pthread_cond_t cond;
  26. static unsigned finished = 0;
  27. static unsigned cnt;
  28. starpu_data_handle v_handle;
  29. static unsigned *v;
  30. static void callback(void *arg)
  31. {
  32. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  33. //fprintf(stderr, "res ...%d\n", res);
  34. //fflush(stderr);
  35. if (res == 0)
  36. {
  37. pthread_mutex_lock(&mutex);
  38. finished = 1;
  39. pthread_cond_signal(&cond);
  40. pthread_mutex_unlock(&mutex);
  41. }
  42. }
  43. static void codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  44. {
  45. // fprintf(stderr, "pif\n");
  46. // fflush(stderr);
  47. }
  48. static starpu_access_mode select_random_mode(void)
  49. {
  50. int r = rand();
  51. switch (r % 3) {
  52. case 0:
  53. return STARPU_R;
  54. case 1:
  55. return STARPU_RW;
  56. //return STARPU_W;
  57. case 2:
  58. return STARPU_RW;
  59. };
  60. }
  61. static starpu_codelet cl = {
  62. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  63. .cpu_func = codelet_null,
  64. .cuda_func = codelet_null,
  65. .opencl_func = codelet_null,
  66. .nbuffers = 1
  67. };
  68. int main(int argc, char **argv)
  69. {
  70. starpu_init(NULL);
  71. starpu_data_malloc_pinned_if_possible((void **)&v, VECTORSIZE*sizeof(unsigned));
  72. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  73. unsigned nworker = starpu_worker_get_count();
  74. cnt = nworker*N;
  75. unsigned iter, worker;
  76. for (iter = 0; iter < N; iter++)
  77. {
  78. for (worker = 0; worker < nworker; worker++)
  79. {
  80. /* synchronous prefetch */
  81. unsigned node = starpu_worker_get_memory_node(worker);
  82. starpu_data_prefetch_on_node(v_handle, node, 0);
  83. /* execute a task */
  84. struct starpu_task *task = starpu_task_create();
  85. task->cl = &cl;
  86. task->buffers[0].handle = v_handle;
  87. task->buffers[0].mode = select_random_mode();
  88. task->callback_func = callback;
  89. task->callback_arg = NULL;
  90. task->synchronous = 1;
  91. int ret = starpu_task_submit(task);
  92. if (ret == -ENODEV)
  93. goto enodev;
  94. }
  95. }
  96. pthread_mutex_lock(&mutex);
  97. if (!finished)
  98. pthread_cond_wait(&cond, &mutex);
  99. pthread_mutex_unlock(&mutex);
  100. starpu_shutdown();
  101. return 0;
  102. enodev:
  103. fprintf(stderr, "WARNING: No one can execute this task\n");
  104. /* yes, we do not perform the computation but we did detect that no one
  105. * could perform the kernel, so this is not an error from StarPU */
  106. return 0;
  107. }