Fork me on GitHub
 All Classes Files Functions Variables Groups Pages
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/array1d.h>
27 #include <cusp/detail/matrix_base.h>
28 
29 namespace cusp
30 {
31 
33 // forward definition
34 template <typename ArrayType1, typename ArrayType2, typename ArrayType3,
35  typename IndexType, typename ValueType, typename MemorySpace> class coo_matrix_view;
105 template <typename IndexType, typename ValueType, class MemorySpace>
106 class coo_matrix : public cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::coo_format>
107 {
108 private:
109 
110  typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::coo_format> Parent;
111 
112 public:
113 
115  typedef typename cusp::array1d<IndexType, MemorySpace> row_indices_array_type;
116  typedef typename cusp::array1d<IndexType, MemorySpace> column_indices_array_type;
117  typedef typename cusp::array1d<ValueType, MemorySpace> values_array_type;
118 
119  typedef typename cusp::coo_matrix<IndexType, ValueType, MemorySpace> container;
120 
121  typedef typename cusp::coo_matrix_view<
122  typename row_indices_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::coo_matrix_view<
128  typename row_indices_array_type::const_view,
129  typename column_indices_array_type::const_view,
130  typename values_array_type::const_view,
131  IndexType, ValueType, MemorySpace> const_view;
132 
133  typedef view coo_view_type;
134  typedef const_view const const_coo_view_type;
135 
136  template<typename MemorySpace2>
137  struct rebind
138  {
140  };
145  row_indices_array_type row_indices;
146 
149  column_indices_array_type column_indices;
150 
153  values_array_type values;
154 
157  coo_matrix(void) {}
158 
165  coo_matrix(const size_t num_rows, const size_t num_cols, const size_t num_entries)
166  : Parent(num_rows, num_cols, num_entries),
167  row_indices(num_entries),
168  column_indices(num_entries),
169  values(num_entries) {}
170 
175  template <typename MatrixType>
176  coo_matrix(const MatrixType& matrix);
177 
180  void resize(size_t num_rows, size_t num_cols, size_t num_entries);
181 
186  void swap(coo_matrix& matrix);
187 
193  template <typename MatrixType>
194  coo_matrix& operator=(const MatrixType& matrix);
195 
198  void sort_by_row(void);
199 
202  void sort_by_row_and_column(void);
203 
208  bool is_sorted_by_row(void);
209 
214  bool is_sorted_by_row_and_column(void);
215 }; // class coo_matrix
298 template <typename ArrayType1,
299  typename ArrayType2,
300  typename ArrayType3,
301  typename IndexType = typename ArrayType2::value_type,
302  typename ValueType = typename ArrayType3::value_type,
303  typename MemorySpace = typename cusp::minimum_space<
304  typename ArrayType1::memory_space,
305  typename ArrayType2::memory_space,
306  typename ArrayType3::memory_space>::type >
307 class coo_matrix_view : public cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::coo_format>
308 {
309 private:
310 
311  typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::coo_format> Parent;
312 
313 public:
314 
316  typedef ArrayType1 row_indices_array_type;
317  typedef ArrayType2 column_indices_array_type;
318  typedef ArrayType3 values_array_type;
319 
320  typedef typename cusp::coo_matrix<IndexType, ValueType, MemorySpace> container;
323 
324  typedef view coo_view_type;
325  typedef view const const_coo_view_type;
331  row_indices_array_type row_indices;
332 
336  column_indices_array_type column_indices;
337 
341  values_array_type values;
342 
347 
351  coo_matrix_view(void)
352  : Parent() {}
353 
365  coo_matrix_view(const size_t num_rows,
366  const size_t num_cols,
367  const size_t num_entries,
368  ArrayType1 row_indices,
369  ArrayType2 column_indices,
370  ArrayType3 values)
371  : Parent(num_rows, num_cols, num_entries),
372  row_indices(row_indices),
373  column_indices(column_indices),
374  values(values) {}
375 
381  : Parent(matrix),
382  row_indices(matrix.row_indices),
384  values(matrix.values) {}
385 
391  : Parent(matrix),
392  row_indices(matrix.row_indices),
394  values(matrix.values) {}
395 
400  coo_matrix_view(view& matrix)
401  : Parent(matrix),
402  row_indices(matrix.row_indices),
403  column_indices(matrix.column_indices),
404  values(matrix.values),
405  indices(matrix.indices) {}
406 
411  coo_matrix_view(const view& matrix)
412  : Parent(matrix),
413  row_indices(matrix.row_indices),
414  column_indices(matrix.column_indices),
415  values(matrix.values),
416  indices(matrix.indices) {}
417 
423  template<typename MatrixType>
424  coo_matrix_view(MatrixType& matrix);
425 
431  template<typename MatrixType>
432  coo_matrix_view(const MatrixType& matrix);
433 
440  void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries);
441 
444  void sort_by_row(void);
445 
448  void sort_by_row_and_column(void);
449 
454  bool is_sorted_by_row(void);
455 
460  bool is_sorted_by_row_and_column(void);
461 
462 protected:
467  template<typename MatrixType>
468  void construct_from(MatrixType& matrix, csr_format);
469 
474  template<typename MatrixType>
475  void construct_from(MatrixType& matrix, dia_format);
476 
481  template<typename MatrixType>
482  void construct_from(MatrixType& matrix, ell_format);
483 
488  template<typename MatrixType>
489  void construct_from(MatrixType& matrix, hyb_format);
490 };
491 
492 /* Convenience functions */
493 
510 template <typename ArrayType1,
511  typename ArrayType2,
512  typename ArrayType3>
514 make_coo_matrix_view(const size_t num_rows,
515  const size_t num_cols,
516  const size_t num_entries,
517  ArrayType1 row_indices,
518  ArrayType2 column_indices,
519  ArrayType3 values)
520 {
522  view(num_rows, num_cols, num_entries, row_indices, column_indices, values);
523 
524  return view;
525 }
526 
543 template <typename ArrayType1,
544  typename ArrayType2,
545  typename ArrayType3,
546  typename IndexType,
547  typename ValueType,
548  typename MemorySpace>
551 {
553 }
554 
565 template <typename MatrixType>
566 typename MatrixType::coo_view_type
567 make_coo_matrix_view(MatrixType& m)
568 {
569  typedef typename MatrixType::coo_view_type View;
570 
571  return View(m);
572 }
573 
584 template <typename MatrixType>
585 typename MatrixType::const_coo_view_type
586 make_coo_matrix_view(const MatrixType& m)
587 {
588  typedef typename MatrixType::const_coo_view_type View;
589 
590  return View(m);
591 }
596 } // end namespace cusp
597 
598 #include <cusp/detail/coo_matrix.inl>
The array1d class is a 1D vector container that may contain elements stored in "host" or "device" mem...
Definition: array1d.h:98
column_indices_array_type column_indices
Definition: coo_matrix.h:149
View of a coo_matrix.
Definition: coo_matrix.h:306
values_array_type values
Definition: coo_matrix.h:153
void swap(coo_matrix &matrix)
MatrixType::const_coo_view_type make_coo_matrix_view(const MatrixType &m)
Definition: coo_matrix.h:585
bool is_sorted_by_row_and_column(void)
bool is_sorted_by_row(void)
coo_matrix(const size_t num_rows, const size_t num_cols, const size_t num_entries)
Definition: coo_matrix.h:165
void sort_by_row_and_column(void)
column_indices_array_type column_indices
Definition: coo_matrix.h:335
Coordinate (COO) representation a sparse matrix.
Definition: coo_matrix.h:106
void sort_by_row_and_column(void)
row_indices_array_type row_indices
Definition: coo_matrix.h:330
void construct_from(MatrixType &matrix, csr_format)
bool is_sorted_by_row_and_column(void)
row_indices_array_type row_indices
Definition: coo_matrix.h:145
values_array_type values
Definition: coo_matrix.h:340
coo_matrix & operator=(const MatrixType &matrix)
void resize(size_t num_rows, size_t num_cols, size_t num_entries)
1D array of elements that may reside in "host" or "device" memory space
bool is_sorted_by_row(void)
cusp::array1d< typename thrust::detail::remove_const< IndexType >::type, MemorySpace > indices
Definition: coo_matrix.h:345
void resize(const size_t num_rows, const size_t num_cols, const size_t num_entries)
void sort_by_row(void)