sync_with_data_with_mem_non_blocking.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU 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. * StarPU 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 "../helper.h"
  22. #include <common/thread.h>
  23. /*
  24. * Mix submitting tasks and asynchronously acquiring the corresponding data.
  25. */
  26. #define NBUFFERS_DEF 64
  27. #define NITER_DEF 128
  28. #define VECTORSIZE_DEF 1024
  29. static unsigned nbuffers = NBUFFERS_DEF;
  30. static unsigned niter = NITER_DEF;
  31. static unsigned vectorsize = VECTORSIZE_DEF;
  32. float *buffer[NBUFFERS_DEF];
  33. starpu_data_handle_t v_handle[NBUFFERS_DEF];
  34. void dummy_codelet(void *descr[], void *_args)
  35. {
  36. (void)descr;
  37. (void)_args;
  38. }
  39. static struct starpu_codelet cl =
  40. {
  41. .modes = { STARPU_RW },
  42. .cpu_funcs = {dummy_codelet},
  43. #ifdef STARPU_USE_CUDA
  44. .cuda_funcs = {dummy_codelet},
  45. #endif
  46. #ifdef STARPU_USE_OPENCL
  47. .opencl_funcs = {dummy_codelet},
  48. #endif
  49. .cpu_funcs_name = {"dummy_codelet"},
  50. .nbuffers = 1
  51. };
  52. static
  53. int use_handle(starpu_data_handle_t handle)
  54. {
  55. int ret;
  56. struct starpu_task *task;
  57. task = starpu_task_create();
  58. task->cl = &cl;
  59. task->handles[0] = handle;
  60. ret = starpu_task_submit(task);
  61. return ret;
  62. }
  63. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  64. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  65. static unsigned n_synced_buffers;
  66. static
  67. void callback_sync_data(void *arg)
  68. {
  69. (void)arg;
  70. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  71. n_synced_buffers++;
  72. if (n_synced_buffers == nbuffers)
  73. STARPU_PTHREAD_COND_SIGNAL(&cond);
  74. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  75. }
  76. int main(int argc, char **argv)
  77. {
  78. int ret;
  79. #ifdef STARPU_QUICK_CHECK
  80. nbuffers /= 4;
  81. niter /= 4;
  82. vectorsize /= 8;
  83. #endif
  84. ret = starpu_initialize(NULL, &argc, &argv);
  85. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  87. /* Allocate all buffers and register them to StarPU */
  88. unsigned b;
  89. for (b = 0; b < nbuffers; b++)
  90. {
  91. ret = starpu_malloc((void **)&buffer[b], vectorsize);
  92. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  93. starpu_vector_data_register(&v_handle[b], STARPU_MAIN_RAM,
  94. (uintptr_t)buffer[b], vectorsize, sizeof(char));
  95. starpu_data_set_sequential_consistency_flag(v_handle[b], 0);
  96. }
  97. unsigned iter;
  98. for (iter = 0; iter < niter; iter++)
  99. {
  100. /* Use the buffers on the different workers so that it may not
  101. * be in main memory anymore */
  102. for (b = 0; b < nbuffers; b++)
  103. {
  104. ret = use_handle(v_handle[b]);
  105. if (ret == -ENODEV) goto enodev;
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  107. }
  108. ret = starpu_task_wait_for_all();
  109. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  110. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  111. n_synced_buffers = 0;
  112. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  113. /* Grab the different pieces of data into main memory */
  114. for (b = 0; b < nbuffers; b++)
  115. {
  116. ret = starpu_data_acquire_cb(v_handle[b], STARPU_RW,
  117. callback_sync_data, NULL);
  118. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  119. }
  120. /* Wait for all buffers to be available */
  121. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  122. while (n_synced_buffers != nbuffers)
  123. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  124. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  125. /* Release them */
  126. for (b = 0; b < nbuffers; b++)
  127. starpu_data_release(v_handle[b]);
  128. }
  129. /* do some cleanup */
  130. for (b = 0; b < nbuffers; b++)
  131. {
  132. starpu_data_unregister(v_handle[b]);
  133. starpu_free_noflag(buffer[b], vectorsize);
  134. }
  135. starpu_shutdown();
  136. return EXIT_SUCCESS;
  137. enodev:
  138. fprintf(stderr, "WARNING: No one can execute this task\n");
  139. /* yes, we do not perform the computation but we did detect that no one
  140. * could perform the kernel, so this is not an error from StarPU */
  141. starpu_shutdown();
  142. return STARPU_TEST_SKIPPED;
  143. }