1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #include <stdio.h> #include <math.h> #define BLOCK_SIZE 16 const double EPS = 1E-6;
__global__ void gpu_matrix_mult_shared(int *a,int *b, int *c, int m, int n, int k) { int row = blockIdx.y * blockDim.y + threadIdx.y; int col = blockIdx.x * blockDim.x + threadIdx.x;
__shared__ int s_a[BLOCK_SIZE][BLOCK_SIZE]; __shared__ int s_b[BLOCK_SIZE][BLOCK_SIZE];
int sum = 0; for (int sub = 0; sub < gridDim.x; sub++) {
int index_a = (sub * BLOCK_SIZE + threadIdx.x) + row * n; if (row < m && (sub * BLOCK_SIZE + threadIdx.x) < n) { s_a[threadIdx.y][threadIdx.x] = a[index_a]; } else { s_a[threadIdx.y][threadIdx.x] = 0; }
int index_b = col + n * (sub * BLOCK_SIZE + threadIdx.y); if (col < n && (sub * BLOCK_SIZE + threadIdx.y) < k) { s_b[threadIdx.y][threadIdx.x] = b[index_b]; } else { s_b[threadIdx.y][threadIdx.x] = 0; }
__syncthreads();
for (int i = 0; i < BLOCK_SIZE; i++) { sum += s_a[threadIdx.y][i] * s_b[i][threadIdx.x]; } __syncthreads();
if (row < m && col < k) { c[row * k + col] = sum; } } }
void cpu_matrix_mult(int *a, int *b, int *c, int m, int n, int k) { for (int i = 0; i < m; i++) { for (int j = 0; j < k; j++) { int sum = 0; for (int h = 0; h < n; h++) { sum += a[i * n + h] * b[h * k + j]; } c[i * k + j] = sum; } } }
void check(int *c_cpu, int *c_gpu, int m, int k) { int error = false; for (int i = 0; i < m; i++) { for (int j = 0; j < k; j++) { if(fabs(c_gpu[i * k + j] - c_cpu[i * k + j]) > EPS) { error = true; } } } printf("检查结果:%s\n", error ? "Errors" : "Pass"); }
int main() { int m = 5; int n = 5; int k = 5;
int *a, *b, *c_gpu, *c_cpu;
cudaMallocManaged((void **) &a, sizeof(int) * m * n); cudaMallocManaged((void **) &b, sizeof(int) * n * k); cudaMallocManaged((void **) &c_gpu, sizeof(int) * m * k); cudaMallocManaged((void **) &c_cpu, sizeof(int) * m * k);
for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { a[i * n + j] = rand() % 1024; } } for (int i = 0; i < n; i++) { for (int j = 0; j < k; j++) { b[i * k + j] = rand() % 1024; } }
unsigned int grid_rows = (m + BLOCK_SIZE - 1) / BLOCK_SIZE; unsigned int grid_cols = (k + BLOCK_SIZE - 1) / BLOCK_SIZE; dim3 grid(grid_cols, grid_rows); dim3 block(BLOCK_SIZE, BLOCK_SIZE); gpu_matrix_mult_shared<<<grid, block>>>(a, b, c_gpu, m, n, k);
cudaDeviceSynchronize();
printf("GPU执行结果:\n"); for (int i = 0; i < m; i++) { for (int j = 0; j < k; j++) { printf("%d ", c_gpu[i * k + j]); } printf("\n"); }
cpu_matrix_mult(a, b, c_cpu, m, n, k);
printf("CPU执行结果:\n"); for (int i = 0; i < m; i++) { for (int j = 0; j < k; j++) { printf("%d ", c_cpu[i * k + j]); } printf("\n"); }
check(c_cpu, c_gpu, m, k);
cudaFree(a); cudaFree(b); cudaFree(c_cpu); cudaFree(c_gpu);
return 0; }
|