RTOp Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RTOpPack_SparseSubVectorT.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // RTOp: Interfaces and Support Software for Vector Reduction Transformation
4 // Operations
5 //
6 // Copyright 2006 NTESS and the RTOp contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10 
11 
12 #ifndef RTOPPACK_SPARSE_SUB_VECTOR_T_HPP
13 #define RTOPPACK_SPARSE_SUB_VECTOR_T_HPP
14 
15 
16 #include "RTOpPack_Types.hpp"
17 #include "Teuchos_Assert.hpp"
18 
19 
20 namespace RTOpPack {
21 
22 
102 template<class Scalar>
104 public:
107  :globalOffset_(0), subDim_(0), subNz_(0),
109  isSorted_(false)
110  {}
113  Teuchos_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in, Teuchos_Ordinal subNz_in,
114  const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in,
115  const ArrayRCP<const Teuchos_Ordinal> &indices_in, ptrdiff_t indicesStride_in,
116  ptrdiff_t localOffset_in, bool isSorted_in
117  )
118  :globalOffset_(globalOffset_in), subDim_(subDim_in), subNz_(subNz_in),
119  values_(values_in), valuesStride_(valuesStride_in), indices_(indices_in),
120  indicesStride_(indicesStride_in), localOffset_(localOffset_in), isSorted_(isSorted_in)
121  {
122 #ifdef TEUCHOS_DEBUG
123  // Call initialize(...) just to check the preconditions
124  initialize(globalOffset_in, subDim_in, subNz_in, values_in, valuesStride_in,
125  indices_in, indicesStride_in, localOffset_in, isSorted_in);
126 #endif
127  }
130  Teuchos_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in,
131  const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in
132  )
133  :globalOffset_(globalOffset_in), subDim_(subDim_in), subNz_(subDim_in),
134  values_(values_in), valuesStride_(valuesStride_in),
136  {
137 #ifdef TEUCHOS_DEBUG
138  // Call initialize(...) just to check the preconditions
139  initialize(globalOffset_in, subDim_in, values_in, valuesStride_in);
140 #endif
141  }
144  :globalOffset_(sv.globalOffset()), subDim_(sv.subDim()), subNz_(sv.subDim()),
145  values_(sv.values()), valuesStride_(sv.stride()), indicesStride_(0),
146  localOffset_(0), isSorted_(true)
147  {}
150  Teuchos_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in, Teuchos_Ordinal subNz_in,
151  const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in,
152  const ArrayRCP<const Teuchos_Ordinal> &indices_in, ptrdiff_t indicesStride_in,
153  ptrdiff_t localOffset_in, bool isSorted_in
154  )
155  {
156  using Teuchos::as;
157 #ifdef TEUCHOS_DEBUG
158  TEUCHOS_ASSERT(globalOffset_in >= 0);
159  TEUCHOS_ASSERT(subDim_in > 0);
160  TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE(subNz_in, 0, subDim_in+1);
161  TEUCHOS_ASSERT_EQUALITY(values_in.lowerOffset(), 0);
162  TEUCHOS_ASSERT(valuesStride_in != 0);
163  TEUCHOS_ASSERT_EQUALITY(values_in.size(),
164  subNz_in*as<Teuchos_Ordinal>(std::abs(as<int>(valuesStride_in))));
165  if (!is_null(indices_in)) {
166  TEUCHOS_ASSERT(indicesStride_in != 0);
167  TEUCHOS_ASSERT_EQUALITY(indices_in.size(),
168  subNz_in*as<Teuchos_Ordinal>(std::abs(as<int>(indicesStride_in))));
169  // Note: localOffset can be +, -, or 0 so there is nothing to assert!
170  if (isSorted_in) {
171  for (int k = 0; k < subNz_in-1; ++k) {
172  const Teuchos_Ordinal idx_k = indices_in[k*indicesStride_in];
173  const Teuchos_Ordinal idx_kp1 = indices_in[(k+1)*indicesStride_in];
174  TEUCHOS_TEST_FOR_EXCEPTION( !(idx_k < idx_kp1), std::out_of_range,
175  "Error indices["<<k<<"]="<<idx_k<<" >= indices["<<k+1<<"]="<<idx_kp1
176  <<"!" );
177  }
178  }
179  }
180 #endif
181  globalOffset_ = globalOffset_in;
182  subDim_ = subDim_in;
183  subNz_ = subNz_in;
184  values_ = values_in;
185  valuesStride_ = valuesStride_in;
186  indices_ = indices_in;
187  indicesStride_ = indicesStride_in;
188  localOffset_ = localOffset_in;
189  isSorted_ = isSorted_in;
190  }
193  Teuchos_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in,
194  const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in
195  )
196  {
197  initialize(globalOffset_in, subDim_in, subDim_in, values_in, valuesStride_in,
198  Teuchos::null, 0, 0, true);
199  }
202  {
203  globalOffset_ = 0; subDim_ = 0; subNz_ = 0;
205  indicesStride_ = 0; localOffset_ = 0; isSorted_ = false;
206  }
208  void setGlobalOffset(Teuchos_Ordinal globalOffset_in) { globalOffset_ = globalOffset_in; }
212  Teuchos_Ordinal subDim() const { return subDim_; }
215  Teuchos_Ordinal subNz() const { return subNz_; }
218  const ArrayRCP<const Scalar> values() const { return values_; }
220  ptrdiff_t valuesStride() const { return valuesStride_; }
227  ptrdiff_t indicesStride() const { return indicesStride_; }
230  ptrdiff_t localOffset() const { return localOffset_; }
233  bool isSorted() const { return isSorted_; }
234 private:
239  ptrdiff_t valuesStride_;
241  ptrdiff_t indicesStride_;
242  ptrdiff_t localOffset_;
243  bool isSorted_;
244 public:
245 };
246 
247 
248 } // namespace RTOpPack
249 
250 
251 #endif // RTOPPACK_SPARSE_SUB_VECTOR_T_HPP
bool is_null(const boost::shared_ptr< T > &p)
void initialize(Teuchos_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in, const ArrayRCP< const Scalar > &values_in, ptrdiff_t valuesStride_in)
ArrayRCP< const Teuchos_Ordinal > indices_
SparseSubVectorT(Teuchos_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in, Teuchos_Ordinal subNz_in, const ArrayRCP< const Scalar > &values_in, ptrdiff_t valuesStride_in, const ArrayRCP< const Teuchos_Ordinal > &indices_in, ptrdiff_t indicesStride_in, ptrdiff_t localOffset_in, bool isSorted_in)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Class for a (sparse or dense) sub-vector.
SparseSubVectorT(const ConstSubVectorView< Scalar > &sv)
size_type size() const
Class for a non-changeable sub-vector.
#define TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE(index, lower_inclusive, upper_exclusive)
TEUCHOS_ORDINAL_TYPE Teuchos_Ordinal
bool isSorted() const
If isSorted == false then the vector is not sorted, otherwise it is sorted (sparse vectors only)...
size_type lowerOffset() const
void initialize(Teuchos_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in, Teuchos_Ordinal subNz_in, const ArrayRCP< const Scalar > &values_in, ptrdiff_t valuesStride_in, const ArrayRCP< const Teuchos_Ordinal > &indices_in, ptrdiff_t indicesStride_in, ptrdiff_t localOffset_in, bool isSorted_in)
ptrdiff_t localOffset() const
Offset of indices[] into local sub-vector (sparse vectors only).
Teuchos_Ordinal subNz() const
Number of nonzero elements (subNz == subDim for dense vectors).
TypeTo as(const TypeFrom &t)
const ArrayRCP< const Scalar > values() const
Array (size min{|valueStride*subNz|,1}) for the values in the vector.
Teuchos_Ordinal subDim() const
Dimension of the sub-vector.
void setGlobalOffset(Teuchos_Ordinal globalOffset_in)
SparseSubVectorT(Teuchos_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in, const ArrayRCP< const Scalar > &values_in, ptrdiff_t valuesStride_in)
ptrdiff_t indicesStride() const
Stride between indices in indices[] (sparse vectors only).
ptrdiff_t valuesStride() const
Stride between elements in values[].
Teuchos_Ordinal globalOffset() const
Offset for the sub-vector into the global vector.
const ArrayRCP< const Teuchos_Ordinal > indices() const
Array (size indicesStride*subNz) if not Teuchos::null) for the indices of the nonzero elements in the...
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)