EpetraExt Development
EpetraExt_RestrictedCrsMatrixWrapper.cpp
Go to the documentation of this file.
00001 /*
00002 //@HEADER
00003 // ***********************************************************************
00004 //
00005 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00006 //                 Copyright (2011) Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 //@HEADER
00042 */
00043 
00044 #include "EpetraExt_ConfigDefs.h"
00045 #include "Epetra_ConfigDefs.h"
00046 
00047 
00048 #ifdef HAVE_MPI
00049 
00050 
00051 #include "EpetraExt_RestrictedCrsMatrixWrapper.h"
00052 #include "Epetra_MpiComm.h"
00053 #include "Epetra_Map.h"
00054 #include "Epetra_CrsMatrix.h"
00055 
00056 
00057 namespace EpetraExt{
00058 
00059 RestrictedCrsMatrixWrapper::RestrictedCrsMatrixWrapper()
00060   : proc_is_active(true),
00061     subcomm_is_set(false),
00062     MPI_SubComm_(MPI_COMM_NULL),
00063     RestrictedComm_(0),
00064     ResRowMap_(0),
00065     ResColMap_(0),
00066     input_matrix_(),
00067     restricted_matrix_()  
00068 {
00069   
00070 }
00071 
00072 RestrictedCrsMatrixWrapper::~RestrictedCrsMatrixWrapper() {
00073   delete ResRowMap_;
00074   delete ResColMap_;
00075   delete RestrictedComm_;
00076 }
00077 
00078 
00079 
00080 int RestrictedCrsMatrixWrapper::SetMPISubComm(MPI_Comm MPI_SubComm){
00081   if(!subcomm_is_set){
00082     MPI_SubComm_=MPI_SubComm; delete RestrictedComm_; subcomm_is_set=true;
00083     return 0;
00084   }
00085   else return -1;
00086 }
00087 
00088 
00089 template<typename int_type>
00090 int RestrictedCrsMatrixWrapper::Trestrict_comm(Teuchos::RCP<Epetra_CrsMatrix> input_matrix){
00091   /* Pull the Matrix Info */
00092   input_matrix_=input_matrix;
00093   
00094   const Epetra_MpiComm *InComm = dynamic_cast<const Epetra_MpiComm*>(& input_matrix_->Comm());
00095   const Epetra_Map *InRowMap= dynamic_cast<const Epetra_Map* >(& input_matrix_->RowMap());
00096   const Epetra_Map *InColMap= dynamic_cast<const Epetra_Map* >(& input_matrix_->ColMap());
00097 
00098   if(!InComm || !InRowMap || !InColMap) return (-1);
00099   
00100   int_type Nrows = (int_type) InRowMap->NumGlobalElements64();
00101   int_type Ncols = (int_type) InColMap->NumGlobalElements64();
00102   
00103   if(!subcomm_is_set){
00104     /* Build the Split Communicators, If Needed */
00105     int color;
00106     if(InRowMap->NumMyElements()) color=1;
00107     else color=MPI_UNDEFINED;
00108     MPI_Comm_split(InComm->Comm(),color,InComm->MyPID(),&MPI_SubComm_);
00109   }
00110   else{
00111     /* Sanity check user-provided subcomm - drop an error if the MPISubComm
00112        does not include a processor with data. */
00113     if (input_matrix->NumMyRows() && MPI_SubComm_ == MPI_COMM_NULL)
00114       return(-2);
00115   }
00116 
00117   /* Mark active processors */
00118   if(MPI_SubComm_ == MPI_COMM_NULL) proc_is_active=false;
00119   else proc_is_active=true;
00120   
00121 
00122   if(proc_is_active){      
00123     RestrictedComm_=new Epetra_MpiComm(MPI_SubComm_);
00124     
00125     int_type* RowMapGlobalElements = 0;
00126   InRowMap->MyGlobalElementsPtr(RowMapGlobalElements);
00127     int_type* ColMapGlobalElements = 0;
00128   InColMap->MyGlobalElementsPtr(ColMapGlobalElements);
00129 
00130     /* Build the Restricted Maps */
00131     ResRowMap_ = new Epetra_Map(Nrows,InRowMap->NumMyElements(),RowMapGlobalElements,
00132                                 (int_type) InRowMap->IndexBase64(),*RestrictedComm_);
00133     ResColMap_ = new Epetra_Map(Ncols,InColMap->NumMyElements(),ColMapGlobalElements,
00134                                 (int_type) InColMap->IndexBase64(),*RestrictedComm_);        
00135     
00136     int *colind,Nr;
00137     double *values;
00138     
00139     /* Allocate the Restricted Matrix */
00140     restricted_matrix_= Teuchos::rcp(new Epetra_CrsMatrix(View,*ResRowMap_,*ResColMap_,0));
00141     for(int i=0;i<input_matrix_->NumMyRows();i++) {
00142       input_matrix_->ExtractMyRowView(i,Nr,values,colind);
00143       restricted_matrix_->InsertMyValues(i,Nr,values,colind);
00144     }
00145     restricted_matrix_->FillComplete();      
00146   }
00147 
00148   return 0;
00149 }/*end restrict_comm*/
00150 
00151 int RestrictedCrsMatrixWrapper::restrict_comm(Teuchos::RCP<Epetra_CrsMatrix> input_matrix)
00152 {
00153 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
00154   if(input_matrix->RowMap().GlobalIndicesInt()) {
00155   return Trestrict_comm<int>(input_matrix);
00156   }
00157   else
00158 #endif
00159 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
00160   if(input_matrix->RowMap().GlobalIndicesLongLong()) {
00161   return Trestrict_comm<long long>(input_matrix);
00162   }
00163   else
00164 #endif
00165     throw "EpetraExt::Trestrict_comm: ERROR, GlobalIndices type unknown.";
00166 }
00167 
00168 
00169 }  
00170 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines