cuda_bandwidth.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #ifndef _GNU_SOURCE
  18. #define _GNU_SOURCE
  19. #endif
  20. #include <sched.h>
  21. #include <cuda.h>
  22. #include <cublas.h>
  23. #include <cblas.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <stdint.h>
  28. #include <pthread.h>
  29. #include <assert.h>
  30. #include <sys/time.h>
  31. int GPU_LD = 2048;
  32. int CPU_LD = 2048;
  33. int MATRIXSIZE = 1024;
  34. int pinned = 0;
  35. int htod = 0;
  36. int ITER = 100;
  37. #define CPUBUFFERSIZE (4*CPU_LD*CPU_LD)
  38. #define GPUBUFFERSIZE (4*GPU_LD*GPU_LD)
  39. float *h_A;
  40. void * d_A;
  41. float *A, *B, *C;
  42. unsigned cuda_initialized = 0;
  43. static void parse_args(int argc, char **argv)
  44. {
  45. int i;
  46. for (i = 1; i < argc; i++)
  47. {
  48. if (strcmp(argv[i], "-gpu-ld") == 0)
  49. {
  50. char *argptr;
  51. GPU_LD = strtol(argv[++i], &argptr, 10);
  52. }
  53. if (strcmp(argv[i], "-cpu-ld") == 0)
  54. {
  55. char *argptr;
  56. CPU_LD = strtol(argv[++i], &argptr, 10);
  57. }
  58. if (strcmp(argv[i], "-size") == 0)
  59. {
  60. char *argptr;
  61. MATRIXSIZE = strtol(argv[++i], &argptr, 10);
  62. }
  63. if (strcmp(argv[i], "-pin") == 0)
  64. {
  65. pinned = 1;
  66. }
  67. if (strcmp(argv[i], "-HtoD") == 0)
  68. {
  69. htod = 1;
  70. }
  71. if (strcmp(argv[i], "-iter") == 0)
  72. {
  73. char *argptr;
  74. ITER = strtol(argv[++i], &argptr, 10);
  75. }
  76. if (strcmp(argv[i], "-h") == 0)
  77. {
  78. printf("usage : %s [-pin] [-HtoD] [-size size] [-cpu-ld ld] [-gpu-ld ld] [-iter n]\n", argv[0]);
  79. }
  80. }
  81. STARPU_ASSERT(CPU_LD >= MATRIXSIZE);
  82. STARPU_ASSERT(GPU_LD >= MATRIXSIZE);
  83. }
  84. void bind_thread(int cpu)
  85. {
  86. /* bind the thread to a cpu */
  87. cpu_set_t mask;
  88. CPU_ZERO(&mask);
  89. CPU_SET(cpu, &mask);
  90. sched_setaffinity(0, sizeof(cpu_set_t), &mask);
  91. }
  92. void benchmark_memcpy(void)
  93. {
  94. unsigned count;
  95. struct timeval tv_start, tv_end;
  96. unsigned long long usecs;
  97. double bytes = 4.0*MATRIXSIZE*MATRIXSIZE*ITER;
  98. cublasInit();
  99. if (pinned)
  100. {
  101. cuMemAllocHost((void **)&h_A, CPUBUFFERSIZE);
  102. }
  103. else
  104. {
  105. h_A = malloc(CPUBUFFERSIZE);
  106. }
  107. STARPU_ASSERT(h_A);
  108. /* malloc a buffer on the device */
  109. cublasAlloc(GPU_LD*GPU_LD, sizeof(float), &d_A);
  110. STARPU_ASSERT(d_A);
  111. gettimeofday(&tv_start, NULL);
  112. if (!pinned)
  113. {
  114. /* pageable memory */
  115. if (!htod)
  116. {
  117. for (count = 0; count < ITER; count++)
  118. {
  119. cublasGetMatrix(MATRIXSIZE, MATRIXSIZE, sizeof(float),
  120. (void *)d_A, GPU_LD, h_A, CPU_LD);
  121. cuCtxSynchronize();
  122. }
  123. }
  124. else
  125. {
  126. for (count = 0; count < ITER; count++)
  127. {
  128. cublasSetMatrix(MATRIXSIZE, MATRIXSIZE, sizeof(float),
  129. h_A, CPU_LD, (void *)d_A, GPU_LD);
  130. cuCtxSynchronize();
  131. }
  132. }
  133. }
  134. else
  135. {
  136. if (!htod)
  137. {
  138. for (count = 0; count < ITER; count++)
  139. {
  140. cublasGetMatrix(MATRIXSIZE, MATRIXSIZE, sizeof(float),
  141. d_A, GPU_LD, h_A, CPU_LD);
  142. cuCtxSynchronize();
  143. }
  144. }
  145. else
  146. {
  147. for (count = 0; count < ITER; count++)
  148. {
  149. cublasSetMatrix(MATRIXSIZE, MATRIXSIZE, sizeof(float),
  150. h_A, CPU_LD, d_A, GPU_LD);
  151. cuCtxSynchronize();
  152. }
  153. }
  154. }
  155. gettimeofday(&tv_end, NULL);
  156. usecs = (tv_end.tv_usec - tv_start.tv_usec) + 1000000*(tv_end.tv_sec - tv_start.tv_sec);
  157. printf("%2.2f\n", bytes/usecs);
  158. if (pinned)
  159. {
  160. cuMemFreeHost(&h_A);
  161. }
  162. else
  163. {
  164. free(h_A);
  165. }
  166. }
  167. int main(int argc, char **argv)
  168. {
  169. parse_args(argc, argv);
  170. bind_thread(0);
  171. // printf("Memcpy alone\n");
  172. benchmark_memcpy();
  173. return EXIT_SUCCESS;
  174. }