queso-0.56.0
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
QUESO::InterpolationSurrogateBuilder< V, M > Class Template Reference

Build interpolation-based surrogate. More...

#include <InterpolationSurrogateBuilder.h>

Inheritance diagram for QUESO::InterpolationSurrogateBuilder< V, M >:
Inheritance graph
[legend]
Collaboration diagram for QUESO::InterpolationSurrogateBuilder< V, M >:
Collaboration graph
[legend]

Public Member Functions

 InterpolationSurrogateBuilder (InterpolationSurrogateDataSet< V, M > &data)
 Constructor. More...
 
virtual ~InterpolationSurrogateBuilder ()
 
void build_values ()
 Execute the user's model and populate m_values for the given n_points. More...
 
- Public Member Functions inherited from QUESO::SurrogateBuilderBase< V >
 SurrogateBuilderBase ()
 
virtual ~SurrogateBuilderBase ()
 
virtual void evaluate_model (const V &domainVector, std::vector< double > &values)=0
 

Protected Member Functions

void partition_work ()
 Partition the workload of model evaluations across the subenvironments. More...
 
void set_work_bounds (unsigned int &n_begin, unsigned int &n_end) const
 Set the starting and ending global indices for the current subenvironment. More...
 
void sync_data (std::vector< unsigned int > &local_n, std::vector< double > &local_values, InterpolationSurrogateData< V, M > &data)
 Take the local values computed from each process and communicate. More...
 
void set_domain_vector (unsigned int n, V &domain_vector) const
 Provide the spatial coordinates for the global index n. More...
 
void compute_strides (std::vector< int > &strides) const
 Helper function to compute strides needed for MPI_Gatherv. More...
 
const
InterpolationSurrogateData< V,
M > & 
get_default_data () const
 Helper function to grab representative dataset from m_data. More...
 

Protected Attributes

InterpolationSurrogateDataSet
< V, M > & 
m_data
 
std::vector< int > m_njobs
 Cache the amount of work for each subenvironment. More...
 

Private Member Functions

 InterpolationSurrogateBuilder ()
 

Detailed Description

template<class V = GslVector, class M = GslMatrix>
class QUESO::InterpolationSurrogateBuilder< V, M >

Build interpolation-based surrogate.

Interpolation surrogates assume a structured grid. So, given the domain and the number of equally space points desired in each dimension, this class will handle calling the user's model to populate the values needed by the surrogate objects. User should subclass this object and implement the evaluate_model method.

Definition at line 43 of file InterpolationSurrogateBuilder.h.

Constructor & Destructor Documentation

Constructor.

We do not take a const& to the data because we want to compute and set the values directly.

Definition at line 41 of file InterpolationSurrogateBuilder.C.

References QUESO::InterpolationSurrogateBuilder< V, M >::partition_work().

42  : SurrogateBuilderBase<V>(),
43  m_data(data),
44  m_njobs(this->get_default_data().get_paramDomain().env().numSubEnvironments(), 0)
45  {
46  this->partition_work();
47  }
void partition_work()
Partition the workload of model evaluations across the subenvironments.
std::vector< int > m_njobs
Cache the amount of work for each subenvironment.
const InterpolationSurrogateData< V, M > & get_default_data() const
Helper function to grab representative dataset from m_data.
InterpolationSurrogateDataSet< V, M > & m_data
template<class V = GslVector, class M = GslMatrix>
virtual QUESO::InterpolationSurrogateBuilder< V, M >::~InterpolationSurrogateBuilder ( )
inlinevirtual

Definition at line 52 of file InterpolationSurrogateBuilder.h.

52 {};
template<class V = GslVector, class M = GslMatrix>
QUESO::InterpolationSurrogateBuilder< V, M >::InterpolationSurrogateBuilder ( )
private

Member Function Documentation

template<class V , class M >
void QUESO::InterpolationSurrogateBuilder< V, M >::build_values ( )

Execute the user's model and populate m_values for the given n_points.

Definition at line 83 of file InterpolationSurrogateBuilder.C.

References it.

