cuda_latency.c 4.7 KB

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