Doxygen 1.15.0
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2
Loading...
Searching...
No Matches
tsgGridSequence.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_NESTED_HPP
32#define __TASMANIAN_SPARSE_GRID_GLOBAL_NESTED_HPP
33
34#include "tsgGridCore.hpp"
35
36namespace TasGrid{
37
38#ifndef __TASMANIAN_DOXYGEN_SKIP
39class GridSequence : public BaseCanonicalGrid{
40public:
41 GridSequence(AccelerationContext const *acc) : BaseCanonicalGrid(acc), rule(rule_none){}
42 friend struct GridReaderVersion5<GridSequence>;
43 GridSequence(AccelerationContext const *acc, const GridSequence *seq, int ibegin, int iend);
44 GridSequence(AccelerationContext const *acc, int cnum_dimensions, int cnum_outputs, int depth, TypeDepth type, TypeOneDRule crule, const std::vector<int> &anisotropic_weights, const std::vector<int> &level_limits);
45 GridSequence(AccelerationContext const *acc, int cnum_dimensions, int depth, TypeDepth type, TypeOneDRule crule, const std::vector<int> &anisotropic_weights, const std::vector<int> &level_limits);
46 GridSequence(AccelerationContext const *acc, MultiIndexSet &&pset, int cnum_outputs, TypeOneDRule crule);
47 ~GridSequence() = default;
48
49 bool isSequence() const override{ return true; }
50
51 void write(std::ostream &os, bool iomode) const override{ if (iomode == mode_ascii) write<mode_ascii>(os); else write<mode_binary>(os); }
52
53 template<bool iomode> void write(std::ostream &os) const;
54
55 void updateGrid(int depth, TypeDepth type, const std::vector<int> &anisotropic_weights, const std::vector<int> &level_limits);
56
57 TypeOneDRule getRule() const override{ return rule; }
58
59 void getLoadedPoints(double *x) const override;
60 void getNeededPoints(double *x) const override;
61 void getPoints(double *x) const override; // returns the loaded points unless no points are loaded, then returns the needed points
62
63 void getQuadratureWeights(double weights[]) const override;
64 void getInterpolationWeights(const double x[], double weights[]) const override;
65 void getDifferentiationWeights(const double x[], double weights[]) const override;
66
67 void loadNeededValues(const double *vals) override;
68
69 void evaluate(const double x[], double y[]) const override;
70 void integrate(double q[], double *conformal_correction) const override;
71 void differentiate(const double x[], double jacobian[]) const override;
72
73 void evaluateBatch(const double x[], int num_x, double y[]) const override;
74
75 void evaluateBatchGPU(const double gpu_x[], int cpu_num_x, double gpy_y[]) const override;
76 void evaluateHierarchicalFunctionsGPU(const double x[], int num_x, double y[]) const override;
77 void evaluateBatchGPU(const float gpu_x[], int cpu_num_x, float gpy_y[]) const override;
78 template<typename T> void evaluateBatchGPUtempl(const T gpu_x[], int cpu_num_x, T gpy_y[]) const;
79 void evaluateHierarchicalFunctionsGPU(const float gpu_x[], int num_x, float gpu_y[]) const override;
80
81 void evaluateHierarchicalFunctions(const double x[], int num_x, double y[]) const override;
82
83 void estimateAnisotropicCoefficients(TypeDepth type, int output, std::vector<int> &weights) const;
84 void setAnisotropicRefinement(TypeDepth type, int min_growth, int output, const std::vector<int> &level_limits);
85 void setSurplusRefinement(double tolerance, int output, const std::vector<int> &level_limits);
86 void clearRefinement() override;
87 void mergeRefinement() override;
88
89 void beginConstruction() override;
90 void writeConstructionData(std::ostream &ofs, bool) const override;
91 void readConstructionData(std::istream &ifs, bool) override;
92 std::vector<double> getCandidateConstructionPoints(TypeDepth type, const std::vector<int> &weights, const std::vector<int> &level_limits);
93 std::vector<double> getCandidateConstructionPoints(TypeDepth type, int output, const std::vector<int> &level_limits);
94 std::vector<double> getCandidateConstructionPoints(std::function<double(const int *)> getTensorWeight, const std::vector<int> &level_limits);
95 void loadConstructedPoint(const double x[], const std::vector<double> &y) override;
96 void loadConstructedPoint(const double x[], int numx, const double y[]) override;
97 void finishConstruction() override;
98
99 void setHierarchicalCoefficients(const double c[]) override;
100 void integrateHierarchicalFunctions(double integrals[]) const override;
101
102 std::vector<int> getPolynomialSpace(bool interpolation) const;
103
104 const double* getSurpluses() const;
105
106 void updateAccelerationData(AccelerationContext::ChangeType change) const override;
107
108 double getNode(int i) const{ return nodes[i]; }
109
110protected:
111 void evalHierarchicalFunctions(const double x[], double fvalues[]) const;
112
114 void prepareSequence(int num_external);
115 std::vector<double> cacheBasisIntegrals() const;
116
117 template<typename T>
118 std::vector<std::vector<T>> cacheBasisValues(const T x[]) const{
119 std::vector<std::vector<T>> cache(num_dimensions);
120 for(int j=0; j<num_dimensions; j++){
121 cache[j].resize(max_levels[j] + 1);
122 T b = 1.0;
123 T this_x = x[j];
124 cache[j][0] = b;
125 for(int i=0; i<max_levels[j]; i++){
126 b *= (this_x - nodes[i]);
127 cache[j][i+1] = b;
128 }
129 for(int i=1; i<=max_levels[j]; i++){
130 cache[j][i] /= coeff[i];
131 }
132 }
133 return cache;
134 }
135
136 template<typename T>
137 std::vector<std::vector<T>> cacheBasisDerivatives(const T x[]) const {
138 std::vector<std::vector<T>> cache(num_dimensions);
139 for(int j=0; j<num_dimensions; j++){
140 cache[j].resize(max_levels[j] + 1);
141 T this_x = x[j];
142 T b = 1.0;
143 T s = 1.0;
144 cache[j][0] = 0.0;
145 if (max_levels[j] > 0) cache[j][1] = 1.0 / coeff[1];
146 for(int i=2; i <= max_levels[j]; i++){
147 s *= (this_x - nodes[i-1]);
148 b *= (this_x - nodes[i-2]);
149 s += b;
150 cache[j][i] = s / coeff[i];
151 }
152 }
153 return cache;
154 }
155
156 std::vector<int> getMultiIndex(const double x[]);
157 void expandGrid(const std::vector<int> &point, const std::vector<double> &values, const std::vector<double> &surplus);
158 void loadConstructedPoints();
159 void recomputeSurpluses();
160 template<int mode> void applyTransformationTransposed(double weights[]) const;
161
162 double evalBasis(const int f[], const int p[]) const; // evaluate function corresponding to f at p
163
164 void clearGpuNodes() const;
165 void clearGpuSurpluses() const;
166
167private:
168 TypeOneDRule rule;
169
170 Data2D<double> surpluses;
171 std::vector<double> nodes;
172 std::vector<double> coeff;
173
174 std::vector<int> max_levels;
175
176 std::unique_ptr<SimpleConstructData> dynamic_values;
177
178 // specialize below for the float case
179 std::unique_ptr<CudaSequenceData<double>>& getGpuCacheOverload(double) const{ return gpu_cache; }
180 std::unique_ptr<CudaSequenceData<float>>& getGpuCacheOverload(float) const{ return gpu_cachef; }
181 template<typename T> std::unique_ptr<CudaSequenceData<T>>& getGpuCache() const{
182 return getGpuCacheOverload(static_cast<T>(0.0));
183 }
184 template<typename T>
185 void loadGpuNodes() const{
186 auto& ccache = getGpuCache<T>();
187 if (!ccache) ccache = Utils::make_unique<CudaSequenceData<T>>();
188 if (!ccache->num_nodes.empty()) return;
189
190 ccache->nodes.load(acceleration, nodes);
191 ccache->coeff.load(acceleration, coeff);
192
193 std::vector<int> num_nodes(num_dimensions);
194 std::transform(max_levels.begin(), max_levels.end(), num_nodes.begin(), [](int i)->int{ return i+1; });
195 ccache->num_nodes.load(acceleration, num_nodes);
196
197 const MultiIndexSet *work = (points.empty()) ? &needed : &points;
198 int num_points = work->getNumIndexes();
199 Data2D<int> transpoints(work->getNumIndexes(), num_dimensions);
200 for(int i=0; i<num_points; i++){
201 for(int j=0; j<num_dimensions; j++){
202 transpoints.getStrip(j)[i] = work->getIndex(i)[j];
203 }
204 }
205 ccache->points.load(acceleration, transpoints.begin(), transpoints.end());
206 }
207 template<typename T> void loadGpuSurpluses() const{
208 auto& ccache = getGpuCache<T>();
209 if (!ccache) ccache = Utils::make_unique<CudaSequenceData<T>>();
210 if (ccache->surpluses.empty()) ccache->surpluses.load(acceleration, surpluses.begin(), surpluses.end());
211 }
212 mutable std::unique_ptr<CudaSequenceData<double>> gpu_cache;
213 mutable std::unique_ptr<CudaSequenceData<float>> gpu_cachef;
214};
215
216// Old version reader
217template<> struct GridReaderVersion5<GridSequence>{
218 template<typename iomode> static std::unique_ptr<GridSequence> read(AccelerationContext const *acc, std::istream &is){
219 std::unique_ptr<GridSequence> grid = Utils::make_unique<GridSequence>(acc);
220
221 grid->num_dimensions = IO::readNumber<iomode, int>(is);
222 grid->num_outputs = IO::readNumber<iomode, int>(is);
223 grid->rule = IO::readRule<iomode>(is);
224
225 if (IO::readFlag<iomode>(is)) grid->points = MultiIndexSet(is, iomode());
226 if (IO::readFlag<iomode>(is)) grid->needed = MultiIndexSet(is, iomode());
227
228 if (IO::readFlag<iomode>(is))
229 grid->surpluses = IO::readData2D<iomode, double>(is, grid->num_outputs, grid->points.getNumIndexes());
230
231 if (grid->num_outputs > 0) grid->values = StorageSet(is, iomode());
232
233 grid->prepareSequence(0);
234
235 return grid;
236 }
237};
238#endif // __TASMANIAN_DOXYGEN_SKIP
239
240}
241
242#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