Zoltan2
Zoltan2_StridedData.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 #ifndef _ZOLTAN2_STRIDEDDATA_HPP_
46 #define _ZOLTAN2_STRIDEDDATA_HPP_
47 
48 #include <Zoltan2_Standards.hpp>
49 #include <Zoltan2_Environment.hpp>
50 #include <typeinfo>
51 
56 namespace Zoltan2{
57 
75 template<typename lno_t, typename scalar_t>
76 class StridedData {
77 private:
78  ArrayRCP<const scalar_t> vec_;
79  int stride_;
80 
81 public:
82 
89  StridedData(ArrayRCP<const scalar_t> x, int stride) :
90  vec_(x), stride_(stride)
91  { }
92 
95  StridedData(): vec_(), stride_(0)
96  { }
97 
103  lno_t size() const { return vec_.size(); }
104 
111  scalar_t operator[](lno_t idx) const { return vec_[idx*stride_]; }
112 
122  template <typename T> void getInputArray(ArrayRCP<const T> &array) const;
123 
129  void getStridedList(ArrayRCP<const scalar_t> &vec, int &stride) const
130  {
131  vec = vec_;
132  stride = stride_;
133  }
134 
142  void getStridedList(size_t &len, const scalar_t *&vec, int &stride) const
143  {
144  len = vec_.size();
145  if (len != 0) vec = vec_.getRawPtr();
146  else vec = NULL;
147  stride = stride_;
148  }
149 
153  {
154  if (this != &sInput)
155  sInput.getStridedList(vec_, stride_);
156 
157  return *this;
158  }
159 };
160 
161 template<typename lno_t, typename scalar_t>
162  template<typename T>
164  ArrayRCP<const T> &array) const
165 {
166  if (vec_.size() < 1){
167  array = ArrayRCP<const T>();
168  }
169  else if (stride_==1 && typeid(T()) == typeid(scalar_t())){
170  array = vec_;
171  }
172  else {
173  // Create an unstrided copy
174  Environment env; // a default environment for error reporting
175  size_t n = vec_.size() / stride_;
176  T *tmp = new T [n];
177  env.localMemoryAssertion(__FILE__, __LINE__, n, tmp);
178  for (size_t i=0,j=0; i < n; i++,j+=stride_){
179  tmp[i] = static_cast<T>(vec_[j]);
180  }
181  array = arcp(tmp, 0, n);
182  }
183 
184  return;
185 }
186 
187 } // namespace Zoltan2
188 
189 #endif
StridedData()
Default constructor. A zero-length strided array.
void getStridedList(size_t &len, const scalar_t *&vec, int &stride) const
Get the raw input information.
void getStridedList(ArrayRCP< const scalar_t > &vec, int &stride) const
Get a reference counted pointer to the input.
The StridedData class manages lists of weights or coordinates.
lno_t size() const
Return the length of the strided array.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
Gathering definitions used in software development.
Defines the Environment class.
scalar_t operator[](lno_t idx) const
Access an element of the input array.
StridedData & operator=(const StridedData &sInput)
Assignment operator.
void getInputArray(ArrayRCP< const T > &array) const
Create a contiguous array of the required type, perhaps for a TPL.
StridedData(ArrayRCP< const scalar_t > x, int stride)
Constructor.
void localMemoryAssertion(const char *file, int lineNum, size_t nobj, bool ok) const
Test for successful memory allocation on local process only.