Zoltan2  Version of the Day
XpetraCrsMatrixInput.cpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 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 Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 //
46 // Basic testing of Zoltan2::XpetraCrsMatrixInput
47 
53 #include <string>
54 
56 #include <Zoltan2_InputTraits.hpp>
57 #include <Zoltan2_TestHelpers.hpp>
58 
59 #include <Teuchos_GlobalMPISession.hpp>
60 #include <Teuchos_DefaultComm.hpp>
61 #include <Teuchos_RCP.hpp>
62 #include <Teuchos_Comm.hpp>
63 #include <Teuchos_CommHelpers.hpp>
64 
65 using namespace std;
66 using Teuchos::RCP;
67 using Teuchos::rcp;
68 using Teuchos::rcp_const_cast;
69 using Teuchos::Comm;
70 using Teuchos::DefaultComm;
71 
73 typedef Tpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t> tmatrix_t;
74 typedef Xpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t> xmatrix_t;
75 typedef Epetra_CrsMatrix ematrix_t;
76 
77 void printMatrix(RCP<const Comm<int> > &comm, lno_t nrows,
78  const gno_t *rowIds, const lno_t *offsets, const gno_t *colIds)
79 {
80  int rank = comm->getRank();
81  int nprocs = comm->getSize();
82  comm->barrier();
83  for (int p=0; p < nprocs; p++){
84  if (p == rank){
85  std::cout << rank << ":" << std::endl;
86  for (lno_t i=0; i < nrows; i++){
87  std::cout << " row " << rowIds[i] << ": ";
88  for (lno_t j=offsets[i]; j < offsets[i+1]; j++){
89  std::cout << colIds[j] << " ";
90  }
91  std::cout << std::endl;
92  }
93  std::cout.flush();
94  }
95  comm->barrier();
96  }
97  comm->barrier();
98 }
99 
100 template <typename User>
103 {
104  RCP<const Comm<int> > comm = M.getComm();
105  int fail = 0, gfail=0;
106 
107  if (!fail && ia.getLocalNumRows() != M.getNodeNumRows())
108  fail = 4;
109 
110  if (M.getNodeNumRows()){
111  if (!fail && ia.getLocalNumColumns() != M.getNodeNumCols())
112  fail = 6;
113  }
114 
115  gfail = globalFail(comm, fail);
116 
117  const gno_t *rowIds=NULL, *colIds=NULL;
118  const lno_t *offsets=NULL;
119  size_t nrows=0;
120 
121  if (!gfail){
122 
123  nrows = ia.getRowListView(rowIds, offsets, colIds);
124 
125  if (nrows != M.getNodeNumRows())
126  fail = 8;
127 
128  gfail = globalFail(comm, fail);
129 
130  if (gfail == 0){
131  printMatrix(comm, nrows, rowIds, offsets, colIds);
132  }
133  else{
134  if (!fail) fail = 10;
135  }
136  }
137  return fail;
138 }
139 
140 int main(int argc, char *argv[])
141 {
142  Teuchos::GlobalMPISession session(&argc, &argv);
143  RCP<const Comm<int> > comm = DefaultComm<int>::getComm();
144  int rank = comm->getRank();
145  int fail = 0, gfail=0;
146 
147  // Create object that can give us test Tpetra, Xpetra
148  // and Epetra matrices for testing.
149 
150  RCP<uinput_t> uinput;
151 
152  try{
153  uinput =
154  rcp(new uinput_t(
155  testDataFilePath,std::string("simple"), comm, true));
156  }
157  catch(std::exception &e){
158  TEST_FAIL_AND_EXIT(*comm, 0, string("input ")+e.what(), 1);
159  }
160 
161  RCP<tmatrix_t> tM; // original matrix (for checking)
162  RCP<tmatrix_t> newM; // migrated matrix
163 
164  tM = uinput->getTpetraCrsMatrix();
165  size_t nrows = tM->getNodeNumRows();
166  Teuchos::ArrayView<const gno_t> rowGids =
167  tM->getRowMap()->getNodeElementList();
168 
169  // To test migration in the input adapter we need a Solution
170  // object. The Solution needs an IdentifierMap.
171 
172  typedef Zoltan2::IdentifierMap<tmatrix_t> idmap_t;
173 
174  RCP<const Zoltan2::Environment> env = rcp(new Zoltan2::Environment);
175 
176  ArrayRCP<const gno_t> gidArray = arcpFromArrayView(rowGids);
177  RCP<const idmap_t> idMap = rcp(new idmap_t(env, comm, gidArray));
178 
179  int weightDim = 1;
180 
181 
182  zoltan2_partId_t *p = new zoltan2_partId_t [nrows];
183  memset(p, 0, sizeof(zoltan2_partId_t) * nrows);
184  ArrayRCP<zoltan2_partId_t> solnParts(p, 0, nrows, true);
185 
188  soln_t solution(env, comm, idMap, weightDim);
189  solution.setParts(gidArray, solnParts, false);//could use true, but test false
190 
192  // User object is Tpetra::CrsMatrix
193  if (!gfail){
194  RCP<const tmatrix_t> ctM = rcp_const_cast<const tmatrix_t>(tM);
195  RCP<Zoltan2::XpetraCrsMatrixInput<tmatrix_t> > tMInput;
196 
197  try {
198  tMInput =
200  }
201  catch (std::exception &e){
202  TEST_FAIL_AND_EXIT(*comm, 0,
203  string("XpetraCrsMatrixInput ")+e.what(), 1);
204  }
205 
206  if (rank==0)
207  std::cout << "Input adapter for Tpetra::CrsMatrix" << std::endl;
208 
209  fail = verifyInputAdapter<tmatrix_t>(*tMInput, *tM);
210 
211  gfail = globalFail(comm, fail);
212 
213  if (!gfail){
214  tmatrix_t *mMigrate = NULL;
215  try{
216  tMInput->applyPartitioningSolution(*tM, mMigrate, solution);
217  newM = rcp(mMigrate);
218  }
219  catch (std::exception &e){
220  fail = 11;
221  cout << "Error caught: " << e.what() << endl;
222  }
223 
224  gfail = globalFail(comm, fail);
225 
226  if (!gfail){
227  RCP<const tmatrix_t> cnewM = rcp_const_cast<const tmatrix_t>(newM);
228  RCP<Zoltan2::XpetraCrsMatrixInput<tmatrix_t> > newInput;
229  try{
230  newInput = rcp(new Zoltan2::XpetraCrsMatrixInput<tmatrix_t>(cnewM));
231  }
232  catch (std::exception &e){
233  TEST_FAIL_AND_EXIT(*comm, 0,
234  string("XpetraCrsMatrixInput 2 ")+e.what(), 1);
235  }
236 
237  if (rank==0){
238  std::cout <<
239  "Input adapter for Tpetra::CrsMatrix migrated to proc 0" <<
240  std::endl;
241  }
242  fail = verifyInputAdapter<tmatrix_t>(*newInput, *newM);
243  if (fail) fail += 100;
244  gfail = globalFail(comm, fail);
245  }
246  }
247  if (gfail){
248  printFailureCode(comm, fail);
249  }
250  }
251 
253  // User object is Xpetra::CrsMatrix
254  if (!gfail){
255  RCP<xmatrix_t> xM = uinput->getXpetraCrsMatrix();
256  RCP<const xmatrix_t> cxM = rcp_const_cast<const xmatrix_t>(xM);
257  RCP<Zoltan2::XpetraCrsMatrixInput<xmatrix_t> > xMInput;
258 
259  try {
260  xMInput =
262  }
263  catch (std::exception &e){
264  TEST_FAIL_AND_EXIT(*comm, 0,
265  string("XpetraCrsMatrixInput 3 ")+e.what(), 1);
266  }
267 
268  if (rank==0){
269  std::cout << "Input adapter for Xpetra::CrsMatrix" << std::endl;
270  }
271  fail = verifyInputAdapter<xmatrix_t>(*xMInput, *tM);
272 
273  gfail = globalFail(comm, fail);
274 
275  if (!gfail){
276  xmatrix_t *mMigrate =NULL;
277  try{
278  xMInput->applyPartitioningSolution(*xM, mMigrate, solution);
279  }
280  catch (std::exception &e){
281  cout << "Error caught: " << e.what() << endl;
282  fail = 11;
283  }
284 
285  gfail = globalFail(comm, fail);
286 
287  if (!gfail){
288  RCP<const xmatrix_t> cnewM(mMigrate);
289  RCP<Zoltan2::XpetraCrsMatrixInput<xmatrix_t> > newInput;
290  try{
291  newInput =
293  }
294  catch (std::exception &e){
295  TEST_FAIL_AND_EXIT(*comm, 0,
296  string("XpetraCrsMatrixInput 4 ")+e.what(), 1);
297  }
298 
299  if (rank==0){
300  std::cout <<
301  "Input adapter for Xpetra::CrsMatrix migrated to proc 0" <<
302  std::endl;
303  }
304  fail = verifyInputAdapter<xmatrix_t>(*newInput, *newM);
305  if (fail) fail += 100;
306  gfail = globalFail(comm, fail);
307  }
308  }
309  if (gfail){
310  printFailureCode(comm, fail);
311  }
312  }
313 
314 #ifdef HAVE_EPETRA_DATA_TYPES
315  // User object is Epetra_CrsMatrix
317  if (!gfail){
318  RCP<ematrix_t> eM = uinput->getEpetraCrsMatrix();
319  RCP<const ematrix_t> ceM = rcp_const_cast<const ematrix_t>(eM);
320  RCP<Zoltan2::XpetraCrsMatrixInput<ematrix_t> > eMInput;
321 
322  try {
323  eMInput =
325  }
326  catch (std::exception &e){
327  TEST_FAIL_AND_EXIT(*comm, 0,
328  string("XpetraCrsMatrixInput 5 ")+e.what(), 1);
329  }
330 
331  if (rank==0){
332  std::cout << "Input adapter for Epetra_CrsMatrix" << std::endl;
333  }
334  fail = verifyInputAdapter<ematrix_t>(*eMInput, *tM);
335 
336  gfail = globalFail(comm, fail);
337 
338  if (!gfail){
339  ematrix_t *mMigrate =NULL;
340  try{
341  eMInput->applyPartitioningSolution(*eM, mMigrate, solution);
342  }
343  catch (std::exception &e){
344  cout << "Error caught: " << e.what() << endl;
345  fail = 11;
346  }
347 
348  gfail = globalFail(comm, fail);
349 
350  if (!gfail){
351  RCP<const ematrix_t> cnewM(mMigrate, true);
352  RCP<Zoltan2::XpetraCrsMatrixInput<ematrix_t> > newInput;
353  try{
354  newInput =
356  }
357  catch (std::exception &e){
358  TEST_FAIL_AND_EXIT(*comm, 0,
359  string("XpetraCrsMatrixInput 6 ")+e.what(), 1);
360  }
361 
362  if (rank==0){
363  std::cout <<
364  "Input adapter for Epetra_CrsMatrix migrated to proc 0" <<
365  std::endl;
366  }
367  fail = verifyInputAdapter<ematrix_t>(*newInput, *newM);
368  if (fail) fail += 100;
369  gfail = globalFail(comm, fail);
370  }
371  }
372  if (gfail){
373  printFailureCode(comm, fail);
374  }
375  }
376 #endif
377 
379  // DONE
380 
381  if (rank==0)
382  std::cout << "PASS" << std::endl;
383 }
An IdentifierMap manages a global space of object identifiers.
Defines the XpetraCrsMatrixInput adapter class.
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
UserInputForTests uinput_t
int globalFail(const RCP< const Comm< int > > &comm, int fail)
size_t getLocalNumRows() const
Returns the number rows on this process.
A class that generates typical user input for testing.
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
int main(int argc, char *argv[])
common code used by tests
size_t getRowListView(const gid_t *&rowIds, const lno_t *&offsets, const gid_t *&colIds) const
Sets pointers to this process&#39; matrix entries. If this optional call is defined in the adapter...
void printMatrix(RCP< const Comm< int > > &comm, lno_t nrows, const gno_t *rowIds, const lno_t *offsets, const gno_t *colIds)
int zoltan2_partId_t
a data type for part IDs
A PartitioningSolution is a solution to a partitioning problem.
Traits for application input objects.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
void printFailureCode(const RCP< const Comm< int > > &comm, int fail)
size_t getLocalNumColumns() const
Returns the number columns on this process.
Tpetra::CrsMatrix< scalar_t, lno_t, gno_t, node_t > tmatrix_t
Epetra_CrsMatrix ematrix_t
Xpetra::CrsMatrix< scalar_t, lno_t, gno_t, node_t > xmatrix_t
int verifyInputAdapter(Zoltan2::XpetraCrsMatrixInput< User > &ia, tmatrix_t &M)
std::string testDataFilePath(".")