sync_with_data_with_mem_non_blocking_implicit.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 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 NBUFFERS 64
  24. #define NITER 128
  25. #define VECTORSIZE 1024
  26. float *buffer[NBUFFERS];
  27. starpu_data_handle v_handle[NBUFFERS];
  28. static void dummy_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  29. {
  30. }
  31. static starpu_codelet cl = {
  32. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  33. .cpu_func = dummy_codelet,
  34. #ifdef STARPU_USE_CUDA
  35. .cuda_func = dummy_codelet,
  36. #endif
  37. #ifdef STARPU_USE_OPENCL
  38. .opencl_func = dummy_codelet,
  39. #endif
  40. .nbuffers = 1
  41. };
  42. void use_handle(starpu_data_handle handle)
  43. {
  44. int ret;
  45. struct starpu_task *task;
  46. task = starpu_task_create();
  47. task->cl = &cl;
  48. task->buffers[0].handle = handle;
  49. task->buffers[0].mode = STARPU_RW;
  50. task->detach = 0;
  51. ret = starpu_task_submit(task);
  52. if (ret == -ENODEV)
  53. {
  54. /* No one can execute such a task, but that's not a failure
  55. * of the test either. */
  56. exit(0);
  57. }
  58. }
  59. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  60. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  61. static unsigned n_synced_buffers;
  62. void callback_sync_data(void *arg __attribute__ ((unused)))
  63. {
  64. pthread_mutex_lock(&mutex);
  65. n_synced_buffers++;
  66. if (n_synced_buffers == NBUFFERS)
  67. pthread_cond_signal(&cond);
  68. pthread_mutex_unlock(&mutex);
  69. }
  70. int main(int argc, char **argv)
  71. {
  72. starpu_init(NULL);
  73. /* Allocate all buffers and register them to StarPU */
  74. unsigned b;
  75. for (b = 0; b < NBUFFERS; b++)
  76. {
  77. starpu_malloc((void **)&buffer[b], VECTORSIZE);
  78. starpu_vector_data_register(&v_handle[b], 0,
  79. (uintptr_t)buffer[b], VECTORSIZE, sizeof(char));
  80. }
  81. unsigned iter;
  82. for (iter = 0; iter < NITER; iter++)
  83. {
  84. /* Use the buffers on the different workers so that it may not
  85. * be in main memory anymore */
  86. for (b = 0; b < NBUFFERS; b++)
  87. use_handle(v_handle[b]);
  88. pthread_mutex_lock(&mutex);
  89. n_synced_buffers = 0;
  90. pthread_mutex_unlock(&mutex);
  91. /* Grab the different pieces of data into main memory */
  92. for (b = 0; b < NBUFFERS; b++)
  93. {
  94. starpu_data_acquire_cb(v_handle[b], STARPU_RW,
  95. callback_sync_data, NULL);
  96. }
  97. /* Wait for all buffers to be available */
  98. pthread_mutex_lock(&mutex);
  99. while (n_synced_buffers != NBUFFERS)
  100. pthread_cond_wait(&cond, &mutex);
  101. pthread_mutex_unlock(&mutex);
  102. /* Release them */
  103. for (b = 0; b < NBUFFERS; b++)
  104. starpu_data_release(v_handle[b]);
  105. }
  106. starpu_task_wait_for_all();
  107. /* do some cleanup */
  108. for (b = 0; b < NBUFFERS; b++) {
  109. starpu_data_unregister(v_handle[b]);
  110. starpu_free(buffer[b]);
  111. }
  112. starpu_shutdown();
  113. return 0;
  114. }