-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cu
156 lines (117 loc) · 4.26 KB
/
main.cu
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
151
152
153
154
155
156
#include <iostream>
#include "./transpose_cpu.h"
#include "./transpose_tiled.cuh"
#include "./transpose_naive.cuh"
#include "./transpose_no_bank_conflicts.cuh"
#include "../common/common.hpp"
#include "../common/Timer.hpp"
#define M 10240U
#define N 1024U
#define TILE_SIZE 32U
float *x_cpu, *y_cpu;
float *x_gpu, *y_gpu, *y_gpu_copyback;
void prepare_input() {
x_cpu = new float[M * N];
y_cpu = new float[N * M];
y_gpu_copyback = new float[N * M];
randomInit(x_cpu, M * N);
CUDA_CHECK(cudaMalloc(&x_gpu, M * N * sizeof(float)));
CUDA_CHECK(cudaMalloc(&y_gpu, N * M * sizeof(float)));
CUDA_CHECK(cudaMemcpy(x_gpu, x_cpu, sizeof(float) * M * N, cudaMemcpyHostToDevice));
}
void cleanup() {
delete[] x_cpu;
delete[] y_cpu;
delete[] y_gpu_copyback;
CUDA_CHECK(cudaFree(x_gpu));
CUDA_CHECK(cudaFree(y_gpu));
}
void test_transpose_cpu() {
Timer::Get()->Start("transpose_cpu");
transpose_cpu(x_cpu, y_cpu, M, N);
Timer::Get()->End("transpose_cpu");
}
void test_transpose_naive_read_coalesced() {
randomInit(y_gpu_copyback, M * N);
CUDA_CHECK(cudaDeviceSynchronize());
Timer::Get()->Start("transpose_naive_read_coalesced");
dim3 block = {TILE_SIZE, TILE_SIZE, 1};
dim3 grid = {ceil_div(N, TILE_SIZE), ceil_div(M, TILE_SIZE), 1};
transpose_naive_read_coalesced<<<grid, block>>>(y_gpu, x_gpu, M, N);
CUDA_CHECK(cudaDeviceSynchronize());
Timer::Get()->End("transpose_naive_read_coalesced");
CUDA_CHECK(cudaMemcpy(y_gpu_copyback, y_gpu, sizeof(float) * M * N, cudaMemcpyDeviceToHost));
if (!all_close(y_cpu, y_gpu_copyback, M * N)) {
fprintf(stderr, "Error: transpose_naive_read_coalesced does not match CPU result!\n");
exit(1);
}
}
void test_transpose_naive_write_coalesced() {
randomInit(y_gpu_copyback, M * N);
CUDA_CHECK(cudaDeviceSynchronize());
Timer::Get()->Start("transpose_naive_write_coalesced");
dim3 block = {TILE_SIZE, TILE_SIZE, 1};
dim3 grid = {ceil_div(M, TILE_SIZE), ceil_div(N, TILE_SIZE), 1};
transpose_naive_write_coalesced<<<grid, block>>>(y_gpu, x_gpu, M, N);
CUDA_CHECK(cudaDeviceSynchronize());
Timer::Get()->End("transpose_naive_write_coalesced");
CUDA_CHECK(cudaMemcpy(y_gpu_copyback, y_gpu, sizeof(float) * M * N, cudaMemcpyDeviceToHost));
if (!all_close(y_cpu, y_gpu_copyback, M * N)) {
fprintf(stderr, "Error: transpose_naive_write_coalesced does not match CPU result!\n");
exit(1);
}
}
void test_transpose_tiled() {
randomInit(y_gpu_copyback, M * N);
CUDA_CHECK(cudaDeviceSynchronize());
Timer::Get()->Start("transpose_tiled");
dim3 block = {TILE_SIZE, TILE_SIZE, 1};
dim3 grid = {ceil_div(N, TILE_SIZE), ceil_div(M, TILE_SIZE), 1};
transpose_tiled<TILE_SIZE><<<grid, block>>>(y_gpu, x_gpu, M, N);
CUDA_CHECK(cudaDeviceSynchronize());
Timer::Get()->End("transpose_tiled");
CUDA_CHECK(cudaMemcpy(y_gpu_copyback, y_gpu, sizeof(float) * M * N, cudaMemcpyDeviceToHost));
if (!all_close(y_cpu, y_gpu_copyback, M * N)) {
fprintf(stderr, "Error: transpose_tiled does not match CPU result!\n");
// print cpu result
printf("CPU:\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
printf("%f ", y_cpu[i * M + j]);
printf("\n");
}
// print gpu result
printf("GPU:\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
printf("%f ", y_gpu_copyback[i * M + j]);
printf("\n");
}
exit(1);
}
}
void test_transpose_no_bc() {
randomInit(y_gpu_copyback, M * N);
CUDA_CHECK(cudaDeviceSynchronize());
Timer::Get()->Start("transpose_no_bc");
dim3 block = {TILE_SIZE, TILE_SIZE, 1};
dim3 grid = {ceil_div(N, TILE_SIZE), ceil_div(M, TILE_SIZE), 1};
transpose_no_bank_conflicts<TILE_SIZE><<<grid, block>>>(y_gpu, x_gpu, M, N);
CUDA_CHECK(cudaDeviceSynchronize());
Timer::Get()->End("transpose_no_bc");
CUDA_CHECK(cudaMemcpy(y_gpu_copyback, y_gpu, sizeof(float) * M * N, cudaMemcpyDeviceToHost));
if (!all_close(y_cpu, y_gpu_copyback, M * N)) {
fprintf(stderr, "Error: transpose_no_bc does not match CPU result!\n");
exit(1);
}
}
int main() {
prepare_input();
test_transpose_cpu();
test_transpose_naive_read_coalesced();
test_transpose_naive_write_coalesced();
test_transpose_tiled();
test_transpose_no_bc();
cleanup();
return 0;
}