CUSP
Loading...
Searching...
No Matches
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/memory.h>
27
28#include <cusp/detail/format.h>
29#include <cusp/detail/matrix_base.h>
30#include <cusp/detail/type_traits.h>
31
32namespace cusp
33{
34
35// forward definition
36template <typename ArrayType1, typename ArrayType2, typename ArrayType3, typename IndexType, typename ValueType, typename MemorySpace> class csr_matrix_view;
37
106template <typename IndexType, typename ValueType, class MemorySpace>
107class csr_matrix : public cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::csr_format>
108{
109private:
110
111 typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::csr_format> Parent;
112
113public:
114
116 typedef typename cusp::array1d<IndexType, MemorySpace> row_offsets_array_type;
117 typedef typename cusp::array1d<IndexType, MemorySpace> column_indices_array_type;
118 typedef typename cusp::array1d<ValueType, MemorySpace> values_array_type;
119
121
122 typedef typename cusp::csr_matrix_view<typename row_offsets_array_type::view,
123 typename column_indices_array_type::view,
124 typename values_array_type::view,
125 IndexType, ValueType, MemorySpace> view;
126
127 typedef typename cusp::csr_matrix_view<typename row_offsets_array_type::const_view,
128 typename column_indices_array_type::const_view,
129 typename values_array_type::const_view,
130 IndexType, ValueType, MemorySpace> const_view;
131
132 typedef typename cusp::detail::coo_view_type<row_offsets_array_type,
133 column_indices_array_type,
134 values_array_type,
135 cusp::csr_format>::view coo_view_type;
136 typedef typename cusp::detail::coo_view_type<row_offsets_array_type,
137 column_indices_array_type,
138 values_array_type,
139 cusp::csr_format>::view const_coo_view_type;
140
141 template<typename MemorySpace2>
142 struct rebind
143 {
145 };
150 row_offsets_array_type row_offsets;
151
154 column_indices_array_type column_indices;
155
158 values_array_type values;
159
162 csr_matrix(void) {}
163
170 csr_matrix(size_t num_rows, size_t num_cols, size_t num_entries)
171 : Parent(num_rows, num_cols, num_entries),
172 row_offsets(num_rows + 1),
173 column_indices(num_entries),
174 values(num_entries) {}
175
183 template <typename MatrixType>
184 csr_matrix(const MatrixType& matrix);
185
192 void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries);
193
198 void swap(csr_matrix& matrix);
199
207 template <typename MatrixType>
208 csr_matrix& operator=(const MatrixType& matrix);
209
210}; // class csr_matrix
299template <typename ArrayType1,
300 typename ArrayType2,
301 typename ArrayType3,
302 typename IndexType = typename ArrayType1::value_type,
303 typename ValueType = typename ArrayType3::value_type,
304 typename MemorySpace = typename cusp::minimum_space<
305 typename ArrayType1::memory_space,
306 typename ArrayType2::memory_space,
307 typename ArrayType3::memory_space>::type >
308class csr_matrix_view : public cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::csr_format>
309{
310private:
311
312 typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::csr_format> Parent;
313
314public:
315
317 typedef ArrayType1 row_offsets_array_type;
318 typedef ArrayType2 column_indices_array_type;
319 typedef ArrayType3 values_array_type;
320
324
325 typedef typename cusp::detail::coo_view_type<row_offsets_array_type,
326 column_indices_array_type,
327 values_array_type,
328 cusp::csr_format>::view coo_view_type;
329 typedef typename cusp::detail::coo_view_type<row_offsets_array_type,
330 column_indices_array_type,
331 values_array_type,
332 cusp::csr_format>::view const_coo_view_type;
338 row_offsets_array_type row_offsets;
339
343 column_indices_array_type column_indices;
344
348 values_array_type values;
349
354 : Parent() {}
355
367 csr_matrix_view(const size_t num_rows,
368 const size_t num_cols,
369 const size_t num_entries,
370 ArrayType1 row_offsets,
371 ArrayType2 column_indices,
372 ArrayType3 values)
373 : Parent(num_rows, num_cols, num_entries),
376 values(values) {}
377
387
393 : Parent(matrix),
394 row_offsets(matrix.row_offsets),
396 values(matrix.values) {}
397
403 : Parent(matrix),
404 row_offsets(matrix.row_offsets),
406 values(matrix.values) {}
407
413 : Parent(matrix),
414 row_offsets(matrix.row_offsets),
416 values(matrix.values) {}
417
424 void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries);
425};
426
427/* Convenience functions */
428
445template <typename ArrayType1,
446 typename ArrayType2,
447 typename ArrayType3>
449make_csr_matrix_view(size_t num_rows,
450 size_t num_cols,
451 size_t num_entries,
452 ArrayType1 row_offsets,
453 ArrayType2 column_indices,
454 ArrayType3 values)
455{
457 view(num_rows, num_cols, num_entries, row_offsets, column_indices, values);
458
459 return view;
460}
461
478template <typename ArrayType1,
479 typename ArrayType2,
480 typename ArrayType3,
481 typename IndexType,
482 typename ValueType,
483 typename MemorySpace>
484csr_matrix_view<ArrayType1,ArrayType2,ArrayType3,IndexType,ValueType,MemorySpace>
489
502template <typename IndexType, typename ValueType, class MemorySpace>
503typename csr_matrix<IndexType,ValueType,MemorySpace>::view
512
525template <typename IndexType, typename ValueType, class MemorySpace>
526typename csr_matrix<IndexType,ValueType,MemorySpace>::const_view
538} // end namespace cusp
539
540#include <cusp/detail/csr_matrix.inl>
1D array of elements that may reside in "host" or "device" memory space
The array1d class is a 1D vector container that may contain elements stored in "host" or "device" mem...
Definition array1d.h:99
View of a csr_matrix.
Definition csr_matrix.h:309
csr_matrix_view(const csr_matrix< IndexType, ValueType, MemorySpace > &matrix)
Definition csr_matrix.h:392
csr_matrix_view(const size_t num_rows, const size_t num_cols, const size_t num_entries, ArrayType1 row_offsets, ArrayType2 column_indices, ArrayType3 values)
Definition csr_matrix.h:367
csr_matrix_view(const csr_matrix_view &matrix)
Definition csr_matrix.h:412
values_array_type values
Definition csr_matrix.h:348
csr_matrix_view(csr_matrix_view &matrix)
Definition csr_matrix.h:402
row_offsets_array_type row_offsets
Definition csr_matrix.h:338
void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries)
csr_matrix_view(csr_matrix< IndexType, ValueType, MemorySpace > &matrix)
Definition csr_matrix.h:382
column_indices_array_type column_indices
Definition csr_matrix.h:343
Compressed sparse row (CSR) representation a sparse matrix.
Definition csr_matrix.h:108
void swap(csr_matrix &matrix)
values_array_type values
Definition csr_matrix.h:158
row_offsets_array_type row_offsets
Definition csr_matrix.h:150
column_indices_array_type column_indices
Definition csr_matrix.h:154
csr_matrix(size_t num_rows, size_t num_cols, size_t num_entries)
Definition csr_matrix.h:170
csr_matrix(const MatrixType &matrix)
void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries)
csr_matrix & operator=(const MatrixType &matrix)
array1d_view< Iterator > make_array1d_view(Iterator first, Iterator last)
Definition array1d.h:695
csr_matrix_view< ArrayType1, ArrayType2, ArrayType3 > make_csr_matrix_view(size_t num_rows, size_t num_cols, size_t num_entries, ArrayType1 row_offsets, ArrayType2 column_indices, ArrayType3 values)
Definition csr_matrix.h:449