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  params.set("bisection_num_test_cuts", 7);
107  if (doRemap) params.set("remap_parts", "yes");
108 
109 #ifdef HAVE_ZOLTAN2_MPI
111  MPI_COMM_WORLD);
112 #else
113  Zoltan2::PartitioningProblem<inputAdapter_t> problem(&ia, &params);
114 #endif
115  if (me == 0)
116  cout << "Problem constructed" << endl;
117 
118 
119  problem.solve();
120  if (me == 0)
121  cout << "Problem solved" << endl;
122 
123  if (comm->getRank() == 0)
124  problem.printMetrics(cout);
125 }
126 
127 void serialTest(int numParts, bool doRemap)
128 {
129  int numCoords = 1000;
130  numParts *= 8;
131 
132  cout << "Serial partitioning: " << numParts << " parts." << endl;
133 
134  zgno_t *ids = new zgno_t [numCoords];
135  if (!ids)
136  throw std::bad_alloc();
137  for (int i=0; i < numCoords; i++)
138  ids[i] = i;
139  ArrayRCP<zgno_t> globalIds(ids, 0, numCoords, true);
140 
141  Array<ArrayRCP<zscalar_t> > randomCoords(3);
142  UserInputForTests::getUIRandomData(555, numCoords, 0, 10,
143  randomCoords.view(0,3));
144 
145  typedef Zoltan2::BasicVectorAdapter<myTypes_t> inputAdapter_t;
146 
147  inputAdapter_t ia(numCoords, ids,
148  randomCoords[0].getRawPtr(), randomCoords[1].getRawPtr(),
149  randomCoords[2].getRawPtr(), 1,1,1);
150 
151  Teuchos::ParameterList params("test params");
152  params.set("debug_level", "basic_status");
153  params.set("num_global_parts", numParts);
154  params.set("algorithm", "rcb");
155  params.set("imbalance_tolerance", 1.1);
156  params.set("bisection_num_test_cuts", 7);
157  if (doRemap) params.set("remap_parts", "yes");
158 
159 #ifdef HAVE_ZOLTAN2_MPI
161  &ia, &params, MPI_COMM_SELF);
162 #else
163  Zoltan2::PartitioningProblem<inputAdapter_t> serialProblem(&ia, &params);
164 #endif
165 
166  serialProblem.solve();
167 
168  serialProblem.printMetrics(cout);
169 }
170 
171 void meshCoordinatesTest(const RCP<const Teuchos::Comm<int> > & comm)
172 {
173  int xdim = 40;
174  int ydim = 60;
175  int zdim = 20;
176  UserInputForTests uinput(xdim, ydim, zdim, string("Laplace3D"), comm, true, true);
177 
178  RCP<tMVector_t> coords = uinput.getUICoordinates();
179 
180  size_t localCount = coords->getLocalLength();
181 
182  zscalar_t *x=NULL, *y=NULL, *z=NULL;
183  x = coords->getDataNonConst(0).getRawPtr();
184  y = coords->getDataNonConst(1).getRawPtr();
185  z = coords->getDataNonConst(2).getRawPtr();
186 
187  const zgno_t *globalIds = coords->getMap()->getNodeElementList().getRawPtr();
188  typedef Zoltan2::BasicVectorAdapter<tMVector_t> inputAdapter_t;
189 
190  inputAdapter_t ia(localCount, globalIds, x, y, z, 1, 1, 1);
191 
192  Teuchos::ParameterList params("test params");
193  params.set("bisection_num_test_cuts", 7);
194  params.set("rectilinear", "yes");
195 
196 #ifdef HAVE_ZOLTAN2_MPI
197  Zoltan2::PartitioningProblem<inputAdapter_t> problem(&ia, &params, MPI_COMM_WORLD);
198 #else
199  Zoltan2::PartitioningProblem<inputAdapter_t> problem(&ia, &params);
200 #endif
201 
202  problem.solve();
203 
204  if (comm->getRank() == 0)
205  problem.printMetrics(cout);
206 }
207 
208 int main(int argc, char *argv[])
209 {
210  Teuchos::GlobalMPISession session(&argc, &argv);
211  RCP<const Teuchos::Comm<int> > tcomm = Teuchos::DefaultComm<int>::getComm();
212  int rank = tcomm->getRank();
213  int nParts = tcomm->getSize();
214  bool doRemap = false;
215  string filename = "USAir97";
216 
217  // Read run-time options.
218  Teuchos::CommandLineProcessor cmdp (false, false);
219  cmdp.setOption("file", &filename, "Name of the Matrix Market file to read");
220  cmdp.setOption("nparts", &nParts, "Number of parts.");
221  cmdp.setOption("remap", "no-remap", &doRemap, "Remap part numbers.");
222  cmdp.parse(argc, argv);
223 
224  meshCoordinatesTest(tcomm);
225 
226  testFromDataFile(tcomm, nParts, filename, doRemap);
227 
228  if (rank == 0)
229  serialTest(nParts, doRemap);
230 
231  if (rank == 0)
232  std::cout << "PASS" << std::endl;
233 }
void meshCoordinatesTest(const RCP< const Teuchos::Comm< int > > &comm)
Definition: rcbTest.cpp:171
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:208
void printMetrics(std::ostream &os) const
Print the array of metrics.
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:127
Tpetra::MultiVector< zscalar_t, zlno_t, zgno_t, znode_t > tMVector_t
Definition: rcbTest.cpp:62
Tpetra::MultiVector< double, int, int > tMVector_t
#define nParts
Defines the PartitioningProblem class.
RCP< tMVector_t > getUICoordinates()
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(".")