-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimingtest.cpp
374 lines (355 loc) · 8.12 KB
/
timingtest.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include <stdlib.h>
#include <stdio.h>
//#define CPOLY
//#define STATIC
#ifdef CPOLY
#include "./cpoly.hpp"
#else
#include "./rpoly.hpp"
#endif
#define WP 100
#define WPD 20
#define WPBS 200
#define MPC_MP
//#define GMP_MP
#ifdef CPP_MP
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <boost/multiprecision/cpp_complex.hpp>
using namespace boost;
using namespace boost::multiprecision;
using namespace boost::multiprecision::backends;
using numty = number<cpp_bin_float<WP>>;
using cmplx = cpp_complex<WP>;
using dntype= number<cpp_bin_float<WPD>>;
using dcmplx= cpp_complex<WPD>;
#elif defined(GMP_MP)
#include <boost/multiprecision/gmp.hpp>
#include <boost/multiprecision/complex_adaptor.hpp>
using namespace boost;
using namespace boost::multiprecision;
using namespace boost::multiprecision::backends;
using numty=number<gmp_float<WP>>;
using cmplx=number<complex_adaptor<gmp_float<WP>>>;
using dntype=number<gmp_float<WPD>>;
using dcmplx=number<complex_adaptor<gmp_float<WPD>>>;
#ifdef BACKSTAB
using bsdbl=number<gmp_float<WPBS>>;
using bscmplx=number<complex_adaptor<gmp_float<WPBS>>>;
#endif
#elif defined(MPC_MP)
#include <boost/multiprecision/mpc.hpp>
#include <boost/multiprecision/mpfr.hpp>
using namespace boost;
using namespace boost::multiprecision;
using namespace boost::multiprecision::backends;
using numty=number<mpfr_float_backend<WP>>;
using cmplx=number<mpc_complex_backend<WP>>;
using dntype=number<mpfr_float_backend<WPD>>;
using dcmplx=number<mpc_complex_backend<WPD>>;
#ifdef BACKSTAB
using bsdbl=number<mpfr_float_backend<WPBS>>;
using bscmplx=number<mpc_complex_backend<BS>>;
#endif
#else
#ifdef BACKSTAB
using bsdbl=number<mpfr_float_backend<WPBS>>;
using bscmplx=number<mpc_complex_backend<WPBS>>;
#endif
using numty=double;
using cmplx=complex<numty>;
using dntype=numty;
using dcmplx=cmplx;
#endif
//#include<complex>
#ifndef NDEG
#define NDEG 1000
#endif
double gauss(void)
{
double a1=3.949846138, a3 = 0.252408784, a5 = 0.076542912,
a7 = 0.008355968, a9 = 0.029899776;
double sum, r, r2;
int i;
sum = 0.0;
for(i=0; i < 12; i++)
{
sum = sum + drand48();
}
r = ( sum - 6.0 ) / 4.0;
r2 = r * r;
return (((( a9 * r2 + a7 ) * r2 + a5 ) * r2 + a3 ) * r2 + a1 ) * r;
}
#ifdef BACKSTAB
#ifdef CPOLY
void calc_coeff(bscmplx co[], bscmplx er[])
{
bscmplx c[NDEG+1], alpha;
int ii, jj;
for (ii=0; ii < NDEG; ii++)
{
c[ii] = 0.0;
}
c[NDEG]=1.0;
ii=0;
while (ii < NDEG)
{
alpha = -er[ii];
for (jj=ii; jj >= 0; jj--)
{
//do jj=ii,1,-1
if (jj==0)
c[jj] = c[jj] + alpha;
else
c[jj] = c[jj] + alpha*c[jj-1];
}
ii=ii+1;
}
for (ii=0; ii < NDEG; ii++)
co[ii] = c[NDEG-ii-1];
co[NDEG]=1.0;
}
#else
void calc_coeff(bsdbl co[], bscmplx er[])
{
bscmplx c[NDEG+1], alpha;
int ii, jj;
for (ii=0; ii < NDEG; ii++)
{
c[ii] = 0.0;
}
c[NDEG]=1.0;
ii=0;
while (ii < NDEG)
{
alpha = -er[ii];
for (jj=ii; jj >= 0; jj--)
{
//do jj=ii,1,-1
if (jj==0)
c[jj] = c[jj] + alpha;
else
c[jj] = c[jj] + alpha*c[jj-1];
}
ii=ii+1;
}
for (ii=0; ii < NDEG; ii++)
co[ii] = real(c[NDEG-ii-1]);
co[NDEG]=1.0;
}
#endif
#ifdef STATIC
#define NN NDEG
#else
#define NN -1
#endif
#ifdef CPOLY
bsdbl calc_backward_err(pvector<cmplx,NN>& roots, pvector<cmplx,NN>& c)
{
int i;
vector<cmplx> rv;
bscmplx *er = new bscmplx[NDEG];
bscmplx *cbs = new bscmplx[NDEG+1];
rv.resize(NDEG);
for (i=0; i < NDEG; i++)
{
rv[i] = cmplx(roots[i]);
}
// sort is needed to obtain the right backward error
std::sort(rv.begin(),rv.end(),[&](cmplx a, cmplx b)->bool {return abs(a) < abs(b);});
for (i=0; i < NDEG; i++)
{
er[i] = bscmplx(rv[i]);
}
calc_coeff(cbs, er);
bsdbl err, errmax;
for (i=0; i < NDEG+1; i++)
{
if (c[i]==cmplx(0.0))
err=abs(cbs[i] - bscmplx(c[i]));
else
err=abs((cbs[i] - bscmplx(c[i]))/bscmplx(c[i]));
if (err > 100)
cout << "i=" << i << " cbs=" << cbs[i] << " c=" << c[i] << "\n";
if (i==0 || err > errmax)
errmax=err;
}
return errmax;
}
#else
bsdbl calc_backward_err(pvector<cmplx,NN> roots, pvector<numty,NN> c)
{
int i;
bscmplx *er = new bscmplx[NDEG];
bsdbl *cbs = new bsdbl[NDEG+1];
vector<cmplx> rv;
bsdbl err, errmax;
rv.resize(NDEG);
for (i=0; i < NDEG; i++)
{
rv[i] = cmplx(roots[i]);
}
std::sort(rv.begin(),rv.end(),[&](cmplx a, cmplx b)->bool {return abs(a) < abs(b);});
for (i=0; i < NDEG; i++)
{
er[i] = bscmplx(rv[i]);
}
calc_coeff(cbs, er);
for (i=0; i < NDEG+1; i++)
{
if (abs(c[i])==0)
err=abs(cbs[i] - bsdbl(c[i]));
else
err=abs((cbs[i] - bsdbl(c[i]))/bsdbl(c[i]));
if (i==0 || err > errmax)
errmax=err;
}
return errmax;
}
#endif
#endif
using namespace std;
int main(int argc, char* argv[])
{
#ifdef BACKSTAB
bsdbl berr, berrmax;
int poltype, m;
#endif
int j, maxiter;
#ifdef STATIC
#ifdef CPOLY
cpoly<cmplx,NDEG,numty> rp;
pvector<cmplx,NDEG+1> c;
pvector<cmplx,NDEG> roots;
#else
rpoly<numty,NDEG,cmplx> rp;
pvector<numty,NDEG+1> c;
pvector<cmplx,NDEG> roots;
#endif
#else
#ifdef CPOLY
cpoly<cmplx,-1,numty> rp(NDEG);
pvector<cmplx,-1> c(NDEG+1);
pvector<cmplx,-1> roots(NDEG);
#else
rpoly<numty,-1,cmplx> rp(NDEG);
pvector<numty,-1> c(NDEG+1);
pvector<cmplx,-1> roots(NDEG);
#endif
#endif
#ifdef _OPENMP
cout << "# thread=" << omp_get_max_threads() << "\n";
#endif
//srand48(time(0));
srand48(4242);
numty sig=1.0;
if (argc>=2)
{
maxiter = atoi(argv[1]);
}
else
maxiter = 1000000;
#ifdef BACKSTAB
if (argc >= 3)
poltype = atoi(argv[2]);
else
poltype=0;
#endif
c[NDEG]=1.0;
#ifdef BACKSTAB
if (poltype !=0 )
maxiter=1;
#endif
for (int i=0; i < maxiter; i++)
{
//cout << "iter #" << i << "\n";
#ifdef BACKSTAB
#ifdef CPOLY
for (j=0; j < NDEG; j++)
c[j]=cmplx(2.0*sig*(drand48()-0.5),0.0);
#else
if (poltype==0)
{
for (j=0; j < NDEG; j++)
c[j]=sig*(drand48()-0.5);
}
else if (poltype==1)
{
if (NDEG%2==1)
{
cout << "You have to set an even polynomial degree for this test\n";
exit(1);
}
for (j=0; j < NDEG; j++)
c[j] = 0.0;
c[0] = 1.0;
c[NDEG/2] = NDEG/(NDEG+1.0)+(NDEG+1.0)/NDEG;
c[NDEG]=1.0;
}
else if (poltype==2)
{
if (NDEG%2==1)
{
cout << "You have to set an even polynomial degree for this test\n";
exit(1);
}
m=NDEG/2;
c[m]=m+1.0;
for (j=0; j < m-1; j++)
c[j] = m+j;
for (j=2*m; j >= m+1; j--)
c[j] = 3*m-j;
for (j=0; j <= 2*m; j++)
c[j] /= m;
}
else if (poltype==3)
{
m=NDEG-1;
numty lambda=0.9;
for (j=0; j <= NDEG; j++)
c[j] = 0;
c[m+1] = 1.0-lambda;
c[m] = -(lambda+1.0);
c[1] = lambda+1.0;
c[0] = -(1.0-lambda);
for (j=1; j <= NDEG; j++)
c[j] /= c[NDEG];
c[NDEG]=1.0;
}
else if (poltype==4)
{
m=NDEG-1;
numty lambda=0.99;
for (j=0; j <= NDEG; j++)
c[j] = 0;
c[m+1] = 1.0-lambda;
c[m] = -(lambda+1.0);
c[1] = lambda+1.0;
c[0] = -(1.0-lambda);
for (j=1; j <= NDEG; j++)
c[j] /= c[NDEG];
c[NDEG]=1.0;
}
#endif
#else
#ifdef CPOLY
for (j=0; j < NDEG; j++)
c[j]=cmplx(2.0*sig*(drand48()-0.5),0.0);
#else
for (j=0; j < NDEG; j++)
c[j]=sig*(drand48()-0.5);
#endif
#endif
rp.set_coeff(c);
rp.find_roots(roots);
//rp.aberth(roots);
#ifdef BACKSTAB
berr=calc_backward_err(roots, c);
if (i==0 || berr > berrmax)
berrmax=berr;
#endif
}
#ifdef BACKSTAB
cout << "Max backward error=" << berrmax << "\n";
#endif
return 0;
}