-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
178 lines (141 loc) · 4.32 KB
/
main.cpp
File metadata and controls
178 lines (141 loc) · 4.32 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
#include <cuda_runtime.h>
#include <iostream>
#include <algorithm>
#include <cassert>
#include <numeric>
#include <cmath>
#include "moving_average_cuda.hpp"
#include "moving_average_custom.hpp"
#include "rotate_image_cuda.hpp"
#include "rotate_image_custom.hpp"
template<typename T, typename S>
inline bool
compareData(const cimg_library::CImg<T>&diff_image, const S epsilon, const float threshold)
{
assert(epsilon >= 0);
bool result = true;
unsigned int error_count = 0;
auto len = diff_image.size();
int index = 0;
for (auto p : diff_image)
{
float diff = p;
bool comp = diff < epsilon;
// if(!comp)
// {
// std::cout << diff << std::endl;
// }
result &= comp;
error_count += !comp;
index++;
}
if (threshold == 0.0f)
{
return (result) ? true : false;
}
else
{
if (error_count)
{
printf("%4.2f(%%) of bytes mismatched (count=%d)\n", (float)error_count*100/(float)len, error_count);
}
return (len*threshold > error_count) ? true : false;
}
}
template <typename T>
bool test_moving_average(const int N, const int R,
cudaTextureFilterMode filterMode,
cudaTextureAddressMode addressMode,
int normalization)
{
T* host_memory = new T[N];
T* h_cuda_output = new T[N];
T* h_hip_output = new T[N];
for (int i = 0; i < N; i++) host_memory[i] = i;
//original cuda texture version
moving_average_gpu(h_cuda_output, host_memory, N, R,
filterMode, addressMode, normalization);
//alternative implementation
moving_average_tr(h_hip_output, host_memory, N, R,
filterMode, addressMode, normalization);
bool implicit_comp = true;
bool result = true;
if (implicit_comp)
{
for(int i = 0; i < N; i++)
{
float eps = std::fabs(h_hip_output[i] - h_cuda_output[i]);
if ( eps > 0)
{
std::cout << i << " " << h_hip_output[i] << " eps: " << eps << std::endl;
result = false;
}
}
}
else
{
for(int i = 0; i < N; i++)
{
float eps = std::fabs(h_hip_output[i] - h_cuda_output[i]);
if ( eps > 0)
result = false;
std::cout << i << " "
<< "my : " << h_hip_output[i] << " "
<< "rf : " << h_cuda_output[i] << std::endl;
}
}
delete [] host_memory;
delete [] h_cuda_output;
delete [] h_hip_output;
return result;
}
template<typename T>
bool test_rotate(const float angle,
cudaTextureFilterMode filterMode,
cudaTextureAddressMode addressMode,
int normalization)
{
std::string lena_path = "data/lena_bw.pgm";
cimg_library::CImg<float> cuda_image =
rotate_cuda(lena_path, angle, filterMode, addressMode, normalization);
cimg_library::CImg<float> custom_image =
rotate_custom(lena_path, angle, filterMode, addressMode, normalization);
bool result = true;
cimg_library::CImg<float> difference = cuda_image - custom_image;
difference.abs();
//five percent;
float treshold = 0.05f;
result = compareData(difference, 1e-2f, treshold);
//calculate rms
double sum = 0;
for (auto p : difference)
{
sum += p*p;
}
sum /= difference.size();
auto rms = sqrt(sum);
std::cout << "RMS: " << rms << std::endl;
difference.normalize(0.f, 255.f);
difference.save("data/diff.pgm");
return result;
}
int main(int argc, char *argv[])
{
cudaTextureFilterMode filterMode = cudaFilterModePoint;
cudaTextureAddressMode addressMode = cudaAddressModeWrap;
int normalization = 1;
bool result_avg =
test_moving_average<float>(100, -13, filterMode,
addressMode, normalization);
if (!result_avg)
{
std::cout << "\n*** Result average FAILED! ***\n" << std::endl;
}
bool result_rot =
test_rotate<float>(-0.5, filterMode,
addressMode, normalization);
if (!result_rot)
{
std::cout << "\n*** Result rotation FAILED! ***\n" << std::endl;
}
}