FEI Version of the Day
snl_fei_BlkSizeMsgHandler.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_macros.hpp>
00045 
00046 #include <snl_fei_BlkSizeMsgHandler.hpp>
00047 
00048 #include <fei_utils.hpp>
00049 
00050 #include <snl_fei_Utils.hpp>
00051 #include <fei_FieldMask.hpp>
00052 #include <snl_fei_RecordCollection.hpp>
00053 #include <fei_VectorSpace.hpp>
00054 #include <fei_ParameterSet.hpp>
00055 #include <fei_Graph.hpp>
00056 #include <snl_fei_Constraint.hpp>
00057 #include <fei_TemplateUtils.hpp>
00058 
00059 #include <fei_EqnBuffer.hpp>
00060 #include <fei_EqnCommMgr.hpp>
00061 #include <SNL_FEI_Structure.hpp>
00062 
00063 #undef fei_file
00064 #define fei_file "snl_fei_BlkSizeMsgHandler.cpp"
00065 #include <fei_ErrMacros.hpp>
00066 
00067 //----------------------------------------------------------------------------
00068 snl_fei::BlkSizeMsgHandler::BlkSizeMsgHandler(fei::VectorSpace* vspace,
00069                                               fei::Graph* graph,
00070                                               MPI_Comm comm)
00071   : remote_colIndices_(NULL),
00072     local_colIndices_(NULL),
00073     vecSpace_(vspace),
00074     ptBlkMap_(NULL),
00075     graph_(graph),
00076     comm_(comm),
00077     sendProcs_(0, 64),
00078     recvProcs_(0, 64),
00079     firstExchange_(true)
00080 {
00081   remote_colIndices_ = new fei::comm_map(0,1);
00082   local_colIndices_ = new fei::comm_map(0,1);
00083 
00084   ptBlkMap_ = vspace->getPointBlockMap();
00085 }
00086 
00087 //----------------------------------------------------------------------------
00088 snl_fei::BlkSizeMsgHandler::~BlkSizeMsgHandler()
00089 {
00090   delete remote_colIndices_;
00091   delete local_colIndices_;
00092 }
00093 
00094 //----------------------------------------------------------------------------
00095 int snl_fei::BlkSizeMsgHandler::do_the_exchange()
00096 {
00097   int local_proc = fei::localProc(comm_);
00098   if (fei::numProcs(comm_) < 2) {
00099     return(0);
00100   }
00101 
00102   fei::Graph::table_type* localgraph = graph_->getLocalGraph();
00103   fei::Graph::table_type::iterator
00104     g_iter = localgraph->begin(),
00105     g_end = localgraph->end();
00106 
00107   //First create a table that maps remote processors to column-indices from our
00108   //graph.
00109   //These are remotely-owned column-indices for which we will need block-sizes.
00110 
00111   for(; g_iter != g_end; ++g_iter) {
00112     fei::Graph::table_type::row_type* row = (*g_iter).second;
00113 
00114     fei::Graph::table_type::row_type::const_iterator
00115       iter = row->begin(),
00116       iter_end = row->end();
00117 
00118     int owner;
00119 
00120     for(; iter != iter_end; ++iter) {
00121       int col = *iter;
00122       owner = vecSpace_->getOwnerProcBlkIndex(col);
00123 
00124       if (owner != local_proc) {
00125         remote_colIndices_->addIndices(owner, 1, &col);
00126       }
00127     }
00128   }
00129 
00130   //Next, we need to send our lists of remotely-owned column-indices to the
00131   //owning processors. After that, those processors can respond by sending us
00132   //the sizes for those column-indices.
00133   fei::copyKeysToVector(remote_colIndices_->getMap(), sendProcs_);
00134 
00135   CHK_ERR( fei::mirrorProcs(comm_, sendProcs_, recvProcs_) );
00136 
00137   firstExchange_ = true;
00138 
00139   CHK_ERR( fei::exchange(comm_, this) );
00140 
00141   firstExchange_ = false;
00142 
00143   CHK_ERR( fei::exchange(comm_, this) );
00144 
00145   return(0);
00146 }
00147 
00148 //----------------------------------------------------------------------------
00149 std::vector<int>& snl_fei::BlkSizeMsgHandler::getSendProcs()
00150 {
00151   if (firstExchange_) {
00152     return(sendProcs_);
00153   }
00154   else {
00155     return(recvProcs_);
00156   }
00157 }
00158 
00159 //----------------------------------------------------------------------------
00160 std::vector<int>& snl_fei::BlkSizeMsgHandler::getRecvProcs()
00161 {
00162   if (firstExchange_) {
00163     return(recvProcs_);
00164   }
00165   else {
00166     return(sendProcs_);
00167   }
00168 }
00169 
00170 //----------------------------------------------------------------------------
00171 int snl_fei::BlkSizeMsgHandler::getSendMessageLength(int destProc,
00172                                                      int& messageLength)
00173 {
00174   if (firstExchange_) {
00175     fei::comm_map::row_type* cols = remote_colIndices_->getRow(destProc);
00176     messageLength = cols->size();
00177     return(0);
00178   }
00179   else {
00180     fei::comm_map::row_type* cols = local_colIndices_->getRow(destProc);
00181     messageLength = cols->size()*2;
00182     return(0);
00183   }
00184 }
00185 
00186 //----------------------------------------------------------------------------
00187 int snl_fei::BlkSizeMsgHandler::getSendMessage(int destProc,
00188                                                std::vector<int>& message)
00189 {
00190   if (firstExchange_) {
00191     fei::comm_map::row_type* cols = remote_colIndices_->getRow(destProc);
00192     message.resize(cols->size());
00193     fei::copySetToArray(*cols, message.size(), &message[0]);
00194     return(0);
00195   }
00196   else {
00197     fei::comm_map::row_type* cols = local_colIndices_->getRow(destProc);
00198 
00199     message.resize(cols->size()*2);
00200 
00201     fei::comm_map::row_type::const_iterator
00202       iter = cols->begin(),
00203       iter_end = cols->end();
00204 
00205     int offset = 0;
00206     for(; iter != iter_end; ++iter) {
00207       CHK_ERR( ptBlkMap_->getBlkEqnInfo(*iter,
00208                                         message[offset], message[offset+1]) );
00209       offset += 2;
00210     }
00211 
00212     return( 0 );
00213   }
00214 }
00215 
00216 //----------------------------------------------------------------------------
00217 int snl_fei::BlkSizeMsgHandler::processRecvMessage(int srcProc,
00218                                                    std::vector<int>& message)
00219 {
00220   if (firstExchange_) {
00221     for(unsigned i=0; i<message.size(); ++i) {
00222       local_colIndices_->addIndices(srcProc, 1, &(message[i]));
00223     }
00224   }
00225   else {
00226     fei::comm_map::row_type* cols = remote_colIndices_->getRow(srcProc);
00227     fei::comm_map::row_type::const_iterator
00228       iter = cols->begin(),
00229       iter_end = cols->end();
00230 
00231     int offset = 0;
00232     for(; iter != iter_end; ++iter) {
00233       int ptEqn = message[offset];
00234       int blkSize = message[offset+1];
00235       for(int i=0; i<blkSize; ++i) {
00236         CHK_ERR( ptBlkMap_->setEqn(ptEqn+i, *iter, blkSize) );
00237       }
00238       offset += 2;
00239     }
00240   }
00241 
00242   return(0);
00243 }
00244 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends