Doxygen 1.15.0
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2
Loading...
Searching...
No Matches
tsgGridGlobal.hpp
1/*
2 * Copyright (c) 2017, Miroslav Stoyanov
3 *
4 * This file is part of
5 * Toolkit for Adaptive Stochastic Modeling And Non-Intrusive ApproximatioN: TASMANIAN
6 *
7 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
12 * and the following disclaimer in the documentation and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * UT-BATTELLE, LLC AND THE UNITED STATES GOVERNMENT MAKE NO REPRESENTATIONS AND DISCLAIM ALL WARRANTIES, BOTH EXPRESSED AND IMPLIED.
25 * THERE ARE NO EXPRESS OR IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE ANY PATENT,
26 * COPYRIGHT, TRADEMARK, OR OTHER PROPRIETARY RIGHTS, OR THAT THE SOFTWARE WILL ACCOMPLISH THE INTENDED RESULTS OR THAT THE SOFTWARE OR ITS USE WILL NOT RESULT IN INJURY OR DAMAGE.
27 * THE USER ASSUMES RESPONSIBILITY FOR ALL LIABILITIES, PENALTIES, FINES, CLAIMS, CAUSES OF ACTION, AND COSTS AND EXPENSES, CAUSED BY, RESULTING FROM OR ARISING OUT OF,
28 * IN WHOLE OR IN PART THE USE, STORAGE OR DISPOSAL OF THE SOFTWARE.
29 */
30
31#ifndef __TASMANIAN_SPARSE_GRID_GLOBAL_HPP
32#define __TASMANIAN_SPARSE_GRID_GLOBAL_HPP
33
34#include "tsgGridSequence.hpp"
35#include "tsgCacheLagrange.hpp"
36
37namespace TasGrid{
38
39#ifndef __TASMANIAN_DOXYGEN_SKIP
40class GridGlobal : public BaseCanonicalGrid{
41public:
42 GridGlobal(AccelerationContext const *acc) : BaseCanonicalGrid(acc), rule(rule_none), alpha(0.0), beta(0.0){}
43 friend struct GridReaderVersion5<GridGlobal>;
44 GridGlobal(AccelerationContext const *acc, const GridGlobal *global, int ibegin, int iend);
45 GridGlobal(AccelerationContext const *acc, int cnum_dimensions, int cnum_outputs, int depth, TypeDepth type, TypeOneDRule crule, const std::vector<int> &anisotropic_weights, double calpha, double cbeta, const char* custom_filename, const std::vector<int> &level_limits) : BaseCanonicalGrid(acc){
46 makeGrid(cnum_dimensions, cnum_outputs, depth, type, crule, anisotropic_weights, calpha, cbeta, custom_filename, level_limits);
47 }
48 GridGlobal(AccelerationContext const *acc, int cnum_dimensions, int cnum_outputs, int depth, TypeDepth type,
49 CustomTabulated &&crule, const std::vector<int> &anisotropic_weights, const std::vector<int> &level_limits)
50 : BaseCanonicalGrid(acc), custom(std::move(crule))
51 {
52 setTensors(selectTensors((size_t) cnum_dimensions, depth, type, anisotropic_weights, rule_customtabulated, level_limits),
53 cnum_outputs, rule_customtabulated, 0.0, 0.0);
54 }
55
56 bool isGlobal() const override{ return true; }
57
58 void write(std::ostream &os, bool iomode) const override{ if (iomode == mode_ascii) write<mode_ascii>(os); else write<mode_binary>(os); }
59
60 template<bool iomode> void write(std::ostream &os) const;
61
62 void makeGrid(int cnum_dimensions, int cnum_outputs, int depth, TypeDepth type, TypeOneDRule crule, const std::vector<int> &anisotropic_weights, double calpha, double cbeta, const char* custom_filename, const std::vector<int> &level_limits);
63
64 void setTensors(MultiIndexSet &&tset, int cnum_outputs, TypeOneDRule crule, double calpha, double cbeta);
65
66 void updateGrid(int depth, TypeDepth type, const std::vector<int> &anisotropic_weights, const std::vector<int> &level_limits);
67
68 TypeOneDRule getRule() const override{ return rule; }
69 const char* getCustomRuleDescription() const{ return (custom.getNumLevels() > 0) ? custom.getDescription() : ""; }
70
71 double getAlpha() const{ return alpha; }
72 double getBeta() const{ return beta; }
73
74 void getLoadedPoints(double *x) const override;
75 void getNeededPoints(double *x) const override;
76 void getPoints(double *x) const override; // returns the loaded points unless no points are loaded, then returns the needed points
77
78 void getQuadratureWeights(double weights[]) const override;
79 void getInterpolationWeights(const double x[], double weights[]) const override;
80 void getDifferentiationWeights(const double x[], double weights[]) const override;
81
82 void loadNeededValues(const double *vals) override;
83
84 void evaluate(const double x[], double y[]) const override;
85 void integrate(double q[], double *conformal_correction) const override;
86 void differentiate(const double x[], double jacobian[]) const override;
87
88 void evaluateBatch(const double x[], int num_x, double y[]) const override;
89
90 void evaluateBatchGPU(const double[], int, double[]) const override;
91 void evaluateBatchGPU(const float[], int, float[]) const override;
92 template<typename T> void evaluateBatchGPUtempl(T const[], int, T *) const;
93 void evaluateHierarchicalFunctionsGPU(const double[], int, double *) const override;
94 void evaluateHierarchicalFunctionsGPU(const float[], int, float *) const override;
95 template<typename T> void evaluateHierarchicalFunctionsGPUtempl(T const[], int, T *) const;
96
97 void estimateAnisotropicCoefficients(TypeDepth type, int output, std::vector<int> &weights) const;
98
99 void setAnisotropicRefinement(TypeDepth type, int min_growth, int output, const std::vector<int> &level_limits);
100 void setSurplusRefinement(double tolerance, int output, const std::vector<int> &level_limits);
101 void clearRefinement() override;
102 void mergeRefinement() override;
103
104 void beginConstruction() override;
105 void writeConstructionData(std::ostream &os, bool) const override;
106 void readConstructionData(std::istream &is, bool) override;
107 std::vector<double> getCandidateConstructionPoints(TypeDepth type, const std::vector<int> &weights, const std::vector<int> &level_limits);
108 std::vector<double> getCandidateConstructionPoints(TypeDepth type, int output, const std::vector<int> &level_limits);
109 std::vector<double> getCandidateConstructionPoints(std::function<double(const int *)> getTensorWeight, const std::vector<int> &level_limits);
110 void loadConstructedPoint(const double x[], const std::vector<double> &y) override;
111 void loadConstructedPoint(const double x[], int numx, const double y[]) override;
112 void finishConstruction() override;
113
114 void evaluateHierarchicalFunctions(const double x[], int num_x, double y[]) const override;
115 void setHierarchicalCoefficients(const double c[]) override;
116 void integrateHierarchicalFunctions(double integrals[]) const override;
117
118 void updateAccelerationData(AccelerationContext::ChangeType change) const override;
119
120 std::vector<int> getPolynomialSpace(bool interpolation) const;
121
122protected:
123 std::vector<double> computeSurpluses(int output, bool normalize) const; // only for sequence rules, select the output to compute the surpluses
124
125 static double legendre(int n, double x);
126
127 // assumes that if rule == rule_customtabulated, then custom is already loaded
128 MultiIndexSet selectTensors(size_t dims, int depth, TypeDepth type, const std::vector<int> &anisotropic_weights,
129 TypeOneDRule rule, std::vector<int> const &level_limits) const;
130
131 void recomputeTensorRefs(const MultiIndexSet &work);
132 void proposeUpdatedTensors();
133 void acceptUpdatedTensors();
134 MultiIndexSet getPolynomialSpaceSet(bool interpolation) const;
135
136 void loadConstructedTensors();
137 std::vector<int> getMultiIndex(const double x[]);
138
139 void clearGpuValues() const;
140 void clearGpuNodes() const;
141
142private:
143 TypeOneDRule rule;
144 double alpha, beta;
145
146 OneDimensionalWrapper wrapper;
147
148 MultiIndexSet tensors;
149 MultiIndexSet active_tensors;
150 std::vector<int> active_w;
151
152 std::vector<std::vector<int>> tensor_refs;
153
154 std::vector<int> max_levels; // for evaluation purposes, counts the maximum level in each direction (only counts tensors)
155
156 MultiIndexSet updated_tensors;
157 MultiIndexSet updated_active_tensors;
158 std::vector<int> updated_active_w;
159
160 CustomTabulated custom;
161
162 std::unique_ptr<DynamicConstructorDataGlobal> dynamic_values;
163
164 template<typename T> void loadGpuNodes() const;
165 template<typename T> void loadGpuValues() const;
166 inline std::unique_ptr<CudaGlobalData<double>>& getGpuCacheOverload(double) const{ return gpu_cache; }
167 inline std::unique_ptr<CudaGlobalData<float>>& getGpuCacheOverload(float) const{ return gpu_cachef; }
168 template<typename T> inline std::unique_ptr<CudaGlobalData<T>>& getGpuCache() const{
169 return getGpuCacheOverload(static_cast<T>(0.0));
170 }
171 mutable std::unique_ptr<CudaGlobalData<double>> gpu_cache;
172 mutable std::unique_ptr<CudaGlobalData<float>> gpu_cachef;
173};
174
175// Old version reader
176template<> struct GridReaderVersion5<GridGlobal>{
177 template<typename iomode> static std::unique_ptr<GridGlobal> read(AccelerationContext const *acc, std::istream &is){
178 std::unique_ptr<GridGlobal> grid = Utils::make_unique<GridGlobal>(acc);
179
180 grid->num_dimensions = IO::readNumber<iomode, int>(is);
181 grid->num_outputs = IO::readNumber<iomode, int>(is);
182 grid->alpha = IO::readNumber<iomode, double>(is);
183 grid->beta = IO::readNumber<iomode, double>(is);
184 grid->rule = IO::readRule<iomode>(is);
185 if (grid->rule == rule_customtabulated) grid->custom = CustomTabulated(is, iomode());
186 grid->tensors = MultiIndexSet(is, iomode());
187 grid->active_tensors = MultiIndexSet(is, iomode());
188 grid->active_w = IO::readVector<iomode, int>(is, grid->active_tensors.getNumIndexes());
189
190 if (IO::readFlag<iomode>(is)) grid->points = MultiIndexSet(is, iomode());
191 if (IO::readFlag<iomode>(is)) grid->needed = MultiIndexSet(is, iomode());
192
193 grid->max_levels = IO::readVector<iomode, int>(is, grid->num_dimensions);
194
195 if (grid->num_outputs > 0) grid->values = StorageSet(is, iomode());
196
197 int oned_max_level;
198 if (IO::readFlag<iomode>(is)){
199 grid->updated_tensors = MultiIndexSet(is, iomode());
200 oned_max_level = grid->updated_tensors.getMaxIndex();
201
202 grid->updated_active_tensors = MultiIndexSet(is, iomode());
203
204 grid->updated_active_w = IO::readVector<iomode, int>(is, grid->updated_active_tensors.getNumIndexes());
205 }else{
206 oned_max_level = *std::max_element(grid->max_levels.begin(), grid->max_levels.end());
207 }
208
209 grid->wrapper = OneDimensionalWrapper(grid->custom, oned_max_level, grid->rule, grid->alpha, grid->beta);
210
211 grid->recomputeTensorRefs((grid->points.empty()) ? grid->needed : grid->points);
212
213 return grid;
214 }
215};
216
217#endif // __TASMANIAN_DOXYGEN_SKIP
218
219}
220
221#endif
TypeOneDRule
Used to specify the one dimensional family of rules that induces the sparse grid.
Definition tsgEnumerates.hpp:285
@ rule_none
Null rule, should never be used as input (default rule for an empty grid).
Definition tsgEnumerates.hpp:287
void loadNeededValues(std::function< void(double const x[], double y[], size_t thread_id)> model, TasmanianSparseGrid &grid, size_t num_threads)
Loads the current grid with model values, does not perform any refinement.
Definition tsgLoadNeededValues.hpp:104
Encapsulates the Tasmanian Sparse Grid module.
Definition TasmanianSparseGrid.hpp:68