31 #ifndef __TASMANIAN_ADDONS_LOADNEEDEDVALS_HPP
32 #define __TASMANIAN_ADDONS_LOADNEEDEDVALS_HPP
103 template<
bool parallel_construction = true,
bool overwrite_loaded = false>
107 if (grid.
isUsingConstruction())
throw std::runtime_error(
"ERROR: cannot call loadNeededPoints() addon when isUsingConstruction() is true");
108 if (num_outputs == 0)
throw std::runtime_error(
"ERROR: cannot call loadNeededPoints() addon when the grid has no outputs");
109 if (num_points == 0)
return;
120 if (parallel_construction && (num_threads > 0)){
121 std::vector<bool> checked_out(num_points,
false);
122 std::mutex checked_out_lock;
124 std::vector<std::thread> workers;
125 workers.reserve(num_threads);
126 for(
size_t thread_id=0; thread_id<num_threads; thread_id++){
127 workers.emplace_back(
128 [&, thread_id](
void)->
void{
132 std::lock_guard<std::mutex> lock(checked_out_lock);
133 while ((sample < num_points) && checked_out[sample]) sample++;
134 if (sample < num_points) checked_out[sample] =
true;
136 if (sample < num_points)
138 }
while(sample < num_points);
143 for(
auto &w : workers) w.join();
146 for(
int i=0; i<num_points; i++)
163 template<
bool parallel_construction = true,
bool overwrite_loaded = false>
164 void loadNeededValues(std::function<
void(std::vector<double>
const &x, std::vector<double> &y,
size_t thread_id)> model,
168 loadNeededValues<parallel_construction, overwrite_loaded>(
169 [&](
double const x[],
double y[],
size_t thread_id)->
void{
170 std::vector<double> vecy(num_outputs);
171 model(std::vector<double>(x, x + num_dimensions), vecy, thread_id);
172 std::copy(vecy.begin(), vecy.end(), y);
173 }, grid, num_threads);
179 template<
bool parallel_construction = true,
bool overwrite_loaded = false>
181 loadNeededValues<parallel_construction, overwrite_loaded>(model, grid, num_threads);
187 template<
bool parallel_construction = true,
bool overwrite_loaded = false>
188 void loadNeededPoints(std::function<
void(std::vector<double>
const &x, std::vector<double> &y,
size_t thread_id)> model,
190 loadNeededValues<parallel_construction, overwrite_loaded>(model, grid, num_threads);
The master-class that represents an instance of a Tasmanian sparse grid.
Definition: TasmanianSparseGrid.hpp:293
int getNumOutputs() const
Return the outputs of the grid, i.e., number of model outputs.
Definition: TasmanianSparseGrid.hpp:644
int getNumNeeded() const
Return the number of points that should be provided to the next call of loadNeededValues().
Definition: TasmanianSparseGrid.hpp:659
int getNumLoaded() const
Return the number of points already associated with model values via loadNeededValues().
Definition: TasmanianSparseGrid.hpp:657
std::vector< double > getLoadedPoints() const
Return the points already associated with model values.
Definition: TasmanianSparseGrid.hpp:671
std::vector< double > getNeededPoints() const
Return the points that require model values.
Definition: TasmanianSparseGrid.hpp:695
void clearRefinement()
Remove all needed points from the grid.
bool isUsingConstruction() const
Returns true if the dynamic construction procedure has been initialized, false otherwise.
Definition: TasmanianSparseGrid.hpp:1403
void loadNeededPoints(std::vector< double > const &vals)
Alias of loadNeededValues().
Definition: TasmanianSparseGrid.hpp:884
int getNumDimensions() const
Return the dimensions of the grid, i.e., number of model inputs.
Definition: TasmanianSparseGrid.hpp:642
Wraps around a C-style of an array and mimics 2D data-structure.
Definition: tsgUtils.hpp:127
T * getStrip(int i)
Return a pointer to the i-th strip.
Definition: tsgUtils.hpp:135
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
void loadNeededPoints(std::function< void(double const x[], double y[], size_t thread_id)> model, TasmanianSparseGrid &grid, size_t num_threads)
Alias to loadNeededValues(), array variant.
Definition: tsgLoadNeededValues.hpp:180
size_t size_mult(IntA a, IntB b)
Converts two integer-like variables to size_t and returns the product..
Definition: tsgUtils.hpp:82
Encapsulates the Tasmanian Sparse Grid module.
Definition: TasmanianSparseGrid.hpp:68
Common includes and methods for all addons.