sync_with_data_with_mem_non_blocking.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include "../helper.h"
  24. #define NBUFFERS 64
  25. #define NITER 128
  26. #define VECTORSIZE 1024
  27. float *buffer[NBUFFERS];
  28. starpu_data_handle_t v_handle[NBUFFERS];
  29. static void dummy_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  30. {
  31. }
  32. static struct starpu_codelet cl =
  33. {
  34. .modes = { STARPU_RW },
  35. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  36. .cpu_funcs = {dummy_codelet, NULL},
  37. #ifdef STARPU_USE_CUDA
  38. .cuda_funcs = {dummy_codelet, NULL},
  39. #endif
  40. #ifdef STARPU_USE_OPENCL
  41. .opencl_funcs = {dummy_codelet, NULL},
  42. #endif
  43. .nbuffers = 1
  44. };
  45. int use_handle(starpu_data_handle_t handle)
  46. {
  47. int ret;
  48. struct starpu_task *task;
  49. task = starpu_task_create();
  50. task->cl = &cl;
  51. task->handles[0] = handle;
  52. task->detach = 0;
  53. ret = starpu_task_submit(task);
  54. return ret;
  55. }
  56. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  57. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  58. static unsigned n_synced_buffers;
  59. void callback_sync_data(void *arg __attribute__ ((unused)))
  60. {
  61. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  62. n_synced_buffers++;
  63. if (n_synced_buffers == NBUFFERS)
  64. _STARPU_PTHREAD_COND_SIGNAL(&cond);
  65. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  66. }
  67. int main(int argc, char **argv)
  68. {
  69. int ret;
  70. ret = starpu_init(NULL);
  71. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  72. /* Allocate all buffers and register them to StarPU */
  73. unsigned b;
  74. for (b = 0; b < NBUFFERS; b++)
  75. {
  76. ret = starpu_malloc((void **)&buffer[b], VECTORSIZE);
  77. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  78. starpu_vector_data_register(&v_handle[b], 0,
  79. (uintptr_t)buffer[b], VECTORSIZE, sizeof(char));
  80. starpu_data_set_sequential_consistency_flag(v_handle[b], 0);
  81. }
  82. unsigned iter;
  83. for (iter = 0; iter < NITER; iter++)
  84. {
  85. /* Use the buffers on the different workers so that it may not
  86. * be in main memory anymore */
  87. for (b = 0; b < NBUFFERS; b++)
  88. {
  89. ret = use_handle(v_handle[b]);
  90. if (ret == -ENODEV) goto enodev;
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  92. }
  93. ret = starpu_task_wait_for_all();
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  95. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  96. n_synced_buffers = 0;
  97. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  98. /* Grab the different pieces of data into main memory */
  99. for (b = 0; b < NBUFFERS; b++)
  100. {
  101. ret = starpu_data_acquire_cb(v_handle[b], STARPU_RW,
  102. callback_sync_data, NULL);
  103. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  104. }
  105. /* Wait for all buffers to be available */
  106. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  107. while (n_synced_buffers != NBUFFERS)
  108. _STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  109. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  110. /* Release them */
  111. for (b = 0; b < NBUFFERS; b++)
  112. starpu_data_release(v_handle[b]);
  113. }
  114. /* do some cleanup */
  115. for (b = 0; b < NBUFFERS; b++)
  116. {
  117. starpu_data_unregister(v_handle[b]);
  118. starpu_free(buffer[b]);
  119. }
  120. starpu_shutdown();
  121. return EXIT_SUCCESS;
  122. enodev:
  123. fprintf(stderr, "WARNING: No one can execute this task\n");
  124. /* yes, we do not perform the computation but we did detect that no one
  125. * could perform the kernel, so this is not an error from StarPU */
  126. starpu_shutdown();
  127. return STARPU_TEST_SKIPPED;
  128. }