queso-0.53.0
Private Attributes | List of all members
QUESO::ScalarFunctionSynchronizer< V, M > Class Template Reference

A templated class for synchronizing the calls of scalar functions (BaseScalarFunction and derived classes). More...

#include <ScalarFunctionSynchronizer.h>

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

Public Member Functions

Constructor/Destructor methods
 ScalarFunctionSynchronizer (const BaseScalarFunction< V, M > &inputFunction, const V &auxVec)
 Default constructor. More...
 
 ~ScalarFunctionSynchronizer ()
 Destructor. More...
 
Mathematical methods
const VectorSet< V, M > & domainSet () const
 Access to the domain set of the scalar function which will be synchronized. More...
 
Sync method
double callFunction (const V *vecValues, const V *vecDirection, V *gradVector, M *hessianMatrix, V *hessianEffect, double *extraOutput1, double *extraOutput2) const
 Calls the scalar function which will be synchronized. More...
 

Private Attributes

const BaseEnvironmentm_env
 
const BaseScalarFunction< V, M > & m_scalarFunction
 
const BayesianJointPdf< V, M > * m_bayesianJointPdfPtr
 
const V & m_auxVec
 

Detailed Description

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

A templated class for synchronizing the calls of scalar functions (BaseScalarFunction and derived classes).

This class creates a synchronization point among processes which call scalar functions. This means that all processes must reach a point in their code before they can all begin executing again.

Definition at line 47 of file ScalarFunctionSynchronizer.h.

Constructor & Destructor Documentation

template<class V, class M>
QUESO::ScalarFunctionSynchronizer< V, M >::ScalarFunctionSynchronizer ( const BaseScalarFunction< V, M > &  inputFunction,
const V &  auxVec 
)

Default constructor.

Definition at line 36 of file ScalarFunctionSynchronizer.C.

39  : m_env(inputFunction.domainSet().env()),
40  m_scalarFunction(inputFunction),
41  m_bayesianJointPdfPtr(dynamic_cast<const BayesianJointPdf<V,M>* >(&m_scalarFunction)),
42  m_auxVec(auxVec)
43 {
44 }
const BaseScalarFunction< V, M > & m_scalarFunction
const BayesianJointPdf< V, M > * m_bayesianJointPdfPtr
template<class V , class M >
QUESO::ScalarFunctionSynchronizer< V, M >::~ScalarFunctionSynchronizer ( )

Destructor.

Definition at line 48 of file ScalarFunctionSynchronizer.C.

49 {
50 }

Member Function Documentation

template<class V, class M>
double QUESO::ScalarFunctionSynchronizer< V, M >::callFunction ( const V *  vecValues,
const V *  vecDirection,
V *  gradVector,
M *  hessianMatrix,
V *  hessianEffect,
double *  extraOutput1,
double *  extraOutput2 
) const

Calls the scalar function which will be synchronized.

This procedure forms a barrier, and no processes in the communicator can pass the barrier until all of them call the function.

Definition at line 61 of file ScalarFunctionSynchronizer.C.

References queso_require_msg, RawValue_MPI_CHAR, and RawValue_MPI_DOUBLE.

