-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions1.cpp
310 lines (277 loc) · 6.07 KB
/
functions1.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <iostream>
#include <map>
#include <bench.h>
using std::cerr;
using std::endl;
#if defined(HAVE_BLITZ)
#include <blitz/array.h>
using namespace blitz;
using namespace blitz::tensor;
struct Functions {
enum funcEnums { func1=1, func2, func3 };
typedef Array<double,2> Array2d;
void f1(Array2d &x) const
{
x=temin;
}
void f2(Array2d &x) const
{
int ny = x.cols();
int jst(x.lbound(secondDim));
x = dte*exp(-pow2(2.0*(j-jst)/(ny-1)-1.0)/(2.0*stdte2))+temin;
}
void f3(Array2d &x) const
{
int nx = x.rows();
int ist = x.lbound(firstDim);
x = dte*exp(-pow2(2.0*(i-ist)/(nx-1)-1.0)/(2.0*stdte2))+temin;
}
Functions(int nargs, char *args[])
: funId(nargs>1 ? atoi(args[1]) : func1), temin(2.0), dte(2.0), stdte2(0.25)
{
if (funId<func1 || funId>func3) {
cerr << "Exception in Function1: funId not registered" << endl;
throw !0;
}
}
int funId;
double temin, dte, stdte2;
};
class Function1 : protected Functions {
public:
Function1(int nargs, char *args[]) : Functions(nargs, args) {}
void get
(Array2d &x) const
{
switch (funId) {
case func1:
f1(x);
return;
case func2:
f2(x);
return;
case func3:
f3(x);
return;
}
}
void operator()(Array2d &x) const
{
switch (funId) {
case func1:
f1(x);
return;
case func2:
f2(x);
return;
case func3:
f3(x);
return;
}
}
};
class Function2 : protected Functions {
public:
Function2(int nargs, char *args[]) : Functions(nargs, args), f()
{
switch (funId) {
case func1:
f = &Function2::f1;
break;
case func2:
f = &Function2::f2;
break;
case func3:
f = &Function2::f3;
break;
}
}
void get
(Array2d &x) const
{
(this->*f)(x);
}
void operator()(Array2d &x) const
{
(this->*f)(x);
}
private:
void (Function2::*f)(Array2d &x) const;
};
class Function3 : protected Functions {
public:
Function3(int nargs, char *args[]) : Functions(nargs, args), f()
{
f[1] = &Function3::f1;
f[2] = &Function3::f2;
f[3] = &Function3::f3;
}
void get
(Array2d &x) const
{
(this->*f[funId])(x);
}
void operator()(Array2d &x) const
{
(this->*f[funId])(x);
}
private:
void (Function3::*f[func3+1])(Array2d &x) const;
};
class Function4 : protected Functions {
public:
Function4(int nargs, char *args[]) : Functions(nargs, args), f()
{
f[func1] = &Function4::f1;
f[func2] = &Function4::f2;
f[func3] = &Function4::f3;
}
void get
(Array2d &x) const
{
flist::const_iterator i = this->f.find(funId);
(this->*(i->second))(x);
}
void operator()(Array2d &x) const
{
flist::const_iterator i = this->f.find(funId);
(this->*(i->second))(x);
}
private:
typedef std::map<int, void (Function4::*)(Array2d &) const> flist;
flist f;
};
// Functionoid
#include <factory.h>
struct Funoid {
typedef Array<double,2> Array2d;
Funoid() : temin(2.0), dte(2.0), stdte2(0.25) {}
virtual ~Funoid() {}
virtual void doit(Array2d & x) const = 0;
double temin, dte, stdte2;
};
struct Funoid1 : Funoid {
virtual void doit(Array2d &x) const
{
x=temin;
}
};
RegisterInFactory<Funoid, Funoid1, int> r1(1);
struct Funoid2 : Funoid {
virtual void doit(Array2d &x) const
{
int ny = x.cols();
int jst(x.lbound(secondDim));
x = dte*exp(-pow2(2.0*(j-jst)/(ny-1)-1.0)/(2.0*stdte2))+temin;
}
};
RegisterInFactory<Funoid, Funoid2, int> r2(2);
struct Funoid3 : Funoid {
void doit(Array2d &x) const
{
int nx = x.rows();
int ist = x.lbound(firstDim);
x = dte*exp(-pow2(2.0*(i-ist)/(nx-1)-1.0)/(2.0*stdte2))+temin;
}
};
RegisterInFactory<Funoid, Funoid3, int> r3(3);
class Function5 {
public:
typedef Funoid::Array2d Array2d;
typedef GenericFactory<Funoid, int> Factory;
Function5(int nargs, char *args[]) :
fun(Factory::instance()
.create(nargs>1 ? atoi(args[1]) : Functions::func1)) {}
void get (Array2d & x) const
{
fun->doit(x);
}
void operator()(Array2d & x) const
{
fun->doit(x);
}
private:
GenericFactory<Funoid, int>::BasePtr fun;
};
namespace bench {
const int nx = 10;
const int ny = 10;
Functions::Array2d x(nx, ny);
template <class Function>
struct Test1 : bench::Test {
explicit Test1(Function _function) : function(_function) {}
void op()
{
x = 0.0;
function.get(x);
}
std::string opName() const
{
std::string s(typeid(function).name());
s.append("::get()");
return s;
}
Function function;
};
template <class Function>
struct Test2 : bench::Test {
explicit Test2(Function _function) : function(_function) {}
void op()
{
x = 0.0;
function(x);
}
std::string opName() const
{
std::string s(typeid(function).name());
s.append("() ");
return s;
};
Function function;
};
}
int main(int nargs, char *args[])
{
try {
double minTime = 2.0;
bench::verbose = true;
Function1 f1(nargs, args);
Function2 f2(nargs, args);
Function3 f3(nargs, args);
Function4 f4(nargs, args);
Function5 f5(nargs, args);
{
bench::Test1<Function1> T1(f1);
bench::Test1<Function2> T2(f2);
bench::Test1<Function3> T3(f3);
bench::Test1<Function4> T4(f4);
bench::Test1<Function5> T5(f5);
bench::benchClassOp(T1, minTime);
bench::benchClassOp(T2, minTime);
bench::benchClassOp(T3, minTime);
bench::benchClassOp(T4, minTime);
bench::benchClassOp(T5, minTime);
}
{
bench::Test2<Function1> T1(f1);
bench::Test2<Function2> T2(f2);
bench::Test2<Function3> T3(f3);
bench::Test2<Function4> T4(f4);
bench::Test2<Function5> T5(f5);
bench::benchClassOp(T1, minTime);
bench::benchClassOp(T2, minTime);
bench::benchClassOp(T3, minTime);
bench::benchClassOp(T4, minTime);
bench::benchClassOp(T5, minTime);
}
}
catch (int status) {
return status;
}
}
#else
int main(int nargs, char *args[])
{
cerr << args[0] << ": Blitz test not available\n";
}
#endif