queso-0.51.1
VectorFunctionSynchronizer.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,2009,2010,2011,2012,2013 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/VectorFunctionSynchronizer.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
28 
29 namespace QUESO {
30 
31 // Default constructor -----------------------------
32 template <class P_V, class P_M, class Q_V, class Q_M>
34  const BaseVectorFunction<P_V,P_M,Q_V,Q_M>& inputFunction,
35  const P_V& auxPVec,
36  const Q_V& auxQVec)
37  :
38  m_env (inputFunction.domainSet().env()),
39  m_vectorFunction(inputFunction),
40  m_auxPVec (auxPVec),
41  m_auxQVec (auxQVec)
42 {
43 }
44 
45 // Destructor ---------------------------------------
46 template <class P_V, class P_M, class Q_V, class Q_M>
48 {
49 }
50 // Math methods -------------------------------------
51 template<class P_V, class P_M, class Q_V, class Q_M>
52 const VectorSet<P_V,P_M>&
54 {
55  return m_vectorFunction.domainSet();
56 }
57 
58 // Sync methods -------------------------------------
59 template <class P_V, class P_M, class Q_V, class Q_M>
60 void
62  const P_V* vecValues,
63  const P_V* vecDirection,
64  Q_V* imageVector,
65  DistArray<P_V*>* gradVectors, // Yes, 'P_V'
66  DistArray<P_M*>* hessianMatrices, // Yes, 'P_M'
67  DistArray<P_V*>* hessianEffects) const
68 {
69  if ((m_env.numSubEnvironments() < (unsigned int) m_env.fullComm().NumProc()) &&
70  (m_auxPVec.numOfProcsForStorage() == 1 ) &&
71  (m_auxQVec.numOfProcsForStorage() == 1 )) {
72  bool stayInRoutine = true;
73  do {
74  const P_V* internalValues = NULL;
75  const P_V* internalDirection = NULL;
76  Q_V* internalImageVec = NULL;
77  DistArray<P_V*>* internalGrads = NULL; // Yes, 'P_V'
78  DistArray<P_M*>* internalHessians = NULL; // Yes, 'P_M'
79  DistArray<P_V*>* internalEffects = 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' (imageVector is NULL or not)
87  // bufferChar[3] = '0' or '1' (gradVectors is NULL or not)
88  // bufferChar[4] = '0' or '1' (hessianMatrices is NULL or not)
89  // bufferChar[5] = '0' or '1' (hessianEffects is NULL or not)
90  std::vector<char> bufferChar(6,'0');
91 
92  if (m_env.subRank() == 0) {
93  UQ_FATAL_TEST_MACRO((vecValues != NULL) && (imageVector == NULL),
94  m_env.worldRank(),
95  "VectorFunctionSynchronizer<P_V,P_M,Q_V,Q_M>::callFunction()",
96  "imageVector should not be NULL");
97  internalValues = vecValues;
98  internalDirection = vecDirection;
99  internalImageVec = imageVector;
100  internalGrads = gradVectors;
101  internalHessians = hessianMatrices;
102  internalEffects = hessianEffects;
103 
104  if (internalValues != NULL) bufferChar[0] = '1';
105  if (internalDirection != NULL) bufferChar[1] = '1';
106  if (internalImageVec != NULL) bufferChar[2] = '1';
107  if (internalGrads != NULL) bufferChar[3] = '1';
108  if (internalHessians != NULL) bufferChar[4] = '1';
109  if (internalEffects != NULL) bufferChar[5] = '1';
110  }
111 
112  m_env.subComm().syncPrintDebugMsg("In VectorFunctionSynchronizer<V,M>::callFunction(), just before char Bcast()",3,3000000);
113  //if (m_env.subId() != 0) while (true) sleep(1);
114 
115  int count = (int) bufferChar.size();
116  m_env.subComm().Bcast((void *) &bufferChar[0], count, RawValue_MPI_CHAR, 0,
117  "VectorFunctionSynchronizer<P_V,P_M,Q_V,Q_M>::callFunction()",
118  "failed broadcast 1 of 3");
119 
120  if (bufferChar[0] == '1') {
122  // Broadcast 2 of 3
124 
125  // bufferDouble[0...] = contents for (eventual) vecValues
126  std::vector<double> bufferDouble(m_auxPVec.sizeLocal(),0);
127 
128  if (m_env.subRank() == 0) {
129  for (unsigned int i = 0; i < internalValues->sizeLocal(); ++i) {
130  bufferDouble[i] = (*internalValues)[i];
131  }
132  }
133 
134  count = (int) bufferDouble.size();
135  m_env.subComm().Bcast((void *) &bufferDouble[0], count, RawValue_MPI_DOUBLE, 0,
136  "VectorFunctionSynchronizer<P_V,P_M,Q_V,Q_M>::callFunction()",
137  "failed broadcast 2 of 3");
138 
139  if (m_env.subRank() != 0) {
140  P_V tmpPVec(m_auxPVec);
141  for (unsigned int i = 0; i < tmpPVec.sizeLocal(); ++i) {
142  tmpPVec[i] = bufferDouble[i];
143  }
144  internalValues = new P_V(tmpPVec);
145  }
146 
147  if (bufferChar[1] == '1') {
149  // Broadcast 3 of 3
151  // bufferDouble[0...] = contents for (eventual) vecDirection
152 
153  if (m_env.subRank() == 0) {
154  for (unsigned int i = 0; i < internalDirection->sizeLocal(); ++i) {
155  bufferDouble[i] = (*internalDirection)[i];
156  }
157  }
158 
159  count = (int) bufferDouble.size();
160  m_env.subComm().Bcast((void *) &bufferDouble[0], count, RawValue_MPI_DOUBLE, 0,
161  "VectorFunctionSynchronizer<P_V,P_M,Q_V,Q_M>::callFunction()",
162  "failed broadcast 3 of 3");
163 
164  if (m_env.subRank() != 0) {
165  P_V tmpPVec(m_auxPVec);
166  for (unsigned int i = 0; i < tmpPVec.sizeLocal(); ++i) {
167  tmpPVec[i] = bufferDouble[i];
168  }
169  internalDirection = new P_V(tmpPVec);
170  }
171  }
172 
174  // All processors now call 'vectorFunction()'
176  if (m_env.subRank() != 0) {
177  if (bufferChar[2] == '1') internalImageVec = new Q_V(m_auxQVec);
178  //if (bufferChar[3] == '1') internalGrads = new P_V(m_auxPVec);
179  //if (bufferChar[4] == '1') internalHessians = new P_M(m_auxPVec);
180  //if (bufferChar[5] == '1') internalEffects = new P_V(m_auxPVec);
181  }
182 
183  m_env.subComm().Barrier();
184  m_vectorFunction.compute(*internalValues,
185  internalDirection,
186  *internalImageVec,
187  internalGrads,
188  internalHessians,
189  internalEffects);
190  }
191 
193  // Prepare to exit routine or to stay in it
195  if (m_env.subRank() == 0) {
196  stayInRoutine = false; // Always for processor 0
197  }
198  else {
199  if (internalValues != NULL) delete internalValues;
200  if (internalDirection != NULL) delete internalDirection;
201  if (internalImageVec != NULL) delete internalImageVec;
202  //if (internalGrads != NULL) delete internalGrads;
203  //if (internalHessians != NULL) delete internalHessians;
204  //if (internalEffects != NULL) delete internalEffects;
205 
206  stayInRoutine = (vecValues == NULL) && (bufferChar[0] == '1');
207  }
208  } while (stayInRoutine);
209  }
210  else {
211  UQ_FATAL_TEST_MACRO((vecValues == NULL) || (imageVector == NULL),
212  m_env.worldRank(),
213  "VectorFunctionSynchronizer<V,M>::callFunction()",
214  "Neither vecValues nor imageVector should not be NULL");
215  UQ_FATAL_TEST_MACRO((m_auxPVec.numOfProcsForStorage() != m_auxQVec.numOfProcsForStorage()),
216  m_env.worldRank(),
217  "VectorFunctionSynchronizer<V,M>::callFunction()",
218  "Number of processors required for storage should be the same");
219 
220  m_env.subComm().Barrier();
221  m_vectorFunction.compute(*vecValues,
222  vecDirection,
223  *imageVector,
224  gradVectors,
225  hessianMatrices,
226  hessianEffects);
227  }
228 
229  return;
230 }
231 
232 } // End namespace QUESO
233 
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
VectorFunctionSynchronizer(const BaseVectorFunction< P_V, P_M, Q_V, Q_M > &inputFunction, const P_V &auxPVec, const Q_V &auxQVec)
Default constructor.
A class for partitioning vectors and matrices.
Definition: DistArray.h:56
A templated class for synchronizing the calls of vector-valued functions.
#define RawValue_MPI_CHAR
Definition: MpiComm.h:46
const VectorSet< P_V, P_M > & domainSet() const
Access to the domain set of the vector-valued function which will be synchronized.
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
A templated (base) class for handling vector functions.
void callFunction(const P_V *vecValues, const P_V *vecDirection, Q_V *imageVector, DistArray< P_V * > *gradVectors, DistArray< P_M * > *hessianMatrices, DistArray< P_V * > *hessianEffects) const
Calls the vector-valued function which will be synchronized.

Generated on Thu Apr 23 2015 19:26:15 for queso-0.51.1 by  doxygen 1.8.5