cuda_bandwidth.c 3.8 KB

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