prefetch_data_on_node.c 3.1 KB

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