FEI  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fei_VectorReducer.cpp
1 /*--------------------------------------------------------------------*/
2 /* Copyright 2005 Sandia Corporation. */
3 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
4 /* non-exclusive license for use of this work by or on behalf */
5 /* of the U.S. Government. Export of this program may require */
6 /* a license from the United States Government. */
7 /*--------------------------------------------------------------------*/
8 
9 #include <fei_VectorReducer.hpp>
10 #include <fei_VectorSpace.hpp>
11 #include <fei_Vector_core.hpp>
12 #include <fei_Vector.hpp>
13 #include <fei_CommUtils.hpp>
14 
15 #undef fei_file
16 #define fei_file "fei_VectorReducer.cpp"
17 
18 #include <fei_ErrMacros.hpp>
19 
20 namespace fei {
21 
22 //----------------------------------------------------------------------------
23 VectorReducer::VectorReducer(fei::SharedPtr<fei::Reducer> reducer,
25  bool isSolutionVector)
26  : reducer_(reducer),
27  target_(target),
28  isSolution_(isSolutionVector)
29 {
30  localProc_ = fei::localProc(target->getVectorSpace()->getCommunicator());
31  numProcs_ = fei::numProcs(target->getVectorSpace()->getCommunicator());
32 
33  fei::Vector_core* target_core = dynamic_cast<fei::Vector_core*>(target.get());
34  if (target_core == NULL) {
35  throw std::runtime_error("fei::VectorReducer ERROR, target vector not dynamic_cast-able to fei::Vector_core.");
36  }
37 
39  int numEqns = vecspace->getNumIndices_SharedAndOwned();
40  std::vector<int> eqns;
41  vecspace->getIndices_SharedAndOwned(eqns);
42 
43  std::vector<int> overlap;
44  for(int i=0; i<numEqns; ++i) {
45  if (!reducer->isSlaveEqn(eqns[i])) {
46  overlap.push_back(reducer->translateToReducedEqn(eqns[i]));
47  }
48  else {
49  std::vector<int> masters;
50  reducer->getSlaveMasterEqns(eqns[i], masters);
51  for(unsigned j=0; j<masters.size(); ++j) {
52  overlap.push_back(reducer->translateToReducedEqn(masters[j]));
53  }
54  }
55  }
56 
57  int* overlap_ptr = overlap.empty() ? NULL : &overlap[0];
58  target_core->setOverlap(overlap.size(), overlap_ptr);
59 }
60 
61 //----------------------------------------------------------------------------
62 VectorReducer::~VectorReducer()
63 {
64 }
65 
66 //----------------------------------------------------------------------------
67 int VectorReducer::putScalar(double scalar)
68 {
69  return(target_->putScalar(scalar));
70 }
71 
72 //----------------------------------------------------------------------------
73 int VectorReducer::update(double a, const fei::Vector* x, double b)
74 {
75  return(target_->update(a, x, b));
76 }
77 
78 //----------------------------------------------------------------------------
79 int VectorReducer::scatterToOverlap()
80 {
81  return(target_->scatterToOverlap());
82 }
83 
84 //----------------------------------------------------------------------------
85 int VectorReducer::gatherFromOverlap(bool accumulate)
86 {
87  reducer_->assembleReducedVector(isSolution_, *target_);
88  target_->setCommSizes();
89  return(target_->gatherFromOverlap(accumulate));
90 }
91 
92 //----------------------------------------------------------------------------
93 int VectorReducer::sumIn(int numValues,
94  const int* indices, const double* values,
95  int vectorIndex)
96 {
97  return(reducer_->addVectorValues(numValues, indices, values, true,
98  isSolution_, vectorIndex, *target_));
99 }
100 
101 //----------------------------------------------------------------------------
102 int VectorReducer::copyIn(int numValues,
103  const int* indices, const double* values,
104  int vectorIndex)
105 {
106  return(reducer_->addVectorValues(numValues, indices, values, false,
107  isSolution_, vectorIndex, *target_));
108 }
109 
110 //----------------------------------------------------------------------------
111 int VectorReducer::giveToUnderlyingVector(int numValues,
112  const int* indices,
113  const double* values,
114  bool sumInto,
115  int vectorIndex)
116 {
117  int err = reducer_->addVectorValues(numValues, indices, values, sumInto,
118  isSolution_, vectorIndex, *target_);
119  return(err);
120 }
121 
122 //----------------------------------------------------------------------------
123 int VectorReducer::sumInFieldData(int fieldID,
124  int idType,
125  int numIDs,
126  const int* IDs,
127  const double* data,
128  int vectorIndex)
129 {
130  fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
131  int fieldSize = vspace->getFieldSize(fieldID);
132  int numIndices = numIDs*fieldSize;
133  std::vector<int> indices(numIndices);
134  int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
135  if (err != 0) {
136  throw std::runtime_error("fei::VectorReducer::sumInFieldData ERROR in vspace->getGlobalIndices.");
137  }
138 
139  return(sumIn(numIndices, &indices[0], data, vectorIndex));
140 }
141 
142 //----------------------------------------------------------------------------
143 int VectorReducer::copyInFieldData(int fieldID,
144  int idType,
145  int numIDs,
146  const int* IDs,
147  const double* data,
148  int vectorIndex)
149 {
150  fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
151  int fieldSize = vspace->getFieldSize(fieldID);
152  int numIndices = numIDs*fieldSize;
153  std::vector<int> indices(numIndices);
154  int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
155  if (err != 0) {
156  throw std::runtime_error("fei::VectorReducer::copyInFieldData ERROR in vspace->getGlobalIndices.");
157  }
158 
159  return(copyIn(numIndices, &indices[0], data, vectorIndex));
160 }
161 
162 //----------------------------------------------------------------------------
163 int VectorReducer::copyInFieldDataLocalIDs(int fieldID,
164  int idType,
165  int numIDs,
166  const int* localIDs,
167  const double* data,
168  int vectorIndex)
169 {
170  fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
171  int fieldSize = vspace->getFieldSize(fieldID);
172  int numIndices = numIDs*fieldSize;
173  std::vector<int> indices(numIndices);
174  int err = vspace->getGlobalIndicesLocalIDs(numIDs, localIDs, idType, fieldID, &indices[0]);
175  if (err != 0) {
176  throw std::runtime_error("fei::VectorReducer::copyInFieldData ERROR in vspace->getGlobalIndices.");
177  }
178 
179  return(copyIn(numIndices, &indices[0], data, vectorIndex));
180 }
181 
182 //----------------------------------------------------------------------------
183 int VectorReducer::copyOut_FE(int nodeNumber, int dofOffset,
184  double& value)
185 {
186  return(-1);
187 }
188 
189 //----------------------------------------------------------------------------
190 int VectorReducer::copyOutFieldData(int fieldID,
191  int idType,
192  int numIDs,
193  const int* IDs,
194  double* data,
195  int vectorIndex)
196 {
197  fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
198  int fieldSize = vspace->getFieldSize(fieldID);
199  int numIndices = numIDs*fieldSize;
200  std::vector<int> indices(numIndices);
201  int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
202  if (err != 0) {
203  throw std::runtime_error("fei::VectorReducer::copyOutFieldData ERROR in vspace->getGlobalIndices.");
204  }
205 
206  return(copyOut(numIndices, &indices[0], data, vectorIndex));
207 }
208 
209 //----------------------------------------------------------------------------
210 int VectorReducer::writeToFile(const char* filename,
211  bool matrixMarketFormat)
212 {
213  return( target_->writeToFile(filename, matrixMarketFormat) );
214 }
215 
216 //----------------------------------------------------------------------------
217 int VectorReducer::writeToStream(FEI_OSTREAM& ostrm,
218  bool matrixMarketFormat)
219 {
220  return( target_->writeToStream(ostrm, matrixMarketFormat) );
221 }
222 
223 //----------------------------------------------------------------------------
224 int VectorReducer::copyOut(int numValues,
225  const int* indices,
226  double* values,
227  int vectorIndex) const
228 {
229  int err = reducer_->copyOutVectorValues(numValues, indices, values,
230  isSolution_, vectorIndex,
231  *target_);
232  return(err);
233 }
234 
235 //----------------------------------------------------------------------------
236 int VectorReducer::sumIntoFEVector(int blockID,
237  int connOffset,
238  int numNodes,
239  const int* nodeNumbers,
240  const int* numIndicesPerNode,
241  const double* values)
242 {
243  return(-1);
244 }
245 
246 }//namespace fei
247 
MPI_Comm getCommunicator() const
int getGlobalIndicesLocalIDs(int numIDs, const int *localIDs, int idType, int fieldID, int *globalIndices)
int getIndices_SharedAndOwned(std::vector< int > &globalIndices) const
virtual fei::SharedPtr< fei::VectorSpace > getVectorSpace() const =0
int getGlobalIndices(int numIDs, const int *IDs, int idType, int fieldID, int *globalIndices)
T * get() const
int localProc(MPI_Comm comm)
int getNumIndices_SharedAndOwned() const
unsigned getFieldSize(int fieldID)
int numProcs(MPI_Comm comm)