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>
35 template <
class V,
class M>
39 : m_env(inputFunction.domainSet().env()),
40 m_scalarFunction(inputFunction),
41 m_bayesianJointPdfPtr(dynamic_cast<const
BayesianJointPdf<V,M>* >(&m_scalarFunction)),
47 template <
class V,
class M>
53 template<
class V,
class M>
56 return m_scalarFunction.domainSet();
60 template <
class V,
class M>
62 const V* vecDirection,
67 double* extraOutput2)
const
71 if ((m_env.numSubEnvironments() < (
unsigned int) m_env.fullComm().NumProc()) &&
72 (m_auxVec.numOfProcsForStorage() == 1 )) {
73 bool stayInRoutine =
true;
75 const V* internalValues = NULL;
76 const V* internalDirection = NULL;
77 V* internalGrad = NULL;
78 M* internalHessian = NULL;
79 V* internalEffect = NULL;
89 std::vector<char> bufferChar(5,
'0');
91 if (m_env.subRank() == 0) {
92 internalValues = vecValues;
93 internalDirection = vecDirection;
94 internalGrad = gradVector;
95 internalHessian = hessianMatrix;
96 internalEffect = hessianEffect;
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';
105 m_env.subComm().syncPrintDebugMsg(
"In ScalarFunctionSynchronizer<V,M>::callFunction(), just before char Bcast()",3,3000000);
108 int count = (int) bufferChar.size();
110 "ScalarFunctionSynchronizer<V,M>::callFunction()",
111 "failed broadcast 1 of 3");
113 m_env.subComm().syncPrintDebugMsg(
"In ScalarFunctionSynchronizer<V,M>::callFunction(), just after char Bcast()",3,3000000);
117 if (bufferChar[0] ==
'1') {
123 std::vector<double> bufferDouble(m_auxVec.sizeLocal(),0.);
125 if (m_env.subRank() == 0) {
126 for (
unsigned int i = 0; i < internalValues->sizeLocal(); ++i) {
127 bufferDouble[i] = (*internalValues)[i];
155 count = (int) bufferDouble.size();
157 "ScalarFunctionSynchronizer<V,M>::callFunction()",
158 "failed broadcast 2 of 3");
160 if (m_env.subRank() != 0) {
162 for (
unsigned int i = 0; i < tmpVec.sizeLocal(); ++i) {
163 tmpVec[i] = bufferDouble[i];
165 internalValues =
new V(tmpVec);
169 if (bufferChar[1] ==
'1') {
175 if (m_env.subRank() == 0) {
176 for (
unsigned int i = 0; i < internalDirection->sizeLocal(); ++i) {
177 bufferDouble[i] = (*internalDirection)[i];
181 count = (int) bufferDouble.size();
183 "ScalarFunctionSynchronizer<V,M>::callFunction()",
184 "failed broadcast 3 of 3");
186 if (m_env.subRank() != 0) {
188 for (
unsigned int i = 0; i < tmpVec.sizeLocal(); ++i) {
189 tmpVec[i] = bufferDouble[i];
191 internalDirection =
new V(tmpVec);
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);
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,
212 if (m_bayesianJointPdfPtr) {
213 *extraOutput1 = m_bayesianJointPdfPtr->lastComputedLogPrior();
217 if (m_bayesianJointPdfPtr) {
218 *extraOutput2 = m_bayesianJointPdfPtr->lastComputedLogLikelihood();
226 if (m_env.subRank() == 0) {
227 stayInRoutine =
false;
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;
236 stayInRoutine = (vecValues == NULL) && (bufferChar[0] ==
'1');
239 }
while (stayInRoutine);
244 m_env.subComm().Barrier();
245 result = m_scalarFunction.lnValue(*vecValues,
251 if (m_bayesianJointPdfPtr) {
252 *extraOutput1 = m_bayesianJointPdfPtr->lastComputedLogPrior();
256 if (m_bayesianJointPdfPtr) {
257 *extraOutput2 = m_bayesianJointPdfPtr->lastComputedLogLikelihood();
A templated class for handling sets.
A templated (base) class for handling scalar functions.
#define queso_require_msg(asserted, msg)
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.
~ScalarFunctionSynchronizer()
Destructor.
#define RawValue_MPI_CHAR
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