-
Notifications
You must be signed in to change notification settings - Fork 5
/
testsweeper.cc
1297 lines (1205 loc) · 39.2 KB
/
testsweeper.cc
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017-2023, University of Tennessee. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
// This program is free software: you can redistribute it and/or modify it under
// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <string>
#include <cmath>
// prefer OpenMP get_wtime; else use gettimeofday
#ifdef _OPENMP
#include <omp.h>
#else
#include <sys/time.h>
#endif
#include "testsweeper.hh"
namespace testsweeper {
//==================================================
// Copied from blaspp/include/blas/util.hh
// -----------------------------------------------------------------------------
// for any combination of types, determine associated real, scalar,
// and complex types.
//
// real_type< float > is float
// real_type< float, double, complex<float> > is double
//
// complex_type< float > is complex<float>
// complex_type< float, double > is complex<double>
// complex_type< float, double, complex<float> > is complex<double>
// for zero types
template <typename... Types>
struct real_type_traits;
// define real_type<> type alias
template <typename... Types>
using real_type = typename real_type_traits< Types... >::real_t;
// define complex_type<> type alias
template <typename... Types>
using complex_type = std::complex< real_type< Types... > >;
// for one type
template <typename T>
struct real_type_traits<T>
{
using real_t = T;
};
// for one complex type, strip complex
template <typename T>
struct real_type_traits< std::complex<T> >
{
using real_t = T;
};
//==================================================
//------------------------------------------------------------------------------
// Global constants
// numeric flag indicating no data; printed as "NA" instead of "nan"
double no_data_flag = nan("1234");
// -----------------------------------------------------------------------------
// ANSI color codes - enabled by default
#ifndef NO_COLOR
const char *ansi_esc = "\x1b[";
const char *ansi_red = "\x1b[31m";
const char *ansi_green = "\x1b[92m";
const char *ansi_blue = "\x1b[34m";
const char *ansi_cyan = "\x1b[36m";
const char *ansi_magenta = "\x1b[35m";
const char *ansi_yellow = "\x1b[33m";
const char *ansi_white = "\x1b[37m";
const char *ansi_gray = "\x1b[90m"; // "bright black"
const char *ansi_bold = "\x1b[1m";
const char *ansi_normal = "\x1b[0m";
#else
const char *ansi_esc = "";
const char *ansi_red = "";
const char *ansi_green = "";
const char *ansi_blue = "";
const char *ansi_cyan = "";
const char *ansi_magenta = "";
const char *ansi_yellow = "";
const char *ansi_white = "";
const char *ansi_gray = "";
const char *ansi_bold = "";
const char *ansi_normal = "";
#endif
//------------------------------------------------------------------------------
const char* DataType_help =
"one of:"
" r16, h, or half;"
" r32, s, single, or float;"
" r64, d, or double;"
" c32, c, or complex-float;"
" c64, z, or complex-double;"
" i, int, or integer";
// -----------------------------------------------------------------------------
// static class variables
std::vector< ParamBase* > ParamBase::s_params;
// -----------------------------------------------------------------------------
// Compare a == b, bitwise. Returns true if a and b are both the same NaN value,
// unlike (a == b) which is false for NaNs.
bool same( double a, double b )
{
return (memcmp( &a, &b, sizeof(double) ) == 0);
}
// -----------------------------------------------------------------------------
/// Throws a std::runtime_error, using a printf-formatted message in format
/// and subsequent arguments.
void throw_error( const char* format, ... )
{
char msg[1000];
va_list va;
va_start( va, format );
vsnprintf( msg, sizeof(msg), format, va );
throw std::runtime_error( msg );
}
//------------------------------------------------------------------------------
/// Scan string for metric or binary prefix and return multiplier.
/// E.g., for "k" returns 1000, for "Ki" returns 1024.
/// If no prefix is recognized, returns 1.
/// Also recognizes exponents "eN" for integer N >= 0.
///
/// metric | binary
/// -----------------------|----------------------------
/// k: kilo 1000 = 1e3 | Ki: kibi kilobinary 1024
/// M: mega 1000^2 = 1e6 | Mi: mebi megabinary 1024^2
/// G: giga 1000^3 = 1e9 | Gi: gibi gigabinary 1024^3
/// T: tera 1000^4 = 1e12 | Ti: tebi terabinary 1024^4
/// P: peta 1000^5 = 1e15 | Pi: pebi petabinary 1024^5
/// E: exa 1000^6 = 1e18 | Ei: exbi exabinary 1024^6
///
/// @param[in,out] pstr
/// Pointer to string to scan.
/// On exit, updated to point after any prefix that was scanned.
///
/// @return multiplier
///
int64_t scan_multiplier( const char **pstr )
{
const int64_t i1024 = 1024;
int exponent, bytes;
// Metric is lowercase k, but IEC is uppercase Ki for binary;
// ignore case for k only.
if (tolower(**pstr) == 'k') {
*pstr += 1;
if (**pstr == 'i') {
*pstr += 1;
return i1024;
}
else {
return 1e3;
}
}
else if (**pstr == 'M') {
*pstr += 1;
if (**pstr == 'i') {
*pstr += 1;
return i1024 * i1024;
}
else {
return 1e6;
}
}
else if (**pstr == 'G') {
*pstr += 1;
if (**pstr == 'i') {
*pstr += 1;
return i1024 * i1024 * i1024;
}
else {
return 1e9;
}
}
else if (**pstr == 'T') {
*pstr += 1;
if (**pstr == 'i') {
*pstr += 1;
return i1024 * i1024 * i1024 * i1024;
}
else {
return 1e12;
}
}
else if (**pstr == 'P') {
*pstr += 1;
if (**pstr == 'i') {
*pstr += 1;
return i1024 * i1024 * i1024 * i1024 * i1024;
}
else {
return 1e15;
}
}
else if (**pstr == 'E') {
*pstr += 1;
if (**pstr == 'i') {
*pstr += 1;
return i1024 * i1024 * i1024 * i1024 * i1024 * i1024;
}
else {
return 1e18;
}
}
else if (sscanf( *pstr, "e%d%n", &exponent, &bytes ) == 1) {
// pow() function can lead to undesireable rounding, 1e6 = 999999,
// so multiply with loop.
*pstr += bytes;
int64_t mul = 1;
for (int i = 0; i < exponent; ++i)
mul *= 10;
return mul;
}
else {
return 1;
}
}
///-----------------------------------------------------------------------------
/// Scans string for single integer or range of integers (start:end:step).
/// Advances the string to after the range or number.
///
/// @param[in,out] pstr: pointer to string containing an integer or range.
/// On output, advanced to after the number or range.
/// @param[out] start: start of range
/// @param[out] end: end of range
/// @param[out] step: step size; default is start; 0 if start = end.
///
/// @retval 1: failure
/// @retval 0: success
///
int scan_range( const char** pstr, int64_t* start, int64_t* end, int64_t* step )
{
long long start_ = 0, end_ = 0, step_ = 0;
int bytes;
const char* str = *pstr;
// cnt of numbers scanned (start, end, step).
int cnt = 0;
// Start number.
int info = sscanf( str, "%lld %n", &start_, &bytes );
if (info == 1) {
cnt += 1;
str += bytes;
start_ *= scan_multiplier( &str );
// End number.
info = sscanf( str, " : %lld %n", &end_, &bytes );
if (info == 1) {
cnt += 1;
str += bytes;
end_ *= scan_multiplier( &str );
// Step number.
info = sscanf( str, " : %lld %n", &step_, &bytes );
if (info == 1) {
cnt += 1;
str += bytes;
step_ *= scan_multiplier( &str );
}
}
}
*pstr = str;
*start = start_;
*end = end_;
*step = step_;
if (cnt == 3) {
if (*start == *end)
*step = 0;
}
else if (cnt == 2) {
if (*start == *end)
*step = 0;
else
*step = *start;
}
else if (cnt == 1) {
*end = *start;
*step = 0;
}
else {
return 1; // failure
}
return ! ((*step == 0 && *start == *end) ||
(*step > 0 && *start < *end) ||
(*step < 0 && *start > *end));
}
///-----------------------------------------------------------------------------
/// Scans string for a double or range of doubles (start:end:step).
/// Advances the string to after the number or range.
///
/// @param[in,out] pstr: pointer to string containing a double or range.
/// On output, advanced to after the number or range.
/// @param[out] start: start of range
/// @param[out] end: end of range
/// @param[out] step: step size; default is start; 0 if start = end.
///
/// @retval 1: failure
/// @retval 0: success
///
int scan_range( const char** pstr, double* start, double* end, double* step )
{
int bytes1, bytes2, bytes3, cnt;
cnt = sscanf( *pstr, "%lf %n: %lf %n: %lf %n",
start, &bytes1, end, &bytes2, step, &bytes3 );
if (cnt == 3) {
if (*start == *end)
*step = 0;
*pstr += bytes3;
}
else if (cnt == 2) {
*pstr += bytes2;
if (*start == *end)
*step = 0;
else
*step = *start;
}
else if (cnt == 1) {
*pstr += bytes1;
*end = *start;
*step = 0;
}
else {
return 1; // failure
}
return ! ((*step == 0 && *start == *end) ||
(*step > 0 && *start < *end) ||
(*step < 0 && *start > *end));
}
// -----------------------------------------------------------------------------
// Flushes cache by allocating buffer of 2*cache size (in MiB),
// and writing it in parallel.
void flush_cache( size_t cache_size )
{
size_t len = 2 * cache_size * 1024 * 1024;
unsigned char *buf = (unsigned char*) malloc( len );
int nthread = 1;
#pragma omp parallel
#pragma omp master
{
#ifdef _OPENMP
nthread = omp_get_num_threads();
#endif
}
size_t per_core = len / nthread;
#pragma omp parallel
{
int tid = 0;
#ifdef _OPENMP
tid = omp_get_thread_num();
#endif
for (size_t i = tid * per_core; i < (tid + 1) * per_core; ++i) {
buf[i] = i % 256;
}
}
free( buf );
}
// =============================================================================
// ParamBase class
// -----------------------------------------------------------------------------
// if name has \n, for line=0, print 1st line; for line=1, print 2nd line.
// otherwise, for line=0, print blank; for line=1, print name.
// virtual
void ParamBase::header( int line ) const
{
if (used_ && width_ > 0) {
size_t i = name_.find( '\n' );
std::string str;
if (i != std::string::npos) {
str = (line == 0
? name_.substr( 0, i ).c_str()
: name_.substr( i+1 ).c_str() );
}
else {
str = (line == 0
? ""
: name_.c_str() );
}
printf( "%*s ", width_, str.c_str() );
}
}
// -----------------------------------------------------------------------------
// virtual
void ParamBase::help() const
{
if (type_ == ParamType::Value || type_ == ParamType::List) {
printf( " %-16s %s\n", option_.c_str(), help_.c_str() );
}
}
// -----------------------------------------------------------------------------
// virtual
bool ParamBase::next()
{
assert( index_ >= 0 && index_ < size() );
if (index_ == size() - 1) {
index_ = 0;
return false;
}
else {
index_ += 1;
return true;
}
}
// =============================================================================
// ParamInt class
// Integer parameters
// -----------------------------------------------------------------------------
// virtual
void ParamInt::parse( const char *str )
{
while (true) {
int64_t start, end, step;
if (scan_range( &str, &start, &end, &step ) != 0) {
throw_error( "invalid argument at '%s',"
" expected integer or range start:end:step", str );
}
if (start == end) {
push_back( start );
}
else {
for (int64_t val = start; val <= end; val += step) {
push_back( val );
}
}
if (*str == '\0') {
break;
}
if (*str != ',' && *str != ';') {
throw_error( "invalid argument at '%s', expected comma", str );
}
str += 1;
}
}
// -----------------------------------------------------------------------------
void ParamInt::push_back( int64_t val )
{
if (val < min_value_ || val > max_value_) {
throw_error( "invalid argument, %lld outside [%lld, %lld]",
(long long) val,
(long long) min_value_,
(long long) max_value_ );
}
TParamBase<int64_t>::push_back( val );
}
// -----------------------------------------------------------------------------
// virtual
void ParamInt::print() const
{
if (used_ && width_ > 0) {
printf( "%*lld ", width_, (long long) values_[ index_ ] );
}
}
// -----------------------------------------------------------------------------
// virtual
void ParamInt::help() const
{
if (type_ == ParamType::Value || type_ == ParamType::List) {
printf( " %-16s %s; default %lld\n",
option_.c_str(), help_.c_str(), (long long) default_value_ );
}
}
// =============================================================================
// ParamOkay class
// same as ParamInt, but prints pass (for non-zero) or FAILED (for zero).
// -----------------------------------------------------------------------------
// virtual
void ParamOkay::print() const
{
if (used_ && width_ > 0) {
const char *msg = "";
switch (values_[ index_ ]) {
case 0: msg = "FAILED"; break;
case 1: msg = "pass"; break;
case no_check: msg = "no check"; break;
case skipped: msg = "skipped"; break;
}
printf( "%-*s ", width_, msg );
}
}
// =============================================================================
// ParamInt3 class
// Integer 3-tuple parameters for M x N x K dimensions
// -----------------------------------------------------------------------------
// virtual
void ParamInt3::parse( const char *str )
{
int64_t m_start, m_end, m_step;
int64_t n_start, n_end, n_step;
int64_t k_start, k_end, k_step;
int len;
while (true) {
// scan M
if (scan_range( &str, &m_start, &m_end, &m_step ) != 0) {
throw_error( "invalid m dimension at '%s', "
"expected integer or range start:end:step", str );
}
// if "*", use Cartesian product
// if "x", use "inner" product
// if "*" or "x", scan N; else K = N = M
len = 0;
bool cartesian = false;
sscanf( str, " * %n", &len );
if (len > 0)
cartesian = true;
else
sscanf( str, " x %n", &len );
if (len > 0) {
str += len;
if (scan_range( &str, &n_start, &n_end, &n_step ) != 0) {
throw_error( "invalid n dimension at '%s', "
"expected integer or range start:end:step", str );
}
// if "*" or "x", scan K; else K = N
len = 0;
if (cartesian)
sscanf( str, " * %n", &len );
else
sscanf( str, " x %n", &len );
if (len > 0) {
str += len;
if (scan_range( &str, &k_start, &k_end, &k_step ) != 0) {
throw_error( "invalid k dimension at '%s', "
"expected integer or range start:end:step", str );
}
}
else {
k_start = n_start;
k_end = n_end;
k_step = n_step;
}
}
else {
k_start = n_start = m_start;
k_end = n_end = m_end;
k_step = n_step = m_step;
}
if (m_start == m_end && n_start == n_end && k_start == k_end) {
// single size
int3_t dim = { m_start, n_start, k_start };
push_back( dim );
}
else if (cartesian) {
// Cartesian product of M x N x K
// require non-zero step
if (m_step == 0) m_step = 1;
if (n_step == 0) n_step = 1;
if (k_step == 0) k_step = 1;
for (int64_t m = m_start;
(m_step >= 0 ? m <= m_end : m >= m_end);
m += m_step) {
for (int64_t n = n_start;
(n_step >= 0 ? n <= n_end : n >= n_end);
n += n_step) {
for (int64_t k = k_start;
(k_step >= 0 ? k <= k_end : k >= k_end);
k += k_step)
{
int3_t dim = { m, n, k };
push_back( dim );
}
}
}
}
else {
// inner product of M x N x K
// at least one of the variables must advance
assert( m_step != 0 || n_step != 0 || k_step != 0 );
for (int64_t m = m_start,
n = n_start,
k = k_start;
(m_step >= 0 ? m <= m_end : m >= m_end) &&
(n_step >= 0 ? n <= n_end : n >= n_end) &&
(k_step >= 0 ? k <= k_end : k >= k_end);
m += m_step,
n += n_step,
k += k_step)
{
int3_t dim = { m, n, k };
push_back( dim );
}
}
if (*str == '\0') {
break;
}
if (*str != ',' && *str != ';') {
throw_error( "invalid argument at '%s', expected comma", str );
}
str += 1;
}
}
// -----------------------------------------------------------------------------
void ParamInt3::push_back( int3_t val )
{
if (val.m < min_value_ || val.m > max_value_ ||
val.n < min_value_ || val.n > max_value_ ||
val.k < min_value_ || val.k > max_value_)
{
throw_error( "invalid value, %lld x %lld x %lld outside [%lld, %lld]",
(long long) val.m,
(long long) val.n,
(long long) val.k,
(long long) min_value_,
(long long) max_value_ );
}
TParamBase<int3_t>::push_back( val );
}
// -----------------------------------------------------------------------------
// virtual
void ParamInt3::print() const
{
if (width_ > 0) {
if (used_ & m_mask) {
printf( "%*lld ", width_, (long long) values_[ index_ ].m );
}
if (used_ & n_mask) {
printf( "%*lld ", width_, (long long) values_[ index_ ].n );
}
if (used_ & k_mask) {
printf( "%*lld ", width_, (long long) values_[ index_ ].k );
}
}
}
// -----------------------------------------------------------------------------
// for line=0, print blanks
// for line=1, print whichever of m, n, k are used
// virtual
void ParamInt3::header( int line ) const
{
if (width_ > 0) {
if (used_ & m_mask) {
printf( "%*s ", width_, (line == 0 ? "" : m_name_.c_str()) );
}
if (used_ & n_mask) {
printf( "%*s ", width_, (line == 0 ? "" : n_name_.c_str()) );
}
if (used_ & k_mask) {
printf( "%*s ", width_, (line == 0 ? "" : k_name_.c_str()) );
}
}
}
// =============================================================================
// ParamComplex class
// -----------------------------------------------------------------------------
// Scans single real or complex value and lists of values.
//
std::complex<double> ParamComplex::scan_complex( const char** str )
{
char op, suffix;
double x, y;
std::complex<double> value;
int cnt, bytes1, bytes2, bytes3;
cnt = sscanf( *str, "%lf %n %c %n %lf%c %n", &x, &bytes1, &op, &bytes2, &y, &suffix, &bytes3 );
if (cnt == 4 && (op == '+' || op == '-') && suffix == 'i') {
// x+yi or x-yi complex value
if (op == '+')
value = std::complex<double>( x, y );
else
value = std::complex<double>( x, -y );
*str += bytes3;
}
else if (cnt == 2 && op == 'i') {
// xi imaginary value
value = std::complex<double>( 0, x );
*str += bytes2;
}
else if (cnt == 1) {
// x real value
value = x;
*str += bytes1;
}
else if (cnt > 1 && op == ',') { //handles case of two real values "2.34,1.11", or real and img "2.34,1.11i"
// x real value
value = x;
*str += bytes2 - 1; //leave comma
}
else {
std::string err = std::string("invalid value '") + *str
+ "'; expected format like '1.2' or '1.2+3.4i'";
throw std::runtime_error( err );
}
return value;
}
// -----------------------------------------------------------------------------
// virtual
void ParamComplex::parse( const char *str )
{
//printf("ParamComplex::parse\n");
while (true) {
std::complex<double> val = scan_complex( &str );
TParamBase< std::complex<double> >::push_back( val );
if (*str == '\0') {
break;
}
if (*str != ',' && *str != ';') {
throw_error( "invalid argument at '%s', expected comma", str );
}
str += 1;
}
}
//------------------------------------------------------------------------------
// Output the entire field in a fixed width W,
// different than the width passed here. W = 2*width + 3,
// to account for initial -, [+-] between real & complex parts, and i at end.
// For instance, print calls snprintf_value with width=4, precision=2,
// and then prints the resulting string in a field width 11 (%-11s).
//
template <typename scalar_t>
const char* snprintf_value(
char* buf, size_t buf_len, int width, int precision, scalar_t value)
{
using real_t = real_type<scalar_t>;
real_t re = std::real( value );
real_t im = std::imag( value );
if (re == int(re) && im == int(im)) {
// exactly integer, print without digits after decimal point
if (im != 0) {
snprintf(buf, buf_len, "% *.0f%c%.0fi",
width - precision, re,
(im >= 0 ? '+' : '-'),
std::abs(im));
}
else {
snprintf(buf, buf_len, "% *.0f%*s",
width - precision, re, precision, "");
}
}
else {
// general case
if (im != 0) {
snprintf(buf, buf_len, "% *.*f%c%.*fi",
width, precision, re,
(im >= 0 ? '+' : '-'),
precision, std::abs(im));
}
else {
snprintf(buf, buf_len, "% *.*f",
width, precision, re);
}
}
return buf;
}
// -----------------------------------------------------------------------------
/// If field has been used, prints the value.
/// If value is set to no_data_flag, it prints "NA".
/// The output width and precision are set in the constructor.
// virtual
void ParamComplex::print() const
{
char buf[ 1000 ];
if (used_ && display_width_ > 0) {
if (same( no_data_flag, values_[ index_ ].real() )) { //TODO: check also for imaginary?
printf( "%*s ", display_width_, "NA" );
}
else {
snprintf_value( buf, sizeof(buf), display_width_, precision_,
values_[ index_ ] );
printf( "%-*s ", width_, buf);
}
}
}
// -----------------------------------------------------------------------------
// virtual
void ParamComplex::help() const
{
if (type_ == ParamType::Value || type_ == ParamType::List) {
printf( " %-16s %s; default ",
option_.c_str(), help_.c_str() );
if (same( no_data_flag, default_value_.real() )) {
printf( "NA\n" );
}
else {
char buf[ 1000 ];
snprintf_value( buf, sizeof(buf), display_width_, precision_,
default_value_ );
printf( "%s\n", buf );
}
}
}
// =============================================================================
// ParamDouble class
// Double precision parameter
// -----------------------------------------------------------------------------
// virtual
void ParamDouble::parse( const char *str )
{
double start = 0, end = 0, step = 0;
while (true) {
if (scan_range( &str, &start, &end, &step ) != 0) {
throw_error( "invalid argument at '%s', "
"expected float or range start:end:step", str );
}
if (start == end) {
push_back( start );
}
else {
end += step / 10.; // avoid rounding issues
for (double val = start; val <= end; val += step) {
push_back( val );
}
}
if (*str == '\0') {
break;
}
if (*str != ',' && *str != ';') {
throw_error( "invalid argument at '%s', expected comma", str );
}
str += 1;
}
}
// -----------------------------------------------------------------------------
void ParamDouble::push_back( double val )
{
if (val < min_value_ || val > max_value_) {
throw_error( "invalid argument, %.*f outside [%.*f, %.*f]",
precision_, val,
precision_, min_value_,
precision_, max_value_ );
}
TParamBase<double>::push_back( val );
}
// -----------------------------------------------------------------------------
/// If field has been used, prints the floating point value.
/// If value is set to no_data_flag, it prints "NA".
/// If value < 1, it prints with precision (p) significant digits (%.pg).
/// Otherwise, it prints with precision (p) digits after the decimal point (%.pf).
/// The output width and precision are set in the constructor.
// virtual
void ParamDouble::print() const
{
if (used_ && width_ > 0) {
if (same( no_data_flag, values_[ index_ ] )) {
printf( "%*s ", width_, "NA" );
}
else {
if (std::abs( values_[ index_ ] ) < 1)
printf( "%#*.*g ", width_, precision_, values_[ index_ ] );
else
printf( "%*.*f ", width_, precision_, values_[ index_ ] );
}
}
}
// -----------------------------------------------------------------------------
// virtual
void ParamDouble::help() const
{
if (type_ == ParamType::Value || type_ == ParamType::List) {
printf( " %-16s %s; default ",
option_.c_str(), help_.c_str() );
if (same( no_data_flag, default_value_ )) {
printf( "NA\n" );
}
else {
printf( "%.*f\n", precision_, default_value_ );
}
}
}
// =============================================================================
// ParamScientific class
// same as ParamDouble, but prints using scientific notation (%e)
// -----------------------------------------------------------------------------
// virtual
void ParamScientific::print() const
{
if (used_ && width_ > 0) {
if (same( no_data_flag, values_[ index_ ] )) {
printf( "%*s ", width_, "NA" );
}
else {
printf( "%*.*e ", width_, precision_, values_[ index_ ] );
}
}
}
// -----------------------------------------------------------------------------
// virtual
void ParamScientific::help() const
{
if (type_ == ParamType::Value || type_ == ParamType::List) {
printf( " %-16s %s; default ",
option_.c_str(), help_.c_str() );
if (same( no_data_flag, default_value_ )) {
printf( "NA\n" );
}
else {
printf( "%.*e\n", precision_, default_value_ );
}
}
}
// =============================================================================
// ParamString class
// String parameters
// -----------------------------------------------------------------------------
// Return true if str is in the list of valid strings.
// If no valid strings are set, always returns true.
bool ParamString::is_valid( const std::string& str )
{
if (valid_.size() == 0)
return true;
for (auto iter = valid_.begin(); iter != valid_.end(); ++iter) {
if (str == *iter)
return true;
}
return false;
}
// -----------------------------------------------------------------------------
// virtual
void ParamString::parse( const char *str )
{
char* copy = strdup( str );
char* token = strtok( copy, "," );
while (token != nullptr) {
push_back( token );
token = strtok( nullptr, "," );
}
free( copy );
}
// -----------------------------------------------------------------------------
void ParamString::push_back( const char* str )
{
if (! is_valid(str)) {
throw_error( "invalid argument '%s'", str );
}
TParamBase< std::string >::push_back( str );
}
// -----------------------------------------------------------------------------
// virtual
void ParamString::print() const
{
if (used_ && width_ > 0) {
printf( "%-*s ", width_, values_[ index_ ].c_str() );
}
}
// -----------------------------------------------------------------------------
// virtual
void ParamString::help() const
{