sync_with_data_with_mem_non_blocking_implicit.c 4.1 KB

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