EpetraExt  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraExt_SolverMap_CrsMatrix.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ***********************************************************************
3 //
4 // EpetraExt: Epetra Extended - Linear Algebra Services Package
5 // Copyright (2011) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 
43 
44 #include <Epetra_CrsGraph.h>
45 #include <Epetra_CrsMatrix.h>
46 #include <Epetra_Map.h>
47 #include <Epetra_Comm.h>
48 
49 #include <vector>
50 
51 namespace EpetraExt {
52 
55 {
56  if( newObj_ && newObj_ != origObj_ ) delete newObj_;
57  if( NewGraph_ ) delete NewGraph_;
58  if( NewColMap_ ) delete NewColMap_;
59 }
60 
64 {
65  origObj_ = &orig;
66 
67  assert( !orig.IndicesAreGlobal() );
68 
69  //test if matrix has missing local columns in its col std::map
70  const Epetra_Map & RowMap = orig.RowMap();
71  const Epetra_Map & DomainMap = orig.DomainMap();
72  const Epetra_Map & ColMap = orig.ColMap();
73  const Epetra_Comm & Comm = RowMap.Comm();
74  int NumMyRows = RowMap.NumMyElements();
75  int NumCols = DomainMap.NumMyElements();
76  int Match = 0;
77  for( int i = 0; i < NumCols; ++i )
78  if( DomainMap.GID(i) != ColMap.GID(i) )
79  {
80  Match = 1;
81  break;
82  }
83 
84  int MatchAll = 0;
85  Comm.SumAll( &Match, &MatchAll, 1 );
86 
87  if( !MatchAll )
88  {
89  newObj_ = origObj_;
90  }
91  else
92  {
93  //create ColMap with all local rows included
94  std::vector<int> Cols(NumCols);
95  //fill Cols list with GIDs of all local columns
96  for( int i = 0; i < NumCols; ++i )
97  Cols[i] = DomainMap.GID(i);
98 
99  //now append to Cols any ghost column entries
100  int NumMyCols = ColMap.NumMyElements();
101  for( int i = 0; i < NumMyCols; ++i )
102  if( !DomainMap.MyGID( ColMap.GID(i) ) ) Cols.push_back( ColMap.GID(i) );
103 
104  int NewNumMyCols = Cols.size();
105  int NewNumGlobalCols;
106  Comm.SumAll( &NewNumMyCols, &NewNumGlobalCols, 1 );
107  //create new column std::map
108  NewColMap_ = new Epetra_Map( NewNumGlobalCols, NewNumMyCols,&Cols[0], DomainMap.IndexBase(), Comm );
109 
110  //New Graph
111  std::vector<int> NumIndicesPerRow( NumMyRows );
112  for( int i = 0; i < NumMyRows; ++i )
113  NumIndicesPerRow[i] = orig.NumMyEntries(i);
114  NewGraph_ = new Epetra_CrsGraph( Copy, RowMap, *NewColMap_, &NumIndicesPerRow[0] );
115 
116  int MaxNumEntries = orig.MaxNumEntries();
117  int NumEntries;
118  std::vector<int> Indices( MaxNumEntries );
119  for( int i = 0; i < NumMyRows; ++i )
120  {
121  int RowGID = RowMap.GID(i);
122  orig.Graph().ExtractGlobalRowCopy( RowGID, MaxNumEntries, NumEntries, &Indices[0] );
123  NewGraph_->InsertGlobalIndices( RowGID, NumEntries, &Indices[0] );
124  }
125  const Epetra_Map & RangeMap = orig.RangeMap();
126  NewGraph_->FillComplete(DomainMap,RangeMap);
127 
128  //intial construction of matrix
129  Epetra_CrsMatrix * NewMatrix = new Epetra_CrsMatrix( View, *NewGraph_ );
130 
131  //insert views of row values
132  int * myIndices;
133  double * myValues;
134  int indicesCnt;
135  int numMyRows = NewMatrix->NumMyRows();
136  for( int i = 0; i < numMyRows; ++i )
137  {
138  orig.ExtractMyRowView( i, indicesCnt, myValues, myIndices );
139  NewGraph_->ExtractMyRowView( i, indicesCnt, myIndices );
140 
141  NewMatrix->InsertMyValues( i, indicesCnt, myValues, myIndices );
142  }
143 
144  NewMatrix->FillComplete(DomainMap,RangeMap);
145 
146  newObj_ = NewMatrix;
147  }
148 
149  return *newObj_;
150 }
151 
152 } // namespace EpetraExt
153 
NewTypeRef operator()(OriginalTypeRef orig)
Constructs fixed view of Epetra_CrsMatrix as necessary.