Zoltan2
rcbTest.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 
51 #include <Zoltan2_config.h>
52 #include <Zoltan2_TestHelpers.hpp>
57 
58 using namespace std;
59 using Teuchos::RCP;
60 using Teuchos::rcp;
61 
62 typedef Tpetra::MultiVector<zscalar_t, zlno_t, zgno_t, znode_t> tMVector_t;
64 // Need to use zgno_t as zzgid_t in basic types, since zzgid_t=zgno_t in MultiVector
65 
66 
75  const RCP<const Teuchos::Comm<int> > & comm,
76  int nParts,
77  string &filename,
78  bool doRemap
79 )
80 {
81  int me = comm->getRank();
82  if (me == 0)
83  cout << "Parallel partitioning of " << filename << ".mtx: "
84  << nParts << " parts." << endl;
85 
86  std::string fname(filename);
87  UserInputForTests uinput(testDataFilePath, fname, comm, true);
88 
89  RCP<tMVector_t> coords = uinput.getUICoordinates();
90  if (me == 0)
91  cout << "Multivector length = " << coords->getGlobalLength()
92  << " Num vectors = " << coords->getNumVectors() << endl;
93 
94  RCP<const tMVector_t> coordsConst = rcp_const_cast<const tMVector_t>(coords);
95 
96  typedef Zoltan2::XpetraMultiVectorAdapter<tMVector_t> inputAdapter_t;
97  inputAdapter_t ia(coordsConst);
98  if (me == 0)
99  cout << "Adapter constructed" << endl;
100 
101  Teuchos::ParameterList params("test params");
102  params.set("debug_level", "basic_status");
103  params.set("num_global_parts", nParts);
104  params.set("algorithm", "rcb");
105  params.set("imbalance_tolerance", 1.1);
106  if (doRemap) params.set("remap_parts", true); // bool parameter
107 
108 #ifdef HAVE_ZOLTAN2_MPI
110  MPI_COMM_WORLD);
111 #else
112  Zoltan2::PartitioningProblem<inputAdapter_t> problem(&ia, &params);
113 #endif
114  if (me == 0)
115  cout << "Problem constructed" << endl;
116 
117 
118  problem.solve();
119  if (me == 0)
120  cout << "Problem solved" << endl;
121 }
122 
123 void serialTest(int numParts, bool doRemap)
124 {
125  int numCoords = 1000;
126  numParts *= 8;
127 
128  cout << "Serial partitioning: " << numParts << " parts." << endl;
129 
130  zgno_t *ids = new zgno_t [numCoords];
131  if (!ids)
132  throw std::bad_alloc();
133  for (int i=0; i < numCoords; i++)
134  ids[i] = i;
135  ArrayRCP<zgno_t> globalIds(ids, 0, numCoords, true);
136 
137  Array<ArrayRCP<zscalar_t> > randomCoords(3);
138  UserInputForTests::getUIRandomData(555, numCoords, 0, 10,
139  randomCoords.view(0,3));
140 
141  typedef Zoltan2::BasicVectorAdapter<myTypes_t> inputAdapter_t;
142 
143  inputAdapter_t ia(numCoords, ids,
144  randomCoords[0].getRawPtr(), randomCoords[1].getRawPtr(),
145  randomCoords[2].getRawPtr(), 1,1,1);
146 
147  Teuchos::ParameterList params("test params");
148  params.set("debug_level", "basic_status");
149  params.set("num_global_parts", numParts);
150  params.set("algorithm", "rcb");
151  params.set("imbalance_tolerance", 1.1);
152  if (doRemap) params.set("remap_parts", true); // bool parameter
153 
154 #ifdef HAVE_ZOLTAN2_MPI
156  &ia, &params, MPI_COMM_SELF);
157 #else
158  Zoltan2::PartitioningProblem<inputAdapter_t> serialProblem(&ia, &params);
159 #endif
160 
161  serialProblem.solve();
162 }
163 
164 void meshCoordinatesTest(const RCP<const Teuchos::Comm<int> > & comm)
165 {
166  int xdim = 40;
167  int ydim = 60;
168  int zdim = 20;
169  UserInputForTests uinput(xdim, ydim, zdim, string("Laplace3D"), comm, true, true);
170 
171  RCP<tMVector_t> coords = uinput.getUICoordinates();
172 
173  size_t localCount = coords->getLocalLength();
174 
175  zscalar_t *x=NULL, *y=NULL, *z=NULL;
176  x = coords->getDataNonConst(0).getRawPtr();
177  y = coords->getDataNonConst(1).getRawPtr();
178  z = coords->getDataNonConst(2).getRawPtr();
179 
180  const zgno_t *globalIds = coords->getMap()->getNodeElementList().getRawPtr();
181  typedef Zoltan2::BasicVectorAdapter<tMVector_t> inputAdapter_t;
182 
183  inputAdapter_t ia(localCount, globalIds, x, y, z, 1, 1, 1);
184 
185  Teuchos::ParameterList params("test params");
186  params.set("rectilinear", true); // bool parameter
187 
188 #ifdef HAVE_ZOLTAN2_MPI
189  Zoltan2::PartitioningProblem<inputAdapter_t> problem(&ia, &params, MPI_COMM_WORLD);
190 #else
191  Zoltan2::PartitioningProblem<inputAdapter_t> problem(&ia, &params);
192 #endif
193 
194  problem.solve();
195 }
196 
197 int main(int argc, char *argv[])
198 {
199  Teuchos::GlobalMPISession session(&argc, &argv);
200  RCP<const Teuchos::Comm<int> > tcomm = Teuchos::DefaultComm<int>::getComm();
201  int rank = tcomm->getRank();
202  int nParts = tcomm->getSize();
203  bool doRemap = false;
204  string filename = "USAir97";
205 
206  // Read run-time options.
207  Teuchos::CommandLineProcessor cmdp (false, false);
208  cmdp.setOption("file", &filename, "Name of the Matrix Market file to read");
209  cmdp.setOption("nparts", &nParts, "Number of parts.");
210  cmdp.setOption("remap", "no-remap", &doRemap, "Remap part numbers.");
211  cmdp.parse(argc, argv);
212 
213  meshCoordinatesTest(tcomm);
214 
215  testFromDataFile(tcomm, nParts, filename, doRemap);
216 
217  if (rank == 0)
218  serialTest(nParts, doRemap);
219 
220  if (rank == 0)
221  std::cout << "PASS" << std::endl;
222 }
void meshCoordinatesTest(const RCP< const Teuchos::Comm< int > > &comm)
Definition: rcbTest.cpp:164
double zscalar_t
A simple class that can be the User template argument for an InputAdapter.
static void getUIRandomData(unsigned int seed, zlno_t length, zscalar_t min, zscalar_t max, ArrayView< ArrayRCP< zscalar_t > > data)
Generate lists of random scalars.
Defines the PartitioningSolution class.
common code used by tests
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > myTypes_t
Definition: rcbTest.cpp:63
Defines the XpetraMultiVectorAdapter.
int main(int argc, char *argv[])
Definition: rcbTest.cpp:197
BasicVectorAdapter represents a vector (plus optional weights) supplied by the user as pointers to st...
An adapter for Xpetra::MultiVector.
int zgno_t
PartitioningProblem sets up partitioning problems for the user.
void serialTest(int numParts, bool doRemap)
Definition: rcbTest.cpp:123
Tpetra::MultiVector< zscalar_t, zlno_t, zgno_t, znode_t > tMVector_t
Definition: rcbTest.cpp:62
Tpetra::MultiVector< double, int, int > tMVector_t
RCP< tMVector_t > getUICoordinates()
#define nParts
Defines the PartitioningProblem class.
Defines the BasicVectorAdapter class.
void testFromDataFile(const RCP< const Teuchos::Comm< int > > &comm, int nParts, string &filename, bool doRemap)
Definition: rcbTest.cpp:74
void solve(bool updateInputData=true)
Direct the problem to create a solution.
std::string testDataFilePath(".")