Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mj_epetra.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 <vector>
52 #include <numeric>
53 #include "Epetra_ConfigDefs.h"
54 #ifdef HAVE_MPI
55 #include <Epetra_MpiComm.h>
56 #else
57 #include <Epetra_SerialComm.h>
58 #endif
59 #include <Epetra_MultiVector.h>
63 #include <Zoltan2_InputTraits.hpp>
64 
65 int main(int narg, char *arg[])
66 {
67  Tpetra::ScopeGuard scope(&narg, &arg);
68  const Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
69 
70  typedef int gid_t;
71 
72  const int N = 100; // num coords
73  const int num_parts = 5;
74  const int dim = 3;
75 
76  int rank = comm->getRank();
77 
78  // params
79  Teuchos::ParameterList params("test params");
80  params.set("algorithm", "multijagged");
81  params.set("num_global_parts", num_parts);
82 
83  // create gids
84  std::vector<gid_t> global_ids(N);
85  std::iota(global_ids.begin(), global_ids.end(), rank * N);
86 
87 #ifdef HAVE_MPI
88  Epetra_MpiComm epetra_comm(MPI_COMM_WORLD);
89 #else
90  Epetra_SerialComm epetra_comm;
91 #endif
92 
93  Epetra_BlockMap map(-1, N, 1, 0, epetra_comm);
94 
95  typedef Epetra_MultiVector mv_t;
96  Teuchos::RCP<mv_t> mv = Teuchos::rcp(new mv_t(map, dim)); // all 0's
97 
98  std::vector<int> stride;
99  std::vector<const double *> weights;
100  Teuchos::RCP<const mv_t> cmv = Teuchos::rcp_const_cast<const mv_t>(mv);
101 
102  typedef Zoltan2::XpetraMultiVectorAdapter<mv_t> inputAdapter_t;
103 
104  Teuchos::RCP<inputAdapter_t> ia =
105  Teuchos::rcp(new inputAdapter_t(cmv, weights, stride));
106 
107  Teuchos::RCP<Zoltan2::PartitioningProblem<inputAdapter_t>> problem =
108  Teuchos::rcp(new Zoltan2::PartitioningProblem<inputAdapter_t>(ia.get(), &params));
109 
110  problem->solve();
111 
113  Teuchos::RCP<quality_t> metricObject = Teuchos::rcp(new quality_t(
114  ia.get(), &params, comm, &problem->getSolution()));
115 
116  int err = 0;
117  if (comm->getRank() == 0) {
118  metricObject->printMetrics(std::cout);
119  double imb = metricObject->getObjectCountImbalance();
120  if (imb <= 1.01)
121  std::cout << "balance satisfied " << imb << std::endl;
122  else {
123  std::cout << "balance failed " << imb << std::endl;
124  err++;
125  }
126  }
127 
128  if(rank == 0 && err == 0) {
129  std::cout << "PASS" << std::endl;
130  }
131 
132  return 0;
133 }
134 
static ArrayRCP< ArrayRCP< zscalar_t > > weights
Zoltan2::EvaluatePartition< matrixAdapter_t > quality_t
int main(int narg, char **arg)
Definition: coloring1.cpp:199
Defines the PartitioningSolution class.
Defines the XpetraMultiVectorAdapter.
Traits for application input objects.
An adapter for Xpetra::MultiVector.
PartitioningProblem sets up partitioning problems for the user.
Defines the PartitioningProblem class.
A class that computes and returns quality metrics.