EpetraExt Development
Loading...
Searching...
No Matches
EpetraExt_Transpose_CrsGraph.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_Export.h>
45#include <Epetra_CrsGraph.h>
46#include <Epetra_Map.h>
47
48#include <vector>
49
50namespace EpetraExt {
51
54{
55 delete newObj_;
56}
57
60operator()( OriginalTypeRef orig )
61{
62 origObj_ = &orig;
63
64 int nRows = orig.NumMyRows();
65 int nCols = orig.NumMyCols();
66
67 const Epetra_BlockMap & RowMap = orig.RowMap();
68
69 int numIndices;
70 int * Indices;
71
72 Epetra_CrsGraph * TransposeGraph = 0;
73
74 if( !ignoreNonLocalCols_ && orig.DistributedGlobal() )
75 {
76 std::vector<int> TransNumNZ( nCols, 0 );
77 for( int i = 0; i < nRows; ++i )
78 {
79 orig.ExtractMyRowView( i, numIndices, Indices );
80 for( int j = 0; j < numIndices; ++j ) ++TransNumNZ[ Indices[j] ];
81 }
82
83 std::vector< std::vector<int> > TransIndices( nCols );
84 for( int i = 0; i < nCols; ++i )
85 if( TransNumNZ[i] )
86 {
87 TransIndices[i].resize( TransNumNZ[i] );
88 TransNumNZ[i] = 0;
89 }
90
91 for( int i = 0; i < nRows; ++i )
92 {
93 orig.ExtractMyRowView( i, numIndices, Indices );
94 for( int j = 0; j < numIndices; ++j )
95 TransIndices[ Indices[j] ][ TransNumNZ[ Indices[j] ]++ ] = i;
96 }
97
98 Epetra_CrsGraph SharedTransGraph( View, orig.ImportMap(), RowMap, &TransNumNZ[0] );
99 for( int i = 0; i < nCols; ++i )
100 if( TransNumNZ[i] ) SharedTransGraph.InsertMyIndices( i, TransNumNZ[i], &TransIndices[i][0] );
101 SharedTransGraph.FillComplete();
102
103 TransposeGraph = new Epetra_CrsGraph( Copy, RowMap, 0 );
104 Epetra_Export Exporter( orig.ImportMap(), RowMap );
105 TransposeGraph->Export( SharedTransGraph, Exporter, Add );
106 TransposeGraph->FillComplete();
107 }
108 else
109 {
110 std::vector<int> TransNumNZ( nRows, 0 );
111 for( int i = 0; i < nRows; ++i )
112 {
113 orig.ExtractMyRowView( i, numIndices, Indices );
114 for( int j = 0; j < numIndices; ++j )
115 if( Indices[j] < nRows ) ++TransNumNZ[ Indices[j] ];
116 }
117
118 std::vector< std::vector<int> > TransIndices( nRows );
119 for( int i = 0; i < nRows; ++i )
120 if( TransNumNZ[i] )
121 {
122 TransIndices[i].resize( TransNumNZ[i] );
123 TransNumNZ[i] = 0;
124 }
125
126 for( int i = 0; i < nRows; ++i )
127 {
128 orig.ExtractMyRowView( i, numIndices, Indices );
129 for( int j = 0; j < numIndices; ++j )
130 if( Indices[j] < nRows ) TransIndices[ Indices[j] ][ TransNumNZ[ Indices[j] ]++ ] = i;
131 }
132
133 TransposeGraph = new Epetra_CrsGraph( Copy, RowMap, RowMap, &TransNumNZ[0] );
134
135 for( int i = 0; i < nRows; ++i )
136 if( TransNumNZ[i] ) TransposeGraph->InsertMyIndices( i, TransNumNZ[i], &TransIndices[i][0] );
137
138 TransposeGraph->FillComplete();
139 }
140
141 newObj_ = TransposeGraph;
142
143 return *TransposeGraph;
144}
145
146} // namespace EpetraExt
147
Add
View
Copy
NewTypeRef operator()(OriginalTypeRef orig)
Transpose Transform Operator.
int InsertMyIndices(int LocalRow, int NumIndices, int *Indices)
int Export(const Epetra_SrcDistObject &A, const Epetra_Import &Importer, Epetra_CombineMode CombineMode, const Epetra_OffsetIndex *Indexor=0)
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.