-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
199 lines (159 loc) · 4.63 KB
/
main.cpp
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
#include <vector>
#include <cmath>
#include <string>
#include <iostream>
#include <omp.h>
#include <ctime>
#include <sys/time.h>
double get_wall_time(){
struct timeval time;
if (gettimeofday(&time,NULL)){
// Handle error
return 0;
}
return (double)time.tv_sec + (double)time.tv_usec * .000001;
}
double get_cpu_time(){
return (double)clock() / CLOCKS_PER_SEC;
}
using namespace std;
vector<bool> function1(const vector<double> &a,
const vector<double> &b)
{
const size_t N = a.size();
vector<bool> res(N);
#pragma omp parallel for num_threads(8)
for (size_t i=0; i<N; i++){
res[i] = a[i] * b[i] - 4.1 * a[i] > 2.5 * b[i];
}
return res;
}
vector<double> function2(const vector<double> &a,
const vector<double> &b)
{
const size_t N = a.size();
vector<double> res(N);
#pragma omp parallel for num_threads(8)
for (size_t i=0; i<N; i++){
res[i] = sin(a[i]) + asinh(a[i] / b[i]);
}
return res;
}
void convolve(const vector< vector<long> > &f,
const vector< vector<long> > &g,
vector< vector<long> > &h)
{
/*
* f is an image and is indexed by (v, w)
* g is a filter kernel and is indexed by (s, t),
* it needs odd dimensions
* h is the output image and is indexed by (x, y),
*/
const size_t vmax = f.size();
const size_t wmax = f[0].size();
const size_t smax = g.size();
const size_t tmax = g[0].size();
if (smax % 2 == 0 || tmax % 2 == 0)
{
throw string("Only odd dimensions on filter supported");
}
// smid and tmid are number of pixels between the center pixel
// and the edge, ie for a 5x5 filter they will be 2.
const size_t smid = smax / 2;
const size_t tmid = tmax / 2;
// Allocate result image.
for(size_t x = 0; x < vmax; ++x) {
for(size_t y = 0; y < wmax; ++y) {
h[x][y] = 0;
}
}
// Do convolution
#pragma omp parallel for num_threads(8)
for (size_t x=smid; x<vmax - smid; x++)
{
for (size_t y=tmid; y<wmax - tmid; y++)
{
/*
* Calculate pixel value for h at (x,y). Sum one component
* for each pixel (s, t) of the filter g.
*/
long value = 0;
for (size_t s=0; s<smax; s++)
{
for (size_t t=0; t<tmax; t++)
{
size_t v = x - smid + s;
size_t w = y - tmid + t;
value += g[s][t] * f[v][w];
}
}
h[x][y] = value;
}
}
}
vector<double> data1D(const size_t N)
{
vector<double> data(N);
for (size_t i=0; i<N; i++){
data[i] = double(i+1);
}
return data;
}
vector< vector<long> > data2D(const size_t N, const size_t M)
{
vector< vector<long> > data(N, vector<long>(M));
for (size_t i=0; i<N; i++){
for (size_t j=0; j<M; j++){
data[i][j] = double(i * M + j);
}
}
return data;
}
int main(int argc, char *argv[])
{
vector<double> a;
vector<double> b;
vector< vector<long> > f;
vector< vector<long> > g;
a = data1D(1000000);
b = data1D(1000000);
cout << "Warm up the CPU" << endl;
double start = get_wall_time();
for (size_t i=0; (get_wall_time() - start) < 10.; i++)
{
vector<bool> c = function1(a,b);
}
cout << "Testing function1" << endl;
start = get_wall_time();
for (size_t i=0; i< 700; i++)
{
vector<bool> c = function1(a,b);
}
cout << "Time: " << (get_wall_time() - start) / double(700. / 1000.) << " ms" << endl;
cout << "Testing function2" << endl;
start = get_wall_time();
for (size_t i=0; i< 70; i++)
{
vector<double> c = function2(a,b);
}
cout << "Time: " << (get_wall_time() - start) / double(70. / 1000.) << " ms" << endl;
f = data2D(200, 200);
g = data2D(9, 9);
vector< vector<long> > h(200, vector<long>(200));
cout << "Testing convolution small case" << endl;
start = get_wall_time();
for (size_t i=0; i< 1000; i++)
{
convolve(f,g,h);
}
cout << "Time: " << (get_wall_time() - start) / double(1000. / 1000.) << " ms" << endl;
f = data2D(2000, 2000);
h = vector< vector<long> > (2000, vector<long>(2000));
cout << "Testing convolution large case" << endl;
start = get_wall_time();
for (size_t i=0; i< 70; i++)
{
convolve(f,g,h);
}
cout << "Time: " << (get_wall_time() - start) / double(70. / 1000.) << " ms" << endl;
}