Zoltan2  Version of the Day
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 
94  StridedData(): vec_(), stride_(0) { }
95 
101  lno_t size() const { return vec_.size(); }
102 
109  scalar_t operator[](lno_t idx) const { return vec_[idx*stride_]; }
110 
120  template <typename T> void getInputArray(ArrayRCP<const T> &array) const;
121 
127  void getStridedList(ArrayRCP<const scalar_t> &vec, int &stride) const
128  {
129  vec = vec_;
130  stride = stride_;
131  }
132 
140  void getStridedList(size_t &len, const scalar_t *&vec, int &stride) const
141  {
142  len = vec_.size();
143  vec = vec_.getRawPtr();
144  stride = stride_;
145  }
146 
150  {
151  if (this != &sInput)
152  sInput.getStridedList(vec_, stride_);
153 
154  return *this;
155  }
156 };
157 
158 template<typename lno_t, typename scalar_t>
159  template<typename T>
161  ArrayRCP<const T> &array) const
162 {
163  if (vec_.size() < 1){
164  array = ArrayRCP<const T>();
165  }
166  else if (stride_==1 && typeid(T()) == typeid(scalar_t())){
167  array = vec_;
168  }
169  else{
170  Environment env; // a default environment for error reporting
171  size_t n = vec_.size() / stride_;
172  T *tmp = new T [n];
173  env.localMemoryAssertion(__FILE__, __LINE__, n, tmp);
174  for (size_t i=0,j=0; i < n; i++,j+=stride_){
175  tmp[i] = static_cast<T>(vec_[j]);
176  }
177  array = arcp(tmp, 0, n);
178  }
179 
180  return;
181 }
182 
183 } // namespace Zoltan2
184 
185 #endif
scalar_t operator[](lno_t idx) const
Access an element of the input array.
lno_t size() const
Return the length of the strided array.
void localMemoryAssertion(const char *file, int lineNum, size_t nobj, bool ok) const
Test for successful memory allocation on local process only.
StridedData()
Default constructor. A zero-length strided array.
void getInputArray(ArrayRCP< const T > &array) const
Create a contiguous array of the required type, perhaps for a TPL.
The StridedData class manages lists of weights or coordinates.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
void getStridedList(ArrayRCP< const scalar_t > &vec, int &stride) const
Get a reference counted pointer to the input.
double scalar_t
Gathering definitions used in software development.
Defines the Environment class.
StridedData & operator=(const StridedData &sInput)
Assignment operator.
void getStridedList(size_t &len, const scalar_t *&vec, int &stride) const
Get the raw input information.
StridedData(ArrayRCP< const scalar_t > x, int stride)
Constructor.