sync_with_data_with_mem_non_blocking.c 3.2 KB

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