-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixTranspose.cu
More file actions
94 lines (75 loc) · 2.91 KB
/
matrixTranspose.cu
File metadata and controls
94 lines (75 loc) · 2.91 KB
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
#include "helpers.h"
#include <stdio.h>
#include <cuda_runtime.h>
__global__
void matrixTransposeKernel(const float* d_inputMatrix, float* d_outputMatrix, int height, int width){
int row = blockIdx.y * blockDim.y + threadIdx.y;
int col = blockIdx.x * blockDim.x + threadIdx.x;
if((row < height) && (col < width)){
d_outputMatrix[col*height + row] = d_inputMatrix[row*width + col];
}
}
int main(){
int width = 256;
int height = 256;
float* h_inputMatrix= new float[height * width];
float* h_outputMatrix= new float[height * width];
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
h_inputMatrix[i*width+j] = i*j*i;
}
}
float* d_inputMatrix;
float* d_outputMatrix;
CUDA_CHECK(cudaMalloc((void**)&d_inputMatrix, sizeof(float)*height * width));
CUDA_CHECK(cudaMalloc((void**)&d_outputMatrix, sizeof(float)*height * width));
CUDA_CHECK(cudaMemcpy(d_inputMatrix, h_inputMatrix, sizeof(float)*height*width, cudaMemcpyHostToDevice));
dim3 blockDim(32, 32);
dim3 gridDim(((blockDim.x + height - 1)/blockDim.x), ((blockDim.y + width - 1)/blockDim.y));
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start, 0);
matrixTransposeKernel<<<gridDim, blockDim>>>(d_inputMatrix, d_outputMatrix, height, width);
CHECK_KERNEL_ERROR();
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
float milliseconds;
CUDA_CHECK(cudaEventElapsedTime(&milliseconds, start, stop));
CUDA_CHECK(cudaMemcpy(h_outputMatrix, d_outputMatrix, sizeof(float)*width*height, cudaMemcpyDeviceToHost));
bool correct = true;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
if (h_outputMatrix[j * height + i] != h_inputMatrix[i * width + j]) {
printf("Error: h_outputMatrix[%d][%d] = %f, h_inputMatrix[%d][%d] = %f\n",
j, i, h_outputMatrix[j * height + i], i, j, h_inputMatrix[i * width + j]);
correct = false;
break;
}
}
if (!correct) break;
}
if (correct) {
printf("Transpose verification successful!\n");
} else {
printf("Transpose verification failed!\n");
}
printf("\nFirst few elements of Input Matrix:\n");
for (int i = 0; i < min(height, 4); ++i) {
for (int j = 0; j < min(width, 4); ++j) {
printf("%f ", h_inputMatrix[i * width + j]);
}
printf("\n");
}
printf("\nFirst few elements of Output Matrix (Transposed):\n");
for (int i = 0; i < min(width, 4); ++i) {
for (int j = 0; j < min(height, 4); ++j) {
printf("%f ", h_outputMatrix[i * height + j]);
}
printf("\n");
}
printf("Elapsed Time(GPU): %f\n", milliseconds);
CUDA_CHECK(cudaFree(d_inputMatrix));
CUDA_CHECK(cudaFree(d_outputMatrix));
return 0;
}