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   return(target_->gatherFromOverlap(accumulate));
00123 }
00124 
00125 //----------------------------------------------------------------------------
00126 int VectorReducer::sumIn(int numValues,
00127                          const int* indices, const double* values,
00128                          int vectorIndex)
00129 {
00130   return(reducer_->addVectorValues(numValues, indices, values, true,
00131                                    isSolution_, vectorIndex, *target_));
00132 }
00133 
00134 //----------------------------------------------------------------------------
00135 int VectorReducer::copyIn(int numValues,
00136                           const int* indices, const double* values,
00137                           int vectorIndex)
00138 {
00139   return(reducer_->addVectorValues(numValues, indices, values, false,
00140                                    isSolution_, vectorIndex, *target_));
00141 }
00142 
00143 //----------------------------------------------------------------------------
00144 int VectorReducer::giveToUnderlyingVector(int numValues,
00145                                           const int* indices,
00146                                           const double* values,
00147                                           bool sumInto,
00148                                           int vectorIndex)
00149 {
00150   int err = reducer_->addVectorValues(numValues, indices, values, sumInto,
00151                                       isSolution_, vectorIndex, *target_);
00152   return(err);
00153 }
00154 
00155 //----------------------------------------------------------------------------
00156 int VectorReducer::sumInFieldData(int fieldID,
00157                                   int idType,
00158                                   int numIDs,
00159                                   const int* IDs,
00160                                   const double* data,
00161                                   int vectorIndex)
00162 {
00163   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00164   int fieldSize = vspace->getFieldSize(fieldID);
00165   int numIndices = numIDs*fieldSize;
00166   std::vector<int> indices(numIndices);
00167   int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
00168   if (err != 0) {
00169     throw std::runtime_error("fei::VectorReducer::sumInFieldData ERROR in vspace->getGlobalIndices.");
00170   }
00171 
00172   return(sumIn(numIndices, &indices[0], data, vectorIndex));
00173 }
00174 
00175 //----------------------------------------------------------------------------
00176 int VectorReducer::copyInFieldData(int fieldID,
00177                                    int idType,
00178                                    int numIDs,
00179                                    const int* IDs,
00180                                    const double* data,
00181                                    int vectorIndex)
00182 {
00183   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00184   int fieldSize = vspace->getFieldSize(fieldID);
00185   int numIndices = numIDs*fieldSize;
00186   std::vector<int> indices(numIndices);
00187   int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
00188   if (err != 0) {
00189     throw std::runtime_error("fei::VectorReducer::copyInFieldData ERROR in vspace->getGlobalIndices.");
00190   }
00191 
00192   return(copyIn(numIndices, &indices[0], data, vectorIndex));
00193 }
00194 
00195 //----------------------------------------------------------------------------
00196 int VectorReducer::copyOut_FE(int nodeNumber, int dofOffset,
00197                               double& value)
00198 {
00199   return(-1);
00200 }
00201 
00202 //----------------------------------------------------------------------------
00203 int VectorReducer::copyOutFieldData(int fieldID,
00204                                     int idType,
00205                                     int numIDs,
00206                                     const int* IDs,
00207                                     double* data,
00208                                     int vectorIndex)
00209 {
00210   fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
00211   int fieldSize = vspace->getFieldSize(fieldID);
00212   int numIndices = numIDs*fieldSize;
00213   std::vector<int> indices(numIndices);
00214   int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
00215   if (err != 0) {
00216     throw std::runtime_error("fei::VectorReducer::copyOutFieldData ERROR in vspace->getGlobalIndices.");
00217   }
00218 
00219   return(copyOut(numIndices, &indices[0], data, vectorIndex));
00220 }
00221 
00222 //----------------------------------------------------------------------------
00223 int VectorReducer::writeToFile(const char* filename,
00224                                bool matrixMarketFormat)
00225 {
00226   return( target_->writeToFile(filename, matrixMarketFormat) );
00227 }
00228 
00229 //----------------------------------------------------------------------------
00230 int VectorReducer::writeToStream(FEI_OSTREAM& ostrm,
00231                                  bool matrixMarketFormat)
00232 {
00233   return( target_->writeToStream(ostrm, matrixMarketFormat) );
00234 }
00235 
00236 //----------------------------------------------------------------------------
00237 int VectorReducer::copyOut(int numValues,
00238                            const int* indices,
00239                            double* values,
00240                            int vectorIndex) const
00241 {
00242   int err = reducer_->copyOutVectorValues(numValues, indices, values,
00243                                           isSolution_, vectorIndex,
00244                                           *target_);
00245   return(err);
00246 }
00247 
00248 //----------------------------------------------------------------------------
00249 int VectorReducer::sumIntoFEVector(int blockID,
00250                                    int connOffset,
00251                                    int numNodes,
00252                                    const int* nodeNumbers,
00253                                    const int* numIndicesPerNode,
00254                                    const double* values)
00255 {
00256   return(-1);
00257 }
00258 
00259 }//namespace fei
00260 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends