-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate_image_custom.hip.cpp
More file actions
224 lines (185 loc) · 7.12 KB
/
rotate_image_custom.hip.cpp
File metadata and controls
224 lines (185 loc) · 7.12 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "hip_runtime.h"
#include "rotate_image_custom.hpp"
#include "memorytraverser.hpp"
#include "hip_errors.hpp"
#include "cuda_errors.hpp"
using namespace cimg_library;
inline int iDivUp(int a, int b)
{
return ((a % b) != 0) ? (a / b + 1) : (a / b);
}
template<typename T, typename TraverserType>
__global__ void transformKernel(hipLaunchParm lp,
T* outputData,
const T* __restrict__ sourceData,
int width,
int height,
T theta,
TraverserType* mt)
{
// calculate normalized texture coordinates
unsigned int x = hipBlockIdx_x*hipBlockDim_x + hipThreadIdx_x;
unsigned int y = hipBlockIdx_y*hipBlockDim_y + hipThreadIdx_y;
T u = (T)x - (T)width/2;
T v = (T)y - (T)height/2;
T tu = u*cosf(theta) - v*sinf(theta);
T tv = v*cosf(theta) + u*sinf(theta);
tu /= (T)width;
tv /= (T)height;
// read from texture and write to global memory
T val = mt->get2D(sourceData, tu + 0.5f, tv + 0.5f);
outputData[y*width + x] = val;
}
template<typename T, typename TraverserType>
CImg<T> rotate_custom_impl(const std::string& filename, const float angle)
{
CImg<T> image(filename.c_str());
T* d = image.data();
unsigned int width = image.width();
unsigned int height = image.height();
//prepare input and output on device
T* d_input;
T* d_output;
size_t line_bytes = width * sizeof(T);
size_t h_elems_per_line = width;
size_t pitch_bytes = line_bytes; //put proper here
size_t d_elems_per_line = pitch_bytes / sizeof(T);
size_t host_memory_size = line_bytes * height;
size_t devcie_memory_size = pitch_bytes * height;
hipSafeCall(hipMalloc((void**)&d_input, devcie_memory_size));
hipSafeCall(hipMalloc((void**)&d_output, devcie_memory_size));
for (int i = 0; i < height; i++)
{
// std::cout << image.at(i*width, -1) << " orig : " << image[i*width] << " " << d[i*width] << std::endl;
cudaError err = cudaMemcpy(&d_input[i*d_elems_per_line],
&d[i*h_elems_per_line], line_bytes, cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
std::cerr << "Failed at i = " << i << " "<< d[i*h_elems_per_line] << std::endl;
}
}
cudaDeviceSynchronize();
//prepare memory traverser;
TraverserType* d_mt = nullptr;
{
TraverserType host_traverser;
host_traverser.width = width;
host_traverser.height = height;
hipSafeCall(hipMalloc((void**)&d_mt, sizeof(TraverserType)));
hipSafeCall(hipMemcpy(d_mt, &host_traverser, sizeof(TraverserType), hipMemcpyHostToDevice));
}
//call kernel
dim3 dimBlock(8, 8, 1);
dim3 dimGrid(width / dimBlock.x, height / dimBlock.y, 1);
hipLaunchKernel(HIP_KERNEL_NAME(transformKernel<T>), dim3(dimGrid), dim3(dimBlock), 0, 0,
d_output,
d_input,
width, height, angle,
d_mt);
hipCheckError();
hipSafeCall(hipDeviceSynchronize());
//TIMING
hipEvent_t start, stop;
hipSafeCall(hipEventCreate(&start));
hipSafeCall(hipEventCreate(&stop));
const int NTimes = 100;
hipEventRecord(start);
for (int i = 0; i < NTimes; i++)
{
hipLaunchKernel(HIP_KERNEL_NAME(transformKernel<T>), dim3(dimGrid), dim3(dimBlock), 0, 0, d_output,
d_input,
width, height, angle,
d_mt);
}
hipEventRecord(stop);
hipEventSynchronize(stop);
float miliseconds = 0;
hipEventElapsedTime(&miliseconds, start, stop);
std::cout << "CUSTOM TIME: " << miliseconds / (float)NTimes << " ms" <<std::endl;
hipEventDestroy(start);
hipEventDestroy(stop);
//copy back the data to host
//hipSafeCall(hipMemcpy(d, d_output, size, hipMemcpyDeviceToHost));
for (int i = 0; i < height; i++)
{
// std::cout << " i " << i <<std::endl;
hipSafeCall(hipMemcpy(&d[i*h_elems_per_line],
&d_output[i*d_elems_per_line], line_bytes, hipMemcpyDeviceToHost));
}
//cleanup
hipSafeCall(hipFree(d_input));
hipSafeCall(hipFree(d_output));
hipSafeCall(hipFree(d_mt));
//image.normalize(0, 255);
image.save("data/custom_result.pgm");
return image;
}
//create a library function in memorytraverser
//which will use perfect forwading to launch implementation function
CImg<float> rotate_custom(const std::string& filename,
const float angle,
cudaTextureFilterMode filterMode,
cudaTextureAddressMode addressMode,
int normalization)
{
typedef float T;
//This defines the behaviour
using TraverserClampNormPixel = MemoryTraverser<float, Clamp<NORMALIZED, float>, PixelFilter<NEAREST, float>>;
using TraverserClampUNormPixel = MemoryTraverser<float, Clamp<NON_NORMALIZED, float>, PixelFilter<NEAREST, float>>;
using TraverserClampNormLinear = MemoryTraverser<float, Clamp<NORMALIZED, float>, PixelFilter<LINEAR, float>>;
using TraverserClampUNormLinear = MemoryTraverser<float, Clamp<NON_NORMALIZED, float>, PixelFilter<LINEAR, float>>;
using TraverserWrapNormPixel = MemoryTraverser<float, Wrap<NORMALIZED, float>, PixelFilter<NEAREST, float>>;
using TraverserWrapUNormPixel = MemoryTraverser<float, Wrap<NON_NORMALIZED, float>, PixelFilter<NEAREST, float>>;
using TraverserWrapNormLinear = MemoryTraverser<float, Wrap<NORMALIZED, float>, PixelFilter<LINEAR, float>>;
using TraverserWrapUNormLinear = MemoryTraverser<float, Wrap<NON_NORMALIZED, float>, PixelFilter<LINEAR, float>>;
if(filterMode == hipFilterModePoint)
{
if (addressMode == cudaAddressModeWrap)
{
if(normalization)
{
return rotate_custom_impl<T, TraverserWrapNormPixel>(filename, angle);
}
else
{
return rotate_custom_impl<T, TraverserWrapUNormPixel>(filename, angle);
}
}
else //clamp
{
if(normalization)
{
return rotate_custom_impl<T, TraverserClampNormPixel>(filename, angle);
}
else
{
return rotate_custom_impl<T, TraverserClampUNormPixel>(filename, angle);
}
}
}
else //Linear interpolation
{
if (addressMode == cudaAddressModeWrap)
{
if(normalization)
{
return rotate_custom_impl<T, TraverserWrapNormLinear>(filename, angle);
}
else
{
return rotate_custom_impl<T, TraverserWrapUNormLinear>(filename, angle);
}
}
else //clamp
{
if(normalization)
{
return rotate_custom_impl<T, TraverserClampNormLinear>(filename, angle);
}
else
{
return rotate_custom_impl<T, TraverserClampUNormLinear>(filename, angle);
}
}
}
}