68 {
69  double result = 0.;
70 
71  if ((m_env.numSubEnvironments() < (unsigned int) m_env.fullComm().NumProc()) &&
72  (m_auxVec.numOfProcsForStorage() == 1 )) {
73  bool stayInRoutine = true;
74  do {
75  const V* internalValues = NULL;
76  const V* internalDirection = NULL;
77  V* internalGrad = NULL;
78  M* internalHessian = NULL;
79  V* internalEffect = NULL;
80 
82  // Broadcast 1 of 3
84  // bufferChar[0] = '0' or '1' (vecValues is NULL or not)
85  // bufferChar[1] = '0' or '1' (vecDirection is NULL or not)
86  // bufferChar[2] = '0' or '1' (gradVector is NULL or not)
87  // bufferChar[3] = '0' or '1' (hessianMatrix is NULL or not)
88  // bufferChar[4] = '0' or '1' (hessianEffect is NULL or not)
89  std::vector<char> bufferChar(5,'0');
90 
91  if (m_env.subRank() == 0) {
92  internalValues = vecValues;
93  internalDirection = vecDirection;
94  internalGrad = gradVector;
95  internalHessian = hessianMatrix;
96  internalEffect = hessianEffect;
97 
98  if (internalValues != NULL) bufferChar[0] = '1';
99  if (internalDirection != NULL) bufferChar[1] = '1';
100  if (internalGrad != NULL) bufferChar[2] = '1';
101  if (internalHessian != NULL) bufferChar[3] = '1';
102  if (internalEffect != NULL) bufferChar[4] = '1';
103  }
104 
105  m_env.subComm().syncPrintDebugMsg("In ScalarFunctionSynchronizer<V,M>::callFunction(), just before char Bcast()",3,3000000);
106  //if (m_env.subId() != 0) while (true) sleep(1);
107 
108  int count = (int) bufferChar.size();
109  m_env.subComm().Bcast((void *) &bufferChar[0], count, RawValue_MPI_CHAR, 0,
110  "ScalarFunctionSynchronizer<V,M>::callFunction()",
111  "failed broadcast 1 of 3");
112 
113  m_env.subComm().syncPrintDebugMsg("In ScalarFunctionSynchronizer<V,M>::callFunction(), just after char Bcast()",3,3000000);
114  //std::cout << "char contents = " << bufferChar[0] << " " << bufferChar[1] << " " << bufferChar[2] << " " << bufferChar[3] << " " << bufferChar[4]
115  // << std::endl;
116 
117  if (bufferChar[0] == '1') {
119  // Broadcast 2 of 3
121 
122  // bufferDouble[0...] = contents for (eventual) vecValues
123  std::vector<double> bufferDouble(m_auxVec.sizeLocal(),0.);
124 
125  if (m_env.subRank() == 0) {
126  for (unsigned int i = 0; i < internalValues->sizeLocal(); ++i) {
127  bufferDouble[i] = (*internalValues)[i];
128  }
129  }
130 
131  //m_env.fullComm().Barrier();
132  //for (int i = 0; i < m_env.fullComm().NumProc(); ++i) {
133  // if (i == m_env.fullRank()) {
134  // std::cout << " In ScalarFunctionSynchronizer<V,M>::callFunction(), just before double Bcast()"
135  // << ": fullRank " << m_env.fullRank()
136  // << ", subEnvironment " << m_env.subId()
137  // << ", subRank " << m_env.subRank()
138  // << ": buffer related to first double Bcast() is ready to be broadcasted"
139  // << " and has size " << bufferDouble.size()
140  // << std::endl;
141  // if (m_env.subRank() == 0) {
142  // std::cout << "Buffer contents are";
143  // for (unsigned int i = 0; i < bufferDouble.size(); ++i) {
144  // std::cout << " " << bufferDouble[i];
145  // }
146  // std::cout << std::endl;
147  // }
148  // }
149  // m_env.fullComm().Barrier();
150  //}
151  //if (m_env.fullRank() == 0) std::cout << "Sleeping 3 seconds..."
152  // << std::endl;
153  //sleep(3);
154 
155  count = (int) bufferDouble.size();
156  m_env.subComm().Bcast((void *) &bufferDouble[0], count, RawValue_MPI_DOUBLE, 0,
157  "ScalarFunctionSynchronizer<V,M>::callFunction()",
158  "failed broadcast 2 of 3");
159 
160  if (m_env.subRank() != 0) {
161  V tmpVec(m_auxVec);
162  for (unsigned int i = 0; i < tmpVec.sizeLocal(); ++i) {
163  tmpVec[i] = bufferDouble[i];
164  }
165  internalValues = new V(tmpVec);
166  //if (vecValues) *vecValues = tmpVec; // prudencio 2010-08-01
167  }
168 
169  if (bufferChar[1] == '1') {
171  // Broadcast 3 of 3
173  // bufferDouble[0...] = contents for (eventual) vecDirection
174 
175  if (m_env.subRank() == 0) {
176  for (unsigned int i = 0; i < internalDirection->sizeLocal(); ++i) {
177  bufferDouble[i] = (*internalDirection)[i];
178  }
179  }
180 
181  count = (int) bufferDouble.size();
182  m_env.subComm().Bcast((void *) &bufferDouble[0], count, RawValue_MPI_DOUBLE, 0,
183  "ScalarFunctionSynchronizer<V,M>::callFunction()",
184  "failed broadcast 3 of 3");
185 
186  if (m_env.subRank() != 0) {
187  V tmpVec(m_auxVec);
188  for (unsigned int i = 0; i < tmpVec.sizeLocal(); ++i) {
189  tmpVec[i] = bufferDouble[i];
190  }
191  internalDirection = new V(tmpVec);
192  }
193  }
194 
196  // All processors now call 'scalarFunction()'
198  if (m_env.subRank() != 0) {
199  if (bufferChar[2] == '1') internalGrad = new V(m_auxVec);
200  if (bufferChar[3] == '1') internalHessian = new M(m_auxVec);
201  if (bufferChar[4] == '1') internalEffect = new V(m_auxVec);
202  }
203 
204  m_env.subComm().syncPrintDebugMsg("In ScalarFunctionSynchronizer<V,M>::callFunction(), just before actual lnValue()",3,3000000);
205  m_env.subComm().Barrier();
206  result = m_scalarFunction.lnValue(*internalValues, // input
207  internalDirection, // input
208  internalGrad, // output
209  internalHessian, // output
210  internalEffect); // output
211  if (extraOutput1) {
212  if (m_bayesianJointPdfPtr) {
213  *extraOutput1 = m_bayesianJointPdfPtr->lastComputedLogPrior();
214  }
215  }
216  if (extraOutput2) {
217  if (m_bayesianJointPdfPtr) {
218  *extraOutput2 = m_bayesianJointPdfPtr->lastComputedLogLikelihood();
219  }
220  }
221  } // if (bufferChar[0] == '1')
222 
224  // Prepare to exit routine or to stay in it
226  if (m_env.subRank() == 0) {
227  stayInRoutine = false; // Always for processor 0
228  }
229  else {
230  if (internalValues != NULL) delete internalValues;
231  if (internalDirection != NULL) delete internalDirection;
232  if (internalGrad != NULL) delete internalGrad;
233  if (internalHessian != NULL) delete internalHessian;
234  if (internalEffect != NULL) delete internalEffect;
235 
236  stayInRoutine = (vecValues == NULL) && (bufferChar[0] == '1');
237  //if (!stayInRoutine) std::cout << "Fullrank " << m_env.fullRank() << " is leaving scalarFunctionSync()" << std::endl;
238  }
239  } while (stayInRoutine);
240  }
241  else {
242  queso_require_msg(vecValues, "vecValues should not be NULL");
243 
244  m_env.subComm().Barrier();
245  result = m_scalarFunction.lnValue(*vecValues,
246  vecDirection,
247  gradVector,
248  hessianMatrix,
249  hessianEffect);
250  if (extraOutput1) {
251  if (m_bayesianJointPdfPtr) {
252  *extraOutput1 = m_bayesianJointPdfPtr->lastComputedLogPrior();
253  }
254  }
255  if (extraOutput2) {
256  if (m_bayesianJointPdfPtr) {
257  *extraOutput2 = m_bayesianJointPdfPtr->lastComputedLogLikelihood();
258  }
259  }
260  }
261 
262  return result;
263 }
const MpiComm & subComm() const
Access function for MpiComm sub communicator.
Definition: Environment.C:247
int subRank() const
Access function for sub-rank.
Definition: Environment.C:241
void Barrier() const
Pause every process in *this communicator until all the processes reach this point.
Definition: MpiComm.C:122
int NumProc() const
Returns total number of processes.
Definition: MpiComm.C:103
const MpiComm & fullComm() const
Access function for MpiComm full communicator.
Definition: Environment.C:228
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
void Bcast(void *buffer, int count, RawType_MPI_Datatype datatype, int root, const char *whereMsg, const char *whatMsg) const
Broadcast values from the root process to the slave processes.
Definition: MpiComm.C:133
const BaseScalarFunction< V, M > & m_scalarFunction
const BayesianJointPdf< V, M > * m_bayesianJointPdfPtr
#define RawValue_MPI_CHAR
Definition: MpiComm.h:46
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
void syncPrintDebugMsg(const char *msg, unsigned int msgVerbosity, unsigned int numUSecs) const
Synchronizes all the processes and print debug message.
Definition: MpiComm.C:195
template<class V , class M >
const VectorSet< V, M > & QUESO::ScalarFunctionSynchronizer< V, M >::domainSet ( ) const

