imate
C++/CUDA Reference
c_csc_matrix.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright 2021, Siavash Ameli <sameli@berkeley.edu>
3  * SPDX-License-Identifier: BSD-3-Clause
4  * SPDX-FileType: SOURCE
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the license found in the LICENSE.txt file in the root
8  * directory of this source tree.
9  */
10 
11 
12 // =======
13 // Headers
14 // =======
15 
16 #include "./c_csc_matrix.h"
17 #include <cstddef> // NULL
18 #include "../_c_basic_algebra/c_matrix_operations.h" // cMatrixOperations
19 
20 
21 // =============
22 // constructor 1
23 // =============
24 
25 template <typename DataType>
27  A_data(NULL),
28  A_indices(NULL),
29  A_index_pointer(NULL)
30 {
31 }
32 
33 
34 // =============
35 // constructor 2
36 // =============
37 
38 template <typename DataType>
40  const DataType* A_data_,
41  const LongIndexType* A_indices_,
42  const LongIndexType* A_index_pointer_,
43  const LongIndexType num_rows_,
44  const LongIndexType num_columns_):
45 
46  // Base class constructor
47  cLinearOperator<DataType>(num_rows_, num_columns_),
48 
49  // Initializer list
50  A_data(A_data_),
51  A_indices(A_indices_),
52  A_index_pointer(A_index_pointer_)
53 {
54 }
55 
56 
57 // ==========
58 // destructor
59 // ==========
60 
61 template <typename DataType>
63 {
64 }
65 
66 
67 // ==================
68 // is identity matrix
69 // ==================
70 
79 
80 template <typename DataType>
82 {
83  FlagType matrix_is_identity = 1;
84 
85  LongIndexType index_pointer;
86  LongIndexType row;
87 
88  // Check matrix element-wise
89  for (LongIndexType column=0; column < this->num_columns; ++column)
90  {
91  for (index_pointer=this->A_index_pointer[column];
92  index_pointer < this->A_index_pointer[column+1];
93  ++index_pointer)
94  {
95  row = this->A_indices[index_pointer];
96 
97  if ((row == column) && \
98  (this->A_data[index_pointer] != 1.0))
99  {
100  matrix_is_identity = 0;
101  return matrix_is_identity;
102  }
103  else if (this->A_data[index_pointer] != 0.0)
104  {
105  matrix_is_identity = 0;
106  return matrix_is_identity;
107  }
108  }
109  }
110 
111  return matrix_is_identity;
112 }
113 
114 
115 // =======
116 // get nnz
117 // =======
118 
126 
127 template <typename DataType>
129 {
130  return this->A_index_pointer[this->num_columns];
131 }
132 
133 
134 // ===
135 // dot
136 // ===
137 
138 template <typename DataType>
140  const DataType* vector,
141  DataType* product)
142 {
144  this->A_data,
145  this->A_indices,
146  this->A_index_pointer,
147  vector,
148  this->num_rows,
149  this->num_columns,
150  product);
151 }
152 
153 
154 // ========
155 // dot plus
156 // ========
157 
158 template <typename DataType>
160  const DataType* vector,
161  const DataType alpha,
162  DataType* product)
163 {
165  this->A_data,
166  this->A_indices,
167  this->A_index_pointer,
168  vector,
169  alpha,
170  this->num_columns,
171  product);
172 }
173 
174 
175 // =============
176 // transpose dot
177 // =============
178 
179 template <typename DataType>
181  const DataType* vector,
182  DataType* product)
183 {
185  this->A_data,
186  this->A_indices,
187  this->A_index_pointer,
188  vector,
189  this->num_columns,
190  product);
191 }
192 
193 
194 // ==================
195 // transpose dot plus
196 // ==================
197 
198 template <typename DataType>
200  const DataType* vector,
201  const DataType alpha,
202  DataType* product)
203 {
205  this->A_data,
206  this->A_indices,
207  this->A_index_pointer,
208  vector,
209  alpha,
210  this->num_columns,
211  product);
212 }
213 
214 
215 // ===============================
216 // Explicit template instantiation
217 // ===============================
218 
219 template class cCSCMatrix<float>;
220 template class cCSCMatrix<double>;
221 template class cCSCMatrix<long double>;
LongIndexType get_nnz() const
Returns the number of non-zero elements of the sparse matrix.
virtual void transpose_dot(const DataType *vector, DataType *product)
virtual ~cCSCMatrix()
virtual void dot(const DataType *vector, DataType *product)
virtual FlagType is_identity_matrix() const
Checks whether the matrix is identity.
virtual void dot_plus(const DataType *vector, const DataType alpha, DataType *product)
virtual void transpose_dot_plus(const DataType *vector, const DataType alpha, DataType *product)
Base class for linear operators. This class serves as interface for all derived classes.
static void csc_transposed_matvec_plus(const DataType *A_data, const LongIndexType *A_row_indices, const LongIndexType *A_index_pointer, const DataType *b, const DataType alpha, const LongIndexType num_columns, DataType *c)
Computes where is compressed sparse column (CSC) matrix and is a dense vector. The output is a de...
static void csc_transposed_matvec(const DataType *A_data, const LongIndexType *A_row_indices, const LongIndexType *A_index_pointer, const DataType *b, const LongIndexType num_columns, DataType *c)
Computes where is compressed sparse column (CSC) matrix and is a dense vector. The output is a de...
static void csc_matvec(const DataType *A_data, const LongIndexType *A_row_indices, const LongIndexType *A_index_pointer, const DataType *b, const LongIndexType num_rows, const LongIndexType num_columns, DataType *c)
Computes where is compressed sparse column (CSC) matrix and is a dense vector. The output is a de...
static void csc_matvec_plus(const DataType *A_data, const LongIndexType *A_row_indices, const LongIndexType *A_index_pointer, const DataType *b, const DataType alpha, const LongIndexType num_columns, DataType *c)
Computes where is compressed sparse column (CSC) matrix and is a dense vector. The output is a de...
int LongIndexType
Definition: types.h:60
int FlagType
Definition: types.h:68