cuda_bandwidth.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010 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. if (strcmp(argv[i], "-gpu-ld") == 0) {
  48. char *argptr;
  49. GPU_LD = strtol(argv[++i], &argptr, 10);
  50. }
  51. if (strcmp(argv[i], "-cpu-ld") == 0) {
  52. char *argptr;
  53. CPU_LD = strtol(argv[++i], &argptr, 10);
  54. }
  55. if (strcmp(argv[i], "-size") == 0) {
  56. char *argptr;
  57. MATRIXSIZE = strtol(argv[++i], &argptr, 10);
  58. }
  59. if (strcmp(argv[i], "-pin") == 0) {
  60. pinned = 1;
  61. }
  62. if (strcmp(argv[i], "-HtoD") == 0) {
  63. htod = 1;
  64. }
  65. if (strcmp(argv[i], "-iter") == 0) {
  66. char *argptr;
  67. ITER = strtol(argv[++i], &argptr, 10);
  68. }
  69. if (strcmp(argv[i], "-h") == 0) {
  70. printf("usage : %s [-pin] [-HtoD] [-size size] [-cpu-ld ld] [-gpu-ld ld] [-iter n]\n", argv[0]);
  71. }
  72. }
  73. assert(CPU_LD >= MATRIXSIZE);
  74. assert(GPU_LD >= MATRIXSIZE);
  75. }
  76. void bind_thread(int cpu)
  77. {
  78. /* bind the thread to a cpu */
  79. cpu_set_t mask;
  80. CPU_ZERO(&mask);
  81. CPU_SET(cpu, &mask);
  82. sched_setaffinity(0, sizeof(cpu_set_t), &mask);
  83. }
  84. void benchmark_memcpy(void)
  85. {
  86. unsigned count;
  87. struct timeval tv_start, tv_end;
  88. unsigned long long usecs;
  89. double bytes = 4.0*MATRIXSIZE*MATRIXSIZE*ITER;
  90. cublasInit();
  91. if (pinned)
  92. {
  93. cuMemAllocHost((void **)&h_A, CPUBUFFERSIZE);
  94. }
  95. else
  96. {
  97. h_A = malloc(CPUBUFFERSIZE);
  98. }
  99. assert(h_A);
  100. /* malloc a buffer on the device */
  101. cublasAlloc(GPU_LD*GPU_LD, sizeof(float), &d_A);
  102. assert(d_A);
  103. gettimeofday(&tv_start, NULL);
  104. if (!pinned)
  105. {
  106. /* pageable memory */
  107. if (!htod)
  108. {
  109. for (count = 0; count < ITER; count++)
  110. {
  111. cublasGetMatrix(MATRIXSIZE, MATRIXSIZE, sizeof(float),
  112. (void *)d_A, GPU_LD, h_A, CPU_LD);
  113. cuCtxSynchronize();
  114. }
  115. }
  116. else
  117. {
  118. for (count = 0; count < ITER; count++)
  119. {
  120. cublasSetMatrix(MATRIXSIZE, MATRIXSIZE, sizeof(float),
  121. h_A, CPU_LD, (void *)d_A, GPU_LD);
  122. cuCtxSynchronize();
  123. }
  124. }
  125. }
  126. else
  127. {
  128. if (!htod)
  129. {
  130. for (count = 0; count < ITER; count++)
  131. {
  132. cublasGetMatrix(MATRIXSIZE, MATRIXSIZE, sizeof(float),
  133. d_A, GPU_LD, h_A, CPU_LD);
  134. cuCtxSynchronize();
  135. }
  136. }
  137. else {
  138. for (count = 0; count < ITER; count++)
  139. {
  140. cublasSetMatrix(MATRIXSIZE, MATRIXSIZE, sizeof(float),
  141. h_A, CPU_LD, d_A, GPU_LD);
  142. cuCtxSynchronize();
  143. }
  144. }
  145. }
  146. gettimeofday(&tv_end, NULL);
  147. usecs = (tv_end.tv_usec - tv_start.tv_usec) + 1000000*(tv_end.tv_sec - tv_start.tv_sec);
  148. printf("%2.2f\n", bytes/usecs);
  149. if (pinned)
  150. {
  151. cuMemFreeHost(&h_A);
  152. }
  153. else
  154. {
  155. free(h_A);
  156. }
  157. }
  158. int main(int argc, char **argv)
  159. {
  160. parse_args(argc, argv);
  161. bind_thread(0);
  162. // printf("Memcpy alone\n");
  163. benchmark_memcpy();
  164. return 0;
  165. }