CUSP
Loading...
Searching...
No Matches
coo_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/detail/format.h>
26#include <cusp/detail/matrix_base.h>
27
28#include <cusp/array1d.h>
29
30namespace cusp
31{
32namespace detail
33{
34template<typename RowArray, typename ColumnArray, typename ValueArray, typename FormatTag = void>
36}
37
39// forward definition
40template <typename ArrayType1,
41 typename ArrayType2,
42 typename ArrayType3,
43 typename IndexType,
44 typename ValueType,
45 typename MemorySpace> class coo_matrix_view;
115template <typename IndexType, typename ValueType, class MemorySpace>
116class coo_matrix : public cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::coo_format>
117{
118private:
119
120 typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::coo_format> Parent;
121
122public:
123
125 typedef typename cusp::array1d<IndexType, MemorySpace> row_indices_array_type;
126 typedef typename cusp::array1d<IndexType, MemorySpace> column_indices_array_type;
127 typedef typename cusp::array1d<ValueType, MemorySpace> values_array_type;
128
130
131 typedef typename cusp::coo_matrix_view<
132 typename row_indices_array_type::view,
133 typename column_indices_array_type::view,
134 typename values_array_type::view,
135 IndexType, ValueType, MemorySpace> view;
136
137 typedef typename cusp::coo_matrix_view<
138 typename row_indices_array_type::const_view,
139 typename column_indices_array_type::const_view,
140 typename values_array_type::const_view,
141 IndexType, ValueType, MemorySpace> const_view;
142
143 typedef view coo_view_type;
144 typedef const_view const const_coo_view_type;
145
146 template<typename MemorySpace2>
147 struct rebind
148 {
150 };
155 row_indices_array_type row_indices;
156
159 column_indices_array_type column_indices;
160
163 values_array_type values;
164
167 coo_matrix(void) {}
168
175 coo_matrix(const size_t num_rows, const size_t num_cols, const size_t num_entries)
176 : Parent(num_rows, num_cols, num_entries),
177 row_indices(num_entries),
178 column_indices(num_entries),
179 values(num_entries) {}
180
185 template <typename MatrixType>
186 coo_matrix(const MatrixType& matrix);
187
190 void resize(size_t num_rows, size_t num_cols, size_t num_entries);
191
196 void swap(coo_matrix& matrix);
197
203 template <typename MatrixType>
204 coo_matrix& operator=(const MatrixType& matrix);
205
208 void sort_by_row(void);
209
213
219
225}; // class coo_matrix
308template <typename ArrayType1,
309 typename ArrayType2,
310 typename ArrayType3,
311 typename IndexType = typename ArrayType2::value_type,
312 typename ValueType = typename ArrayType3::value_type,
313 typename MemorySpace = typename cusp::minimum_space<
314 typename ArrayType1::memory_space,
315 typename ArrayType2::memory_space,
316 typename ArrayType3::memory_space>::type >
317class coo_matrix_view : public cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::coo_format>
318{
319private:
320
321 typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::coo_format> Parent;
322
323public:
324
326 typedef ArrayType1 row_indices_array_type;
327 typedef ArrayType2 column_indices_array_type;
328 typedef ArrayType3 values_array_type;
329
333
334 typedef view coo_view_type;
335 typedef view const const_coo_view_type;
341 row_indices_array_type row_indices;
342
346 column_indices_array_type column_indices;
347
351 values_array_type values;
352
357
362 : Parent() {}
363
375 coo_matrix_view(const size_t num_rows,
376 const size_t num_cols,
377 const size_t num_entries,
378 ArrayType1 row_indices,
379 ArrayType2 column_indices,
380 ArrayType3 values)
381 : Parent(num_rows, num_cols, num_entries),
384 values(values) {}
385
395
401 : Parent(matrix),
402 row_indices(matrix.row_indices),
404 values(matrix.values) {}
405
410 coo_matrix_view(view& matrix)
411 : Parent(matrix),
412 row_indices(matrix.row_indices),
414 values(matrix.values),
415 indices(matrix.indices) {}
416
421 coo_matrix_view(const view& matrix)
422 : Parent(matrix),
423 row_indices(matrix.row_indices),
425 values(matrix.values),
426 indices(matrix.indices) {}
427
433 template<typename MatrixType>
434 coo_matrix_view(MatrixType& matrix);
435
441 template<typename MatrixType>
442 coo_matrix_view(const MatrixType& matrix);
443
450 void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries);
451
454 void sort_by_row(void);
455
459
465
471
472protected:
477 template<typename MatrixType>
478 void construct_from(MatrixType& matrix, csr_format);
479
484 template<typename MatrixType>
485 void construct_from(MatrixType& matrix, dia_format);
486
491 template<typename MatrixType>
492 void construct_from(MatrixType& matrix, ell_format);
493
498 template<typename MatrixType>
499 void construct_from(MatrixType& matrix, hyb_format);
500};
501
502/* Convenience functions */
503
520template <typename ArrayType1,
521 typename ArrayType2,
522 typename ArrayType3>
524make_coo_matrix_view(const size_t num_rows,
525 const size_t num_cols,
526 const size_t num_entries,
527 ArrayType1 row_indices,
528 ArrayType2 column_indices,
529 ArrayType3 values)
530{
532 view(num_rows, num_cols, num_entries, row_indices, column_indices, values);
533
534 return view;
535}
536
553template <typename ArrayType1,
554 typename ArrayType2,
555 typename ArrayType3,
556 typename IndexType,
557 typename ValueType,
558 typename MemorySpace>
559coo_matrix_view<ArrayType1,ArrayType2,ArrayType3,IndexType,ValueType,MemorySpace>
564
575template <typename MatrixType>
576typename MatrixType::coo_view_type
578{
579 typedef typename MatrixType::coo_view_type View;
580
581 return View(m);
582}
583
594template <typename MatrixType>
595typename MatrixType::const_coo_view_type
596make_coo_matrix_view(const MatrixType& m)
597{
598 typedef typename MatrixType::const_coo_view_type View;
599
600 return View(m);
601}
606} // end namespace cusp
607
608#include <cusp/detail/type_traits.h>
609
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 coo_matrix.
Definition coo_matrix.h:318
void construct_from(MatrixType &matrix, hyb_format)
void construct_from(MatrixType &matrix, dia_format)
coo_matrix_view(MatrixType &matrix)
coo_matrix_view(view &matrix)
Definition coo_matrix.h:410
coo_matrix_view(const view &matrix)
Definition coo_matrix.h:421
coo_matrix_view(const coo_matrix< IndexType, ValueType, MemorySpace > &matrix)
Definition coo_matrix.h:400
bool is_sorted_by_row_and_column(void)
void sort_by_row_and_column(void)
row_indices_array_type row_indices
Definition coo_matrix.h:341
coo_matrix_view(coo_matrix< IndexType, ValueType, MemorySpace > &matrix)
Definition coo_matrix.h:390
coo_matrix_view(const size_t num_rows, const size_t num_cols, const size_t num_entries, ArrayType1 row_indices, ArrayType2 column_indices, ArrayType3 values)
Definition coo_matrix.h:375
void construct_from(MatrixType &matrix, ell_format)
cusp::array1d< typename ::cuda::std::remove_const< IndexType >::type, MemorySpace > indices
Definition coo_matrix.h:356
void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries)
void construct_from(MatrixType &matrix, csr_format)
values_array_type values
Definition coo_matrix.h:351
column_indices_array_type column_indices
Definition coo_matrix.h:346
coo_matrix_view(const MatrixType &matrix)
bool is_sorted_by_row(void)
Coordinate (COO) representation a sparse matrix.
Definition coo_matrix.h:117
void sort_by_row(void)
void resize(size_t num_rows, size_t num_cols, size_t num_entries)
values_array_type values
Definition coo_matrix.h:163
coo_matrix & operator=(const MatrixType &matrix)
coo_matrix(const MatrixType &matrix)
bool is_sorted_by_row_and_column(void)
void sort_by_row_and_column(void)
bool is_sorted_by_row(void)
void swap(coo_matrix &matrix)
column_indices_array_type column_indices
Definition coo_matrix.h:159
row_indices_array_type row_indices
Definition coo_matrix.h:155
coo_matrix(const size_t num_rows, const size_t num_cols, const size_t num_entries)
Definition coo_matrix.h:175
coo_matrix_view< ArrayType1, ArrayType2, ArrayType3 > make_coo_matrix_view(const size_t num_rows, const size_t num_cols, const size_t num_entries, ArrayType1 row_indices, ArrayType2 column_indices, ArrayType3 values)
Definition coo_matrix.h:524