cuda_bandwidth.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 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. #ifndef _GNU_SOURCE
  17. #define _GNU_SOURCE
  18. #endif
  19. #include <sched.h>
  20. #include <cuda.h>
  21. #include <cublas.h>
  22. #include <cblas.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdint.h>
  27. #include <pthread.h>
  28. #include <assert.h>
  29. #include <sys/time.h>
  30. int GPU_LD = 2048;
  31. int CPU_LD = 2048;
  32. int MATRIXSIZE = 1024;
  33. int pinned = 0;
  34. int htod = 0;
  35. int ITER = 100;
  36. #define CPUBUFFERSIZE (4*CPU_LD*CPU_LD)
  37. #define GPUBUFFERSIZE (4*GPU_LD*GPU_LD)
  38. float *h_A;
  39. void * d_A;
  40. float *A, *B, *C;
  41. unsigned cuda_initialized = 0;
  42. static void parse_args(int argc, char **argv)
  43. {
  44. int i;
  45. for (i = 1; i < argc; i++) {
  46. if (strcmp(argv[i], "-gpu-ld") == 0) {
  47. char *argptr;
  48. GPU_LD = strtol(argv[++i], &argptr, 10);
  49. }
  50. if (strcmp(argv[i], "-cpu-ld") == 0) {
  51. char *argptr;
  52. CPU_LD = strtol(argv[++i], &argptr, 10);
  53. }
  54. if (strcmp(argv[i], "-size") == 0) {
  55. char *argptr;
  56. MATRIXSIZE = strtol(argv[++i], &argptr, 10);
  57. }
  58. if (strcmp(argv[i], "-pin") == 0) {
  59. pinned = 1;
  60. }
  61. if (strcmp(argv[i], "-HtoD") == 0) {
  62. htod = 1;
  63. }
  64. if (strcmp(argv[i], "-iter") == 0) {
  65. char *argptr;
  66. ITER = strtol(argv[++i], &argptr, 10);
  67. }
  68. if (strcmp(argv[i], "-h") == 0) {
  69. printf("usage : %s [-pin] [-HtoD] [-size size] [-cpu-ld ld] [-gpu-ld ld] [-iter n]\n", argv[0]);
  70. }
  71. }
  72. assert(CPU_LD >= MATRIXSIZE);
  73. assert(GPU_LD >= MATRIXSIZE);
  74. }
  75. void bind_thread(int cpu)
  76. {
  77. /* bind the thread to a cpu */
  78. cpu_set_t mask;
  79. CPU_ZERO(&mask);
  80. CPU_SET(cpu, &mask);
  81. sched_setaffinity(0, sizeof(cpu_set_t), &mask);
  82. }
  83. void benchmark_memcpy(void)
  84. {
  85. unsigned count;
  86. struct timeval tv_start, tv_end;
  87. unsigned long long usecs;
  88. double bytes = 4.0*MATRIXSIZE*MATRIXSIZE*ITER;
  89. cublasInit();
  90. if (pinned)
  91. {
  92. cuMemAllocHost((void **)&h_A, CPUBUFFERSIZE);
  93. }
  94. else
  95. {
  96. h_A = malloc(CPUBUFFERSIZE);
  97. }
  98. assert(h_A);
  99. /* malloc a buffer on the device */
  100. cublasAlloc(GPU_LD*GPU_LD, sizeof(float), &d_A);
  101. assert(d_A);
  102. gettimeofday(&tv_start, NULL);
  103. if (!pinned)
  104. {
  105. /* pageable memory */
  106. if (!htod)
  107. {
  108. for (count = 0; count < ITER; count++)
  109. {
  110. cublasGetMatrix(MATRIXSIZE, MATRIXSIZE, sizeof(float),
  111. (void *)d_A, GPU_LD, h_A, CPU_LD);
  112. cuCtxSynchronize();
  113. }
  114. }
  115. else
  116. {
  117. for (count = 0; count < ITER; count++)
  118. {
  119. cublasSetMatrix(MATRIXSIZE, MATRIXSIZE, sizeof(float),
  120. h_A, CPU_LD, (void *)d_A, GPU_LD);
  121. cuCtxSynchronize();
  122. }
  123. }
  124. }
  125. else
  126. {
  127. if (!htod)
  128. {
  129. for (count = 0; count < ITER; count++)
  130. {
  131. cublasGetMatrix(MATRIXSIZE, MATRIXSIZE, sizeof(float),
  132. d_A, GPU_LD, h_A, CPU_LD);
  133. cuCtxSynchronize();
  134. }
  135. }
  136. else {
  137. for (count = 0; count < ITER; count++)
  138. {
  139. cublasSetMatrix(MATRIXSIZE, MATRIXSIZE, sizeof(float),
  140. h_A, CPU_LD, d_A, GPU_LD);
  141. cuCtxSynchronize();
  142. }
  143. }
  144. }
  145. gettimeofday(&tv_end, NULL);
  146. usecs = (tv_end.tv_usec - tv_start.tv_usec) + 1000000*(tv_end.tv_sec - tv_start.tv_sec);
  147. printf("%2.2f\n", bytes/usecs);
  148. if (pinned)
  149. {
  150. cuMemFreeHost(&h_A);
  151. }
  152. else
  153. {
  154. free(h_A);
  155. }
  156. }
  157. int main(int argc, char **argv)
  158. {
  159. parse_args(argc, argv);
  160. bind_thread(0);
  161. // printf("Memcpy alone\n");
  162. benchmark_memcpy();
  163. return 0;
  164. }