Support Software for Vector Reduction/Transformation Operators  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RTOpPack_SparseSubVectorT.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // RTOp: Interfaces and Support Software for Vector Reduction Transformation
5 // Operations
6 // Copyright (2006) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
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 Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 
43 
44 #ifndef RTOPPACK_SPARSE_SUB_VECTOR_T_HPP
45 #define RTOPPACK_SPARSE_SUB_VECTOR_T_HPP
46 
47 
48 #include "RTOpPack_Types.hpp"
49 #include "Teuchos_Assert.hpp"
50 
51 
52 namespace RTOpPack {
53 
54 
134 template<class Scalar>
136 public:
139  :globalOffset_(0), subDim_(0), subNz_(0),
140  valuesStride_(0), indicesStride_(0), localOffset_(0),
141  isSorted_(false)
142  {}
145  Teuchos_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in, Teuchos_Ordinal subNz_in,
146  const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in,
147  const ArrayRCP<const Teuchos_Ordinal> &indices_in, ptrdiff_t indicesStride_in,
148  ptrdiff_t localOffset_in, bool isSorted_in
149  )
150  :globalOffset_(globalOffset_in), subDim_(subDim_in), subNz_(subNz_in),
151  values_(values_in), valuesStride_(valuesStride_in), indices_(indices_in),
152  indicesStride_(indicesStride_in), localOffset_(localOffset_in), isSorted_(isSorted_in)
153  {
154 #ifdef TEUCHOS_DEBUG
155  // Call initialize(...) just to check the preconditions
156  initialize(globalOffset_in, subDim_in, subNz_in, values_in, valuesStride_in,
157  indices_in, indicesStride_in, localOffset_in, isSorted_in);
158 #endif
159  }
162  Teuchos_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in,
163  const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in
164  )
165  :globalOffset_(globalOffset_in), subDim_(subDim_in), subNz_(subDim_in),
166  values_(values_in), valuesStride_(valuesStride_in),
167  indicesStride_(0), localOffset_(0), isSorted_(true)
168  {
169 #ifdef TEUCHOS_DEBUG
170  // Call initialize(...) just to check the preconditions
171  initialize(globalOffset_in, subDim_in, values_in, valuesStride_in);
172 #endif
173  }
176  :globalOffset_(sv.globalOffset()), subDim_(sv.subDim()), subNz_(sv.subDim()),
177  values_(sv.values()), valuesStride_(sv.stride()), indicesStride_(0),
178  localOffset_(0), isSorted_(true)
179  {}
182  Teuchos_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in, Teuchos_Ordinal subNz_in,
183  const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in,
184  const ArrayRCP<const Teuchos_Ordinal> &indices_in, ptrdiff_t indicesStride_in,
185  ptrdiff_t localOffset_in, bool isSorted_in
186  )
187  {
188  using Teuchos::as;
189 #ifdef TEUCHOS_DEBUG
190  TEUCHOS_ASSERT(globalOffset_in >= 0);
191  TEUCHOS_ASSERT(subDim_in > 0);
192  TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE(subNz_in, 0, subDim_in+1);
193  TEUCHOS_ASSERT_EQUALITY(values_in.lowerOffset(), 0);
194  TEUCHOS_ASSERT(valuesStride_in != 0);
195  TEUCHOS_ASSERT_EQUALITY(values_in.size(),
196  subNz_in*as<Teuchos_Ordinal>(std::abs(as<int>(valuesStride_in))));
197  if (!is_null(indices_in)) {
198  TEUCHOS_ASSERT(indicesStride_in != 0);
199  TEUCHOS_ASSERT_EQUALITY(indices_in.size(),
200  subNz_in*as<Teuchos_Ordinal>(std::abs(as<int>(indicesStride_in))));
201  // Note: localOffset can be +, -, or 0 so there is nothing to assert!
202  if (isSorted_in) {
203  for (int k = 0; k < subNz_in-1; ++k) {
204  const Teuchos_Ordinal idx_k = indices_in[k*indicesStride_in];
205  const Teuchos_Ordinal idx_kp1 = indices_in[(k+1)*indicesStride_in];
206  TEUCHOS_TEST_FOR_EXCEPTION( !(idx_k < idx_kp1), std::out_of_range,
207  "Error indices["<<k<<"]="<<idx_k<<" >= indices["<<k+1<<"]="<<idx_kp1
208  <<"!" );
209  }
210  }
211  }
212 #endif
213  globalOffset_ = globalOffset_in;
214  subDim_ = subDim_in;
215  subNz_ = subNz_in;
216  values_ = values_in;
217  valuesStride_ = valuesStride_in;
218  indices_ = indices_in;
219  indicesStride_ = indicesStride_in;
220  localOffset_ = localOffset_in;
221  isSorted_ = isSorted_in;
222  }
225  Teuchos_Ordinal globalOffset_in, Teuchos_Ordinal subDim_in,
226  const ArrayRCP<const Scalar> &values_in, ptrdiff_t valuesStride_in
227  )
228  {
229  initialize(globalOffset_in, subDim_in, subDim_in, values_in, valuesStride_in,
230  Teuchos::null, 0, 0, true);
231  }
234  {
235  globalOffset_ = 0; subDim_ = 0; subNz_ = 0;
236  values_ = Teuchos::null; valuesStride_ = 0; indices_ = Teuchos::null;
237  indicesStride_ = 0; localOffset_ = 0; isSorted_ = false;
238  }
240  void setGlobalOffset(Teuchos_Ordinal globalOffset_in) { globalOffset_ = globalOffset_in; }
242  Teuchos_Ordinal globalOffset() const { return globalOffset_; }
244  Teuchos_Ordinal subDim() const { return subDim_; }
247  Teuchos_Ordinal subNz() const { return subNz_; }
250  const ArrayRCP<const Scalar> values() const { return values_; }
252  ptrdiff_t valuesStride() const { return valuesStride_; }
257  const ArrayRCP<const Teuchos_Ordinal> indices() const { return indices_; }
259  ptrdiff_t indicesStride() const { return indicesStride_; }
262  ptrdiff_t localOffset() const { return localOffset_; }
265  bool isSorted() const { return isSorted_; }
266 private:
267  Teuchos_Ordinal globalOffset_;
268  Teuchos_Ordinal subDim_;
269  Teuchos_Ordinal subNz_;
270  ArrayRCP<const Scalar> values_;
271  ptrdiff_t valuesStride_;
272  ArrayRCP<const Teuchos_Ordinal> indices_;
273  ptrdiff_t indicesStride_;
274  ptrdiff_t localOffset_;
275  bool isSorted_;
276 public:
277 };
278 
279 
280 } // namespace RTOpPack
281 
282 
283 #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)
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)
#define TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE(index, lower_inclusive, upper_exclusive)
bool isSorted() const
If isSorted == false then the vector is not sorted, otherwise it is sorted (sparse vectors only)...
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).
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).
TypeTo as(const TypeFrom &t)
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)