sync_with_data_with_mem_non_blocking_implicit.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012, 2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 CNRS
  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 "../helper.h"
  23. #include <common/thread.h>
  24. /*
  25. * Mix submitting tasks and asynchronously acquiring the corresponding
  26. * data, but without implicit dependencies.
  27. */
  28. #define NBUFFERS_DEF 64
  29. #define NITER_DEF 128
  30. #define VECTORSIZE_DEF 1024
  31. static unsigned nbuffers = NBUFFERS_DEF;
  32. static unsigned niter = NITER_DEF;
  33. static unsigned vectorsize = VECTORSIZE_DEF;
  34. float *buffer[NBUFFERS_DEF];
  35. starpu_data_handle_t v_handle[NBUFFERS_DEF];
  36. void dummy_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  37. {
  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 STARPU_ATTRIBUTE_UNUSED)
  68. {
  69. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  70. n_synced_buffers++;
  71. if (n_synced_buffers == nbuffers)
  72. STARPU_PTHREAD_COND_SIGNAL(&cond);
  73. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  74. }
  75. int main(int argc, char **argv)
  76. {
  77. int ret;
  78. #ifdef STARPU_QUICK_CHECK
  79. nbuffers /= 4;
  80. niter /= 4;
  81. vectorsize /= 8;
  82. #endif
  83. ret = starpu_initialize(NULL, &argc, &argv);
  84. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  85. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  86. /* Allocate all buffers and register them to StarPU */
  87. unsigned b;
  88. for (b = 0; b < nbuffers; b++)
  89. {
  90. ret = starpu_malloc((void **)&buffer[b], vectorsize);
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  92. starpu_vector_data_register(&v_handle[b], STARPU_MAIN_RAM,
  93. (uintptr_t)buffer[b], vectorsize, sizeof(char));
  94. }
  95. unsigned iter;
  96. for (iter = 0; iter < niter; iter++)
  97. {
  98. /* Use the buffers on the different workers so that it may not
  99. * be in main memory anymore */
  100. for (b = 0; b < nbuffers; b++)
  101. {
  102. ret = use_handle(v_handle[b]);
  103. if (ret == -ENODEV) goto enodev;
  104. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  105. }
  106. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  107. n_synced_buffers = 0;
  108. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  109. /* Grab the different pieces of data into main memory */
  110. for (b = 0; b < nbuffers; b++)
  111. {
  112. ret = starpu_data_acquire_cb(v_handle[b], STARPU_RW,
  113. callback_sync_data, NULL);
  114. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire_cb");
  115. }
  116. /* Wait for all buffers to be available */
  117. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  118. while (n_synced_buffers != nbuffers)
  119. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  120. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  121. /* Release them */
  122. for (b = 0; b < nbuffers; b++)
  123. starpu_data_release(v_handle[b]);
  124. }
  125. ret = starpu_task_wait_for_all();
  126. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  127. /* do some cleanup */
  128. for (b = 0; b < nbuffers; b++)
  129. {
  130. starpu_data_unregister(v_handle[b]);
  131. starpu_free(buffer[b]);
  132. }
  133. starpu_shutdown();
  134. return EXIT_SUCCESS;
  135. enodev:
  136. fprintf(stderr, "WARNING: No one can execute this task\n");
  137. /* yes, we do not perform the computation but we did detect that no one
  138. * could perform the kernel, so this is not an error from StarPU */
  139. starpu_shutdown();
  140. return STARPU_TEST_SKIPPED;
  141. }