queso-0.56.0
ScalarFunctionSynchronizer.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2015 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <queso/Environment.h>
26 #include <queso/ScalarFunction.h>
27 #include <queso/ScalarFunctionSynchronizer.h>
28 #include <queso/BayesianJointPdf.h>
29 #include <queso/JointPdf.h>
30 #include <queso/GslVector.h>
31 #include <queso/GslMatrix.h>
32 
33 namespace QUESO {
34 
35 // Default constructor
36 template <class V, class M>
38  const BaseScalarFunction<V,M>& inputFunction,
39  const V& auxVec)
40  : m_env(inputFunction.domainSet().env()),
41  m_scalarFunction(inputFunction),
42  m_bayesianJointPdfPtr(dynamic_cast<const BayesianJointPdf<V,M>* >(&m_scalarFunction)),
43  m_auxVec(auxVec)
44 {
45 }
46 
47 // Destructor
48 template <class V, class M>
50 {
51 }
52 
53 // Math methods
54 template<class V,class M>
56 {
57  return m_scalarFunction.domainSet();
58 }
59 
60 // Sync methods
61 template <class V,class M>
63  const V* vecDirection,
64  V* gradVector,
65  M* hessianMatrix,
66  V* hessianEffect,
67  double* extraOutput1,
68  double* extraOutput2) const
69 {
70  double result = 0.;
71 
72  if ((m_env.numSubEnvironments() < (unsigned int) m_env.fullComm().NumProc()) &&
73  (m_auxVec.numOfProcsForStorage() == 1 )) {
74  bool stayInRoutine = true;
75  do {
76  const V* internalValues = NULL;
77  const V* internalDirection = NULL;
78  V* internalGrad = NULL;
79  M* internalHessian = NULL;
80  V* internalEffect = NULL;
81 
83  // Broadcast 1 of 3
85  // bufferChar[0] = '0' or '1' (vecValues is NULL or not)
86  // bufferChar[1] = '0' or '1' (vecDirection is NULL or not)
87  // bufferChar[2] = '0' or '1' (gradVector is NULL or not)
88  // bufferChar[3] = '0' or '1' (hessianMatrix is NULL or not)
89  // bufferChar[4] = '0' or '1' (hessianEffect is NULL or not)
90  std::vector<char> bufferChar(5,'0');
91 
92  if (m_env.subRank() == 0) {
93  internalValues = vecValues;
94  internalDirection = vecDirection;
95  internalGrad = gradVector;
96  internalHessian = hessianMatrix;
97  internalEffect = hessianEffect;
98 
99  if (internalValues != NULL) bufferChar[0] = '1';
100  if (internalDirection != NULL) bufferChar[1] = '1';
101  if (internalGrad != NULL) bufferChar[2] = '1';
102  if (internalHessian != NULL) bufferChar[3] = '1';
103  if (internalEffect != NULL) bufferChar[4] = '1';
104  }
105 
106  m_env.subComm().syncPrintDebugMsg("In ScalarFunctionSynchronizer<V,M>::callFunction(), just before char Bcast()",3,3000000);
107  //if (m_env.subId() != 0) while (true) sleep(1);
108 
109  int count = (int) bufferChar.size();
110  m_env.subComm().Bcast((void *) &bufferChar[0], count, RawValue_MPI_CHAR, 0,
111  "ScalarFunctionSynchronizer<V,M>::callFunction()",
112  "failed broadcast 1 of 3");
113 
114  m_env.subComm().syncPrintDebugMsg("In ScalarFunctionSynchronizer<V,M>::callFunction(), just after char Bcast()",3,3000000);
115  //std::cout << "char contents = " << bufferChar[0] << " " << bufferChar[1] << " " << bufferChar[2] << " " << bufferChar[3] << " " << bufferChar[4]
116  // << std::endl;
117 
118  if (bufferChar[0] == '1') {
120  // Broadcast 2 of 3
122 
123  // bufferDouble[0...] = contents for (eventual) vecValues
124  std::vector<double> bufferDouble(m_auxVec.sizeLocal(),0.);
125 
126  if (m_env.subRank() == 0) {
127  for (unsigned int i = 0; i < internalValues->sizeLocal(); ++i) {
128  bufferDouble[i] = (*internalValues)[i];
129  }
130  }
131 
132  //m_env.fullComm().Barrier();
133  //for (int i = 0; i < m_env.fullComm().NumProc(); ++i) {
134  // if (i == m_env.fullRank()) {
135  // std::cout << " In ScalarFunctionSynchronizer<V,M>::callFunction(), just before double Bcast()"
136  // << ": fullRank " << m_env.fullRank()
137  // << ", subEnvironment " << m_env.subId()
138  // << ", subRank " << m_env.subRank()
139  // << ": buffer related to first double Bcast() is ready to be broadcasted"
140  // << " and has size " << bufferDouble.size()
141  // << std::endl;
142  // if (m_env.subRank() == 0) {
143  // std::cout << "Buffer contents are";
144  // for (unsigned int i = 0; i < bufferDouble.size(); ++i) {
145  // std::cout << " " << bufferDouble[i];
146  // }
147  // std::cout << std::endl;
148  // }
149  // }
150  // m_env.fullComm().Barrier();
151  //}
152  //if (m_env.fullRank() == 0) std::cout << "Sleeping 3 seconds..."
153  // << std::endl;
154  //sleep(3);
155 
156  count = (int) bufferDouble.size();
157  m_env.subComm().Bcast((void *) &bufferDouble[0], count, RawValue_MPI_DOUBLE, 0,
158  "ScalarFunctionSynchronizer<V,M>::callFunction()",
159  "failed broadcast 2 of 3");
160 
161  if (m_env.subRank() != 0) {
162  V tmpVec(m_auxVec);
163  for (unsigned int i = 0; i < tmpVec.sizeLocal(); ++i) {
164  tmpVec[i] = bufferDouble[i];
165  }
166  internalValues = new V(tmpVec);
167  //if (vecValues) *vecValues = tmpVec; // prudencio 2010-08-01
168  }
169 
170  if (bufferChar[1] == '1') {
172  // Broadcast 3 of 3
174  // bufferDouble[0...] = contents for (eventual) vecDirection
175 
176  if (m_env.subRank() == 0) {
177  for (unsigned int i = 0; i < internalDirection->sizeLocal(); ++i) {
178  bufferDouble[i] = (*internalDirection)[i];
179  }
180  }
181 
182  count = (int) bufferDouble.size();
183  m_env.subComm().Bcast((void *) &bufferDouble[0], count, RawValue_MPI_DOUBLE, 0,
184  "ScalarFunctionSynchronizer<V,M>::callFunction()",
185  "failed broadcast 3 of 3");
186 
187  if (m_env.subRank() != 0) {
188  V tmpVec(m_auxVec);
189  for (unsigned int i = 0; i < tmpVec.sizeLocal(); ++i) {
190  tmpVec[i] = bufferDouble[i];
191  }
192  internalDirection = new V(tmpVec);
193  }
194  }
195 
197  // All processors now call 'scalarFunction()'
199  if (m_env.subRank() != 0) {
200  if (bufferChar[2] == '1') internalGrad = new V(m_auxVec);
201  if (bufferChar[3] == '1') internalHessian = new M(m_auxVec);
202  if (bufferChar[4] == '1') internalEffect = new V(m_auxVec);
203  }
204 
205  m_env.subComm().syncPrintDebugMsg("In ScalarFunctionSynchronizer<V,M>::callFunction(), just before actual lnValue()",3,3000000);
206  m_env.subComm().Barrier();
207  result = m_scalarFunction.lnValue(*internalValues, // input
208  internalDirection, // input
209  internalGrad, // output
210  internalHessian, // output
211  internalEffect); // output
212  if (extraOutput1) {
213  if (m_bayesianJointPdfPtr) {
214  *extraOutput1 = m_bayesianJointPdfPtr->lastComputedLogPrior();
215  }
216  }
217  if (extraOutput2) {
218  if (m_bayesianJointPdfPtr) {
219  *extraOutput2 = m_bayesianJointPdfPtr->lastComputedLogLikelihood();
220  }
221  }
222  } // if (bufferChar[0] == '1')
223 
225  // Prepare to exit routine or to stay in it
227  if (m_env.subRank() == 0) {
228  stayInRoutine = false; // Always for processor 0
229  }
230  else {
231  if (internalValues != NULL) delete internalValues;
232  if (internalDirection != NULL) delete internalDirection;
233  if (internalGrad != NULL) delete internalGrad;
234  if (internalHessian != NULL) delete internalHessian;
235  if (internalEffect != NULL) delete internalEffect;
236 
237  stayInRoutine = (vecValues == NULL) && (bufferChar[0] == '1');
238  //if (!stayInRoutine) std::cout << "Fullrank " << m_env.fullRank() << " is leaving scalarFunctionSync()" << std::endl;
239  }
240  } while (stayInRoutine);
241  }
242  else {
243  queso_require_msg(vecValues, "vecValues should not be NULL");
244 
245  m_env.subComm().Barrier();
246  result = m_scalarFunction.lnValue(*vecValues,
247  vecDirection,
248  gradVector,
249  hessianMatrix,
250  hessianEffect);
251  if (extraOutput1) {
252  if (m_bayesianJointPdfPtr) {
253  *extraOutput1 = m_bayesianJointPdfPtr->lastComputedLogPrior();
254  }
255  }
256  if (extraOutput2) {
257  if (m_bayesianJointPdfPtr) {
258  *extraOutput2 = m_bayesianJointPdfPtr->lastComputedLogLikelihood();
259  }
260  }
261  }
262 
263  return result;
264 }
265 
266 } // End namespace QUESO
267 
A templated (base) class for handling scalar functions.
const VectorSet< V, M > & domainSet() const
Access to the domain set of the scalar function which will be synchronized.
A class for handling Bayesian joint PDFs.
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.
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:67
#define RawValue_MPI_CHAR
Definition: MpiComm.h:65
ScalarFunctionSynchronizer(const BaseScalarFunction< V, M > &inputFunction, const V &auxVec)
Default constructor.
A templated class for handling sets.
Definition: VectorSet.h:52
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69

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