mpi_like.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <errno.h>
  18. #include <common/thread.h>
  19. #include "../helper.h"
  20. #include "./mpi_like.h"
  21. /*
  22. * Mimic the behavior of libstarpumpi, tested by a ring of threads which
  23. * increment the same variable one after the other.
  24. * This is the synchronous version: the threads wait for completion of each
  25. * step before continuing.
  26. */
  27. #define NTHREADS 4
  28. #define NITER 2
  29. //static starpu_pthread_cond_t cond;
  30. //static starpu_pthread_mutex_t mutex;
  31. struct thread_data
  32. {
  33. unsigned index;
  34. unsigned val;
  35. starpu_data_handle_t handle;
  36. starpu_pthread_t thread;
  37. starpu_pthread_cond_t recv_cond;
  38. starpu_pthread_mutex_t recv_mutex;
  39. unsigned recv_flag; // set when a message is received
  40. unsigned recv_buf;
  41. struct thread_data *neighbour;
  42. };
  43. static struct thread_data problem_data[NTHREADS];
  44. /* We implement some ring transfer, every thread will try to receive a piece of
  45. * data from its neighbour and increment it before transmitting it to its
  46. * successor. */
  47. void increment_handle_cpu_kernel(void *descr[], void *cl_arg)
  48. {
  49. (void)cl_arg;
  50. unsigned *val = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  51. *val += 1;
  52. }
  53. static struct starpu_codelet increment_handle_cl =
  54. {
  55. .modes = { STARPU_RW },
  56. .cpu_funcs = {increment_handle_cpu_kernel},
  57. #ifdef STARPU_USE_CUDA
  58. .cuda_funcs = {cuda_codelet_unsigned_inc},
  59. .cuda_flags = {STARPU_CUDA_ASYNC},
  60. #endif
  61. #ifdef STARPU_USE_OPENCL
  62. .opencl_funcs = {opencl_codelet_unsigned_inc},
  63. .opencl_flags = {STARPU_OPENCL_ASYNC},
  64. #endif
  65. .cpu_funcs_name = {"increment_handle_cpu_kernel"},
  66. .nbuffers = 1
  67. };
  68. static
  69. void increment_handle(struct thread_data *thread_data)
  70. {
  71. struct starpu_task *task = starpu_task_create();
  72. task->cl = &increment_handle_cl;
  73. task->handles[0] = thread_data->handle;
  74. task->cl_arg = thread_data;
  75. task->cl_arg_size = sizeof(thread_data);
  76. task->destroy = 1;
  77. task->detach = 0;
  78. int ret = starpu_task_submit(task);
  79. if (ret == -ENODEV)
  80. exit(STARPU_TEST_SKIPPED);
  81. STARPU_ASSERT(!ret);
  82. ret = starpu_task_wait(task);
  83. STARPU_ASSERT(!ret);
  84. }
  85. static void recv_handle(struct thread_data *thread_data)
  86. {
  87. starpu_data_acquire(thread_data->handle, STARPU_W);
  88. STARPU_PTHREAD_MUTEX_LOCK(&thread_data->recv_mutex);
  89. /* We wait for the previous thread to notify that the data is available */
  90. while (!thread_data->recv_flag)
  91. STARPU_PTHREAD_COND_WAIT(&thread_data->recv_cond, &thread_data->recv_mutex);
  92. /* We overwrite thread's data with the received value */
  93. thread_data->val = thread_data->recv_buf;
  94. /* Notify that we read the value */
  95. thread_data->recv_flag = 0;
  96. STARPU_PTHREAD_COND_SIGNAL(&thread_data->recv_cond);
  97. // FPRINTF(stderr, "Thread %d received value %d from thread %d\n", thread_data->index, thread_data->val, (thread_data->index - 1)%NTHREADS);
  98. STARPU_PTHREAD_MUTEX_UNLOCK(&thread_data->recv_mutex);
  99. starpu_data_release(thread_data->handle);
  100. }
  101. static void send_handle(struct thread_data *thread_data)
  102. {
  103. struct thread_data *neighbour_data = thread_data->neighbour;
  104. starpu_data_acquire(thread_data->handle, STARPU_R);
  105. // FPRINTF(stderr, "Thread %d sends value %d to thread %d\n", thread_data->index, thread_data->val, neighbour_data->index);
  106. /* send the message */
  107. STARPU_PTHREAD_MUTEX_LOCK(&neighbour_data->recv_mutex);
  108. neighbour_data->recv_buf = thread_data->val;
  109. neighbour_data->recv_flag = 1;
  110. STARPU_PTHREAD_COND_SIGNAL(&neighbour_data->recv_cond);
  111. /* wait until it's received (ie. neighbour's recv_flag is set back to 0) */
  112. while (neighbour_data->recv_flag)
  113. STARPU_PTHREAD_COND_WAIT(&neighbour_data->recv_cond, &neighbour_data->recv_mutex);
  114. STARPU_PTHREAD_MUTEX_UNLOCK(&neighbour_data->recv_mutex);
  115. starpu_data_release(thread_data->handle);
  116. }
  117. static void *thread_func(void *arg)
  118. {
  119. unsigned iter;
  120. struct thread_data *thread_data = (struct thread_data *) arg;
  121. unsigned index = thread_data->index;
  122. starpu_variable_data_register(&thread_data->handle, STARPU_MAIN_RAM, (uintptr_t)&thread_data->val, sizeof(unsigned));
  123. for (iter = 0; iter < NITER; iter++)
  124. {
  125. /* The first thread initiates the first transfer */
  126. if (!((index == 0) && (iter == 0)))
  127. {
  128. recv_handle(thread_data);
  129. }
  130. increment_handle(thread_data);
  131. if (!((index == (NTHREADS - 1)) && (iter == (NITER - 1))))
  132. {
  133. send_handle(thread_data);
  134. }
  135. }
  136. return NULL;
  137. }
  138. #ifdef STARPU_USE_OPENCL
  139. struct starpu_opencl_program opencl_program;
  140. #endif
  141. int main(int argc, char **argv)
  142. {
  143. int ret;
  144. ret = starpu_initialize(NULL, &argc, &argv);
  145. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  146. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  147. #ifdef STARPU_USE_OPENCL
  148. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/opencl_codelet_unsigned_inc_kernel.cl",
  149. &opencl_program, NULL);
  150. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  151. #endif
  152. unsigned t;
  153. for (t = 0; t < NTHREADS; t++)
  154. {
  155. problem_data[t].index = t;
  156. problem_data[t].val = 0;
  157. STARPU_PTHREAD_COND_INIT(&problem_data[t].recv_cond, NULL);
  158. STARPU_PTHREAD_MUTEX_INIT(&problem_data[t].recv_mutex, NULL);
  159. problem_data[t].recv_flag = 0;
  160. problem_data[t].neighbour = &problem_data[(t+1)%NTHREADS];
  161. }
  162. for (t = 0; t < NTHREADS; t++)
  163. {
  164. STARPU_PTHREAD_CREATE(&problem_data[t].thread, NULL, thread_func, &problem_data[t]);
  165. }
  166. for (t = 0; t < NTHREADS; t++)
  167. {
  168. void *retval;
  169. STARPU_PTHREAD_JOIN(problem_data[t].thread, &retval);
  170. STARPU_ASSERT(retval == NULL);
  171. }
  172. /* We check that the value in the "last" thread is valid */
  173. starpu_data_handle_t last_handle = problem_data[NTHREADS - 1].handle;
  174. starpu_data_acquire(last_handle, STARPU_R);
  175. starpu_data_release(last_handle);
  176. for (t = 0; t < NTHREADS; t++)
  177. {
  178. starpu_data_unregister(problem_data[t].handle);
  179. }
  180. #ifdef STARPU_USE_OPENCL
  181. ret = starpu_opencl_unload_opencl(&opencl_program);
  182. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  183. #endif
  184. starpu_shutdown();
  185. ret = EXIT_SUCCESS;
  186. if (problem_data[NTHREADS - 1].val != (NTHREADS * NITER))
  187. {
  188. FPRINTF(stderr, "Final value : %u should be %d\n", problem_data[NTHREADS - 1].val, (NTHREADS * NITER));
  189. ret = EXIT_FAILURE;
  190. }
  191. return ret;
  192. }