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 // ***********************************************************************
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 #ifndef _ZOLTAN2_VECTORADAPTER_HPP_
52 #define _ZOLTAN2_VECTORADAPTER_HPP_
53 
54 #include <Zoltan2_Adapter.hpp>
55 
56 namespace Zoltan2 {
57 
97 template <typename User>
98  class VectorAdapter : public AdapterWithCoords<User> {
99 public:
100 
101 #ifndef DOXYGEN_SHOULD_SKIP_THIS
102  typedef typename InputTraits<User>::scalar_t scalar_t;
103  typedef typename InputTraits<User>::lno_t lno_t;
104  typedef typename InputTraits<User>::gno_t gno_t;
105  typedef typename InputTraits<User>::part_t part_t;
106  typedef typename InputTraits<User>::node_t node_t;
107  typedef typename InputTraits<User>::offset_t offset_t;
108  typedef User user_t;
109  typedef User userCoord_t;
110  typedef VectorAdapter<User> base_adapter_t;
111 #endif
112 
114  // The Adapter interface.
116 
117  enum BaseAdapterType adapterType() const override {return VectorAdapterType;}
118 
120  // User's adapter interface:
121  // The user must implement these methods in his VectorAdapter
123 
126  virtual int getNumEntriesPerID() const = 0;
127 
135  virtual void getEntriesView(const scalar_t *&elements,
136  int &stride, int idx = 0) const {
137  // If adapter does not define getEntriesView, getEntriesKokkosView is called.
138  // If adapter does not define getEntriesKokkosView, getEntriesView is called.
139  // Allows forward and backwards compatibility.
140  // coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
141  Kokkos::View<scalar_t **, Kokkos::LayoutLeft,
142  typename node_t::device_type> kokkosEntries;
143  getEntriesKokkosView(kokkosEntries);
144  elements = kokkosEntries.data();
145  stride = 1;
146  }
147 
152  virtual void getEntriesKokkosView(
153  // coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
154  Kokkos::View<scalar_t **, Kokkos::LayoutLeft,
155  typename node_t::device_type> & elements) const {
156  // If adapter does not define getEntriesKokkosView, getEntriesView is called.
157  // If adapter does not define getEntriesView, getEntriesKokkosView is called.
158  // Allows forward and backwards compatibility.
159  // coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
160  typedef Kokkos::View<scalar_t **, Kokkos::LayoutLeft,
161  typename node_t::device_type> kokkos_entries_view_t;
162  elements = kokkos_entries_view_t("entries", this->getLocalNumIDs(),
163  this->getNumEntriesPerID());
164  typename kokkos_entries_view_t::HostMirror host_elements =
165  Kokkos::create_mirror_view(elements);
166  for(int j = 0; j < this->getNumEntriesPerID(); ++j) {
167  const scalar_t * ptr_elements;
168  int stride;
169  getEntriesView(ptr_elements, stride, j);
170  size_t i = 0;
171  for(size_t n = 0; n < this->getLocalNumIDs() * stride; n += stride) {
172  host_elements(i++,j) = ptr_elements[n];
173  }
174  }
175  Kokkos::deep_copy(elements, host_elements);
176  }
177 
182  virtual void getEntriesHostView(typename AdapterWithCoords<User>::CoordsHostView & elements) const {
184 
185  }
186 
191  virtual void getEntriesDeviceView(typename AdapterWithCoords<User>::CoordsDeviceView& elements) const {
193  }
194 
204  const char *fileprefix,
205  const Teuchos::Comm<int> &comm
206  ) const
207  {
208  // Generate the graph file with weights using the base adapter method
209  this->generateWeightFileOnly(fileprefix, comm);
210 
211  // Generate the coords file with local method
212  this->generateCoordsFileOnly(fileprefix, comm);
213  }
214 
216  // Handy pseudonyms, since vectors are often used as coordinates
217  // User should not implement these methods.
219 
220  inline int getDimension() const {return getNumEntriesPerID();}
221 
222  inline void getCoordinatesView(const scalar_t *&elements, int &stride,
223  int idx = 0) const override
224  {
225  getEntriesView(elements, stride, idx);
226  }
227 
229  // coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
230  typename AdapterWithCoords<User>::CoordsDeviceView & elements) const override
231  {
232  getEntriesKokkosView(elements);
233  }
234 
235  void getCoordinatesHostView(typename AdapterWithCoords<User>::CoordsHostView &elements) const override
236  {
237  getEntriesHostView(elements);
238  }
240  {
241  getEntriesDeviceView(elements);
242  }
243 
244 private:
245 
246  void generateCoordsFileOnly(
247  const char* fileprefix,
248  const Teuchos::Comm<int> &comm) const;
249 
250 };
251 
252 template <typename User>
253 void VectorAdapter<User>::generateCoordsFileOnly(
254  const char *fileprefix,
255  const Teuchos::Comm<int> &comm
256 ) const
257 {
258  // Writes a chaco-formatted coordinates file
259  // This function is SERIAL and can be quite slow. Use it for debugging only.
260 
261  int np = comm.getSize();
262  int me = comm.getRank();
263 
264  // append suffix to filename
265 
266  std::string filenamestr = fileprefix;
267  filenamestr = filenamestr + ".coords";
268  const char *filename = filenamestr.c_str();
269 
270  for (int p = 0; p < np; p++) {
271 
272  // Is it this processor's turn to write to files?
273  if (me == p) {
274 
275  std::ofstream fp;
276  if (me == 0) {
277  // open file for writing
278  fp.open(filename, std::ios::out);
279  }
280  else {
281  // open file for appending
282  fp.open(filename, std::ios::app);
283  }
284 
285  // Get the vector entries
286  size_t len = this->getLocalNumIDs();
287  int nvec = this->getNumEntriesPerID();
288  const scalar_t **values = new const scalar_t *[nvec];
289  int *strides = new int[nvec];
290  for (int n = 0; n < nvec; n++)
291  getEntriesView(values[n], strides[n], n);
292 
293  // write vector entries to coordinates file
294 
295  for (size_t i = 0; i < len; i++) {
296  for (int n = 0; n < nvec; n++)
297  fp << values[n][i*strides[n]] << " ";
298  fp << "\n";
299  }
300 
301  // clean up and close the file
302  delete [] strides;
303  delete [] values;
304  fp.close();
305  }
306  comm.barrier();
307  }
308 }
309 
310 
311 } //namespace Zoltan2
312 
313 #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:18
#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:17
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:74