imate
C++/CUDA Reference
c_csr_affine_matrix_function.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 
17 #include <cassert>
18 #include <cstddef> // NULL
19 
20 
21 // =============
22 // constructor 1
23 // =============
24 
27 
28 template <typename DataType>
30  const DataType* A_data_,
31  const LongIndexType* A_indices_,
32  const LongIndexType* A_index_pointer_,
33  const LongIndexType num_rows_,
34  const LongIndexType num_columns_):
35 
36  // Base class constructor
37  cAffineMatrixFunction<DataType>(num_rows_, num_columns_),
38 
39  // Initializer list
40  A(A_data_, A_indices_, A_index_pointer_, num_rows_, num_columns_)
41 {
42  // This constructor is called assuming B is identity
43  this->B_is_identity = true;
44 
45  // When B is identity, the eigenvalues of A+tB are known for any t
46  this->eigenvalue_relation_known = 1;
47 }
48 
49 
50 // =============
51 // constructor 2
52 // =============
53 
54 template <typename DataType>
56  const DataType* A_data_,
57  const LongIndexType* A_indices_,
58  const LongIndexType* A_index_pointer_,
59  const LongIndexType num_rows_,
60  const LongIndexType num_columns_,
61  const DataType* B_data_,
62  const LongIndexType* B_indices_,
63  const LongIndexType* B_index_pointer_):
64 
65  // Base class constructor
66  cAffineMatrixFunction<DataType>(num_rows_, num_columns_),
67 
68  // Initializer list
69  A(A_data_, A_indices_, A_index_pointer_, num_rows_, num_columns_),
70  B(B_data_, B_indices_, B_index_pointer_, num_rows_, num_columns_)
71 {
72  // Matrix B is assumed to be non-zero. Check if it is identity or generic
73  if (this->B.is_identity_matrix())
74  {
75  this->B_is_identity = true;
76  this->eigenvalue_relation_known = 1;
77  }
78 }
79 
80 
81 // ==========
82 // destructor
83 // ==========
84 
85 template <typename DataType>
87 {
88 }
89 
90 
91 // ===
92 // dot
93 // ===
94 
110 
111 template <typename DataType>
113  const DataType* vector,
114  DataType* product)
115 {
116  // Matrix A times vector
117  this->A.dot(vector, product);
118  LongIndexType min_vector_size;
119 
120  // Matrix B times vector to be added to the product
121  if (this->B_is_identity)
122  {
123  // Check parameter is set
124  assert((this->parameters != NULL) && "Parameter is not set.");
125 
126  // Find minimum of the number of rows and columns
127  min_vector_size = \
128  (this->num_rows < this->num_columns) ? \
129  this->num_rows : this->num_columns;
130 
131  // Adding input vector to product
132  this->_add_scaled_vector(vector, min_vector_size,
133  this->parameters[0], product);
134  }
135  else
136  {
137  // Check parameter is set
138  assert((this->parameters != NULL) && "Parameter is not set.");
139 
140  // Adding parameter times B times input vector to the product
141  this->B.dot_plus(vector, this->parameters[0], product);
142  }
143 }
144 
145 
146 // =============
147 // transpose dot
148 // =============
149 
166 
167 template <typename DataType>
169  const DataType* vector,
170  DataType* product)
171 {
172  // Matrix A times vector
173  this->A.transpose_dot(vector, product);
174  LongIndexType min_vector_size;
175 
176  // Matrix B times vector to be added to the product
177  if (this->B_is_identity)
178  {
179  // Check parameter is set
180  assert((this->parameters != NULL) && "Parameter is not set.");
181 
182  // Find minimum of the number of rows and columns
183  min_vector_size = \
184  (this->num_rows < this->num_columns) ? \
185  this->num_rows : this->num_columns;
186 
187  // Adding input vector to product
188  this->_add_scaled_vector(vector, min_vector_size,
189  this->parameters[0], product);
190  }
191  else
192  {
193  // Check parameter is set
194  assert((this->parameters != NULL) && "Parameter is not set.");
195 
196  // Adding "parameter * B * input vector" to the product
197  this->B.transpose_dot_plus(vector, this->parameters[0], product);
198  }
199 }
200 
201 
202 // ===============================
203 // Explicit template instantiation
204 // ===============================
205 
206 template class cCSRAffineMatrixFunction<float>;
207 template class cCSRAffineMatrixFunction<double>;
Base class for affine matrix functions of one parameter.
virtual void dot(const DataType *vector, DataType *product)
Computes the matrix vector product:
virtual void transpose_dot(const DataType *vector, DataType *product)
Computes the matrix vector product:
cCSRAffineMatrixFunction(const DataType *A_data_, const LongIndexType *A_indices_, const LongIndexType *A_index_pointer_, const LongIndexType num_rows_, const LongIndexType num_columns_)
Constructor. Matrix B is assumed to be the identity matrix.
FlagType eigenvalue_relation_known
int LongIndexType
Definition: types.h:60