mpi_like.c 6.5 KB

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