queso-0.53.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 40 of file InterpolationSurrogateBuilder.C.

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

41  : SurrogateBuilderBase<V>(),
42  m_data(data),
43  m_njobs(this->get_default_data().get_paramDomain().env().numSubEnvironments(), 0)
44  {
45  this->partition_work();
46  }
void partition_work()
Partition the workload of model evaluations across the subenvironments.
const InterpolationSurrogateData< V, M > & get_default_data() const
Helper function to grab representative dataset from m_data.
InterpolationSurrogateDataSet< V, M > & m_data
std::vector< int > m_njobs
Cache the amount of work for each subenvironment.
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 82 of file InterpolationSurrogateBuilder.C.

References it.

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

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

References queso_assert_equal_to.

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

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

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

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

127  {
128  unsigned int my_subid = this->get_default_data().get_paramDomain().env().subId();
129 
130  /* Starting index will be the sum of the all the previous num jobs */
131  n_begin = 0;
132  for( unsigned int n = 0; n < my_subid; n++ )
133  n_begin += m_njobs[n];
134 
135  n_end = n_begin + m_njobs[my_subid];
136  }
const InterpolationSurrogateData< V, M > & get_default_data() const
Helper function to grab representative dataset from m_data.
std::vector< int > m_njobs
Cache the amount of work for each subenvironment.
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 139 of file InterpolationSurrogateBuilder.C.

References QUESO::MpiComm::Gatherv(), 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().

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