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 BaseAdapter<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;
111 #endif
112 
115  virtual ~VectorAdapter() {};
116 
118  // The Adapter interface.
120 
122 
124  // User's adapter interface:
125  // The user must implement these methods in his VectorAdapter
127 
130  virtual int getNumEntriesPerID() const = 0;
131 
141  virtual void getEntriesView(const scalar_t *&elements, int &stride,
142  int idx = 0) const = 0;
143 
153  const char *fileprefix,
154  const Teuchos::Comm<int> &comm
155  ) const
156  {
157  // Generate the graph file with weights using the base adapter method
158  this->generateWeightFileOnly(fileprefix, comm);
159 
160  // Generate the coords file with local method
161  this->generateCoordsFileOnly(fileprefix, comm);
162  }
163 
165  // Handy pseudonyms, since vectors are often used as coordinates
166  // User should not implement these methods.
168 
169  inline int getDimension() const {return getNumEntriesPerID();}
170 
171  inline void getCoordinatesView(const scalar_t *&elements, int &stride,
172  int idx = 0) const
173  {
174  getEntriesView(elements, stride, idx);
175  }
176 
177 private:
178 
179  void generateCoordsFileOnly(
180  const char* fileprefix,
181  const Teuchos::Comm<int> &comm) const;
182 
183 };
184 
185 template <typename User>
186 void VectorAdapter<User>::generateCoordsFileOnly(
187  const char *fileprefix,
188  const Teuchos::Comm<int> &comm
189 ) const
190 {
191  // Writes a chaco-formatted coordinates file
192  // This function is SERIAL and can be quite slow. Use it for debugging only.
193 
194  int np = comm.getSize();
195  int me = comm.getRank();
196 
197  // append suffix to filename
198 
199  std::string filenamestr = fileprefix;
200  filenamestr = filenamestr + ".coords";
201  const char *filename = filenamestr.c_str();
202 
203  for (int p = 0; p < np; p++) {
204 
205  // Is it this processor's turn to write to files?
206  if (me == p) {
207 
208  std::ofstream fp;
209  if (me == 0) {
210  // open file for writing
211  fp.open(filename, std::ios::out);
212  }
213  else {
214  // open file for appending
215  fp.open(filename, std::ios::app);
216  }
217 
218  // Get the vector entries
219  size_t len = this->getLocalNumIDs();
220  int nvec = this->getNumEntriesPerID();
221  const scalar_t **values = new const scalar_t *[nvec];
222  int *strides = new int[nvec];
223  for (int n = 0; n < nvec; n++)
224  getEntriesView(values[n], strides[n], n);
225 
226  // write vector entries to coordinates file
227 
228  for (size_t i = 0; i < len; i++) {
229  for (int n = 0; n < nvec; n++)
230  fp << values[n][i*strides[n]] << " ";
231  fp << "\n";
232  }
233 
234  // clean up and close the file
235  delete [] strides;
236  delete [] values;
237  fp.close();
238  }
239  comm.barrier();
240  }
241 }
242 
243 
244 } //namespace Zoltan2
245 
246 #endif
InputTraits< User >::scalar_t scalar_t
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
InputTraits< User >::gno_t gno_t
virtual int getNumEntriesPerID() const =0
Return the number of vectors.
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...
default_part_t part_t
The data type to represent part numbers.
default_offset_t offset_t
The data type to represent offsets.
InputTraits< User >::lno_t lno_t
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.
VectorAdapter defines the interface for vector input.
InputTraits< User >::part_t part_t
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.
void getCoordinatesView(const scalar_t *&elements, int &stride, int idx=0) const
virtual ~VectorAdapter()
Destructor.
enum BaseAdapterType adapterType() const
Returns the type of adapter.
InputTraits< User >::offset_t offset_t
virtual void getEntriesView(const scalar_t *&elements, int &stride, int idx=0) const =0
Provide a pointer to the elements of the specified vector.
default_scalar_t scalar_t
The data type for weights and coordinates.
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition: Metric.cpp:74