Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_AlgSarma.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 #pragma once
15 
16 #include <Teuchos_ParameterEntryValidator.hpp>
17 #include <Zoltan2_Algorithm.hpp>
20 #include <Zoltan2_Util.hpp>
21 #include <Zoltan2_TPLTraits.hpp>
22 
23 #ifndef HAVE_ZOLTAN2_SARMA
24 
25 namespace Zoltan2 {
26  template<typename Adapter>
27  class AlgSarma : public Algorithm<Adapter> {
28  public:
30 
31  AlgSarma(const RCP<const Environment> &/* env */,
32  const RCP<const Comm<int> > &/* problemComm */,
33  const RCP<const base_adapter_t> &/* adapter */) {
34  throw std::runtime_error("SARMA requested but not enabled into Zoltan2\n"
35  "Please set CMake flag TPL_ENABLE_SARMA:BOOL=ON");
36  }
37  };
38 }
39 
40 #else
41 
42 #include <map>
43 #include <utility>
44 #include <vector>
45 #include <iostream>
46 #include <fstream>
47 #include <sarma/sarma.hpp>
48 
49 namespace Zoltan2 {
50  template<typename Adapter>
51  class AlgSarma : public Algorithm<Adapter> {
52  public:
53  using base_adapter_t = typename Adapter::base_adapter_t;
54  using lno_t = typename Adapter::lno_t;
55  using gno_t = typename Adapter::gno_t;
56  using offset_t = typename Adapter::offset_t;
57  using scalar_t = typename Adapter::scalar_t;
58  using part_t = typename Adapter::part_t;
59  using user_t = typename Adapter::user_t;
60  using userCoord_t = typename Adapter::userCoord_t;
61 
62  private:
63  const offset_t *offsets;
64  const gno_t *colids;
65  const scalar_t *vals;
66  size_t offsize;
67  size_t nnz;
68 
69  public:
70  AlgSarma(const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
71  const RCP<const IdentifierAdapter <user_t> > &adapter)
72  : env(env), comm(comm), adapter(adapter),
73  algs(sarma::get_algorithm_map<Ordinal, Value>()), orders(sarma::get_order_map()) {
74  throw std::runtime_error("Cannot build SARMA from IdentifierAdapter");
75  }
76 
77  AlgSarma(const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
78  const RCP<const VectorAdapter <user_t> > &adapter)
79  : env(env), comm(comm), adapter(adapter),
80  algs(sarma::get_algorithm_map<Ordinal, Value>()), orders(sarma::get_order_map()) {
81  throw std::runtime_error("Cannot build SARMA from VectorAdapter");
82 
83  }
84 
85  AlgSarma(const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
86  const RCP<const GraphAdapter <user_t, userCoord_t> > &adapter)
87  : env(env), comm(comm), adapter(adapter),
88  algs(sarma::get_algorithm_map<Ordinal, Value>()), orders(sarma::get_order_map()) {
89  throw std::runtime_error("Cannot build SARMA from GraphAdapter");
90  }
91 
92  AlgSarma(const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
93  const RCP<const MeshAdapter <user_t> > &adapter)
94  : env(env), comm(comm), adapter(adapter),
95  algs(sarma::get_algorithm_map<Ordinal, Value>()), orders(sarma::get_order_map()) {
96 
97  throw std::runtime_error("Cannot build SARMA from MeshAdapter");
98  }
99 
100  AlgSarma(const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
101  const RCP<const MatrixAdapter <user_t, userCoord_t> > &adapter)
102  : env(env), comm(comm), adapter(adapter),
103  offsize(1 + adapter->getLocalNumRows()), nnz(adapter->getLocalNumEntries()),
104  algs(sarma::get_algorithm_map<Ordinal, Value>()), orders(sarma::get_order_map()) {
105  getParams();
106  if (adapter->CRSViewAvailable()) {
107  offsets = new offset_t[offsize]();
108  colids = new gno_t[nnz]();
109  vals = new scalar_t[nnz]();
110  adapter->getCRSView(offsets, colids, vals);
111  } else
112  throw std::runtime_error("NO CRS Available");
113  }
114 
115  static void getValidParameters(ParameterList &pl) {
116  RCP<const Teuchos::ParameterEntryValidator> validator;
117  pl.set("sarma_parameters", ParameterList(), "Sarma options", validator);
118  }
119 
120  void partition(const RCP <PartitioningSolution<Adapter>> &solution);
121  private:
122  const RCP<const Environment> env;
123  const RCP<const Comm<int> > comm;
124  const RCP<const base_adapter_t> adapter;
125  RCP<const CoordinateModel <base_adapter_t> > model;
126 
127  struct Parameters {
128  std::string alg = "pal";
129  sarma::Order order_type = sarma::Order::NAT;
130  std::string order_str = "nat";
131  Ordinal row_parts = 8, col_parts = 0;
132  Value max_load = 0;
133  int seed = 2147483647;
134  double sparsify = 1.0;
135  bool triangular = false, use_data = false;
136  };
137 
138  std::map <std::string, std::pair<std::function < std::pair < std::vector < Ordinal>, std::vector<Ordinal>>(
139  const sarma::Matrix <Ordinal, Value> &, const Ordinal, const Ordinal, const Value, const int)>, bool>>
140  algs;
141  std::map <std::string, sarma::Order> orders;
142  Parameters config;
143 
144 // void buildModel(modelFlag_t &flags);
145  void getParams();
146 
147  };
148 
149  template<typename Adapter>
150  void AlgSarma<Adapter>::getParams() {
151  const Teuchos::ParameterList &pl = env->getParameters();
152  const Teuchos::ParameterList &sparams = pl.sublist("sarma_parameters");
153 
154  // Set Params
155  const Teuchos::ParameterEntry *pe = sparams.getEntryPtr("alg");
156  if (pe)
157  config.alg = pe->getValue(&config.alg);
158  pe = sparams.getEntryPtr("order");
159  if (pe) {
160  std::string s;
161  s = pe->getValue(&s);
162  if (orders.find(s) == orders.end()) {
163  throw std::logic_error("Wrong order type.");
164  }
165  config.order_type = orders.at(s);
166  config.order_str = s;
167  }
168  pe = sparams.getEntryPtr("row_parts"); // row_cut
169  if (pe)
170  config.row_parts = pe->getValue(&config.row_parts);
171  pe = sparams.getEntryPtr("col_parts"); //col_cut
172  if (pe)
173  config.col_parts = pe->getValue(&config.col_parts);
174  pe = sparams.getEntryPtr("max_load"); // max_load
175  if (pe)
176  config.max_load = pe->getValue(&config.max_load);
177  pe = sparams.getEntryPtr("sparsify");
178  if (pe)
179  config.sparsify = pe->getValue(&config.sparsify);
180  pe = sparams.getEntryPtr("triangular");
181  if (pe)
182  config.triangular = pe->getValue(&config.triangular);
183  pe = sparams.getEntryPtr("use_data");
184  if (pe)
185  config.use_data = pe->getValue(&config.use_data);
186  pe = sparams.getEntryPtr("seed");
187  if (pe)
188  config.seed = pe->getValue(&config.seed);
189  }
190 
191  template<typename Adapter>
192  void AlgSarma<Adapter>::partition(const RCP <PartitioningSolution<Adapter>> &solution) {
193 
194  std::ofstream null;
195  null.setstate(std::ios_base::badbit);
196  auto M = std::make_shared<sarma::Matrix<Ordinal, Value> >(std::move(std::vector<Ordinal>(offsets, offsets + offsize)),
197  std::move(std::vector<Ordinal>(colids, colids + nnz)), std::move(std::vector<Value>(vals, vals + nnz)),
198  1 + *std::max_element(colids, colids + nnz));
199  auto parts = sarma::Run<Ordinal, Value>(algs.at(config.alg).first, null, M, config.order_type, config.row_parts,
200  config.col_parts, config.max_load, config.triangular, false, config.sparsify,
201  algs.at(config.alg).second, config.use_data, config.seed);
202 
203  unsigned result_size = parts.first.size() + parts.second.size();
204  auto partl = ArrayRCP<int>(new int[result_size], 0, result_size, true);
205  for (size_t i = 0; i < parts.first.size(); ++i) partl[i] = parts.first[i];
206  for (size_t i = parts.first.size(); i < result_size; ++i)
207  partl[i] = parts.second[i - parts.first.size()];
208 
209  solution->setParts(partl);
210  }
211 }
212 
213 #endif
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
virtual void partition(const RCP< PartitioningSolution< Adapter > > &)
Partitioning method.
typename Adapter::base_adapter_t base_adapter_t
map_t::global_ordinal_type gno_t
Definition: mapRemotes.cpp:27
Defines the PartitioningSolution class.
AlgSarma(const RCP< const Environment > &, const RCP< const Comm< int > > &, const RCP< const base_adapter_t > &)
SparseMatrixAdapter_t::part_t part_t
Adapter::scalar_t scalar_t
Algorithm defines the base class for all algorithms.
map_t::local_ordinal_type lno_t
Definition: mapRemotes.cpp:26
Traits class to handle conversions between gno_t/lno_t and TPL data types (e.g., ParMETIS&#39;s idx_t...
Defines the CoordinateModel classes.
A gathering of useful namespace methods.
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition: Metric.cpp:39