sync_with_data_with_mem_non_blocking_implicit.c 4.1 KB

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