-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranspose_eunjin.cu
192 lines (153 loc) · 5.2 KB
/
transpose_eunjin.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#define TILE_DIM 32
#define BLOCK_ROWS 8
#define BLOCK_DIM 32
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true) {
if (code == cudaSuccess) return;
fprintf(stderr,"Error: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
double timeStamp() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_usec / 1000.0 + tv.tv_sec;
}
void displayResults(float *A, float *T, int M, int N, int fromIdx, int toIdx){
// display results
printf("Matrix A: \n");
printf("----------\n");
for (int i = 0; i < M; ++i) {
if (i >= fromIdx && i < toIdx) {
for (int j = 0; j < N; ++j) {
if (j >= fromIdx && j < toIdx) {
printf("A: %.2f ", A[i * N + j]);
} else {
continue;
}
}
} else {
continue;
}
printf("\n");
}
printf("----------\n");
printf("Transpose: \n");
printf("----------\n");
for (int i = 0; i < N; ++i) {
if (i >= fromIdx && i < toIdx) {
for (int j = 0; j < M; ++j) {
if (j >= fromIdx && j < toIdx) {
printf("%.2f ", T[i * M + j]);
} else {
continue;
}
}
} else {
continue;
}
printf("\n");
}
}
void transposeCPU(float *A, float *T, int M, int N) {
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
T[j * M + i] = A[i * N + j];
}
}
}
void validateResults(float *h_A, float *h_T, int M, int N){
// Allocate memory for the transpose matrix on CPU
float *h_T_CPU = (float *)malloc(M * N * sizeof(float));
// Transpose matrix A on CPU
transposeCPU(h_A, h_T_CPU, M, N);
// Validate the results
int incorrectCount = 0;
for (int i = 0; i < M * N; ++i) {
if (abs(h_T_CPU[i] - h_T[i]) > 1e-5) {
incorrectCount++;
// Uncomment the next line to print each incorrect element
// printf("Mismatch at index %d, CPU: %f, GPU: %f\n", i, h_T_CPU[i], h_T[i]);
}
}
if (incorrectCount == 0) {
printf("Validation Passed!\n");
} else {
printf("Validation Failed: %d elements incorrect.\n", incorrectCount);
}
// Clean up CPU transpose matrix
free(h_T_CPU);
}
__global__ void transposeNaive(float *d_A, float *d_T, int M, int N) {
int row = blockIdx.y * BLOCK_DIM + threadIdx.y;
int col = blockIdx.x * BLOCK_DIM + threadIdx.x;
if (row < M && col < N) {
d_T[col * M + row] = d_A[row * N + col];
}
}
__global__ void transposeSharedMem(float *d_A, float *d_T, int M, int N) {
__shared__ float tile[TILE_DIM][TILE_DIM+1];
unsigned int row = blockIdx.y * TILE_DIM + threadIdx.y;
unsigned int col = blockIdx.x * TILE_DIM + threadIdx.x;
unsigned int index_in = row * N + col;
if((row < M) && (col < N) && (index_in < M*N)) {
tile[threadIdx.y][threadIdx.x] = d_A[index_in];
}
__syncthreads();
row = blockIdx.y * TILE_DIM + threadIdx.x;
col = blockIdx.x * TILE_DIM + threadIdx.y;
if((row < M) && (col < N)) {
unsigned int index_out = col * M + row;
d_T[index_out] = tile[threadIdx.x][threadIdx.y];
}
}
int main(int argc, char *argv[]) {
// Set matrix size
int M = atoi(argv[1]);
int N = atoi(argv[2]);
if (M <= 0 || N <= 0) return 0;
size_t bytes = M * N * sizeof(float);
float *h_A, *h_T;
float *d_A, *d_T;
// allocate host memory
gpuErrchk(cudaHostAlloc((void **)&h_A, bytes, cudaHostAllocMapped));
gpuErrchk(cudaHostAlloc((void **)&h_T, bytes, cudaHostAllocMapped));
// initialize data
for (int i = 0; i < M * N; ++i) {
h_A[i] = (float)(rand() % 10 + 1);
}
// allocate device memory
gpuErrchk(cudaMalloc(&d_A, bytes));
gpuErrchk(cudaMalloc(&d_T, bytes));
// copy host data to device
double start_total_GPU = timeStamp();
gpuErrchk(cudaMemcpy(d_A, h_A, bytes, cudaMemcpyHostToDevice));
// launch kernel instance
dim3 blockDim(BLOCK_DIM, BLOCK_DIM);
dim3 gridDim((N + blockDim.x - 1)/blockDim.x, (M + blockDim.y - 1)/blockDim.y);
// transposeNaive<<<gridDim, blockDim>>>(d_A, d_T, M, N);
transposeSharedMem<<<gridDim, blockDim>>>(d_A, d_T, M, N);
cudaDeviceSynchronize();
gpuErrchk(cudaGetLastError());
// copy result back to host
gpuErrchk(cudaMemcpy(h_T, d_T, bytes, cudaMemcpyDeviceToHost));
double end_total_GPU = timeStamp();
float total_GPU_time = end_total_GPU - start_total_GPU;
printf("GPU execution time: %.4f milliseconds\n", total_GPU_time);
// display results
// displayResults(h_A, h_T, M, N, 30, 40);
validateResults(h_A, h_T, M, N);
// clean up data
gpuErrchk(cudaFreeHost(h_A));
gpuErrchk(cudaFreeHost(h_T));
gpuErrchk(cudaFree(d_A));
gpuErrchk(cudaFree(d_T));
gpuErrchk(cudaDeviceReset());
return 0;
}
// $ nvcc -arch sm_89 transpose_eunjin.cu -o transpose_eunjin
// $ ./transpose_eunjin 5 4
// $ nvcc -arch sm_89 transpose_cuda/transpose_eunjin.cu -o transpose_cuda/transpose_eunjin
// $ transpose_cuda/transpose_eunjin 5 4