mpi_like.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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 <starpu.h>
  18. #include <errno.h>
  19. #include <pthread.h>
  20. #include "../common/helper.h"
  21. #define NTHREADS 4
  22. #define NITER 128
  23. //static pthread_cond_t cond;
  24. //static pthread_mutex_t mutex;
  25. struct thread_data {
  26. unsigned index;
  27. unsigned val;
  28. starpu_data_handle handle;
  29. pthread_t thread;
  30. pthread_cond_t recv_cond;
  31. pthread_mutex_t recv_mutex;
  32. unsigned recv_flag; // set when a message is received
  33. unsigned recv_buf;
  34. struct thread_data *neighbour;
  35. };
  36. static struct thread_data problem_data[NTHREADS];
  37. /* We implement some ring transfer, every thread will try to receive a piece of
  38. * data from its neighbour and increment it before transmitting it to its
  39. * successor. */
  40. #ifdef STARPU_USE_CUDA
  41. void cuda_codelet_unsigned_inc(void *descr[], __attribute__ ((unused)) void *cl_arg);
  42. #endif
  43. static void increment_handle_cpu_kernel(void *descr[], void *cl_arg __attribute__((unused)))
  44. {
  45. unsigned *val = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  46. *val += 1;
  47. }
  48. static starpu_codelet increment_handle_cl = {
  49. .where = STARPU_CPU|STARPU_CUDA,
  50. .cpu_func = increment_handle_cpu_kernel,
  51. #ifdef STARPU_USE_CUDA
  52. .cuda_func = cuda_codelet_unsigned_inc,
  53. #endif
  54. .nbuffers = 1
  55. };
  56. static void increment_handle(struct thread_data *thread_data)
  57. {
  58. struct starpu_task *task = starpu_task_create();
  59. task->cl = &increment_handle_cl;
  60. task->buffers[0].handle = thread_data->handle;
  61. task->buffers[0].mode = STARPU_RW;
  62. task->cl_arg = thread_data;
  63. task->destroy = 1;
  64. task->detach = 0;
  65. int ret = starpu_task_submit(task);
  66. STARPU_ASSERT(!ret);
  67. ret = starpu_task_wait(task);
  68. STARPU_ASSERT(!ret);
  69. }
  70. static void recv_handle(struct thread_data *thread_data)
  71. {
  72. starpu_data_acquire(thread_data->handle, STARPU_W);
  73. PTHREAD_MUTEX_LOCK(&thread_data->recv_mutex);
  74. /* We wait for the previous thread to notify that the data is available */
  75. while (!thread_data->recv_flag)
  76. PTHREAD_COND_WAIT(&thread_data->recv_cond, &thread_data->recv_mutex);
  77. /* We overwrite thread's data with the received value */
  78. thread_data->val = thread_data->recv_buf;
  79. /* Notify that we read the value */
  80. thread_data->recv_flag = 0;
  81. PTHREAD_COND_SIGNAL(&thread_data->recv_cond);
  82. // FPRINTF(stderr, "Thread %d received value %d from thread %d\n", thread_data->index, thread_data->val, (thread_data->index - 1)%NTHREADS);
  83. PTHREAD_MUTEX_UNLOCK(&thread_data->recv_mutex);
  84. starpu_data_release(thread_data->handle);
  85. }
  86. static void send_handle(struct thread_data *thread_data)
  87. {
  88. struct thread_data *neighbour_data = thread_data->neighbour;
  89. starpu_data_acquire(thread_data->handle, STARPU_R);
  90. // FPRINTF(stderr, "Thread %d sends value %d to thread %d\n", thread_data->index, thread_data->val, neighbour_data->index);
  91. /* send the message */
  92. PTHREAD_MUTEX_LOCK(&neighbour_data->recv_mutex);
  93. neighbour_data->recv_buf = thread_data->val;
  94. neighbour_data->recv_flag = 1;
  95. PTHREAD_COND_SIGNAL(&neighbour_data->recv_cond);
  96. /* wait until it's received (ie. neighbour's recv_flag is set back to 0) */
  97. while (neighbour_data->recv_flag)
  98. PTHREAD_COND_WAIT(&neighbour_data->recv_cond, &neighbour_data->recv_mutex);
  99. PTHREAD_MUTEX_UNLOCK(&neighbour_data->recv_mutex);
  100. starpu_data_release(thread_data->handle);
  101. }
  102. static void *thread_func(void *arg)
  103. {
  104. unsigned iter;
  105. struct thread_data *thread_data = (struct thread_data *) arg;
  106. unsigned index = thread_data->index;
  107. starpu_variable_data_register(&thread_data->handle, 0, (uintptr_t)&thread_data->val, sizeof(unsigned));
  108. for (iter = 0; iter < NITER; iter++)
  109. {
  110. /* The first thread initiates the first transfer */
  111. if (!((index == 0) && (iter == 0)))
  112. {
  113. recv_handle(thread_data);
  114. }
  115. increment_handle(thread_data);
  116. if (!((index == (NTHREADS - 1)) && (iter == (NITER - 1))))
  117. {
  118. send_handle(thread_data);
  119. }
  120. }
  121. return NULL;
  122. }
  123. int main(int argc, char **argv)
  124. {
  125. int ret;
  126. ret = starpu_init(NULL);
  127. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  128. unsigned t;
  129. for (t = 0; t < NTHREADS; t++)
  130. {
  131. problem_data[t].index = t;
  132. problem_data[t].val = 0;
  133. PTHREAD_COND_INIT(&problem_data[t].recv_cond, NULL);
  134. PTHREAD_MUTEX_INIT(&problem_data[t].recv_mutex, NULL);
  135. problem_data[t].recv_flag = 0;
  136. problem_data[t].neighbour = &problem_data[(t+1)%NTHREADS];
  137. }
  138. for (t = 0; t < NTHREADS; t++)
  139. {
  140. ret = pthread_create(&problem_data[t].thread, NULL, thread_func, &problem_data[t]);
  141. STARPU_ASSERT(!ret);
  142. }
  143. for (t = 0; t < NTHREADS; t++)
  144. {
  145. void *retval;
  146. ret = pthread_join(problem_data[t].thread, &retval);
  147. STARPU_ASSERT(!ret);
  148. STARPU_ASSERT(retval == NULL);
  149. }
  150. /* We check that the value in the "last" thread is valid */
  151. starpu_data_handle last_handle = problem_data[NTHREADS - 1].handle;
  152. starpu_data_acquire(last_handle, STARPU_R);
  153. if (problem_data[NTHREADS - 1].val != (NTHREADS * NITER))
  154. {
  155. FPRINTF(stderr, "Final value : %u should be %d\n", problem_data[NTHREADS - 1].val, (NTHREADS * NITER));
  156. STARPU_ABORT();
  157. }
  158. starpu_data_release(last_handle);
  159. for (t = 0; t < NTHREADS; t++)
  160. {
  161. starpu_data_unregister(problem_data[t].handle);
  162. }
  163. starpu_shutdown();
  164. return EXIT_SUCCESS;
  165. }