Doxygen 1.17.0
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.3
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 final : 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 GridGlobal(GridGlobal &&) = default;
57 GridGlobal &operator = (GridGlobal &&) = default;
58
59 bool isGlobal() const override{ return true; }
60
61 void write(std::ostream &os, bool iomode) const override{ if (iomode == mode_ascii) write<mode_ascii>(os); else write<mode_binary>(os); }
62
63 template<bool iomode> void write(std::ostream &os) const;
64
65 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);
66
67 void setTensors(MultiIndexSet &&tset, int cnum_outputs, TypeOneDRule crule, double calpha, double cbeta);
68
69 void updateGrid(int depth, TypeDepth type, const std::vector<int> &anisotropic_weights, const std::vector<int> &level_limits);
70
71 TypeOneDRule getRule() const override{ return rule; }
72 const char* getCustomRuleDescription() const{ return (custom.getNumLevels() > 0) ? custom.getDescription() : ""; }
73
74 double getAlpha() const{ return alpha; }
75 double getBeta() const{ return beta; }
76
77 void getLoadedPoints(double *x) const override;
78 void getNeededPoints(double *x) const override;
79 void getPoints(double *x) const override; // returns the loaded points unless no points are loaded, then returns the needed points
80
81 void getQuadratureWeights(double weights[]) const override;
82 void getInterpolationWeights(const double x[], double weights[]) const override;
83 void getDifferentiationWeights(const double x[], double weights[]) const override;
84
85 void loadNeededValues(const double *vals) override;
86
87 void evaluate(const double x[], double y[]) const override;
88 void integrate(double q[], double *conformal_correction) const override;
89 void differentiate(const double x[], double jacobian[]) const override;
90
91 void evaluateBatch(const double x[], int num_x, double y[]) const override;
92
93 void evaluateBatchGPU(const double[], int, double[]) const override;
94 void evaluateBatchGPU(const float[], int, float[]) const override;
95 template<typename T> void evaluateBatchGPUtempl(T const[], int, T *) const;
96 void evaluateHierarchicalFunctionsGPU(const double[], int, double *) const override;
97 void evaluateHierarchicalFunctionsGPU(const float[], int, float *) const override;
98 template<typename T> void evaluateHierarchicalFunctionsGPUtempl(T const[], int, T *) const;
99
100 void estimateAnisotropicCoefficients(TypeDepth type, int output, std::vector<int> &weights) const;
101
102 void setAnisotropicRefinement(TypeDepth type, int min_growth, int output, const std::vector<int> &level_limits);
103 void setSurplusRefinement(double tolerance, int output, const std::vector<int> &level_limits);
104 void clearRefinement() override;
105 void mergeRefinement() override;
106
107 void beginConstruction() override;
108 void writeConstructionData(std::ostream &os, bool) const override;
109 void readConstructionData(std::istream &is, bool) override;
110 std::vector<double> getCandidateConstructionPoints(TypeDepth type, const std::vector<int> &weights, const std::vector<int> &level_limits);
111 std::vector<double> getCandidateConstructionPoints(TypeDepth type, int output, const std::vector<int> &level_limits);
112 std::vector<double> getCandidateConstructionPoints(std::function<double(const int *)> getTensorWeight, const std::vector<int> &level_limits);
113 void loadConstructedPoint(const double x[], const std::vector<double> &y) override;
114 void loadConstructedPoint(const double x[], int numx, const double y[]) override;
115 void finishConstruction() override;
116
117 void evaluateHierarchicalFunctions(const double x[], int num_x, double y[]) const override;
118 void setHierarchicalCoefficients(const double c[]) override;
119 void integrateHierarchicalFunctions(double integrals[]) const override;
120
121 void updateAccelerationData(AccelerationContext::ChangeType change) const override;
122
123 std::vector<int> getPolynomialSpace(bool interpolation) const;
124
125protected:
126 std::vector<double> computeSurpluses(int output, bool normalize) const; // only for sequence rules, select the output to compute the surpluses
127
128 static double legendre(int n, double x);
129
130 // assumes that if rule == rule_customtabulated, then custom is already loaded
131 MultiIndexSet selectTensors(size_t dims, int depth, TypeDepth type, const std::vector<int> &anisotropic_weights,
132 TypeOneDRule rule, std::vector<int> const &level_limits) const;
133
134 void recomputeTensorRefs(const MultiIndexSet &work);
135 void proposeUpdatedTensors();
136 void acceptUpdatedTensors();
137 MultiIndexSet getPolynomialSpaceSet(bool interpolation) const;
138
139 void loadConstructedTensors();
140 std::vector<int> getMultiIndex(const double x[]);
141
142 void clearGpuValues() const;
143 void clearGpuNodes() const;
144
145private:
146 TypeOneDRule rule;
147 double alpha, beta;
148
149 OneDimensionalWrapper wrapper;
150
151 MultiIndexSet tensors;
152 MultiIndexSet active_tensors;
153 std::vector<int> active_w;
154
155 std::vector<std::vector<int>> tensor_refs;
156
157 std::vector<int> max_levels; // for evaluation purposes, counts the maximum level in each direction (only counts tensors)
158
159 MultiIndexSet updated_tensors;
160 MultiIndexSet updated_active_tensors;
161 std::vector<int> updated_active_w;
162
163 CustomTabulated custom;
164
165 template<typename T> void loadGpuNodes() const;
166 template<typename T> void loadGpuValues() const;
167 inline std::optional<CudaGlobalData<double>>& getGpuCacheOverload(double) const{ return gpu_cache; }
168 inline std::optional<CudaGlobalData<float>>& getGpuCacheOverload(float) const{ return gpu_cachef; }
169 template<typename T> inline std::optional<CudaGlobalData<T>>& getGpuCache() const{
170 return getGpuCacheOverload(static_cast<T>(0.0));
171 }
172 mutable std::optional<CudaGlobalData<double>> gpu_cache;
173 mutable std::optional<CudaGlobalData<float>> gpu_cachef;
174
175 std::unique_ptr<DynamicConstructorDataGlobal> dynamic_values;
176};
177
178// Old version reader
179template<> struct GridReaderVersion5<GridGlobal>{
180 template<typename iomode> static void read(std::istream &is, GridGlobal *grid) {
181 grid->num_dimensions = IO::readNumber<iomode, int>(is);
182 grid->num_outputs = IO::readNumber<iomode, int>(is);
183 grid->alpha = IO::readNumber<iomode, double>(is);
184 grid->beta = IO::readNumber<iomode, double>(is);
185 grid->rule = IO::readRule<iomode>(is);
186 if (grid->rule == rule_customtabulated) grid->custom = CustomTabulated(is, iomode());
187 grid->tensors = MultiIndexSet(is, iomode());
188 grid->active_tensors = MultiIndexSet(is, iomode());
189 grid->active_w = IO::readVector<iomode, int>(is, grid->active_tensors.getNumIndexes());
190
191 if (IO::readFlag<iomode>(is)) grid->points = MultiIndexSet(is, iomode());
192 if (IO::readFlag<iomode>(is)) grid->needed = MultiIndexSet(is, iomode());
193
194 grid->max_levels = IO::readVector<iomode, int>(is, grid->num_dimensions);
195
196 if (grid->num_outputs > 0) grid->values = StorageSet(is, iomode());
197
198 int oned_max_level;
199 if (IO::readFlag<iomode>(is)){
200 grid->updated_tensors = MultiIndexSet(is, iomode());
201 oned_max_level = grid->updated_tensors.getMaxIndex();
202
203 grid->updated_active_tensors = MultiIndexSet(is, iomode());
204
205 grid->updated_active_w = IO::readVector<iomode, int>(is, grid->updated_active_tensors.getNumIndexes());
206 }else{
207 oned_max_level = *std::max_element(grid->max_levels.begin(), grid->max_levels.end());
208 }
209
210 grid->wrapper = OneDimensionalWrapper(grid->custom, oned_max_level, grid->rule, grid->alpha, grid->beta);
211
212 grid->recomputeTensorRefs((grid->points.empty()) ? grid->needed : grid->points);
213 }
214};
215
216#endif // __TASMANIAN_DOXYGEN_SKIP
217
218}
219
220#endif
TypeOneDRule
Used to specify the one dimensional family of rules that induces the sparse grid.
Definition tsgEnumerates.hpp:288
@ rule_none
Null rule, should never be used as input (default rule for an empty grid).
Definition tsgEnumerates.hpp:290
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