Zoltan2
Loading...
Searching...
No Matches
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
20namespace Zoltan2 {
21
61template <typename User>
62 class VectorAdapter : public AdapterWithCoords<User> {
63public:
64
65#ifndef DOXYGEN_SHOULD_SKIP_THIS
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;
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
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::host_mirror_type 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
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
200 {
201 getEntriesHostView(elements);
202 }
204 {
205 getEntriesDeviceView(elements);
206 }
207
208private:
209
210 void generateCoordsFileOnly(
211 const char* fileprefix,
212 const Teuchos::Comm<int> &comm) const;
213
214};
215
216template <typename User>
217void 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::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition Metric.cpp:39
#define Z2_THROW_NOT_IMPLEMENTED
Kokkos::View< scalar_t **, Kokkos::LayoutLeft, device_t > CoordsDeviceView
typename BaseAdapter< User >::scalar_t scalar_t
typename CoordsDeviceView::host_mirror_type CoordsHostView
virtual size_t getLocalNumIDs() const =0
Returns the number of objects on this process.
typename InputTraits< User >::node_t node_t
typename InputTraits< User >::lno_t lno_t
void generateWeightFileOnly(const char *fileprefix, const Teuchos::Comm< int > &comm) const
typename InputTraits< User >::gno_t gno_t
typename InputTraits< User >::offset_t offset_t
typename InputTraits< User >::part_t part_t
VectorAdapter defines the interface for vector input.
void getCoordinatesDeviceView(typename AdapterWithCoords< User >::CoordsDeviceView &elements) const override
virtual void getEntriesHostView(typename AdapterWithCoords< User >::CoordsHostView &elements) const
Provide a Kokkos view (Host side) to the elements of the specified vector.
virtual int getNumEntriesPerID() const =0
Return the number of vectors.
void getCoordinatesView(const scalar_t *&elements, int &stride, int idx=0) const override
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...
void getCoordinatesKokkosView(typename AdapterWithCoords< User >::CoordsDeviceView &elements) const override
void getCoordinatesHostView(typename AdapterWithCoords< User >::CoordsHostView &elements) const override
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.
virtual void getEntriesDeviceView(typename AdapterWithCoords< User >::CoordsDeviceView &elements) const
Provide a Kokkos view (Device side) to the elements of the specified vector.
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.
Created by mbenlioglu on Aug 31, 2020.
BaseAdapterType
An enum to identify general types of adapters.
@ VectorAdapterType
vector data
default_offset_t offset_t
The data type to represent offsets.
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
default_node_t node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices.
default_part_t part_t
The data type to represent part numbers.
default_scalar_t scalar_t
The data type for weights and coordinates.