Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_Adapter.hpp
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 
50 #ifndef _ZOLTAN2_ADAPTER_HPP_
51 #define _ZOLTAN2_ADAPTER_HPP_
52 
53 #include "Kokkos_StaticCrsGraph.hpp"
54 #include <Kokkos_Core.hpp>
55 #include <Zoltan2_Standards.hpp>
56 #include <Zoltan2_InputTraits.hpp>
58 #include <fstream>
59 
60 namespace Zoltan2 {
61 
62 //template<typename Adapter>
63 //class PartitioningSolution;
64 
75 };
76 
87 public:
88  virtual ~BaseAdapterRoot() = default; // required virtual declaration
89 
95  virtual size_t getLocalNumIDs() const = 0;
96 
102  virtual int getNumWeightsPerID() const { return 0; };
103 };
104 
105 template <typename User>
106  class BaseAdapter : public BaseAdapterRoot {
107 
108 public:
109  using lno_t = typename InputTraits<User>::lno_t;
110  using gno_t = typename InputTraits<User>::gno_t;
115  using host_t = typename Kokkos::HostSpace::memory_space;
116  using device_t = typename node_t::device_type;
117 
118  using ConstIdsDeviceView = Kokkos::View<const gno_t *, device_t>;
119  using ConstIdsHostView = typename ConstIdsDeviceView::HostMirror;
120 
121  using IdsDeviceView = Kokkos::View<gno_t *, device_t>;
122  using IdsHostView = typename IdsDeviceView::HostMirror;
123 
124  using ConstOffsetsDeviceView = Kokkos::View<const offset_t *, device_t>;
125  using ConstOffsetsHostView = typename ConstOffsetsDeviceView::HostMirror;
126 
127  using OffsetsDeviceView = Kokkos::View<offset_t *, device_t>;
128  using OffsetsHostView = typename OffsetsDeviceView::HostMirror;
129 
130  using ConstScalarsDeviceView = Kokkos::View<const scalar_t *, device_t>;
131  using ConstScalarsHostView = typename ConstScalarsDeviceView::HostMirror;
132 
133  using ScalarsDeviceView = Kokkos::View<scalar_t *, device_t>;
134  using ScalarsHostView = typename ScalarsDeviceView::HostMirror;
135 
136  using ConstWeightsDeviceView1D = Kokkos::View<const scalar_t *, device_t>;
137  using ConstWeightsHostView1D = typename ConstWeightsDeviceView1D::HostMirror;
138 
139  using WeightsDeviceView1D = Kokkos::View<scalar_t *, device_t>;
140  using WeightsHostView1D = typename WeightsDeviceView1D::HostMirror;
141 
142  using ConstWeightsDeviceView = Kokkos::View<const scalar_t **, device_t>;
143  using ConstWeightsHostView = typename ConstWeightsDeviceView::HostMirror;
144 
145  using WeightsDeviceView = Kokkos::View<scalar_t **, device_t>;
146  using WeightsHostView = typename WeightsDeviceView::HostMirror;
147 
150  virtual enum BaseAdapterType adapterType() const = 0;
151 
157  virtual void getIDsView(const gno_t *&ids) const {
158  // If adapter does not define getIDsView, getIDsKokkosView is called.
159  // If adapter does not define getIDsKokkosView, getIDsView is called.
160  // Allows forward and backwards compatibility.
161  ConstIdsDeviceView kokkosIds;
162  getIDsKokkosView(kokkosIds);
163  //deep_copy(?)
164  ids = kokkosIds.data();
165  }
166 
172  virtual void getIDsKokkosView(ConstIdsDeviceView &ids) const {
173  // If adapter does not define getIDsView, getIDsKokkosView is called.
174  // If adapter does not define getIDsKokkosView, getIDsView is called.
175  // Allows forward and backwards compatibility.
176  const gno_t * ptr_ids;
177  getIDsView(ptr_ids);
178 
179  auto non_const_ids = IdsDeviceView("ptr_ids", getLocalNumIDs());
180  auto host_ids = Kokkos::create_mirror_view(non_const_ids);
181  for(size_t i = 0; i < this->getLocalNumIDs(); ++i) {
182  host_ids(i) = ptr_ids[i];
183  }
184  Kokkos::deep_copy(non_const_ids, host_ids);
185  ids = non_const_ids;
186  }
187 
192  virtual void getIDsHostView(ConstIdsHostView& hostIds) const {
194  }
195 
200  virtual void getIDsDeviceView(ConstIdsDeviceView &deviceIds) const {
202  }
203 
205  // * \param wgt on return a pointer to the weights for this idx
206  // * \param stride on return, the value such that
207  // * the \t nth weight should be found at <tt> wgt[n*stride] </tt>.
208  // * \param idx the weight index, zero or greater
209  // * This function or getWeightsKokkosView must be implemented in
210  // * derived adapter if getNumWeightsPerID > 0.
211  // * This function should not be called if getNumWeightsPerID is zero.
212  // */
213  virtual void getWeightsView(const scalar_t *&wgt, int &stride,
214  int idx = 0) const {
215  // If adapter does not define getWeightsView, getWeightsKokkosView is called.
216  // If adapter does not define getWeightsKokkosView, getWeightsView is called.
217  // Allows forward and backwards compatibility.
218  Kokkos::View<scalar_t **, device_t> kokkos_wgts_2d;
219  getWeightsKokkosView(kokkos_wgts_2d);
220  Kokkos::View<scalar_t *, device_t> kokkos_wgts;
221  wgt = Kokkos::subview(kokkos_wgts_2d, Kokkos::ALL, idx).data();
222  stride = 1;
223  }
224 
226  // * \param wgt on return a Kokkos view of the weights for this idx
227  // * This function or getWeightsView must be implemented in
228  // * derived adapter if getNumWeightsPerID > 0.
229  // * This function should not be called if getNumWeightsPerID is zero.
230  // */
231  virtual void getWeightsKokkosView(Kokkos::View<scalar_t **,
232  device_t> & wgt) const {
233  // If adapter does not define getWeightsKokkosView, getWeightsView is called.
234  // If adapter does not define getWeightsView, getWeightsKokkosView is called.
235  // Allows forward and backwards compatibility.
236  wgt = Kokkos::View<scalar_t **, device_t>(
237  "wgts", getLocalNumIDs(), getNumWeightsPerID());
238  auto host_wgt = Kokkos::create_mirror_view(wgt);
239  for(int j = 0; j < this->getNumWeightsPerID(); ++j) {
240  const scalar_t * ptr_wgts;
241  int stride;
242  getWeightsView(ptr_wgts, stride, j);
243  size_t i = 0;
244  for(size_t n = 0; n < this->getLocalNumIDs() * stride; n += stride) {
245  host_wgt(i++,j) = ptr_wgts[n];
246  }
247  }
248  Kokkos::deep_copy(wgt, host_wgt);
249  }
250 
255  virtual void getWeightsHostView(WeightsHostView1D& hostWgts, int idx = 0) const {
257  }
258 
262  virtual void getWeightsHostView(WeightsHostView& hostWgts) const {
264  }
265 
270  virtual void getWeightsDeviceView(WeightsDeviceView1D& deviceWgts, int idx = 0) const {
272  }
273 
277  virtual void getWeightsDeviceView(WeightsDeviceView& deviceWgts) const {
279  }
280 
290  virtual void getPartsView(const part_t *&inputPart) const {
291  // Default behavior: return NULL for inputPart array;
292  // assume input part == rank
293  inputPart = NULL;
294  }
295 
296  virtual void getPartsHostView(Kokkos::View<part_t*, host_t> &inputPart) const {
298  }
299 
300  virtual void getPartsDeviceView(Kokkos::View<part_t*, device_t> &inputPart) const {
302  }
303 
321  template <typename Adapter>
322  void applyPartitioningSolution(const User &in, User *&out,
323  const PartitioningSolution<Adapter> &solution) const {
325  }
326 
327 protected:
328 
329  // Write Chaco-formatted graph and assign files echoing adapter input
330  // This routine is serial and may be slow; use it only for debugging
331  // This function does not write edge info to the graph file, as the
332  // BaseAdapter does not know about edge info; it writes
333  // only the Chaco header and vertex weights (if applicable).
334  void generateWeightFileOnly(const char* fileprefix,
335  const Teuchos::Comm<int> &comm) const;
336 
337 };
338 
339 template <typename User>
340 class AdapterWithCoords : public BaseAdapter<User>
341 {
342 public:
345  using host_t = typename Kokkos::HostSpace::memory_space;
346 
347  // Coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
348  using CoordsDeviceView = Kokkos::View<scalar_t **, Kokkos::LayoutLeft, device_t>;
349  using CoordsHostView = typename CoordsDeviceView::HostMirror;
350 
351 public:
352  virtual void getCoordinatesView(const scalar_t *&coords, int &stride,
353  int coordDim) const = 0;
354  virtual void getCoordinatesKokkosView(CoordsDeviceView &elements) const = 0;
355 
356  virtual void getCoordinatesHostView(CoordsHostView &) const {
358  }
359  virtual void getCoordinatesDeviceView(CoordsDeviceView &elements) const {
361  }
362 };
363 
364 // Forward declare
365 template <typename User>
367 
368 template <typename User, typename UserCoord=User>
370 {
371 public:
372  virtual void setCoordinateInput(VectorAdapter<UserCoord> *coordData) = 0;
373  virtual VectorAdapter<UserCoord> *getCoordinateInput() const = 0;
374 };
375 
376 template <typename User>
378  const char *fileprefix,
379  const Teuchos::Comm<int> &comm
380 ) const
381 {
382  int np = comm.getSize();
383  int me = comm.getRank();
384 
385  size_t nLocalIDs = this->getLocalNumIDs();
386 
387  // Write .graph file: header and weights only (no edges)
388  // Adapters with edges have to implement their own generateFiles function
389  // to provide edge info.
390  {
391  // append suffix to filename
392  std::string filenamestr = fileprefix;
393  filenamestr = filenamestr + ".graph";
394  const char *filename = filenamestr.c_str();
395 
396  size_t nGlobalIDs;
397  Teuchos::reduceAll(comm, Teuchos::REDUCE_SUM, 1, &nLocalIDs, &nGlobalIDs);
398 
399  int nWgts = this->getNumWeightsPerID();
400 
401  for (int p = 0; p < np; p++) {
402 
403  // Is it this processor's turn to write to files?
404  if (me == p) {
405 
406  std::ofstream fp;
407 
408  if (me == 0) {
409  // open file for writing
410  fp.open(filename, std::ios::out);
411  // write Chaco header info
412  // this function assumes no edges
413  fp << nGlobalIDs << " " << 0 << " "
414  << (nWgts ? "010" : "000") << " "
415  << (nWgts > 1 ? std::to_string(nWgts) : " ") << std::endl;
416  }
417  else {
418  // open file for appending
419  fp.open(filename, std::ios::app);
420  }
421 
422  if (nWgts) {
423 
424  // get weight data
425  const scalar_t **wgts = new const scalar_t *[nWgts];
426  int *strides = new int[nWgts];
427  for (int n = 0; n < nWgts; n++)
428  getWeightsView(wgts[n], strides[n], n);
429 
430  // write weights to file
431  for (size_t i = 0; i < nLocalIDs; i++) {
432  for (int n = 0; n < nWgts; n++)
433  fp << wgts[n][i*strides[n]] << " ";
434  fp << "\n";
435  }
436 
437  delete [] strides;
438  delete [] wgts;
439  }
440 
441  fp.close();
442  }
443 
444  comm.barrier();
445  }
446  }
447 
448  // write assignments file
449  {
450  std::string filenamestr = fileprefix;
451  filenamestr = filenamestr + ".assign";
452  const char *filename = filenamestr.c_str();
453 
454  for (int p = 0; p < np; p++) {
455 
456  // Is it this processor's turn to write to files?
457  if (me == p) {
458 
459  std::ofstream fp;
460 
461  if (me == 0) {
462  // open file for writing
463  fp.open(filename, std::ios::out);
464  }
465  else {
466  // open file for appending
467  fp.open(filename, std::ios::app);
468  }
469 
470  const part_t *parts;
471  this->getPartsView(parts);
472 
473  for (size_t i = 0; i < nLocalIDs; i++) {
474  fp << (parts != NULL ? parts[i] : me) << "\n";
475  }
476  fp.close();
477  }
478 
479  comm.barrier();
480  }
481  }
482 }
483 
484 } //namespace Zoltan2
485 
486 #endif
Kokkos::View< const scalar_t *, device_t > ConstWeightsDeviceView1D
virtual int getNumWeightsPerID() const
Returns the number of weights per object. Number of weights per object should be zero or greater...
virtual VectorAdapter< UserCoord > * getCoordinateInput() const =0
virtual void getPartsDeviceView(Kokkos::View< part_t *, device_t > &inputPart) const
virtual void getIDsView(const gno_t *&ids) const
Provide a pointer to this process&#39; identifiers.
typename InputTraits< UserCoord >::scalar_t scalar_t
typename Kokkos::HostSpace::memory_space host_t
Kokkos::View< scalar_t *, device_t > WeightsDeviceView1D
Kokkos::View< scalar_t **, Kokkos::LayoutLeft, device_t > CoordsDeviceView
default_part_t part_t
The data type to represent part numbers.
default_offset_t offset_t
The data type to represent offsets.
virtual void getIDsDeviceView(ConstIdsDeviceView &deviceIds) const
Provide a Kokkos view (Device side) to this process&#39; identifiers.
#define Z2_THROW_NOT_IMPLEMENTED
virtual void setCoordinateInput(VectorAdapter< UserCoord > *coordData)=0
typename node_t::device_type device_t
typename ScalarsDeviceView::HostMirror ScalarsHostView
Defines the PartitioningSolution class.
typename InputTraits< UserCoord >::part_t part_t
Kokkos::View< const gno_t *, device_t > ConstIdsDeviceView
typename ConstScalarsDeviceView::HostMirror ConstScalarsHostView
virtual void getWeightsDeviceView(WeightsDeviceView1D &deviceWgts, int idx=0) const
Provide a Kokkos view (Device side) of the weights.
virtual void getWeightsView(const scalar_t *&wgt, int &stride, int idx=0) const
Provide pointer to a weight array with stride.
virtual void getIDsKokkosView(ConstIdsDeviceView &ids) const
Provide a Kokkos view to this process&#39; identifiers.
typename IdsDeviceView::HostMirror IdsHostView
typename WeightsDeviceView::HostMirror WeightsHostView
Kokkos::View< const scalar_t *, device_t > ConstScalarsDeviceView
typename InputTraits< UserCoord >::node_t node_t
typename CoordsDeviceView::HostMirror CoordsHostView
virtual void getWeightsKokkosView(Kokkos::View< scalar_t **, device_t > &wgt) const
Provide kokkos view of weights.
virtual ~BaseAdapterRoot()=default
A PartitioningSolution is a solution to a partitioning problem.
virtual void getPartsView(const part_t *&inputPart) const
Provide pointer to an array containing the input part assignment for each ID. The input part informat...
typename ConstOffsetsDeviceView::HostMirror ConstOffsetsHostView
typename InputTraits< UserCoord >::gno_t gno_t
typename ConstIdsDeviceView::HostMirror ConstIdsHostView
void generateWeightFileOnly(const char *fileprefix, const Teuchos::Comm< int > &comm) const
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
BaseAdapterType
An enum to identify general types of adapters.
identifier data, just a list of IDs
VectorAdapter defines the interface for vector input.
typename ConstWeightsDeviceView1D::HostMirror ConstWeightsHostView1D
BaseAdapter defines methods required by all Adapters.
Kokkos::View< gno_t *, device_t > IdsDeviceView
Traits for application input objects.
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
virtual void getPartsHostView(Kokkos::View< part_t *, host_t > &inputPart) const
Kokkos::View< offset_t *, device_t > OffsetsDeviceView
default_node_t node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
typename InputTraits< UserCoord >::offset_t offset_t
typename WeightsDeviceView1D::HostMirror WeightsHostView1D
virtual void getWeightsHostView(WeightsHostView &hostWgts) const
Provide a Kokkos view (Host side) of the weights.
void applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
Apply a PartitioningSolution to an input.
virtual size_t getLocalNumIDs() const =0
Returns the number of objects on this process.
typename ConstWeightsDeviceView::HostMirror ConstWeightsHostView
virtual void getCoordinatesHostView(CoordsHostView &) const
Gathering definitions used in software development.
virtual void getCoordinatesDeviceView(CoordsDeviceView &elements) const
Kokkos::View< const scalar_t **, device_t > ConstWeightsDeviceView
Kokkos::View< scalar_t **, device_t > WeightsDeviceView
typename OffsetsDeviceView::HostMirror OffsetsHostView
Kokkos::View< scalar_t *, device_t > ScalarsDeviceView
virtual void getCoordinatesKokkosView(CoordsDeviceView &elements) const =0
virtual void getWeightsHostView(WeightsHostView1D &hostWgts, int idx=0) const
Provide a Kokkos view (Host side) of the weights.
virtual void getWeightsDeviceView(WeightsDeviceView &deviceWgts) const
Provide a Kokkos view (Device side) of the weights.
Kokkos::View< const offset_t *, device_t > ConstOffsetsDeviceView
typename InputTraits< UserCoord >::lno_t lno_t
default_scalar_t scalar_t
The data type for weights and coordinates.
virtual void getCoordinatesView(const scalar_t *&coords, int &stride, int coordDim) const =0
virtual enum BaseAdapterType adapterType() const =0
Returns the type of adapter.
virtual void getIDsHostView(ConstIdsHostView &hostIds) const
Provide a Kokkos view (Host side) to this process&#39; identifiers.