Access to the domain set of the scalar function which will be synchronized.

Definition at line 54 of file ScalarFunctionSynchronizer.C.

55 {
56  return m_scalarFunction.domainSet();
57 }
const BaseScalarFunction< V, M > & m_scalarFunction

Member Data Documentation

template<class V = GslVector, class M = GslMatrix>
const V& QUESO::ScalarFunctionSynchronizer< V, M >::m_auxVec
private

Definition at line 83 of file ScalarFunctionSynchronizer.h.

template<class V = GslVector, class M = GslMatrix>
const BayesianJointPdf<V,M>* QUESO::ScalarFunctionSynchronizer< V, M >::m_bayesianJointPdfPtr
private

Definition at line 82 of file ScalarFunctionSynchronizer.h.

template<class V = GslVector, class M = GslMatrix>
const BaseEnvironment& QUESO::ScalarFunctionSynchronizer< V, M >::m_env
private

Definition at line 80 of file ScalarFunctionSynchronizer.h.

template<class V = GslVector, class M = GslMatrix>
const BaseScalarFunction<V,M>& QUESO::ScalarFunctionSynchronizer< V, M >::m_scalarFunction
private

Definition at line 81 of file ScalarFunctionSynchronizer.h.


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

Generated on Thu Jun 11 2015 13:52:33 for queso-0.53.0 by  doxygen 1.8.5