Fork me on GitHub
 All Classes Files Functions Variables Groups Pages
dia_matrix.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2014 NVIDIA Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
21 #pragma once
22 
23 #include <cusp/detail/config.h>
24 
25 #include <cusp/array1d.h>
26 #include <cusp/array2d.h>
27 #include <cusp/detail/format.h>
28 #include <cusp/detail/matrix_base.h>
29 #include <cusp/detail/utils.h>
30 
31 namespace cusp
32 {
33 
34 // Forward definitions
35 template <typename ArrayType1, typename ArrayType2, typename IndexType, typename ValueType, typename MemorySpace> class dia_matrix_view;
36 
118 template <typename IndexType, typename ValueType, class MemorySpace>
119 class dia_matrix : public cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::dia_format>
120 {
121 private:
122 
123  typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::dia_format> Parent;
124 
125 public:
126  // TODO statically assert is_signed<IndexType>
127 
129  typedef typename cusp::array1d<IndexType, MemorySpace> diagonal_offsets_array_type;
130  typedef typename cusp::array2d<ValueType, MemorySpace, cusp::column_major> values_array_type;
131 
132  typedef typename cusp::dia_matrix<IndexType, ValueType, MemorySpace> container;
133 
134  typedef typename cusp::dia_matrix_view<
135  typename diagonal_offsets_array_type::view,
136  typename values_array_type::view,
137  IndexType, ValueType, MemorySpace> view;
138 
139  typedef typename cusp::dia_matrix_view<
140  typename diagonal_offsets_array_type::const_view,
141  typename values_array_type::const_view,
142  IndexType, ValueType, MemorySpace> const_view;
143 
144  typedef typename cusp::detail::coo_view_type<container,cusp::dia_format>::view coo_view_type;
145  typedef typename cusp::detail::coo_view_type<container const,cusp::dia_format>::view const_coo_view_type;
146 
147  template<typename MemorySpace2>
148  struct rebind
149  {
151  };
156  diagonal_offsets_array_type diagonal_offsets;
157 
160  values_array_type values;
161 
165  dia_matrix(void) {}
166 
176  dia_matrix(const size_t num_rows, const size_t num_cols, const size_t num_entries,
177  const size_t num_diagonals, const size_t alignment = 32);
178 
183  template <typename MatrixType>
184  dia_matrix(const MatrixType& matrix);
185 
193  void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries,
194  const size_t num_diagonals);
195 
204  void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries,
205  const size_t num_diagonals, const size_t alignment);
206 
211  void swap(dia_matrix& matrix);
212 
217  template <typename MatrixType>
218  dia_matrix& operator=(const MatrixType& matrix);
219 }; // class dia_matrix
314 template <typename ArrayType1,
315  typename ArrayType2,
316  typename IndexType = typename ArrayType1::value_type,
317  typename ValueType = typename ArrayType2::value_type,
318  typename MemorySpace = typename cusp::minimum_space<typename ArrayType1::memory_space, typename ArrayType2::memory_space>::type >
319 class dia_matrix_view : public cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::dia_format>
320 {
321 private:
322 
323  typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::dia_format> Parent;
324 
325 public:
327  typedef ArrayType1 diagonal_offsets_array_type;
328  typedef ArrayType2 values_array_type;
329  typedef typename cusp::dia_matrix<IndexType, ValueType, MemorySpace> container;
332 
333  typedef typename cusp::detail::coo_view_type<view,cusp::dia_format>::view coo_view_type;
334  typedef typename cusp::detail::coo_view_type<view const,cusp::dia_format>::view const_coo_view_type;
340  diagonal_offsets_array_type diagonal_offsets;
341 
345  values_array_type values;
346 
350  dia_matrix_view(void) {}
351 
361  template <typename OtherArrayType1, typename OtherArrayType2>
362  dia_matrix_view(size_t num_rows, size_t num_cols, size_t num_entries,
363  OtherArrayType1& diagonal_offsets, OtherArrayType2& values)
364  : Parent(num_rows, num_cols, num_entries),
365  diagonal_offsets(diagonal_offsets),
366  values(values) {}
367 
377  template <typename OtherArrayType1, typename OtherArrayType2>
378  dia_matrix_view(size_t num_rows, size_t num_cols, size_t num_entries,
379  const OtherArrayType1& diagonal_offsets, const OtherArrayType2& values)
380  : Parent(num_rows, num_cols, num_entries),
381  diagonal_offsets(diagonal_offsets),
382  values(values) {}
383 
389  : Parent(matrix),
391  values(make_array2d_view(matrix.values)) {}
392 
397  dia_matrix_view(const dia_matrix<IndexType,ValueType,MemorySpace>& matrix)
398  : Parent(matrix),
400  values(make_array2d_view(matrix.values)) {}
401 
407  : Parent(matrix),
409  values(matrix.values) {}
410 
415  dia_matrix_view(const dia_matrix_view& matrix)
416  : Parent(matrix),
418  values(matrix.values) {}
419 
427  void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries,
428  const size_t num_diagonals);
429 
438  void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries,
439  const size_t num_diagonals, const size_t alignment);
440 }; // class dia_matrix_view
441 
442 /* Convenience functions */
443 
458 template <typename ArrayType1,
459  typename ArrayType2>
461 make_dia_matrix_view(size_t num_rows,
462  size_t num_cols,
463  size_t num_entries,
464  ArrayType1 diagonal_offsets,
465  ArrayType2 values)
466 {
468  view(num_rows, num_cols, num_entries, diagonal_offsets, values);
469 
470  return view;
471 }
472 
488 template <typename ArrayType1,
489  typename ArrayType2,
490  typename IndexType,
491  typename ValueType,
492  typename MemorySpace>
495 {
497 }
498 
511 template <typename IndexType, typename ValueType, class MemorySpace>
514 {
515  return make_dia_matrix_view
516  (m.num_rows, m.num_cols, m.num_entries,
519 }
520 
533 template <typename IndexType, typename ValueType, class MemorySpace>
536 {
537  return make_dia_matrix_view
538  (m.num_rows, m.num_cols, m.num_entries,
541 }
542 
546 } // end namespace cusp
547 
548 #include <cusp/detail/dia_matrix.inl>
The array1d class is a 1D vector container that may contain elements stored in "host" or "device" mem...
Definition: array1d.h:98
diagonal_offsets_array_type diagonal_offsets
Definition: dia_matrix.h:156
void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries, const size_t num_diagonals)
Diagonal (DIA) representation a sparse matrix.
Definition: dia_matrix.h:119
dia_matrix< IndexType, ValueType, MemorySpace >::const_view make_dia_matrix_view(const dia_matrix< IndexType, ValueType, MemorySpace > &m)
Definition: dia_matrix.h:534
View of a dia_matrix.
Definition: dia_matrix.h:35
values_array_type values
Definition: dia_matrix.h:160
array1d_view< Iterator > make_array1d_view(Iterator first, Iterator last)
Definition: array1d.h:693
diagonal_offsets_array_type diagonal_offsets
Definition: dia_matrix.h:339
void swap(dia_matrix &matrix)
2D array of elements that may reside in "host" or "device" memory space
The array2d class is a 2D vector container that may contain elements stored in "host" or "device" mem...
Definition: array2d.h:93
void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries, const size_t num_diagonals)
values_array_type values
Definition: dia_matrix.h:344
array2d_view< typename cusp::array1d_view< Iterator >, Orientation > make_array2d_view(size_t num_rows, size_t num_cols, size_t pitch, const cusp::array1d_view< Iterator > &values, Orientation)
Definition: array2d.h:458
1D array of elements that may reside in "host" or "device" memory space
dia_matrix & operator=(const MatrixType &matrix)