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_, accumulate);
00087   return(target_->gatherFromOverlap(accumulate));
00088 }
00089 
00090 //----------------------------------------------------------------------------
00091 int VectorReducer::sumIn(int numValues,
00092                          const int* indices, const double* values,
00093                          int vectorIndex)
00094 {
00095   return(reducer_->addVectorValues(numValues, indices, values, true,
00096                                    isSolution_, vectorIndex, *target_));
00097 }
00098 
00099 //----------------------------------------------------------------------------
00100 int VectorReducer::copyIn(int numValues,
00101                           const int* indices, const double* values,
00102                           int vectorIndex)
00103 {
00104   return(reducer_->addVectorValues(numValues, indices, values, false,
00105                                    isSolution_, vectorIndex, *target_));
00106 }
00107 
00108 //----------------------------------------------------------------------------
00109 int VectorReducer::giveToUnderlyingVector(int numValues,
00110                                           const int* indices,
00111                                           const double* values,
00112                                           bool sumInto,
00113                                           int vectorIndex)
00114 {
00115   int err = reducer_->addVectorValues(numValues, indices, values, sumInto,
00116                                       isSolution_, vectorIndex, *target_);
00117   return(err);
00118 }
00119 
00120 //----------------------------------------------------------------------------
00121 int VectorReducer::sumInFieldData(int fieldID,
00122                                   int idType,
00123                                   int numIDs,
00124                                   const int* IDs,
00125                                   const double* data,
00126                                   int vectorIndex)
00127 {
00128   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00129   int fieldSize = vspace->getFieldSize(fieldID);
00130   int numIndices = numIDs*fieldSize;
00131   std::vector<int> indices(numIndices);
00132   int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
00133   if (err != 0) {
00134     throw std::runtime_error("fei::VectorReducer::sumInFieldData ERROR in vspace->getGlobalIndices.");
00135   }
00136 
00137   return(sumIn(numIndices, &indices[0], data, vectorIndex));
00138 }
00139 
00140 //----------------------------------------------------------------------------
00141 int VectorReducer::copyInFieldData(int fieldID,
00142                                    int idType,
00143                                    int numIDs,
00144                                    const int* IDs,
00145                                    const double* data,
00146                                    int vectorIndex)
00147 {
00148   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00149   int fieldSize = vspace->getFieldSize(fieldID);
00150   int numIndices = numIDs*fieldSize;
00151   std::vector<int> indices(numIndices);
00152   int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
00153   if (err != 0) {
00154     throw std::runtime_error("fei::VectorReducer::copyInFieldData ERROR in vspace->getGlobalIndices.");
00155   }
00156 
00157   return(copyIn(numIndices, &indices[0], data, vectorIndex));
00158 }
00159 
00160 //----------------------------------------------------------------------------
00161 int VectorReducer::copyOut_FE(int nodeNumber, int dofOffset,
00162                               double& value)
00163 {
00164   return(-1);
00165 }
00166 
00167 //----------------------------------------------------------------------------
00168 int VectorReducer::copyOutFieldData(int fieldID,
00169                                     int idType,
00170                                     int numIDs,
00171                                     const int* IDs,
00172                                     double* data,
00173                                     int vectorIndex)
00174 {
00175   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00176   int fieldSize = vspace->getFieldSize(fieldID);
00177   int numIndices = numIDs*fieldSize;
00178   std::vector<int> indices(numIndices);
00179   int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
00180   if (err != 0) {
00181     throw std::runtime_error("fei::VectorReducer::copyOutFieldData ERROR in vspace->getGlobalIndices.");
00182   }
00183 
00184   return(copyOut(numIndices, &indices[0], data, vectorIndex));
00185 }
00186 
00187 //----------------------------------------------------------------------------
00188 int VectorReducer::writeToFile(const char* filename,
00189                                bool matrixMarketFormat)
00190 {
00191   return( target_->writeToFile(filename, matrixMarketFormat) );
00192 }
00193 
00194 //----------------------------------------------------------------------------
00195 int VectorReducer::writeToStream(FEI_OSTREAM& ostrm,
00196                                  bool matrixMarketFormat)
00197 {
00198   return( target_->writeToStream(ostrm, matrixMarketFormat) );
00199 }
00200 
00201 //----------------------------------------------------------------------------
00202 int VectorReducer::copyOut(int numValues,
00203                            const int* indices,
00204                            double* values,
00205                            int vectorIndex) const
00206 {
00207   int err = reducer_->copyOutVectorValues(numValues, indices, values,
00208                                           isSolution_, vectorIndex,
00209                                           *target_);
00210   return(err);
00211 }
00212 
00213 //----------------------------------------------------------------------------
00214 int VectorReducer::sumIntoFEVector(int blockID,
00215                                    int connOffset,
00216                                    int numNodes,
00217                                    const int* nodeNumbers,
00218                                    const int* numIndicesPerNode,
00219                                    const double* values)
00220 {
00221   return(-1);
00222 }
00223 
00224 }//namespace fei
00225 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends