cuda-latency.c 4.3 KB

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