84  {
85  unsigned int n_begin, n_end;
86  this->set_work_bounds( n_begin, n_end );
87 
88  // Cache each processors work, then we only need to do 1 Allgather
89  std::vector<unsigned int> local_n(n_end-n_begin);
90 
91  // We need to cache (n_end-n_begin) values for each dataset,
92  std::vector<std::vector<double> > local_values(this->m_data.size());
93  for( std::vector<std::vector<double> >::iterator it = local_values.begin();
94  it != local_values.end(); ++it )
95  it->resize(n_end-n_begin);
96 
97  unsigned int count = 0;
98 
99  // vector to store current domain value
100  V domain_vector(this->get_default_data().get_paramDomain().vectorSpace().zeroVector());
101 
102  // vector to store values evaluated at the current domain_vector
103  std::vector<double> values(this->m_data.size());
104 
105  for( unsigned int n = n_begin; n < n_end; n++ )
106  {
107  this->set_domain_vector( n, domain_vector );
108 
109  this->evaluate_model( domain_vector, values );
110 
111  local_n[count] = n;
112 
113  for( unsigned int s = 0; s < this->m_data.size(); s++ )
114  local_values[s][count] = values[s];
115 
116  count += 1;
117  }
118 
119  /* Sync all the locally computed values between the subenvironments
120  so all processes have all the computed values. We need to sync
121  values for every data set. */
122  for( unsigned int s = 0; s < this->m_data.size(); s++ )
123  this->sync_data( local_n, local_values[s], this->m_data.get_dataset(s) );
124  }
void sync_data(std::vector< unsigned int > &local_n, std::vector< double > &local_values, InterpolationSurrogateData< V, M > &data)
Take the local values computed from each process and communicate.
void set_domain_vector(unsigned int n, V &domain_vector) const
Provide the spatial coordinates for the global index n.
that you receive source code or can get it if you want it
Definition: License.txt:41
const InterpolationSurrogateData< V, M > & get_default_data() const
Helper function to grab representative dataset from m_data.
virtual void evaluate_model(const V &domainVector, std::vector< double > &values)=0
void set_work_bounds(unsigned int &n_begin, unsigned int &n_end) const
Set the starting and ending global indices for the current subenvironment.
InterpolationSurrogateDataSet< V, M > & m_data
template<class V , class M >
void QUESO::InterpolationSurrogateBuilder< V, M >::compute_strides ( std::vector< int > &  strides) const
protected

Helper function to compute strides needed for MPI_Gatherv.

Definition at line 201 of file InterpolationSurrogateBuilder.C.

202  {
203  unsigned int n_subenvs = this->get_default_data().get_paramDomain().env().numSubEnvironments();
204 
205  strides.resize(n_subenvs);
206 
207  // Don't stride the first entry
208  strides[0] = 0;
209 
210  int stride = 0;
211  for( unsigned int n = 1; n < n_subenvs; n++ )
212  {
213  // The stride is measured agaisnt the beginning of the buffer
214  // We want things packed tightly together so just stride
215  // by the number of entries from the previous group.
216  stride += this->m_njobs[n-1];
217  strides[n] = stride;
218  }
219  }
std::vector< int > m_njobs
Cache the amount of work for each subenvironment.
const InterpolationSurrogateData< V, M > & get_default_data() const
Helper function to grab representative dataset from m_data.
template<class V = GslVector, class M = GslMatrix>
const InterpolationSurrogateData<V,M>& QUESO::InterpolationSurrogateBuilder< V, M >::get_default_data ( ) const
inlineprotected

Helper function to grab representative dataset from m_data.

We only grab the first data set since the environments are guaranteed to be consistent by the nature of constructing an InterpolationSurrogateDataSet.

Definition at line 89 of file InterpolationSurrogateBuilder.h.

References QUESO::InterpolationSurrogateBuilder< V, M >::m_data.

90  { return m_data.get_dataset(0); }
InterpolationSurrogateDataSet< V, M > & m_data
template<class V , class M >
void QUESO::InterpolationSurrogateBuilder< V, M >::partition_work ( )
protected

Partition the workload of model evaluations across the subenvironments.

Definition at line 50 of file InterpolationSurrogateBuilder.C.

References queso_assert_equal_to.

Referenced by QUESO::InterpolationSurrogateBuilder< V, M >::InterpolationSurrogateBuilder().

51  {
52  // Convenience
53  unsigned int n_values = this->get_default_data().n_values();
54  unsigned int n_workers = this->get_default_data().get_paramDomain().env().numSubEnvironments();
55 
56  unsigned int n_jobs = n_values/n_workers;
57  unsigned int n_leftover = this->get_default_data().n_values() % n_workers;
58 
59  /* If the number of values is evenly divisible over all workers,
60  then everyone gets the same amount work */
61  if( n_leftover == 0 )
62  {
63  for(unsigned int n = 0; n < n_workers; n++)
64  this->m_njobs[n] = n_jobs;
65  }
66  /* Otherwise, some workers get more work than others*/
67  else
68  {
69  for(unsigned int n = 0; n < n_workers; n++)
70  {
71  if( n < n_leftover )
72  this->m_njobs[n] = n_jobs+1;
73  else
74  this->m_njobs[n] = n_jobs;
75  }
76  }
77 
78  // Sanity check
79  queso_assert_equal_to( (int)n_values, std::accumulate( m_njobs.begin(), m_njobs.end(), 0 ) );
80  }
std::vector< int > m_njobs
Cache the amount of work for each subenvironment.
#define queso_assert_equal_to(expr1, expr2)
Definition: asserts.h:135
const InterpolationSurrogateData< V, M > & get_default_data() const
Helper function to grab representative dataset from m_data.
template<class V , class M >
void QUESO::InterpolationSurrogateBuilder< V, M >::set_domain_vector ( unsigned int  n,
V &  domain_vector 
) const
protected

Provide the spatial coordinates for the global index n.

Definition at line 187 of file InterpolationSurrogateBuilder.C.

References dim, and QUESO::InterpolationSurrogateHelper::globalToCoord().

