queso-0.53.0
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 = GslVector, class P_M = GslMatrix, class Q_V = GslVector, class Q_M = GslMatrix>
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 46 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 queso_require_equal_to_msg, queso_require_msg, RawValue_MPI_CHAR, and RawValue_MPI_DOUBLE.

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  if ((vecValues != NULL)) queso_require_msg(imageVector, "imageVector should not be NULL");
94  internalValues = vecValues;
95  internalDirection = vecDirection;
96  internalImageVec = imageVector;
97  internalGrads = gradVectors;
98  internalHessians = hessianMatrices;
99  internalEffects = hessianEffects;
100 
101  if (internalValues != NULL) bufferChar[0] = '1';
102  if (internalDirection != NULL) bufferChar[1] = '1';
103  if (internalImageVec != NULL) bufferChar[2] = '1';
104  if (internalGrads != NULL) bufferChar[3] = '1';
105  if (internalHessians != NULL) bufferChar[4] = '1';
106  if (internalEffects != NULL) bufferChar[5] = '1';
107  }
108 
109  m_env.subComm().syncPrintDebugMsg("In VectorFunctionSynchronizer<V,M>::callFunction(), just before char Bcast()",3,3000000);
110  //if (m_env.subId() != 0) while (true) sleep(1);
111 
112  int count = (int) bufferChar.size();
113  m_env.subComm().Bcast((void *) &bufferChar[0], count, RawValue_MPI_CHAR, 0,
114  "VectorFunctionSynchronizer<P_V,P_M,Q_V,Q_M>::callFunction()",
115  "failed broadcast 1 of 3");
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_auxPVec.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  count = (int) bufferDouble.size();
132  m_env.subComm().Bcast((void *) &bufferDouble[0], count, RawValue_MPI_DOUBLE, 0,
133  "VectorFunctionSynchronizer<P_V,P_M,Q_V,Q_M>::callFunction()",
134  "failed broadcast 2 of 3");
135 
136  if (m_env.subRank() != 0) {
137  P_V tmpPVec(m_auxPVec);
138  for (unsigned int i = 0; i < tmpPVec.sizeLocal(); ++i) {
139  tmpPVec[i] = bufferDouble[i];
140  }
141  internalValues = new P_V(tmpPVec);
142  }
143 
144  if (bufferChar[1] == '1') {
146  // Broadcast 3 of 3
148  // bufferDouble[0...] = contents for (eventual) vecDirection
149 
150  if (m_env.subRank() == 0) {
151  for (unsigned int i = 0; i < internalDirection->sizeLocal(); ++i) {
152  bufferDouble[i] = (*internalDirection)[i];
153  }
154  }
155 
156  count = (int) bufferDouble.size();
157  m_env.subComm().Bcast((void *) &bufferDouble[0], count, RawValue_MPI_DOUBLE, 0,
158  "VectorFunctionSynchronizer<P_V,P_M,Q_V,Q_M>::callFunction()",
159  "failed broadcast 3 of 3");
160 
161  if (m_env.subRank() != 0) {
162  P_V tmpPVec(m_auxPVec);
163  for (unsigned int i = 0; i < tmpPVec.sizeLocal(); ++i) {
164  tmpPVec[i] = bufferDouble[i];
165  }
166  internalDirection = new P_V(tmpPVec);
167  }
168  }
169 
171  // All processors now call 'vectorFunction()'
173  if (m_env.subRank() != 0) {
174  if (bufferChar[2] == '1') internalImageVec = new Q_V(m_auxQVec);
175  //if (bufferChar[3] == '1') internalGrads = new P_V(m_auxPVec);
176  //if (bufferChar[4] == '1') internalHessians = new P_M(m_auxPVec);
177  //if (bufferChar[5] == '1') internalEffects = new P_V(m_auxPVec);
178  }
179 
180  m_env.subComm().Barrier();
181  m_vectorFunction.compute(*internalValues,
182  internalDirection,
183  *internalImageVec,
184  internalGrads,
185  internalHessians,
186  internalEffects);
187  }
188 
190  // Prepare to exit routine or to stay in it
192  if (m_env.subRank() == 0) {
193  stayInRoutine = false; // Always for processor 0
194  }
195  else {
196  if (internalValues != NULL) delete internalValues;
197  if (internalDirection != NULL) delete internalDirection;
198  if (internalImageVec != NULL) delete internalImageVec;
199  //if (internalGrads != NULL) delete internalGrads;
200  //if (internalHessians != NULL) delete internalHessians;
201  //if (internalEffects != NULL) delete internalEffects;
202 
203  stayInRoutine = (vecValues == NULL) && (bufferChar[0] == '1');
204  }
205  } while (stayInRoutine);
206  }
207  else {
208  queso_require_msg(!((vecValues == NULL) || (imageVector == NULL)), "Neither vecValues nor imageVector should not be NULL");
209  queso_require_equal_to_msg(m_auxPVec.numOfProcsForStorage(), m_auxQVec.numOfProcsForStorage(), "Number of processors required for storage should be the same");
210 
211  m_env.subComm().Barrier();
212  m_vectorFunction.compute(*vecValues,
213  vecDirection,
214  *imageVector,
215  gradVectors,
216  hessianMatrices,
217  hessianEffects);
218  }
219 
220  return;
221 }
const MpiComm & subComm() const
Access function for MpiComm sub communicator.
Definition: Environment.C:247
int subRank() const
Access function for sub-rank.
Definition: Environment.C:241
void Barrier() const
Pause every process in *this communicator until all the processes reach this point.
Definition: MpiComm.C:122
int NumProc() const
Returns total number of processes.
Definition: MpiComm.C:103
const MpiComm & fullComm() const
Access function for MpiComm full communicator.
Definition: Environment.C:228
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
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:133
#define RawValue_MPI_CHAR
Definition: MpiComm.h:46
const BaseVectorFunction< P_V, P_M, Q_V, Q_M > & m_vectorFunction
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
void syncPrintDebugMsg(const char *msg, unsigned int msgVerbosity, unsigned int numUSecs) const
Synchronizes all the processes and print debug message.
Definition: MpiComm.C:195
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 = GslVector, class P_M = GslMatrix, class Q_V = GslVector, class Q_M = GslMatrix>
const P_V& QUESO::VectorFunctionSynchronizer< P_V, P_M, Q_V, Q_M >::m_auxPVec
private

Definition at line 80 of file VectorFunctionSynchronizer.h.

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

Definition at line 81 of file VectorFunctionSynchronizer.h.

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

Definition at line 78 of file VectorFunctionSynchronizer.h.

template<class P_V = GslVector, class P_M = GslMatrix, class Q_V = GslVector, class Q_M = GslMatrix>
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 79 of file VectorFunctionSynchronizer.h.


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

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