sync_with_data_with_mem_non_blocking_implicit.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2013 Inria
  4. * Copyright (C) 2010-2014,2016 Université de Bordeaux
  5. * Copyright (C) 2010-2013,2015,2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <starpu.h>
  22. #include <stdlib.h>
  23. #include "../helper.h"
  24. #include <common/thread.h>
  25. /*
  26. * Mix submitting tasks and asynchronously acquiring the corresponding
  27. * data, but without implicit dependencies.
  28. */
  29. #define NBUFFERS_DEF 64
  30. #define NITER_DEF 128
  31. #define VECTORSIZE_DEF 1024
  32. static unsigned nbuffers = NBUFFERS_DEF;
  33. static unsigned niter = NITER_DEF;
  34. static unsigned vectorsize = VECTORSIZE_DEF;
  35. float *buffer[NBUFFERS_DEF];
  36. starpu_data_handle_t v_handle[NBUFFERS_DEF];
  37. void dummy_codelet(void *descr[], void *_args)
  38. {
  39. (void)descr;
  40. (void)_args;
  41. }
  42. static struct starpu_codelet cl =
  43. {
  44. .modes = { STARPU_RW },
  45. .cpu_funcs = {dummy_codelet},
  46. #ifdef STARPU_USE_CUDA
  47. .cuda_funcs = {dummy_codelet},
  48. #endif
  49. #ifdef STARPU_USE_OPENCL
  50. .opencl_funcs = {dummy_codelet},
  51. #endif
  52. .cpu_funcs_name = {"dummy_codelet"},
  53. .nbuffers = 1
  54. };
  55. static
  56. int use_handle(starpu_data_handle_t handle)
  57. {
  58. int ret;
  59. struct starpu_task *task;
  60. task = starpu_task_create();
  61. task->cl = &cl;
  62. task->handles[0] = handle;
  63. ret = starpu_task_submit(task);
  64. return ret;
  65. }
  66. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  67. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  68. static unsigned n_synced_buffers;
  69. static
  70. void callback_sync_data(void *arg)
  71. {
  72. (void)arg;
  73. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  74. n_synced_buffers++;
  75. if (n_synced_buffers == nbuffers)
  76. STARPU_PTHREAD_COND_SIGNAL(&cond);
  77. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  78. }
  79. int main(int argc, char **argv)
  80. {
  81. int ret;
  82. #ifdef STARPU_QUICK_CHECK
  83. nbuffers /= 4;
  84. niter /= 4;
  85. vectorsize /= 8;
  86. #endif
  87. ret = starpu_initialize(NULL, &argc, &argv);
  88. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  89. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  90. /* Allocate all buffers and register them to StarPU */
  91. unsigned b;
  92. for (b = 0; b < nbuffers; b++)
  93. {
  94. ret = starpu_malloc((void **)&buffer[b], vectorsize);
  95. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  96. starpu_vector_data_register(&v_handle[b], STARPU_MAIN_RAM,
  97. (uintptr_t)buffer[b], vectorsize, sizeof(char));
  98. }
  99. unsigned iter;
  100. for (iter = 0; iter < niter; iter++)
  101. {
  102. /* Use the buffers on the different workers so that it may not
  103. * be in main memory anymore */
  104. for (b = 0; b < nbuffers; b++)
  105. {
  106. ret = use_handle(v_handle[b]);
  107. if (ret == -ENODEV) goto enodev;
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  109. }
  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. starpu_do_schedule();
  121. /* Wait for all buffers to be available */
  122. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  123. while (n_synced_buffers != nbuffers)
  124. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  125. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  126. /* Release them */
  127. for (b = 0; b < nbuffers; b++)
  128. starpu_data_release(v_handle[b]);
  129. }
  130. ret = starpu_task_wait_for_all();
  131. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  132. /* do some cleanup */
  133. for (b = 0; b < nbuffers; b++)
  134. {
  135. starpu_data_unregister(v_handle[b]);
  136. starpu_free(buffer[b]);
  137. }
  138. starpu_shutdown();
  139. return EXIT_SUCCESS;
  140. enodev:
  141. fprintf(stderr, "WARNING: No one can execute this task\n");
  142. /* yes, we do not perform the computation but we did detect that no one
  143. * could perform the kernel, so this is not an error from StarPU */
  144. starpu_shutdown();
  145. return STARPU_TEST_SKIPPED;
  146. }