188  {
189  // Convert global index n to local coordinates in each dimension
190  std::vector<unsigned int> indices(this->get_default_data().dim());
191  InterpolationSurrogateHelper::globalToCoord( n, this->get_default_data().get_n_points(), indices );
192 
193  // Use indices to get x coordinates and populate domain_vector
194  for( unsigned int d = 0; d < this->get_default_data().dim(); d++ )
195  {
196  domain_vector[d] = this->get_default_data().get_x( d, indices[d] );
197  }
198  }
int dim
Definition: ann2fig.cpp:81
const InterpolationSurrogateData< V, M > & get_default_data() const
Helper function to grab representative dataset from m_data.
static void globalToCoord(unsigned int global, const std::vector< unsigned int > &n_points, std::vector< unsigned int > &coord_indices)
Inverse of coordToGlobal map.
template<class V , class M >
void QUESO::InterpolationSurrogateBuilder< V, M >::set_work_bounds ( unsigned int &  n_begin,
unsigned int &  n_end 
) const
protected

Set the starting and ending global indices for the current subenvironment.

This environment will evaluate the model for indices [n_begin,n_end)

Definition at line 127 of file InterpolationSurrogateBuilder.C.

128  {
129  unsigned int my_subid = this->get_default_data().get_paramDomain().env().subId();
130 
131  /* Starting index will be the sum of the all the previous num jobs */
132  n_begin = 0;
133  for( unsigned int n = 0; n < my_subid; n++ )
134  n_begin += m_njobs[n];
135 
136  n_end = n_begin + m_njobs[my_subid];
137  }
std::vector< int > m_njobs
Cache the amount of work for each subenvironment.
const InterpolationSurrogateData< V, M > & get_default_data() const
Helper function to grab representative dataset from m_data.
template<class V , class M >
void QUESO::InterpolationSurrogateBuilder< V, M >::sync_data ( std::vector< unsigned int > &  local_n,
std::vector< double > &  local_values,
InterpolationSurrogateData< V, M > &  data 
)
protected

Take the local values computed from each process and communicate.

The end result will be that all processes have all the data. I.e. m_values will be completely populated on all processes. This is done on the InterpolationSurrogateData passed to the function.

Todo:
Would be more efficient to pack local_n and local_values togethers and do Gatherv only once.

Definition at line 140 of file InterpolationSurrogateBuilder.C.

References QUESO::InterpolationSurrogateData< V, M >::get_paramDomain(), QUESO::InterpolationSurrogateData< V, M >::n_values(), QUESO::InterpolationSurrogateData< V, M >::set_value(), and QUESO::InterpolationSurrogateData< V, M >::sync_values().

143  {
144  // Only members of the inter0comm will do the communication of the local values
145  unsigned int my_subrank = data.get_paramDomain().env().subRank();
146 
147  if( my_subrank == 0 )
148  {
149  std::vector<double> all_values(data.n_values());
150 
151  std::vector<unsigned int> all_indices(data.n_values());
152 
153  std::vector<int> strides;
154  this->compute_strides( strides );
155 
156  const MpiComm& inter0comm = data.get_paramDomain().env().inter0Comm();
157 
160  inter0comm.template Gatherv<unsigned int>(&local_n[0], local_n.size(),
161  &all_indices[0], &m_njobs[0], &strides[0],
162  0 /*root*/, "InterpolationSurrogateBuilder::sync_data()",
163  "MpiComm::gatherv() failed!");
164 
165  inter0comm.template Gatherv<double>(&local_values[0],
166  local_values.size(), &all_values[0], &m_njobs[0], &strides[0],
167  0 /*root*/, "InterpolationSurrogateBuilder::sync_data()",
168  "MpiComm::gatherv() failed!");
169 
170  // Now set the values.
171  /* PB: Although we are guaranteed per-rank ordering of the data we gathered,
172  I'm not sure we can assume the same continuity of the inter0 ranks, i.e.
173  I'm not sure how QUESO ordered the inter0 ranks. So, we go ahead and
174  manually set the values. */
175  if( data.get_paramDomain().env().subRank() == 0 )
176  {
177  for( unsigned int n = 0; n < data.n_values(); n++ )
178  data.set_value( all_indices[n], all_values[n] );
179  }
180  }
181 
182  // Now broadcast the values data to all other processes
183  data.sync_values( 0 /*root*/);
184  }
std::vector< int > m_njobs
Cache the amount of work for each subenvironment.
void compute_strides(std::vector< int > &strides) const
Helper function to compute strides needed for MPI_Gatherv.

Member Data Documentation

template<class V = GslVector, class M = GslMatrix>
InterpolationSurrogateDataSet<V,M>& QUESO::InterpolationSurrogateBuilder< V, M >::m_data
protected
template<class V = GslVector, class M = GslMatrix>
std::vector<int> QUESO::InterpolationSurrogateBuilder< V, M >::m_njobs
protected

Cache the amount of work for each subenvironment.

Definition at line 62 of file InterpolationSurrogateBuilder.h.


The documentation for this class was generated from the following files:

Generated on Tue Nov 29 2016 10:53:16 for queso-0.56.0 by  doxygen 1.8.5