queso-0.53.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/JointPdf.h>
29 #include <queso/GslVector.h>
30 #include <queso/GslMatrix.h>
31 
32 namespace QUESO {
33 
34 // Default constructor
35 template <class V, class M>
37  const BaseScalarFunction<V,M>& inputFunction,
38  const V& auxVec)
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 }
45 
46 // Destructor
47 template <class V, class M>
49 {
50 }
51 
52 // Math methods
53 template<class V,class M>
55 {
56  return m_scalarFunction.domainSet();
57 }
58 
59 // Sync methods
60 template <class V,class M>
62  const V* vecDirection,
63  V* gradVector,
64  M* hessianMatrix,
65  V* hessianEffect,
66  double* extraOutput1,
67  double* extraOutput2) const
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 }
264 
265 } // End namespace QUESO
266 
A templated class for handling sets.
Definition: VectorSet.h:52
A templated (base) class for handling scalar functions.
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
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_CHAR
Definition: MpiComm.h:46
const VectorSet< V, M > & domainSet() const
Access to the domain set of the scalar function which will be synchronized.
A templated class for synchronizing the calls of scalar functions (BaseScalarFunction and derived cla...
ScalarFunctionSynchronizer(const BaseScalarFunction< V, M > &inputFunction, const V &auxVec)
Default constructor.
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48

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