mpi_like.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <config.h>
  18. #include <starpu.h>
  19. #include <errno.h>
  20. #include <pthread.h>
  21. #include "../helper.h"
  22. #define NTHREADS 4
  23. #define NITER 2
  24. //static pthread_cond_t cond;
  25. //static pthread_mutex_t mutex;
  26. struct thread_data
  27. {
  28. unsigned index;
  29. unsigned val;
  30. starpu_data_handle_t handle;
  31. pthread_t thread;
  32. _starpu_pthread_cond_t recv_cond;
  33. _starpu_pthread_mutex_t recv_mutex;
  34. unsigned recv_flag; // set when a message is received
  35. unsigned recv_buf;
  36. struct thread_data *neighbour;
  37. };
  38. static struct thread_data problem_data[NTHREADS];
  39. /* We implement some ring transfer, every thread will try to receive a piece of
  40. * data from its neighbour and increment it before transmitting it to its
  41. * successor. */
  42. #ifdef STARPU_USE_CUDA
  43. void cuda_codelet_unsigned_inc(void *descr[], __attribute__ ((unused)) void *cl_arg);
  44. #endif
  45. #ifdef STARPU_USE_OPENCL
  46. void opencl_codelet_unsigned_inc(void *buffers[], void *args);
  47. #endif
  48. static void increment_handle_cpu_kernel(void *descr[], void *cl_arg __attribute__((unused)))
  49. {
  50. STARPU_SKIP_IF_VALGRIND;
  51. unsigned *val = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  52. *val += 1;
  53. }
  54. static struct starpu_codelet increment_handle_cl =
  55. {
  56. .modes = { STARPU_RW },
  57. .cpu_funcs = {increment_handle_cpu_kernel, NULL},
  58. #ifdef STARPU_USE_CUDA
  59. .cuda_funcs = {cuda_codelet_unsigned_inc, NULL},
  60. #endif
  61. #ifdef STARPU_USE_OPENCL
  62. .opencl_funcs = {opencl_codelet_unsigned_inc, NULL},
  63. #endif
  64. .nbuffers = 1
  65. };
  66. static void increment_handle(struct thread_data *thread_data)
  67. {
  68. struct starpu_task *task = starpu_task_create();
  69. task->cl = &increment_handle_cl;
  70. task->handles[0] = thread_data->handle;
  71. task->cl_arg = thread_data;
  72. task->destroy = 1;
  73. task->detach = 0;
  74. int ret = starpu_task_submit(task);
  75. if (ret == -ENODEV)
  76. exit(STARPU_TEST_SKIPPED);
  77. STARPU_ASSERT(!ret);
  78. ret = starpu_task_wait(task);
  79. STARPU_ASSERT(!ret);
  80. }
  81. static void recv_handle(struct thread_data *thread_data)
  82. {
  83. starpu_data_acquire(thread_data->handle, STARPU_W);
  84. _STARPU_PTHREAD_MUTEX_LOCK(&thread_data->recv_mutex);
  85. /* We wait for the previous thread to notify that the data is available */
  86. while (!thread_data->recv_flag)
  87. _STARPU_PTHREAD_COND_WAIT(&thread_data->recv_cond, &thread_data->recv_mutex);
  88. /* We overwrite thread's data with the received value */
  89. thread_data->val = thread_data->recv_buf;
  90. /* Notify that we read the value */
  91. thread_data->recv_flag = 0;
  92. _STARPU_PTHREAD_COND_SIGNAL(&thread_data->recv_cond);
  93. // FPRINTF(stderr, "Thread %d received value %d from thread %d\n", thread_data->index, thread_data->val, (thread_data->index - 1)%NTHREADS);
  94. _STARPU_PTHREAD_MUTEX_UNLOCK(&thread_data->recv_mutex);
  95. starpu_data_release(thread_data->handle);
  96. }
  97. static void send_handle(struct thread_data *thread_data)
  98. {
  99. struct thread_data *neighbour_data = thread_data->neighbour;
  100. starpu_data_acquire(thread_data->handle, STARPU_R);
  101. // FPRINTF(stderr, "Thread %d sends value %d to thread %d\n", thread_data->index, thread_data->val, neighbour_data->index);
  102. /* send the message */
  103. _STARPU_PTHREAD_MUTEX_LOCK(&neighbour_data->recv_mutex);
  104. neighbour_data->recv_buf = thread_data->val;
  105. neighbour_data->recv_flag = 1;
  106. _STARPU_PTHREAD_COND_SIGNAL(&neighbour_data->recv_cond);
  107. /* wait until it's received (ie. neighbour's recv_flag is set back to 0) */
  108. while (neighbour_data->recv_flag)
  109. _STARPU_PTHREAD_COND_WAIT(&neighbour_data->recv_cond, &neighbour_data->recv_mutex);
  110. _STARPU_PTHREAD_MUTEX_UNLOCK(&neighbour_data->recv_mutex);
  111. starpu_data_release(thread_data->handle);
  112. }
  113. static void *thread_func(void *arg)
  114. {
  115. unsigned iter;
  116. struct thread_data *thread_data = (struct thread_data *) arg;
  117. unsigned index = thread_data->index;
  118. starpu_variable_data_register(&thread_data->handle, 0, (uintptr_t)&thread_data->val, sizeof(unsigned));
  119. for (iter = 0; iter < NITER; iter++)
  120. {
  121. /* The first thread initiates the first transfer */
  122. if (!((index == 0) && (iter == 0)))
  123. {
  124. recv_handle(thread_data);
  125. }
  126. increment_handle(thread_data);
  127. if (!((index == (NTHREADS - 1)) && (iter == (NITER - 1))))
  128. {
  129. send_handle(thread_data);
  130. }
  131. }
  132. return NULL;
  133. }
  134. #ifdef STARPU_USE_OPENCL
  135. struct starpu_opencl_program opencl_program;
  136. #endif
  137. int main(int argc, char **argv)
  138. {
  139. int ret;
  140. ret = starpu_init(NULL);
  141. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  142. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  143. #ifdef STARPU_USE_OPENCL
  144. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/opencl_codelet_unsigned_inc_kernel.cl",
  145. &opencl_program, NULL);
  146. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  147. #endif
  148. unsigned t;
  149. for (t = 0; t < NTHREADS; t++)
  150. {
  151. problem_data[t].index = t;
  152. problem_data[t].val = 0;
  153. _STARPU_PTHREAD_COND_INIT(&problem_data[t].recv_cond, NULL);
  154. _STARPU_PTHREAD_MUTEX_INIT(&problem_data[t].recv_mutex, NULL);
  155. problem_data[t].recv_flag = 0;
  156. problem_data[t].neighbour = &problem_data[(t+1)%NTHREADS];
  157. }
  158. for (t = 0; t < NTHREADS; t++)
  159. {
  160. ret = pthread_create(&problem_data[t].thread, NULL, thread_func, &problem_data[t]);
  161. STARPU_ASSERT(!ret);
  162. }
  163. for (t = 0; t < NTHREADS; t++)
  164. {
  165. void *retval;
  166. ret = pthread_join(problem_data[t].thread, &retval);
  167. STARPU_ASSERT(!ret);
  168. STARPU_ASSERT(retval == NULL);
  169. }
  170. /* We check that the value in the "last" thread is valid */
  171. starpu_data_handle_t last_handle = problem_data[NTHREADS - 1].handle;
  172. starpu_data_acquire(last_handle, STARPU_R);
  173. starpu_data_release(last_handle);
  174. for (t = 0; t < NTHREADS; t++)
  175. {
  176. starpu_data_unregister(problem_data[t].handle);
  177. }
  178. #ifdef STARPU_USE_OPENCL
  179. ret = starpu_opencl_unload_opencl(&opencl_program);
  180. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  181. #endif
  182. starpu_shutdown();
  183. ret = EXIT_SUCCESS;
  184. if (problem_data[NTHREADS - 1].val != (NTHREADS * NITER))
  185. {
  186. FPRINTF(stderr, "Final value : %u should be %d\n", problem_data[NTHREADS - 1].val, (NTHREADS * NITER));
  187. ret = EXIT_FAILURE;
  188. }
  189. STARPU_RETURN(ret);
  190. }