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