sync_with_data_with_mem_non_blocking.c 3.0 KB

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