FEI Version of the Day
fei_VectorReducer.cpp
00001 /*
00002 // @HEADER
00003 // ************************************************************************
00004 //             FEI: Finite Element Interface to Linear Solvers
00005 //                  Copyright (2005) Sandia Corporation.
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the
00008 // U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Alan Williams (william@sandia.gov) 
00038 //
00039 // ************************************************************************
00040 // @HEADER
00041 */
00042 
00043 
00044 #include <fei_VectorReducer.hpp>
00045 #include <fei_VectorSpace.hpp>
00046 #include <fei_Vector_core.hpp>
00047 #include <fei_Vector.hpp>
00048 #include <fei_CommUtils.hpp>
00049 
00050 #undef fei_file
00051 #define fei_file "fei_VectorReducer.cpp"
00052 
00053 #include <fei_ErrMacros.hpp>
00054 
00055 namespace fei {
00056 
00057 //----------------------------------------------------------------------------
00058 VectorReducer::VectorReducer(fei::SharedPtr<fei::Reducer> reducer,
00059                              fei::SharedPtr<fei::Vector> target,
00060                              bool isSolutionVector)
00061   : reducer_(reducer),
00062     target_(target),
00063     isSolution_(isSolutionVector)
00064 {
00065   localProc_ = fei::localProc(target->getVectorSpace()->getCommunicator());
00066   numProcs_ = fei::numProcs(target->getVectorSpace()->getCommunicator());
00067 
00068   fei::Vector_core* target_core = dynamic_cast<fei::Vector_core*>(target.get());
00069   if (target_core == NULL) {
00070     throw std::runtime_error("fei::VectorReducer ERROR, target vector not dynamic_cast-able to fei::Vector_core.");
00071   }
00072 
00073   fei::SharedPtr<fei::VectorSpace> vecspace = target->getVectorSpace();
00074   int numEqns = vecspace->getNumIndices_SharedAndOwned();
00075   std::vector<int> eqns;
00076   vecspace->getIndices_SharedAndOwned(eqns);
00077 
00078   std::vector<int> overlap;
00079   for(int i=0; i<numEqns; ++i) {
00080     if (!reducer->isSlaveEqn(eqns[i])) {
00081       overlap.push_back(reducer->translateToReducedEqn(eqns[i]));
00082     }
00083     else {
00084       std::vector<int> masters;
00085       reducer->getSlaveMasterEqns(eqns[i], masters);
00086       for(unsigned j=0; j<masters.size(); ++j) {
00087         overlap.push_back(reducer->translateToReducedEqn(masters[j]));
00088       }
00089     }
00090   }
00091 
00092   target_core->setOverlap(overlap.size(), &overlap[0]);
00093 }
00094 
00095 //----------------------------------------------------------------------------
00096 VectorReducer::~VectorReducer()
00097 {
00098 }
00099 
00100 //----------------------------------------------------------------------------
00101 int VectorReducer::putScalar(double scalar)
00102 {
00103   return(target_->putScalar(scalar));
00104 }
00105 
00106 //----------------------------------------------------------------------------
00107 int VectorReducer::update(double a, const fei::Vector* x, double b)
00108 {
00109   return(target_->update(a, x, b));
00110 }
00111 
00112 //----------------------------------------------------------------------------
00113 int VectorReducer::scatterToOverlap()
00114 {
00115   return(target_->scatterToOverlap());
00116 }
00117 
00118 //----------------------------------------------------------------------------
00119 int VectorReducer::gatherFromOverlap(bool accumulate)
00120 {
00121   reducer_->assembleReducedVector(isSolution_, *target_);
00122   target_->setCommSizes();
00123   return(target_->gatherFromOverlap(accumulate));
00124 }
00125 
00126 //----------------------------------------------------------------------------
00127 int VectorReducer::sumIn(int numValues,
00128                          const int* indices, const double* values,
00129                          int vectorIndex)
00130 {
00131   return(reducer_->addVectorValues(numValues, indices, values, true,
00132                                    isSolution_, vectorIndex, *target_));
00133 }
00134 
00135 //----------------------------------------------------------------------------
00136 int VectorReducer::copyIn(int numValues,
00137                           const int* indices, const double* values,
00138                           int vectorIndex)
00139 {
00140   return(reducer_->addVectorValues(numValues, indices, values, false,
00141                                    isSolution_, vectorIndex, *target_));
00142 }
00143 
00144 //----------------------------------------------------------------------------
00145 int VectorReducer::giveToUnderlyingVector(int numValues,
00146                                           const int* indices,
00147                                           const double* values,
00148                                           bool sumInto,
00149                                           int vectorIndex)
00150 {
00151   int err = reducer_->addVectorValues(numValues, indices, values, sumInto,
00152                                       isSolution_, vectorIndex, *target_);
00153   return(err);
00154 }
00155 
00156 //----------------------------------------------------------------------------
00157 int VectorReducer::sumInFieldData(int fieldID,
00158                                   int idType,
00159                                   int numIDs,
00160                                   const int* IDs,
00161                                   const double* data,
00162                                   int vectorIndex)
00163 {
00164   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00165   int fieldSize = vspace->getFieldSize(fieldID);
00166   int numIndices = numIDs*fieldSize;
00167   std::vector<int> indices(numIndices);
00168   int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
00169   if (err != 0) {
00170     throw std::runtime_error("fei::VectorReducer::sumInFieldData ERROR in vspace->getGlobalIndices.");
00171   }
00172 
00173   return(sumIn(numIndices, &indices[0], data, vectorIndex));
00174 }
00175 
00176 //----------------------------------------------------------------------------
00177 int VectorReducer::copyInFieldData(int fieldID,
00178                                    int idType,
00179                                    int numIDs,
00180                                    const int* IDs,
00181                                    const double* data,
00182                                    int vectorIndex)
00183 {
00184   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00185   int fieldSize = vspace->getFieldSize(fieldID);
00186   int numIndices = numIDs*fieldSize;
00187   std::vector<int> indices(numIndices);
00188   int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
00189   if (err != 0) {
00190     throw std::runtime_error("fei::VectorReducer::copyInFieldData ERROR in vspace->getGlobalIndices.");
00191   }
00192 
00193   return(copyIn(numIndices, &indices[0], data, vectorIndex));
00194 }
00195 
00196 //----------------------------------------------------------------------------
00197 int VectorReducer::copyInFieldDataLocalIDs(int fieldID,
00198                                    int idType,
00199                                    int numIDs,
00200                                    const int* localIDs,
00201                                    const double* data,
00202                                    int vectorIndex)
00203 {
00204   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00205   int fieldSize = vspace->getFieldSize(fieldID);
00206   int numIndices = numIDs*fieldSize;
00207   std::vector<int> indices(numIndices);
00208   int err = vspace->getGlobalIndicesLocalIDs(numIDs, localIDs, idType, fieldID, &indices[0]);
00209   if (err != 0) {
00210     throw std::runtime_error("fei::VectorReducer::copyInFieldData ERROR in vspace->getGlobalIndices.");
00211   }
00212 
00213   return(copyIn(numIndices, &indices[0], data, vectorIndex));
00214 }
00215 
00216 //----------------------------------------------------------------------------
00217 int VectorReducer::copyOut_FE(int nodeNumber, int dofOffset,
00218                               double& value)
00219 {
00220   return(-1);
00221 }
00222 
00223 //----------------------------------------------------------------------------
00224 int VectorReducer::copyOutFieldData(int fieldID,
00225                                     int idType,
00226                                     int numIDs,
00227                                     const int* IDs,
00228                                     double* data,
00229                                     int vectorIndex)
00230 {
00231   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00232   int fieldSize = vspace->getFieldSize(fieldID);
00233   int numIndices = numIDs*fieldSize;
00234   std::vector<int> indices(numIndices);
00235   int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
00236   if (err != 0) {
00237     throw std::runtime_error("fei::VectorReducer::copyOutFieldData ERROR in vspace->getGlobalIndices.");
00238   }
00239 
00240   return(copyOut(numIndices, &indices[0], data, vectorIndex));
00241 }
00242 
00243 //----------------------------------------------------------------------------
00244 int VectorReducer::writeToFile(const char* filename,
00245                                bool matrixMarketFormat)
00246 {
00247   return( target_->writeToFile(filename, matrixMarketFormat) );
00248 }
00249 
00250 //----------------------------------------------------------------------------
00251 int VectorReducer::writeToStream(FEI_OSTREAM& ostrm,
00252                                  bool matrixMarketFormat)
00253 {
00254   return( target_->writeToStream(ostrm, matrixMarketFormat) );
00255 }
00256 
00257 //----------------------------------------------------------------------------
00258 int VectorReducer::copyOut(int numValues,
00259                            const int* indices,
00260                            double* values,
00261                            int vectorIndex) const
00262 {
00263   int err = reducer_->copyOutVectorValues(numValues, indices, values,
00264                                           isSolution_, vectorIndex,
00265                                           *target_);
00266   return(err);
00267 }
00268 
00269 //----------------------------------------------------------------------------
00270 int VectorReducer::sumIntoFEVector(int blockID,
00271                                    int connOffset,
00272                                    int numNodes,
00273                                    const int* nodeNumbers,
00274                                    const int* numIndicesPerNode,
00275                                    const double* values)
00276 {
00277   return(-1);
00278 }
00279 
00280 }//namespace fei
00281 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends