mpi_like_async.c 5.5 KB

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