Fork me on GitHub
 All Classes Files Functions Variables Groups Pages
csr_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/detail/format.h>
27 #include <cusp/detail/matrix_base.h>
28 #include <cusp/detail/type_traits.h>
29 
30 namespace cusp
31 {
32 
33 // forward definition
34 template <typename ArrayType1, typename ArrayType2, typename ArrayType3, typename IndexType, typename ValueType, typename MemorySpace> class csr_matrix_view;
35 
104 template <typename IndexType, typename ValueType, class MemorySpace>
105 class csr_matrix : public cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::csr_format>
106 {
107 private:
108 
109  typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::csr_format> Parent;
110 
111 public:
112 
114  typedef typename cusp::array1d<IndexType, MemorySpace> row_offsets_array_type;
115  typedef typename cusp::array1d<IndexType, MemorySpace> column_indices_array_type;
116  typedef typename cusp::array1d<ValueType, MemorySpace> values_array_type;
117 
118  typedef typename cusp::csr_matrix<IndexType, ValueType, MemorySpace> container;
119 
120  typedef typename cusp::csr_matrix_view<typename row_offsets_array_type::view,
121  typename column_indices_array_type::view,
122  typename values_array_type::view,
123  IndexType, ValueType, MemorySpace> view;
124 
125  typedef typename cusp::csr_matrix_view<typename row_offsets_array_type::const_view,
126  typename column_indices_array_type::const_view,
127  typename values_array_type::const_view,
128  IndexType, ValueType, MemorySpace> const_view;
129 
130  typedef typename cusp::detail::coo_view_type<container,cusp::csr_format>::view coo_view_type;
131  typedef typename cusp::detail::coo_view_type<container const,cusp::csr_format>::view const_coo_view_type;
132 
133  template<typename MemorySpace2>
134  struct rebind
135  {
137  };
142  row_offsets_array_type row_offsets;
143 
146  column_indices_array_type column_indices;
147 
150  values_array_type values;
151 
154  csr_matrix(void) {}
155 
162  csr_matrix(size_t num_rows, size_t num_cols, size_t num_entries)
163  : Parent(num_rows, num_cols, num_entries),
164  row_offsets(num_rows + 1),
165  column_indices(num_entries),
166  values(num_entries) {}
167 
175  template <typename MatrixType>
176  csr_matrix(const MatrixType& matrix);
177 
184  void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries);
185 
190  void swap(csr_matrix& matrix);
191 
199  template <typename MatrixType>
200  csr_matrix& operator=(const MatrixType& matrix);
201 
202 }; // class csr_matrix
291 template <typename ArrayType1,
292  typename ArrayType2,
293  typename ArrayType3,
294  typename IndexType = typename ArrayType1::value_type,
295  typename ValueType = typename ArrayType3::value_type,
296  typename MemorySpace = typename cusp::minimum_space<
297  typename ArrayType1::memory_space,
298  typename ArrayType2::memory_space,
299  typename ArrayType3::memory_space>::type >
300 class csr_matrix_view : public cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::csr_format>
301 {
302 private:
303 
304  typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::csr_format> Parent;
305 
306 public:
307 
309  typedef ArrayType1 row_offsets_array_type;
310  typedef ArrayType2 column_indices_array_type;
311  typedef ArrayType3 values_array_type;
312 
313  typedef typename cusp::csr_matrix<IndexType, ValueType, MemorySpace> container;
316 
317  typedef typename cusp::detail::coo_view_type<view,cusp::csr_format>::view coo_view_type;
318  typedef typename cusp::detail::coo_view_type<view const,cusp::csr_format>::view const_coo_view_type;
324  row_offsets_array_type row_offsets;
325 
329  column_indices_array_type column_indices;
330 
334  values_array_type values;
335 
339  csr_matrix_view(void)
340  : Parent() {}
341 
353  csr_matrix_view(const size_t num_rows,
354  const size_t num_cols,
355  const size_t num_entries,
356  ArrayType1 row_offsets,
357  ArrayType2 column_indices,
358  ArrayType3 values)
359  : Parent(num_rows, num_cols, num_entries),
360  row_offsets(row_offsets),
361  column_indices(column_indices),
362  values(values) {}
363 
368  csr_matrix_view(csr_matrix<IndexType,ValueType,MemorySpace>& matrix)
369  : Parent(matrix),
370  row_offsets(matrix.row_offsets),
372  values(matrix.values) {}
373 
378  csr_matrix_view(const csr_matrix<IndexType,ValueType,MemorySpace>& matrix)
379  : Parent(matrix),
380  row_offsets(matrix.row_offsets),
382  values(matrix.values) {}
383 
389  : Parent(matrix),
390  row_offsets(matrix.row_offsets),
392  values(matrix.values) {}
393 
398  csr_matrix_view(const csr_matrix_view& matrix)
399  : Parent(matrix),
400  row_offsets(matrix.row_offsets),
402  values(matrix.values) {}
403 
410  void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries);
411 };
412 
413 /* Convenience functions */
414 
431 template <typename ArrayType1,
432  typename ArrayType2,
433  typename ArrayType3>
435 make_csr_matrix_view(size_t num_rows,
436  size_t num_cols,
437  size_t num_entries,
438  ArrayType1 row_offsets,
439  ArrayType2 column_indices,
440  ArrayType3 values)
441 {
443  view(num_rows, num_cols, num_entries, row_offsets, column_indices, values);
444 
445  return view;
446 }
447 
464 template <typename ArrayType1,
465  typename ArrayType2,
466  typename ArrayType3,
467  typename IndexType,
468  typename ValueType,
469  typename MemorySpace>
472 {
474 }
475 
488 template <typename IndexType, typename ValueType, class MemorySpace>
491 {
492  return make_csr_matrix_view
493  (m.num_rows, m.num_cols, m.num_entries,
497 }
498 
511 template <typename IndexType, typename ValueType, class MemorySpace>
514 {
515  return make_csr_matrix_view
516  (m.num_rows, m.num_cols, m.num_entries,
520 }
524 } // end namespace cusp
525 
526 #include <cusp/detail/csr_matrix.inl>
row_offsets_array_type row_offsets
Definition: csr_matrix.h:323
The array1d class is a 1D vector container that may contain elements stored in "host" or "device" mem...
Definition: array1d.h:98
values_array_type values
Definition: csr_matrix.h:150
void swap(csr_matrix &matrix)
row_offsets_array_type row_offsets
Definition: csr_matrix.h:142
void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries)
View of a csr_matrix.
Definition: csr_matrix.h:34
Compressed sparse row (CSR) representation a sparse matrix.
Definition: csr_matrix.h:105
array1d_view< Iterator > make_array1d_view(Iterator first, Iterator last)
Definition: array1d.h:693
csr_matrix< IndexType, ValueType, MemorySpace >::const_view make_csr_matrix_view(const csr_matrix< IndexType, ValueType, MemorySpace > &m)
Definition: csr_matrix.h:512
void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries)
csr_matrix & operator=(const MatrixType &matrix)
values_array_type values
Definition: csr_matrix.h:333
column_indices_array_type column_indices
Definition: csr_matrix.h:328
csr_matrix(size_t num_rows, size_t num_cols, size_t num_entries)
Definition: csr_matrix.h:162
1D array of elements that may reside in "host" or "device" memory space
column_indices_array_type column_indices
Definition: csr_matrix.h:146