cuda_latency.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 <stdio.h>
  17. #include <cuda.h>
  18. #include <cuda_runtime.h>
  19. #include <assert.h>
  20. #include <sys/types.h>
  21. static starpu_pthread_t thread[2];
  22. static unsigned thread_is_initialized[2];
  23. static starpu_pthread_cond_t cond;
  24. static starpu_pthread_mutex_t mutex;
  25. static size_t buffer_size = 4;
  26. static void *cpu_buffer;
  27. static void *gpu_buffer[2];
  28. static starpu_pthread_cond_t cond_go;
  29. static unsigned ready = 0;
  30. static unsigned nready_gpu = 0;
  31. static unsigned niter = 250000;
  32. static starpu_pthread_cond_t cond_gpu;
  33. static starpu_pthread_mutex_t mutex_gpu;
  34. static unsigned data_is_available[2];
  35. static cudaStream_t stream[2];
  36. #define ASYNC 1
  37. #define DO_TRANSFER_GPU_TO_RAM 1
  38. #define DO_TRANSFER_RAM_TO_GPU 1
  39. void send_data(unsigned src, unsigned dst)
  40. {
  41. cudaError_t cures;
  42. /* Copy data from GPU to RAM */
  43. #ifdef DO_TRANSFER_GPU_TO_RAM
  44. #ifdef ASYNC
  45. cures = cudaMemcpyAsync(cpu_buffer, gpu_buffer[src], buffer_size, cudaMemcpyDeviceToHost, stream[src]);
  46. STARPU_ASSERT(!cures);
  47. cures = cudaStreamSynchronize(stream[src]);
  48. STARPU_ASSERT(!cures);
  49. #else
  50. cures = cudaMemcpy(cpu_buffer, gpu_buffer[src], buffer_size, cudaMemcpyDeviceToHost);
  51. STARPU_ASSERT(!cures);
  52. cures = cudaDeviceSynchronize();
  53. STARPU_ASSERT(!cures);
  54. #endif
  55. #endif
  56. /* Tell the other GPU that data is in RAM */
  57. STARPU_PTHREAD_MUTEX_LOCK(&mutex_gpu);
  58. data_is_available[src] = 0;
  59. data_is_available[dst] = 1;
  60. STARPU_PTHREAD_COND_SIGNAL(&cond_gpu);
  61. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex_gpu);
  62. //fprintf(stderr, "SEND on %d\n", src);
  63. }
  64. void recv_data(unsigned src, unsigned dst)
  65. {
  66. cudaError_t cures;
  67. /* Wait for the data to be in RAM */
  68. STARPU_PTHREAD_MUTEX_LOCK(&mutex_gpu);
  69. while (!data_is_available[dst])
  70. {
  71. STARPU_PTHREAD_COND_WAIT(&cond_gpu, &mutex_gpu);
  72. }
  73. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex_gpu);
  74. //fprintf(stderr, "RECV on %d\n", dst);
  75. /* Upload data */
  76. #ifdef DO_TRANSFER_RAM_TO_GPU
  77. #ifdef ASYNC
  78. cures = cudaMemcpyAsync(gpu_buffer[dst], cpu_buffer, buffer_size, cudaMemcpyHostToDevice, stream[dst]);
  79. STARPU_ASSERT(!cures);
  80. cures = cudaStreamSynchronize(stream[dst]);
  81. STARPU_ASSERT(!cures);
  82. #else
  83. cures = cudaMemcpy(gpu_buffer[dst], cpu_buffer, buffer_size, cudaMemcpyHostToDevice);
  84. STARPU_ASSERT(!cures);
  85. cures = cudaDeviceSynchronize();
  86. STARPU_ASSERT(!cures);
  87. #endif
  88. #endif
  89. }
  90. void *launch_gpu_thread(void *arg)
  91. {
  92. unsigned *idptr = arg;
  93. unsigned id = *idptr;
  94. starpu_cuda_set_device(id);
  95. cudaFree(0);
  96. cudaMalloc(&gpu_buffer[id], buffer_size);
  97. cudaStreamCreate(&stream[id]);
  98. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  99. thread_is_initialized[id] = 1;
  100. STARPU_PTHREAD_COND_SIGNAL(&cond);
  101. if (id == 0)
  102. {
  103. cudaError_t cures;
  104. cures = cudaHostAlloc(&cpu_buffer, buffer_size, cudaHostAllocPortable);
  105. STARPU_ASSERT(!cures);
  106. cudaDeviceSynchronize();
  107. }
  108. nready_gpu++;
  109. while (!ready)
  110. STARPU_PTHREAD_COND_WAIT(&cond_go, &mutex);
  111. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  112. unsigned iter;
  113. for (iter = 0; iter < niter; iter++)
  114. {
  115. if (id == 0)
  116. {
  117. send_data(0, 1);
  118. recv_data(1, 0);
  119. }
  120. else
  121. {
  122. recv_data(0, 1);
  123. send_data(1, 0);
  124. }
  125. }
  126. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  127. nready_gpu--;
  128. STARPU_PTHREAD_COND_SIGNAL(&cond_go);
  129. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  130. return NULL;
  131. }
  132. int main(int argc, char **argv)
  133. {
  134. STARPU_PTHREAD_MUTEX_INIT(&mutex, NULL);
  135. STARPU_PTHREAD_COND_INIT(&cond, NULL);
  136. STARPU_PTHREAD_COND_INIT(&cond_go, NULL);
  137. unsigned id;
  138. for (id = 0; id < 2; id++)
  139. {
  140. thread_is_initialized[id] = 0;
  141. STARPU_PTHREAD_CREATE(&thread[0], NULL, launch_gpu_thread, &id);
  142. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  143. while (!thread_is_initialized[id])
  144. {
  145. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  146. }
  147. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  148. }
  149. double start;
  150. double end;
  151. /* Start the ping pong */
  152. start = starpu_timing_now();
  153. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  154. ready = 1;
  155. STARPU_PTHREAD_COND_BROADCAST(&cond_go);
  156. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  157. /* Wait for the end of the ping pong */
  158. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  159. while (nready_gpu > 0)
  160. {
  161. STARPU_PTHREAD_COND_WAIT(&cond_go, &mutex);
  162. }
  163. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  164. end = starpu_timing_now();
  165. double timing = end - start;
  166. fprintf(stderr, "Took %.0f ms for %u iterations\n", timing/1000, niter);
  167. fprintf(stderr, "Latency: %.2f us\n", timing/(2*niter));
  168. return EXIT_SUCCESS;
  169. }