Debian Codegraph @

packages/isorropia/src/epetra/Isorropia_EpetraProber.cpp

raw
1 //@HEADER
2 //************************************************************************
3 //
4 // Isorropia: Partitioning and Load Balancing Package
5 // Copyright (2006) Sandia Corporation
6 //
7 //Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 //license for use of this work by or on behalf of the U.S. Government.
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 //************************************************************************
38 //@HEADER
39 #include <Isorropia_EpetraProber.hpp>
40 #ifdef HAVE_EPETRA
41 #include <Epetra_MultiVector.h>
42 #include <Epetra_Operator.h>
43 #include <Epetra_Map.h>
44 #include <Epetra_CrsGraph.h>
45 #include <Epetra_CrsMatrix.h>
46 #include <Epetra_Comm.h>
47
48 namespace Isorropia{
49 namespace Epetra{
50
51
52 Prober::Prober():input_graph_(0),colorer_(0),has_colored(false){}
53
54
55 ////////////////////////////////////////////////////////////////////////////////
56 ////////////////////////////////////////////////////////////////////////////////
57 Prober::Prober(Teuchos::RCP<const Epetra_CrsGraph> input_graph,
58 const Teuchos::ParameterList& paramlist,
59 bool compute_now)
60 :input_graph_(input_graph),colorer_(0),has_colored(false)
61 {
62 setList(paramlist);
63 if(compute_now) color();
64 }
65 ////////////////////////////////////////////////////////////////////////////////
66
67 ////////////////////////////////////////////////////////////////////////////////
68 ////////////////////////////////////////////////////////////////////////////////
69 Prober::Prober(const Epetra_CrsGraph * input_graph,
70 const Teuchos::ParameterList& paramlist, bool compute_now)
71 :input_graph_(Teuchos::rcp<const Epetra_CrsGraph>(input_graph,false)),
72 colorer_(0),has_colored(false)
73 {
74 setList(paramlist);
75 if(compute_now) color();
76 }
77 ////////////////////////////////////////////////////////////////////////////////
78
79 ////////////////////////////////////////////////////////////////////////////////
80 ////////////////////////////////////////////////////////////////////////////////
81 Prober::Prober(Teuchos::RCP<const Epetra_CrsMatrix> input_matrix,
82 const Teuchos::ParameterList & paramlist, bool compute_now)
83 :input_graph_(Teuchos::rcp<const Epetra_CrsGraph>(&input_matrix->Graph(),false)),
84 colorer_(0),List_(paramlist),has_colored(false)
85 {
86 setList(paramlist);
87 if(compute_now) color();
88 }
89 ////////////////////////////////////////////////////////////////////////////////
90
91 ////////////////////////////////////////////////////////////////////////////////
92 ////////////////////////////////////////////////////////////////////////////////
93 Prober::Prober(const Epetra_CrsMatrix * input_matrix,
94 const Teuchos::ParameterList & paramlist, bool compute_now)
95 :input_graph_(Teuchos::rcp<const Epetra_CrsGraph>(&input_matrix->Graph(),false)),
96 colorer_(0),List_(paramlist),has_colored(false)
97 {
98 setList(paramlist);
99 if(compute_now) color();
100 }
101 ////////////////////////////////////////////////////////////////////////////////
102
103
104
105 ////////////////////////////////////////////////////////////////////////////////
106 ////////////////////////////////////////////////////////////////////////////////
107 void Prober::setList(const Teuchos::ParameterList& paramlist)
108 {
109 List_=paramlist;
110
111 /* Ensure Distance 2 */
112 Teuchos::ParameterList dummy;
113 std::string two,s_dummy;
114 Teuchos::ParameterList zoltan=List_.get("ZOLTAN",dummy);
115 // two=zoltan.get("DISTANCE",s_dummy);
116 // if(strcmp(two.c_str(),"2")){
117 // if(!input_graph_->Comm().MyPID())
118 // std::cerr<<"WARNING: Prober requires distance 2 coloring. Resetting coloring type for you."<<std::endl;
119 // zoltan.set("DISTANCE","2");
120 // List_.set("ZOLTAN",zoltan);
121 // }
122
123 }
124 ////////////////////////////////////////////////////////////////////////////////
125
126 ////////////////////////////////////////////////////////////////////////////////
127 ////////////////////////////////////////////////////////////////////////////////
128 void Prober::color()
129 {
130 if(!has_colored)
131 {
132 delete colorer_;
133 colorer_=new Isorropia::Epetra::Colorer(input_graph_,List_,false);
134 }
135
136 colorer_->color(true);
137 has_colored=true;
138 }
139 ////////////////////////////////////////////////////////////////////////////////
140
141 int Prober::getNumOrthogonalVectors()
142 {
143 if (has_colored)
144 return colorer_->numColors();
145 else
146 return -1;
147 }
148
149 ////////////////////////////////////////////////////////////////////////////////
150 ////////////////////////////////////////////////////////////////////////////////
151 int Prober::probe(const Epetra_Operator & op, Epetra_CrsMatrix & out_matrix)
152 {
153 /* Sanity Checks */
154 if(input_graph_.is_null()) return -1;
155 if(input_graph_->DataPtr() != out_matrix.Graph().DataPtr()) return -1;
156
157 if(!has_colored) color();
158 int Ncolors=colorer_->numColors();
159 int N=out_matrix.NumMyRows();
160
161 if(Ncolors==0) return -1;
162
163 /* Allocs */
164 Epetra_MultiVector temp1(out_matrix.DomainMap(),Ncolors,true);
165 Epetra_MultiVector temp2(out_matrix.RangeMap(),Ncolors,false);
166
167 /* Grab the color data */
168 Epetra_MapColoring * col;
169 Teuchos::RCP<Epetra_MapColoring> col_=colorer_->generateRowMapColoring();
170 if(out_matrix.Comm().NumProc() == 1 ||
171 input_graph_->Importer() == 0 )
172 col=&(*col_);
173 else {
174 col= new Epetra_MapColoring(input_graph_->ColMap());
175 col->Import(*col_,*input_graph_->Importer(),Insert);
176 }
177
178 /* Probing */
179 for(int i=0;i<N;i++)
180 temp1[(*col)[i]-1][i]=1.0;
181 op.Apply(temp1,temp2);
182
183 /* Matrix Fill*/
184 out_matrix.FillComplete();
185 int entries, *indices;
186 double *values;
187 for(int i=0;i<N;i++){
188 out_matrix.ExtractMyRowView(i,entries,values,indices);
189 for(int j=0;j<entries;j++)
190 values[j]=temp2[(*col)[indices[j]]-1][i];
191 }
192
193 /* Cleanup */
194 if(out_matrix.Comm().NumProc() != 1 && input_graph_->Importer() != 0)
195 delete col;
196
197 return 0;
198 }
199 ////////////////////////////////////////////////////////////////////////////////
200
201 ////////////////////////////////////////////////////////////////////////////////
202 ////////////////////////////////////////////////////////////////////////////////
203 Teuchos::RCP<Epetra_CrsMatrix> Prober::probe(const Epetra_Operator & op)
204 {
205 Teuchos::RCP<Epetra_CrsMatrix> out_matrix = Teuchos::rcp(new Epetra_CrsMatrix(Copy,*input_graph_));
206 Teuchos::RCP<Epetra_CrsMatrix> null;
207 int rv=probe(op,*out_matrix);
208 if(rv==0 || colorer_->numColors() == 0 ) return out_matrix;
209 else return null;
210 }
211 ////////////////////////////////////////////////////////////////////////////////
212
213
214 }//epetra
215 }//isorropia
216
217
218 #endif