queso-0.51.1
Private Attributes | List of all members
QUESO::VectorFunctionSynchronizer< P_V, P_M, Q_V, Q_M > Class Template Reference

A templated class for synchronizing the calls of vector-valued functions. More...

#include <VectorFunctionSynchronizer.h>

Collaboration diagram for QUESO::VectorFunctionSynchronizer< P_V, P_M, Q_V, Q_M >:
Collaboration graph
[legend]

Public Member Functions

Constructor/Destructor methods
 VectorFunctionSynchronizer (const BaseVectorFunction< P_V, P_M, Q_V, Q_M > &inputFunction, const P_V &auxPVec, const Q_V &auxQVec)
 Default constructor. More...
 
 ~VectorFunctionSynchronizer ()
 Destructor. More...
 
Mathematical methods
const VectorSet< P_V, P_M > & domainSet () const
 Access to the domain set of the vector-valued function which will be synchronized. More...
 
Sync method
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. More...
 

Private Attributes

const BaseEnvironmentm_env
 
const BaseVectorFunction< P_V,
P_M, Q_V, Q_M > & 
m_vectorFunction
 
const P_V & m_auxPVec
 
const Q_V & m_auxQVec
 

Detailed Description

template<class P_V, class P_M, class Q_V, class Q_M>
class QUESO::VectorFunctionSynchronizer< P_V, P_M, Q_V, Q_M >

A templated class for synchronizing the calls of vector-valued functions.

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

Definition at line 43 of file VectorFunctionSynchronizer.h.

Constructor & Destructor Documentation

template<class P_V , class P_M , class Q_V , class Q_M >
QUESO::VectorFunctionSynchronizer< P_V, P_M, Q_V, Q_M >::VectorFunctionSynchronizer ( const BaseVectorFunction< P_V, P_M, Q_V, Q_M > &  inputFunction,
const P_V &  auxPVec,
const Q_V &  auxQVec 
)

Default constructor.

Definition at line 33 of file VectorFunctionSynchronizer.C.

37  :
38  m_env (inputFunction.domainSet().env()),
39  m_vectorFunction(inputFunction),
40  m_auxPVec (auxPVec),
41  m_auxQVec (auxQVec)
42 {
43 }
const BaseVectorFunction< P_V, P_M, Q_V, Q_M > & m_vectorFunction
template<class P_V , class P_M , class Q_V , class Q_M >
QUESO::VectorFunctionSynchronizer< P_V, P_M, Q_V, Q_M >::~VectorFunctionSynchronizer ( )

Destructor.

Definition at line 47 of file VectorFunctionSynchronizer.C.

48 {
49 }

Member Function Documentation

template<class P_V , class P_M , class Q_V , class Q_M >
void QUESO::VectorFunctionSynchronizer< P_V, P_M, Q_V, Q_M >::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.

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 VectorFunctionSynchronizer.C.

References RawValue_MPI_CHAR, RawValue_MPI_DOUBLE, and UQ_FATAL_TEST_MACRO.

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 }
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:157
int subRank() const
Access function for sub-rank.
Definition: Environment.C:263
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
const MpiComm & subComm() const
Access function for MpiComm sub communicator.
Definition: Environment.C:269
int NumProc() const
Returns total number of processes.
Definition: MpiComm.C:121
const MpiComm & fullComm() const
Access function for MpiComm full communicator.
Definition: Environment.C:247
void Barrier() const
Pause every process in *this communicator until all the processes reach this point.
Definition: MpiComm.C:143
void syncPrintDebugMsg(const char *msg, unsigned int msgVerbosity, unsigned int numUSecs) const
Synchronizes all the processes and print debug message.
Definition: MpiComm.C:234
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:319
const BaseVectorFunction< P_V, P_M, Q_V, Q_M > & m_vectorFunction
#define RawValue_MPI_CHAR
Definition: MpiComm.h:46
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
template<class P_V , class P_M , class Q_V , class Q_M >
const VectorSet< P_V, P_M > & QUESO::VectorFunctionSynchronizer< P_V, P_M, Q_V, Q_M >::domainSet ( ) const

Access to the domain set of the vector-valued function which will be synchronized.

Definition at line 53 of file VectorFunctionSynchronizer.C.

54 {
55  return m_vectorFunction.domainSet();
56 }
const BaseVectorFunction< P_V, P_M, Q_V, Q_M > & m_vectorFunction

Member Data Documentation

template<class P_V , class P_M , class Q_V , class Q_M >
const P_V& QUESO::VectorFunctionSynchronizer< P_V, P_M, Q_V, Q_M >::m_auxPVec
private

Definition at line 77 of file VectorFunctionSynchronizer.h.

template<class P_V , class P_M , class Q_V , class Q_M >
const Q_V& QUESO::VectorFunctionSynchronizer< P_V, P_M, Q_V, Q_M >::m_auxQVec
private

Definition at line 78 of file VectorFunctionSynchronizer.h.

template<class P_V , class P_M , class Q_V , class Q_M >
const BaseEnvironment& QUESO::VectorFunctionSynchronizer< P_V, P_M, Q_V, Q_M >::m_env
private

Definition at line 75 of file VectorFunctionSynchronizer.h.

template<class P_V , class P_M , class Q_V , class Q_M >
const BaseVectorFunction<P_V,P_M,Q_V,Q_M>& QUESO::VectorFunctionSynchronizer< P_V, P_M, Q_V, Q_M >::m_vectorFunction
private

Definition at line 76 of file VectorFunctionSynchronizer.h.


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

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