FEI Version of the Day
fei_VectorReducer.cpp
00001 /*--------------------------------------------------------------------*/
00002 /*    Copyright 2005 Sandia Corporation.                              */
00003 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00004 /*    non-exclusive license for use of this work by or on behalf      */
00005 /*    of the U.S. Government.  Export of this program may require     */
00006 /*    a license from the United States Government.                    */
00007 /*--------------------------------------------------------------------*/
00008 
00009 #include <fei_VectorReducer.hpp>
00010 #include <fei_VectorSpace.hpp>
00011 #include <fei_Vector_core.hpp>
00012 #include <fei_Vector.hpp>
00013 #include <fei_CommUtils.hpp>
00014 
00015 #undef fei_file
00016 #define fei_file "fei_VectorReducer.cpp"
00017 
00018 #include <fei_ErrMacros.hpp>
00019 
00020 namespace fei {
00021 
00022 //----------------------------------------------------------------------------
00023 VectorReducer::VectorReducer(fei::SharedPtr<fei::Reducer> reducer,
00024                              fei::SharedPtr<fei::Vector> target,
00025                              bool isSolutionVector)
00026   : reducer_(reducer),
00027     target_(target),
00028     isSolution_(isSolutionVector)
00029 {
00030   localProc_ = fei::localProc(target->getVectorSpace()->getCommunicator());
00031   numProcs_ = fei::numProcs(target->getVectorSpace()->getCommunicator());
00032 
00033   fei::Vector_core* target_core = dynamic_cast<fei::Vector_core*>(target.get());
00034   if (target_core == NULL) {
00035     throw std::runtime_error("fei::VectorReducer ERROR, target vector not dynamic_cast-able to fei::Vector_core.");
00036   }
00037 
00038   fei::SharedPtr<fei::VectorSpace> vecspace = target->getVectorSpace();
00039   int numEqns = vecspace->getNumIndices_SharedAndOwned();
00040   std::vector<int> eqns;
00041   vecspace->getIndices_SharedAndOwned(eqns);
00042 
00043   std::vector<int> overlap;
00044   for(int i=0; i<numEqns; ++i) {
00045     if (!reducer->isSlaveEqn(eqns[i])) {
00046       overlap.push_back(reducer->translateToReducedEqn(eqns[i]));
00047     }
00048     else {
00049       std::vector<int> masters;
00050       reducer->getSlaveMasterEqns(eqns[i], masters);
00051       for(unsigned j=0; j<masters.size(); ++j) {
00052         overlap.push_back(reducer->translateToReducedEqn(masters[j]));
00053       }
00054     }
00055   }
00056 
00057   target_core->setOverlap(overlap.size(), &overlap[0]);
00058 }
00059 
00060 //----------------------------------------------------------------------------
00061 VectorReducer::~VectorReducer()
00062 {
00063 }
00064 
00065 //----------------------------------------------------------------------------
00066 int VectorReducer::putScalar(double scalar)
00067 {
00068   return(target_->putScalar(scalar));
00069 }
00070 
00071 //----------------------------------------------------------------------------
00072 int VectorReducer::update(double a, const fei::Vector* x, double b)
00073 {
00074   return(target_->update(a, x, b));
00075 }
00076 
00077 //----------------------------------------------------------------------------
00078 int VectorReducer::scatterToOverlap()
00079 {
00080   return(target_->scatterToOverlap());
00081 }
00082 
00083 //----------------------------------------------------------------------------
00084 int VectorReducer::gatherFromOverlap(bool accumulate)
00085 {
00086   reducer_->assembleReducedVector(isSolution_, *target_);
00087   target_->setCommSizes();
00088   return(target_->gatherFromOverlap(accumulate));
00089 }
00090 
00091 //----------------------------------------------------------------------------
00092 int VectorReducer::sumIn(int numValues,
00093                          const int* indices, const double* values,
00094                          int vectorIndex)
00095 {
00096   return(reducer_->addVectorValues(numValues, indices, values, true,
00097                                    isSolution_, vectorIndex, *target_));
00098 }
00099 
00100 //----------------------------------------------------------------------------
00101 int VectorReducer::copyIn(int numValues,
00102                           const int* indices, const double* values,
00103                           int vectorIndex)
00104 {
00105   return(reducer_->addVectorValues(numValues, indices, values, false,
00106                                    isSolution_, vectorIndex, *target_));
00107 }
00108 
00109 //----------------------------------------------------------------------------
00110 int VectorReducer::giveToUnderlyingVector(int numValues,
00111                                           const int* indices,
00112                                           const double* values,
00113                                           bool sumInto,
00114                                           int vectorIndex)
00115 {
00116   int err = reducer_->addVectorValues(numValues, indices, values, sumInto,
00117                                       isSolution_, vectorIndex, *target_);
00118   return(err);
00119 }
00120 
00121 //----------------------------------------------------------------------------
00122 int VectorReducer::sumInFieldData(int fieldID,
00123                                   int idType,
00124                                   int numIDs,
00125                                   const int* IDs,
00126                                   const double* data,
00127                                   int vectorIndex)
00128 {
00129   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00130   int fieldSize = vspace->getFieldSize(fieldID);
00131   int numIndices = numIDs*fieldSize;
00132   std::vector<int> indices(numIndices);
00133   int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
00134   if (err != 0) {
00135     throw std::runtime_error("fei::VectorReducer::sumInFieldData ERROR in vspace->getGlobalIndices.");
00136   }
00137 
00138   return(sumIn(numIndices, &indices[0], data, vectorIndex));
00139 }
00140 
00141 //----------------------------------------------------------------------------
00142 int VectorReducer::copyInFieldData(int fieldID,
00143                                    int idType,
00144                                    int numIDs,
00145                                    const int* IDs,
00146                                    const double* data,
00147                                    int vectorIndex)
00148 {
00149   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00150   int fieldSize = vspace->getFieldSize(fieldID);
00151   int numIndices = numIDs*fieldSize;
00152   std::vector<int> indices(numIndices);
00153   int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
00154   if (err != 0) {
00155     throw std::runtime_error("fei::VectorReducer::copyInFieldData ERROR in vspace->getGlobalIndices.");
00156   }
00157 
00158   return(copyIn(numIndices, &indices[0], data, vectorIndex));
00159 }
00160 
00161 //----------------------------------------------------------------------------
00162 int VectorReducer::copyInFieldDataLocalIDs(int fieldID,
00163                                    int idType,
00164                                    int numIDs,
00165                                    const int* localIDs,
00166                                    const double* data,
00167                                    int vectorIndex)
00168 {
00169   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00170   int fieldSize = vspace->getFieldSize(fieldID);
00171   int numIndices = numIDs*fieldSize;
00172   std::vector<int> indices(numIndices);
00173   int err = vspace->getGlobalIndicesLocalIDs(numIDs, localIDs, idType, fieldID, &indices[0]);
00174   if (err != 0) {
00175     throw std::runtime_error("fei::VectorReducer::copyInFieldData ERROR in vspace->getGlobalIndices.");
00176   }
00177 
00178   return(copyIn(numIndices, &indices[0], data, vectorIndex));
00179 }
00180 
00181 //----------------------------------------------------------------------------
00182 int VectorReducer::copyOut_FE(int nodeNumber, int dofOffset,
00183                               double& value)
00184 {
00185   return(-1);
00186 }
00187 
00188 //----------------------------------------------------------------------------
00189 int VectorReducer::copyOutFieldData(int fieldID,
00190                                     int idType,
00191                                     int numIDs,
00192                                     const int* IDs,
00193                                     double* data,
00194                                     int vectorIndex)
00195 {
00196   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00197   int fieldSize = vspace->getFieldSize(fieldID);
00198   int numIndices = numIDs*fieldSize;
00199   std::vector<int> indices(numIndices);
00200   int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
00201   if (err != 0) {
00202     throw std::runtime_error("fei::VectorReducer::copyOutFieldData ERROR in vspace->getGlobalIndices.");
00203   }
00204 
00205   return(copyOut(numIndices, &indices[0], data, vectorIndex));
00206 }
00207 
00208 //----------------------------------------------------------------------------
00209 int VectorReducer::writeToFile(const char* filename,
00210                                bool matrixMarketFormat)
00211 {
00212   return( target_->writeToFile(filename, matrixMarketFormat) );
00213 }
00214 
00215 //----------------------------------------------------------------------------
00216 int VectorReducer::writeToStream(FEI_OSTREAM& ostrm,
00217                                  bool matrixMarketFormat)
00218 {
00219   return( target_->writeToStream(ostrm, matrixMarketFormat) );
00220 }
00221 
00222 //----------------------------------------------------------------------------
00223 int VectorReducer::copyOut(int numValues,
00224                            const int* indices,
00225                            double* values,
00226                            int vectorIndex) const
00227 {
00228   int err = reducer_->copyOutVectorValues(numValues, indices, values,
00229                                           isSolution_, vectorIndex,
00230                                           *target_);
00231   return(err);
00232 }
00233 
00234 //----------------------------------------------------------------------------
00235 int VectorReducer::sumIntoFEVector(int blockID,
00236                                    int connOffset,
00237                                    int numNodes,
00238                                    const int* nodeNumbers,
00239                                    const int* numIndicesPerNode,
00240                                    const double* values)
00241 {
00242   return(-1);
00243 }
00244 
00245 }//namespace fei
00246 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends