cuda-latency.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <pthread.h>
  17. #include <stdio.h>
  18. #include <cuda.h>
  19. #include <cuda_runtime.h>
  20. #include <assert.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include <sys/types.h>
  24. #include <sys/time.h>
  25. #include <pthread.h>
  26. #include <signal.h>
  27. static pthread_t thread[2];
  28. static unsigned thread_is_initialized[2];
  29. static pthread_cond_t cond;
  30. static pthread_mutex_t mutex;
  31. static size_t buffer_size = 1;
  32. static void *cpu_buffer;
  33. static void *gpu_buffer[2];
  34. static pthread_cond_t cond_go;
  35. static unsigned ready = 0;
  36. static unsigned nready_gpu = 0;
  37. static unsigned niter = 100000;
  38. static pthread_cond_t cond_gpu;
  39. static pthread_mutex_t mutex_gpu;
  40. static unsigned data_is_available[2];
  41. static cudaStream_t stream[2];
  42. #define ASYNC 1
  43. void send_data(unsigned src, unsigned dst)
  44. {
  45. cudaError_t cures;
  46. /* Copy data from GPU to RAM */
  47. #ifdef ASYNC
  48. cures = cudaMemcpyAsync(cpu_buffer, gpu_buffer[src], buffer_size, cudaMemcpyDeviceToHost, stream[src]);
  49. assert(!cures);
  50. cures = cudaStreamSynchronize(stream[src]);
  51. assert(!cures);
  52. #else
  53. cures = cudaMemcpy(cpu_buffer, gpu_buffer[src], buffer_size, cudaMemcpyDeviceToHost);
  54. assert(!cures);
  55. cures = cudaThreadSynchronize();
  56. assert(!cures);
  57. #endif
  58. /* Tell the other GPU that data is in RAM */
  59. pthread_mutex_lock(&mutex_gpu);
  60. data_is_available[src] = 0;
  61. data_is_available[dst] = 1;
  62. pthread_cond_signal(&cond_gpu);
  63. pthread_mutex_unlock(&mutex_gpu);
  64. //fprintf(stderr, "SEND on %d\n", src);
  65. }
  66. void recv_data(unsigned src, unsigned dst)
  67. {
  68. cudaError_t cures;
  69. /* Wait for the data to be in RAM */
  70. pthread_mutex_lock(&mutex_gpu);
  71. while (!data_is_available[dst])
  72. {
  73. pthread_cond_wait(&cond_gpu, &mutex_gpu);
  74. }
  75. pthread_mutex_unlock(&mutex_gpu);
  76. //fprintf(stderr, "RECV on %d\n", dst);
  77. /* Upload data */
  78. #ifdef ASYNC
  79. cures = cudaMemcpyAsync(gpu_buffer[dst], cpu_buffer, buffer_size, cudaMemcpyDeviceToHost, stream[dst]);
  80. assert(!cures);
  81. cures = cudaThreadSynchronize();
  82. assert(!cures);
  83. #else
  84. cures = cudaMemcpy(gpu_buffer[dst], cpu_buffer, buffer_size, cudaMemcpyHostToDevice);
  85. assert(!cures);
  86. cures = cudaThreadSynchronize();
  87. assert(!cures);
  88. #endif
  89. }
  90. void *launch_gpu_thread(void *arg)
  91. {
  92. unsigned *idptr = arg;
  93. unsigned id = *idptr;
  94. fprintf(stderr, "Initialize device %d\n", id);
  95. cudaSetDevice(id);
  96. cudaFree(0);
  97. cudaMalloc(&gpu_buffer[id], buffer_size);
  98. cudaStreamCreate(&stream[id]);
  99. pthread_mutex_lock(&mutex);
  100. thread_is_initialized[id] = 1;
  101. pthread_cond_signal(&cond);
  102. nready_gpu++;
  103. while (!ready)
  104. pthread_cond_wait(&cond_go, &mutex);
  105. pthread_mutex_unlock(&mutex);
  106. fprintf(stderr, "Device %d GOGO\n", id);
  107. unsigned iter;
  108. for (iter = 0; iter < niter; iter++)
  109. {
  110. if (id == 0) {
  111. send_data(0, 1);
  112. recv_data(1, 0);
  113. }
  114. else {
  115. recv_data(0, 1);
  116. send_data(1, 0);
  117. }
  118. }
  119. pthread_mutex_lock(&mutex);
  120. nready_gpu--;
  121. pthread_cond_signal(&cond_go);
  122. pthread_mutex_unlock(&mutex);
  123. return NULL;
  124. }
  125. int main(int argc, char **argv)
  126. {
  127. pthread_mutex_init(&mutex, NULL);
  128. pthread_cond_init(&cond, NULL);
  129. pthread_cond_init(&cond_go, NULL);
  130. cudaError_t cures;
  131. cures = cudaHostAlloc(&cpu_buffer, buffer_size, cudaHostAllocPortable);
  132. assert(!cures);
  133. unsigned id;
  134. for (id = 0; id < 2; id++)
  135. {
  136. thread_is_initialized[id] = 0;
  137. pthread_create(&thread[0], NULL, launch_gpu_thread, &id);
  138. pthread_mutex_lock(&mutex);
  139. while (!thread_is_initialized[id])
  140. {
  141. pthread_cond_wait(&cond, &mutex);
  142. }
  143. pthread_mutex_unlock(&mutex);
  144. }
  145. struct timeval start;
  146. struct timeval end;
  147. /* Start the ping pong */
  148. gettimeofday(&start, NULL);
  149. pthread_mutex_lock(&mutex);
  150. ready = 1;
  151. pthread_cond_broadcast(&cond_go);
  152. pthread_mutex_unlock(&mutex);
  153. /* Wait for the end of the ping pong */
  154. pthread_mutex_lock(&mutex);
  155. while (nready_gpu > 0)
  156. {
  157. pthread_cond_wait(&cond_go, &mutex);
  158. }
  159. pthread_mutex_unlock(&mutex);
  160. gettimeofday(&end, NULL);
  161. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 +
  162. (end.tv_usec - start.tv_usec));
  163. fprintf(stderr, "Took %.0f ms for %d iterations\n", timing/1000, niter);
  164. fprintf(stderr, "Latency: %.2f us\n", timing/(2*niter));
  165. return 0;
  166. }