Zoltan2
Loading...
Searching...
No Matches
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
56namespace Zoltan2 {
57
97template <typename User>
98 class VectorAdapter : public AdapterWithCoords<User> {
99public:
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
115 virtual ~VectorAdapter() {};
116
118 // The Adapter interface.
120
121 enum BaseAdapterType adapterType() const override {return VectorAdapterType;}
122
124 // User's adapter interface:
125 // The user must implement these methods in his VectorAdapter
127
130 virtual int getNumEntriesPerID() const = 0;
131
139 virtual void getEntriesView(const scalar_t *&elements,
140 int &stride, int idx = 0) const {
141 // If adapter does not define getEntriesView, getEntriesKokkosView is called.
142 // If adapter does not define getEntriesKokkosView, getEntriesView is called.
143 // Allows forward and backwards compatibility.
144 // coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
145 Kokkos::View<scalar_t **, Kokkos::LayoutLeft,
146 typename node_t::device_type> kokkosEntries;
147 getEntriesKokkosView(kokkosEntries);
148 elements = kokkosEntries.data();
149 stride = 1;
150 }
151
157 // coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
158 Kokkos::View<scalar_t **, Kokkos::LayoutLeft,
159 typename node_t::device_type> & elements) const {
160 // If adapter does not define getEntriesKokkosView, getEntriesView is called.
161 // If adapter does not define getEntriesView, getEntriesKokkosView is called.
162 // Allows forward and backwards compatibility.
163 // coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
164 typedef Kokkos::View<scalar_t **, Kokkos::LayoutLeft,
165 typename node_t::device_type> kokkos_entries_view_t;
166 elements = kokkos_entries_view_t("entries", this->getLocalNumIDs(),
167 this->getNumEntriesPerID());
168 typename kokkos_entries_view_t::HostMirror host_elements =
169 Kokkos::create_mirror_view(elements);
170 for(int j = 0; j < this->getNumEntriesPerID(); ++j) {
171 const scalar_t * ptr_elements;
172 int stride;
173 getEntriesView(ptr_elements, stride, j);
174 size_t i = 0;
175 for(size_t n = 0; n < this->getLocalNumIDs() * stride; n += stride) {
176 host_elements(i++,j) = ptr_elements[n];
177 }
178 }
179 Kokkos::deep_copy(elements, host_elements);
180 }
181
191 const char *fileprefix,
192 const Teuchos::Comm<int> &comm
193 ) const
194 {
195 // Generate the graph file with weights using the base adapter method
196 this->generateWeightFileOnly(fileprefix, comm);
197
198 // Generate the coords file with local method
199 this->generateCoordsFileOnly(fileprefix, comm);
200 }
201
203 // Handy pseudonyms, since vectors are often used as coordinates
204 // User should not implement these methods.
206
207 inline int getDimension() const {return getNumEntriesPerID();}
208
209 inline void getCoordinatesView(const scalar_t *&elements, int &stride,
210 int idx = 0) const override
211 {
212 getEntriesView(elements, stride, idx);
213 }
214
216 // coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
217 Kokkos::View<scalar_t **, Kokkos::LayoutLeft, typename node_t::device_type> & elements) const override
218 {
219 getEntriesKokkosView(elements);
220 }
221
222private:
223
224 void generateCoordsFileOnly(
225 const char* fileprefix,
226 const Teuchos::Comm<int> &comm) const;
227
228};
229
230template <typename User>
231void VectorAdapter<User>::generateCoordsFileOnly(
232 const char *fileprefix,
233 const Teuchos::Comm<int> &comm
234) const
235{
236 // Writes a chaco-formatted coordinates file
237 // This function is SERIAL and can be quite slow. Use it for debugging only.
238
239 int np = comm.getSize();
240 int me = comm.getRank();
241
242 // append suffix to filename
243
244 std::string filenamestr = fileprefix;
245 filenamestr = filenamestr + ".coords";
246 const char *filename = filenamestr.c_str();
247
248 for (int p = 0; p < np; p++) {
249
250 // Is it this processor's turn to write to files?
251 if (me == p) {
252
253 std::ofstream fp;
254 if (me == 0) {
255 // open file for writing
256 fp.open(filename, std::ios::out);
257 }
258 else {
259 // open file for appending
260 fp.open(filename, std::ios::app);
261 }
262
263 // Get the vector entries
264 size_t len = this->getLocalNumIDs();
265 int nvec = this->getNumEntriesPerID();
266 const scalar_t **values = new const scalar_t *[nvec];
267 int *strides = new int[nvec];
268 for (int n = 0; n < nvec; n++)
269 getEntriesView(values[n], strides[n], n);
270
271 // write vector entries to coordinates file
272
273 for (size_t i = 0; i < len; i++) {
274 for (int n = 0; n < nvec; n++)
275 fp << values[n][i*strides[n]] << " ";
276 fp << "\n";
277 }
278
279 // clean up and close the file
280 delete [] strides;
281 delete [] values;
282 fp.close();
283 }
284 comm.barrier();
285 }
286}
287
288
289} //namespace Zoltan2
290
291#endif
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition Metric.cpp:74
virtual size_t getLocalNumIDs() const =0
Returns the number of objects on this process.
InputTraits< User >::node_t node_t
InputTraits< User >::offset_t offset_t
InputTraits< User >::part_t part_t
InputTraits< User >::scalar_t scalar_t
void generateWeightFileOnly(const char *fileprefix, const Teuchos::Comm< int > &comm) const
InputTraits< User >::lno_t lno_t
InputTraits< User >::gno_t gno_t
VectorAdapter defines the interface for vector input.
virtual int getNumEntriesPerID() const =0
Return the number of vectors.
virtual ~VectorAdapter()
Destructor.
void getCoordinatesView(const scalar_t *&elements, int &stride, int idx=0) const override
void getCoordinatesKokkosView(Kokkos::View< scalar_t **, Kokkos::LayoutLeft, typename node_t::device_type > &elements) 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...
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.
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.