Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_VectorAdapter.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 
15 #ifndef _ZOLTAN2_VECTORADAPTER_HPP_
16 #define _ZOLTAN2_VECTORADAPTER_HPP_
17 
18 #include <Zoltan2_Adapter.hpp>
19 
20 namespace Zoltan2 {
21 
61 template <typename User>
62  class VectorAdapter : public AdapterWithCoords<User> {
63 public:
64 
65 #ifndef DOXYGEN_SHOULD_SKIP_THIS
66  typedef typename InputTraits<User>::scalar_t scalar_t;
67  typedef typename InputTraits<User>::lno_t lno_t;
68  typedef typename InputTraits<User>::gno_t gno_t;
69  typedef typename InputTraits<User>::part_t part_t;
70  typedef typename InputTraits<User>::node_t node_t;
71  typedef typename InputTraits<User>::offset_t offset_t;
72  typedef User user_t;
73  typedef User userCoord_t;
74  typedef VectorAdapter<User> base_adapter_t;
75 #endif
76 
78  // The Adapter interface.
80 
81  enum BaseAdapterType adapterType() const override {return VectorAdapterType;}
82 
84  // User's adapter interface:
85  // The user must implement these methods in his VectorAdapter
87 
90  virtual int getNumEntriesPerID() const = 0;
91 
99  virtual void getEntriesView(const scalar_t *&elements,
100  int &stride, int idx = 0) const {
101  // If adapter does not define getEntriesView, getEntriesKokkosView is called.
102  // If adapter does not define getEntriesKokkosView, getEntriesView is called.
103  // Allows forward and backwards compatibility.
104  // coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
105  Kokkos::View<scalar_t **, Kokkos::LayoutLeft,
106  typename node_t::device_type> kokkosEntries;
107  getEntriesKokkosView(kokkosEntries);
108  elements = kokkosEntries.data();
109  stride = 1;
110  }
111 
116  virtual void getEntriesKokkosView(
117  // coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
118  Kokkos::View<scalar_t **, Kokkos::LayoutLeft,
119  typename node_t::device_type> & elements) const {
120  // If adapter does not define getEntriesKokkosView, getEntriesView is called.
121  // If adapter does not define getEntriesView, getEntriesKokkosView is called.
122  // Allows forward and backwards compatibility.
123  // coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
124  typedef Kokkos::View<scalar_t **, Kokkos::LayoutLeft,
125  typename node_t::device_type> kokkos_entries_view_t;
126  elements = kokkos_entries_view_t("entries", this->getLocalNumIDs(),
127  this->getNumEntriesPerID());
128  typename kokkos_entries_view_t::HostMirror host_elements =
129  Kokkos::create_mirror_view(elements);
130  for(int j = 0; j < this->getNumEntriesPerID(); ++j) {
131  const scalar_t * ptr_elements;
132  int stride;
133  getEntriesView(ptr_elements, stride, j);
134  size_t i = 0;
135  for(size_t n = 0; n < this->getLocalNumIDs() * stride; n += stride) {
136  host_elements(i++,j) = ptr_elements[n];
137  }
138  }
139  Kokkos::deep_copy(elements, host_elements);
140  }
141 
146  virtual void getEntriesHostView(typename AdapterWithCoords<User>::CoordsHostView & elements) const {
148 
149  }
150 
155  virtual void getEntriesDeviceView(typename AdapterWithCoords<User>::CoordsDeviceView& elements) const {
157  }
158 
168  const char *fileprefix,
169  const Teuchos::Comm<int> &comm
170  ) const
171  {
172  // Generate the graph file with weights using the base adapter method
173  this->generateWeightFileOnly(fileprefix, comm);
174 
175  // Generate the coords file with local method
176  this->generateCoordsFileOnly(fileprefix, comm);
177  }
178 
180  // Handy pseudonyms, since vectors are often used as coordinates
181  // User should not implement these methods.
183 
184  inline int getDimension() const {return getNumEntriesPerID();}
185 
186  inline void getCoordinatesView(const scalar_t *&elements, int &stride,
187  int idx = 0) const override
188  {
189  getEntriesView(elements, stride, idx);
190  }
191 
193  // coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
194  typename AdapterWithCoords<User>::CoordsDeviceView & elements) const override
195  {
196  getEntriesKokkosView(elements);
197  }
198 
199  void getCoordinatesHostView(typename AdapterWithCoords<User>::CoordsHostView &elements) const override
200  {
201  getEntriesHostView(elements);
202  }
204  {
205  getEntriesDeviceView(elements);
206  }
207 
208 private:
209 
210  void generateCoordsFileOnly(
211  const char* fileprefix,
212  const Teuchos::Comm<int> &comm) const;
213 
214 };
215 
216 template <typename User>
217 void VectorAdapter<User>::generateCoordsFileOnly(
218  const char *fileprefix,
219  const Teuchos::Comm<int> &comm
220 ) const
221 {
222  // Writes a chaco-formatted coordinates file
223  // This function is SERIAL and can be quite slow. Use it for debugging only.
224 
225  int np = comm.getSize();
226  int me = comm.getRank();
227 
228  // append suffix to filename
229 
230  std::string filenamestr = fileprefix;
231  filenamestr = filenamestr + ".coords";
232  const char *filename = filenamestr.c_str();
233 
234  for (int p = 0; p < np; p++) {
235 
236  // Is it this processor's turn to write to files?
237  if (me == p) {
238 
239  std::ofstream fp;
240  if (me == 0) {
241  // open file for writing
242  fp.open(filename, std::ios::out);
243  }
244  else {
245  // open file for appending
246  fp.open(filename, std::ios::app);
247  }
248 
249  // Get the vector entries
250  size_t len = this->getLocalNumIDs();
251  int nvec = this->getNumEntriesPerID();
252  const scalar_t **values = new const scalar_t *[nvec];
253  int *strides = new int[nvec];
254  for (int n = 0; n < nvec; n++)
255  getEntriesView(values[n], strides[n], n);
256 
257  // write vector entries to coordinates file
258 
259  for (size_t i = 0; i < len; i++) {
260  for (int n = 0; n < nvec; n++)
261  fp << values[n][i*strides[n]] << " ";
262  fp << "\n";
263  }
264 
265  // clean up and close the file
266  delete [] strides;
267  delete [] values;
268  fp.close();
269  }
270  comm.barrier();
271  }
272 }
273 
274 
275 } //namespace Zoltan2
276 
277 #endif
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
virtual void getEntriesKokkosView(Kokkos::View< scalar_t **, Kokkos::LayoutLeft, typename node_t::device_type > &elements) const
Provide a Kokkos view to the elements of the specified vector.
typename InputTraits< UserCoord >::scalar_t scalar_t
void getCoordinatesView(const scalar_t *&elements, int &stride, int idx=0) const override
virtual int getNumEntriesPerID() const =0
Return the number of vectors.
void getCoordinatesKokkosView(typename AdapterWithCoords< User >::CoordsDeviceView &elements) const override
Kokkos::View< scalar_t **, Kokkos::LayoutLeft, device_t > CoordsDeviceView
void generateFiles(const char *fileprefix, const Teuchos::Comm< int > &comm) const
Write files that can be used as input to Zoltan or Zoltan2 driver Creates chaco-formatted input files...
map_t::global_ordinal_type gno_t
Definition: mapRemotes.cpp:27
#define Z2_THROW_NOT_IMPLEMENTED
typename Zoltan2::InputTraits< ztcrsmatrix_t >::node_t node_t
typename InputTraits< User >::part_t part_t
virtual void getEntriesHostView(typename AdapterWithCoords< User >::CoordsHostView &elements) const
Provide a Kokkos view (Host side) to the elements of the specified vector.
SparseMatrixAdapter_t::part_t part_t
typename InputTraits< User >::node_t node_t
typename CoordsDeviceView::HostMirror CoordsHostView
enum BaseAdapterType adapterType() const override
Returns the type of adapter.
virtual void getEntriesView(const scalar_t *&elements, int &stride, int idx=0) const
Provide a pointer to the elements of the specified vector.
typename InputTraits< User >::gno_t gno_t
void getCoordinatesHostView(typename AdapterWithCoords< User >::CoordsHostView &elements) const override
void generateWeightFileOnly(const char *fileprefix, const Teuchos::Comm< int > &comm) const
BaseAdapterType
An enum to identify general types of adapters.
map_t::local_ordinal_type lno_t
Definition: mapRemotes.cpp:26
typename InputTraits< User >::offset_t offset_t
virtual size_t getLocalNumIDs() const =0
Returns the number of objects on this process.
typename BaseAdapter< User >::scalar_t scalar_t
typename InputTraits< User >::lno_t lno_t
virtual void getEntriesDeviceView(typename AdapterWithCoords< User >::CoordsDeviceView &elements) const
Provide a Kokkos view (Device side) to the elements of the specified vector.
void getCoordinatesDeviceView(typename AdapterWithCoords< User >::CoordsDeviceView &elements) const override
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition: Metric.cpp:39