EpetraExt  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraExt_RestrictedCrsMatrixWrapper.cpp
Go to the documentation of this file.
1 /*
2 //@HEADER
3 // ***********************************************************************
4 //
5 // EpetraExt: Epetra Extended - Linear Algebra Services Package
6 // Copyright (2011) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //@HEADER
42 */
43 
44 #include "EpetraExt_ConfigDefs.h"
45 
46 
47 #ifdef HAVE_MPI
48 
49 
51 #include "Epetra_MpiComm.h"
52 #include "Epetra_Map.h"
53 #include "Epetra_CrsMatrix.h"
54 
55 
56 namespace EpetraExt{
57 
58 RestrictedCrsMatrixWrapper::RestrictedCrsMatrixWrapper()
59  : proc_is_active(true),
60  subcomm_is_set(false),
61  MPI_SubComm_(MPI_COMM_NULL),
62  RestrictedComm_(0),
63  ResRowMap_(0),
64  ResColMap_(0),
65  input_matrix_(),
66  restricted_matrix_()
67 {
68 
69 }
70 
71 RestrictedCrsMatrixWrapper::~RestrictedCrsMatrixWrapper() {
72  delete ResRowMap_;
73  delete ResColMap_;
74  delete RestrictedComm_;
75 }
76 
77 
78 
79 int RestrictedCrsMatrixWrapper::SetMPISubComm(MPI_Comm MPI_SubComm){
80  if(!subcomm_is_set){
81  MPI_SubComm_=MPI_SubComm; delete RestrictedComm_; subcomm_is_set=true;
82  return 0;
83  }
84  else return -1;
85 }
86 
87 
88 
89 int RestrictedCrsMatrixWrapper::restrict_comm(Teuchos::RCP<Epetra_CrsMatrix> input_matrix){
90  /* Pull the Matrix Info */
91  input_matrix_=input_matrix;
92 
93  const Epetra_MpiComm *InComm = dynamic_cast<const Epetra_MpiComm*>(& input_matrix_->Comm());
94  const Epetra_Map *InRowMap= dynamic_cast<const Epetra_Map* >(& input_matrix_->RowMap());
95  const Epetra_Map *InColMap= dynamic_cast<const Epetra_Map* >(& input_matrix_->ColMap());
96 
97  if(!InComm || !InRowMap || !InColMap) return (-1);
98 
99  int Nrows=InRowMap->NumGlobalElements();
100  int Ncols=InColMap->NumGlobalElements();
101 
102  if(!subcomm_is_set){
103  /* Build the Split Communicators, If Needed */
104  int color;
105  if(InRowMap->NumMyElements()) color=1;
106  else color=MPI_UNDEFINED;
107  MPI_Comm_split(InComm->Comm(),color,InComm->MyPID(),&MPI_SubComm_);
108  }
109  else{
110  /* Sanity check user-provided subcomm - drop an error if the MPISubComm
111  does not include a processor with data. */
112  if (input_matrix->NumMyRows() && MPI_SubComm_ == MPI_COMM_NULL)
113  return(-2);
114  }
115 
116  /* Mark active processors */
117  if(MPI_SubComm_ == MPI_COMM_NULL) proc_is_active=false;
118  else proc_is_active=true;
119 
120 
121  if(proc_is_active){
122  RestrictedComm_=new Epetra_MpiComm(MPI_SubComm_);
123 
124  /* Build the Restricted Maps */
125  ResRowMap_ = new Epetra_Map(Nrows,InRowMap->NumMyElements(),InRowMap->MyGlobalElements(),
126  InRowMap->IndexBase(),*RestrictedComm_);
127  ResColMap_ = new Epetra_Map(Ncols,InColMap->NumMyElements(),InColMap->MyGlobalElements(),
128  InColMap->IndexBase(),*RestrictedComm_);
129 
130  int *colind,Nr;
131  double *values;
132 
133  /* Allocate the Restricted Matrix */
134  restricted_matrix_= Teuchos::rcp(new Epetra_CrsMatrix(View,*ResRowMap_,*ResColMap_,0));
135  for(int i=0;i<input_matrix_->NumMyRows();i++) {
136  input_matrix_->ExtractMyRowView(i,Nr,values,colind);
137  restricted_matrix_->InsertMyValues(i,Nr,values,colind);
138  }
139  restricted_matrix_->FillComplete();
140  }
141 
142  return 0;
143 }/*end restrict_comm*/
144 
145 
146 
147 }
148 #endif