File: benchCommon.hpp

package info (click to toggle)
tasmanian 8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,852 kB
  • sloc: cpp: 34,523; python: 7,039; f90: 5,080; makefile: 224; sh: 64; ansic: 8
file content (236 lines) | stat: -rw-r--r-- 8,749 bytes parent folder | download | duplicates (2)
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
#ifndef _TASMANIAN_BENCHMARK_COMMON_HPP
#define _TASMANIAN_BENCHMARK_COMMON_HPP

#include <chrono>

#include "gridtestCLICommon.hpp"

enum BenchFuction{
    bench_none,
    bench_make,
    bench_loadneeded,
    bench_evaluate,
    bench_evaluate_mixed,
    bench_differentiate,
    bench_iweights,
    bench_refine
};

BenchFuction getTest(std::string const &s){
    std::map<std::string, BenchFuction> str_to_test = {
        {"evaluate", bench_evaluate},
        {"evaluate-mixed", bench_evaluate_mixed},
        {"differentiate", bench_differentiate},
        {"loadneeded", bench_loadneeded},
        {"makegrid", bench_make},
        {"iweights", bench_iweights},
        {"refine", bench_refine}
    };

    try{
        return str_to_test.at(s);
    }catch(std::out_of_range &){
        cout << "ERROR: Unknown test: " << s << endl;
        return bench_none;
    }
}

enum class GridFamily{
    none, global, sequence, localp, fourier, wavelet
};

GridFamily getGridFamily(std::string const &s){
    std::map<std::string, GridFamily> str_to_rule = {
        {"global",   GridFamily::global},
        {"sequence", GridFamily::sequence},
        {"localp",   GridFamily::localp},
        {"fourier",  GridFamily::fourier},
        {"wavelet",  GridFamily::wavelet},
    };

    GridFamily grid_family = GridFamily::none;
    try{
        grid_family = str_to_rule.at(s);
    }catch(std::out_of_range &){
        cout << "ERROR: Unknown grid type: " << s << endl;

    }
    return grid_family;
}

//! \brief Convert \b s.front() to a grid type indicated by the canonical rules, then \b s.pop_front() the processed string.
GridFamily getGridFamily(std::deque<std::string> &s){
    GridFamily grid_family = getGridFamily(s.front());
    s.pop_front();
    return grid_family;
}

//! \brief Convert a string to int and advance the iterator.
template<typename StringListIterator>
void readEntry(StringListIterator &iter, int &val){
    val = std::stoi(*iter++);
}
//! \brief Convert a string to double and advance the iterator.
template<typename StringListIterator>
void readEntry(StringListIterator &iter, double &val){
    val = std::stod(*iter++);
}
//! \brief Convert a string to TypeDepth and advance the iterator.
template<typename StringListIterator>
void readEntry(StringListIterator &iter, TypeDepth &val){
    val = IO::getDepthTypeString(*iter++);
}
//! \brief Convert a string to TypeOneDRule and advance the iterator.
template<typename StringListIterator>
void readEntry(StringListIterator &iter, TypeOneDRule &val){
    val = IO::getRuleString(*iter++);
}
//! \brief Convert a string to TypeRefinement and advance the iterator.
template<typename StringListIterator>
void readEntry(StringListIterator &iter, TypeRefinement &val){
    val = IO::getTypeRefinementString(*iter++);
}
//! \brief Convert a string to TypeAcceleration and advance the iterator.
template<typename StringListIterator>
void readEntry(StringListIterator &iter, TypeAcceleration &val){
    val = AccelerationMeta::getIOAccelerationString((*iter++).c_str());
}

//! \brief Template to terminate recursion, read one entry of type \b ValType and return the iterator.
template<typename StringListIterator, typename ValType>
StringListIterator readEntries(StringListIterator iter, ValType &val){
    readEntry(iter, val);
    return iter;
}
//! \brief Template to read multiple entries from a string list, returns an iterator past the last processed string.
template<typename StringListIterator, typename ValType1, typename ValType2, typename...Other>
StringListIterator readEntries(StringListIterator iter, ValType1 &val1, ValType2 &val2, Other & ...others){
    readEntry(iter, val1);
    return readEntries(iter, val2, others...);
}

//! \brief If the current iterator is pointing to a string dense/sparse return the string and advance, else return "auto" and do nothing.
template<typename StringListIterator>
std::string checkFlavor(StringListIterator &iter, StringListIterator argend){
    if (iter == argend) return "auto";
    if (*iter == "sparse"){
        iter++;
        return "sparse";
    }else if (*iter == "dense"){
        iter++;
        return "dense";
    }else{
        return "auto";
    }
}

template<typename IteratorToList>
std::pair<std::vector<int>, std::vector<int>>
extractWeightsLimits(GridFamily grid_family, int num_dimensions, TypeDepth dtype,
                     IteratorToList &arg, IteratorToList const &argend){
    std::vector<int> anisotropic_weights;
    if (grid_family != GridFamily::localp && grid_family != GridFamily::wavelet){
        int num_weights = (OneDimensionalMeta::getControurType(dtype) == type_curved) ? 2 * num_dimensions : num_dimensions;
        for(int i=0; i<num_weights && arg != argend; i++)
            anisotropic_weights.push_back(std::stoi(*arg++));
    }
    std::vector<int> level_limits;
    for(int i=0; i<num_dimensions && arg != argend; i++)
        level_limits.push_back(std::stoi(*arg++));
    return std::make_pair(anisotropic_weights, level_limits);
}

inline std::function<TasmanianSparseGrid()>
getLambdaMakeGrid(GridFamily grid_family, int const &num_dimensions, const int &num_outputs,
                  int const &num_depth, TypeDepth const &dtype, TypeOneDRule const &rule, int const &order,
                  std::pair<std::vector<int>, std::vector<int>> const &extra){
    if (grid_family == GridFamily::global){
        return [&]()->TasmanianSparseGrid{
            return makeGlobalGrid(num_dimensions, num_outputs, num_depth, dtype, rule, extra.first, 0.0, 0.0, nullptr, extra.second);
        };
    }else if (grid_family == GridFamily::sequence){
        return [&]()->TasmanianSparseGrid{
            return makeSequenceGrid(num_dimensions, num_outputs, num_depth, dtype, rule, extra.first, extra.second);
        };
    }else if (grid_family == GridFamily::localp){
        return [&]()->TasmanianSparseGrid{
            return makeLocalPolynomialGrid(num_dimensions, num_outputs, num_depth, order, rule, extra.second);
        };
    }else if (grid_family == GridFamily::fourier){
        return [&]()->TasmanianSparseGrid{
            return makeFourierGrid(num_dimensions, num_outputs, num_depth, dtype, extra.first, extra.second);
        };
    }else{ // default - wavelet
        return [&]()->TasmanianSparseGrid{
            return makeWaveletGrid(num_dimensions, num_outputs, num_depth, order, extra.second);
        };
    }
}

template<typename T = double>
std::vector<T> getRandomVector(int dim1, int dim2, long int seed){
    std::vector<T> x(Utils::size_mult(dim1, dim2));
    std::minstd_rand park_miller(seed);
    std::uniform_real_distribution<T> unif(-1.0, 1.0);
    for(auto &v : x) v = unif(park_miller);
    return x;
}

template<typename T>
std::vector<std::vector<T>> getRandomVectors(int num_vectors, int dim1, int dim2){
    std::vector<std::vector<T>> result((size_t) num_vectors);
    long int seed = 44;
    for(auto &r : result) r = getRandomVector<T>(dim1, dim2, seed++);
    return result;
}

std::vector<double> getGenericModel(size_t num_dimensions, size_t num_outputs,
                                    std::vector<double> const &points){
    // model output k = k * exp(sum(x_1 ... x_dims))
    size_t num_points = points.size() / num_dimensions;
    std::vector<double> values(num_points * num_outputs);

    auto ip = points.begin();
    auto iv = values.begin();
    while(ip != points.end()){
        double exponent = std::exp( std::accumulate(ip, ip + num_dimensions, 0.0) );
        std::advance(ip, num_dimensions);
        for(size_t i = 0; i < num_outputs; i++)
            *iv++ = double(i) * exponent;
    }

    return values;
}

void loadGenericModel(TasmanianSparseGrid &grid){
    if (grid.getNumNeeded() == 0) return;

    auto values = getGenericModel((size_t) grid.getNumDimensions(),
                                  (size_t) grid.getNumOutputs(),
                                  grid.getNeededPoints());

    grid.loadNeededPoints(values);
}

struct DryRun{};
struct NoDryRun{};
struct NormalizedTime{};
struct RawTime{};
template<typename use_dry_run = NoDryRun, typename normalize = NormalizedTime>
int testMethod(int iteratons, std::function<void(int)> test){
    if (std::is_same<use_dry_run, DryRun>::value)
        test(iteratons-1);

    auto time_start = std::chrono::system_clock::now();
    for(int i=0; i<iteratons; i++) test(i);
    auto time_end = std::chrono::system_clock::now();

    long long elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(time_end - time_start).count();
    if (std::is_same<normalize, NormalizedTime>::value){
        return static_cast<int>( 0.5 + double(elapsed) / double(iteratons) );
    }else{
        return static_cast<int>(elapsed);
    }
}

#endif