imate
C++/CUDA Reference
cu_csc_affine_matrix_function.cu
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 <cstddef> // NULL
18 #include <cassert> // assert
19 #include "../_definitions/debugging.h" // ASSERT
20 
21 
22 // =============
23 // constructor 1
24 // =============
25 
28 
29 template <typename DataType>
31  const DataType* A_data_,
32  const LongIndexType* A_indices_,
33  const LongIndexType* A_index_pointer_,
34  const LongIndexType num_rows_,
35  const LongIndexType num_columns_,
36  const int num_gpu_devices_):
37 
38  // Base class constructor
39  cLinearOperator<DataType>(num_rows_, num_columns_),
40 
41  // Initializer list
42  A(A_data_, A_indices_, A_index_pointer_, num_rows_, num_columns_,
43  num_gpu_devices_)
44 {
45  // This constructor is called assuming B is identity
46  this->B_is_identity = true;
47 
48  // When B is identity, the eigenvalues of A+tB are known for any t
49  this->eigenvalue_relation_known = 1;
50 
51  // Set gpu device
53 }
54 
55 
56 // =============
57 // constructor 2
58 // =============
59 
60 template <typename DataType>
62  const DataType* A_data_,
63  const LongIndexType* A_indices_,
64  const LongIndexType* A_index_pointer_,
65  const LongIndexType num_rows_,
66  const LongIndexType num_columns_,
67  const DataType* B_data_,
68  const LongIndexType* B_indices_,
69  const LongIndexType* B_index_pointer_,
70  const int num_gpu_devices_):
71 
72  // Base class constructor
73  cLinearOperator<DataType>(num_rows_, num_columns_),
74 
75  // Initializer list
76  A(A_data_, A_indices_, A_index_pointer_, num_rows_, num_columns_,
77  num_gpu_devices_),
78  B(B_data_, B_indices_, B_index_pointer_, num_rows_, num_columns_,
79  num_gpu_devices_)
80 {
81  // Matrix B is assumed to be non-zero. Check if it is identity or generic
82  if (this->B.is_identity_matrix())
83  {
84  this->B_is_identity = true;
85  this->eigenvalue_relation_known = 1;
86  }
87 
88  // Set gpu device
90 }
91 
92 
93 // ==========
94 // destructor
95 // ==========
96 
97 template <typename DataType>
99 {
100 }
101 
102 
103 // ===
104 // dot
105 // ===
106 
122 
123 template <typename DataType>
125  const DataType* vector,
126  DataType* product)
127 {
128  // Matrix A times vector
129  this->A.dot(vector, product);
130  LongIndexType min_vector_size;
131 
132  // Matrix B times vector to be added to the product
133  if (this->B_is_identity)
134  {
135  // Check parameter is set
136  ASSERT((this->parameters != NULL), "Parameter is not set.");
137 
138  // Find minimum of the number of rows and columns
139  min_vector_size = \
140  (this->num_rows < this->num_columns) ? \
141  this->num_rows : this->num_columns;
142 
143  // Adding input vector to product
144  this->_add_scaled_vector(vector, min_vector_size,
145  this->parameters[0], product);
146  }
147  else
148  {
149  // Check parameter is set
150  ASSERT((this->parameters != NULL), "Parameter is not set.");
151 
152  // Adding parameter times B times input vector to the product
153  this->B.dot_plus(vector, this->parameters[0], product);
154  }
155 }
156 
157 
158 // =============
159 // transpose dot
160 // =============
161 
178 
179 template <typename DataType>
181  const DataType* vector,
182  DataType* product)
183 {
184  // Matrix A times vector
185  this->A.transpose_dot(vector, product);
186  LongIndexType min_vector_size;
187 
188  // Matrix B times vector to be added to the product
189  if (this->B_is_identity)
190  {
191  // Check parameter is set
192  ASSERT((this->parameters != NULL), "Parameter is not set.");
193 
194  // Find minimum of the number of rows and columns
195  min_vector_size = \
196  (this->num_rows < this->num_columns) ? \
197  this->num_rows : this->num_columns;
198 
199  // Adding input vector to product
200  this->_add_scaled_vector(vector, min_vector_size,
201  this->parameters[0], product);
202  }
203  else
204  {
205  // Check parameter is set
206  ASSERT((this->parameters != NULL), "Parameter is not set.");
207 
208  // Adding "parameter * B * input vector" to the product
209  this->B.transpose_dot_plus(vector, this->parameters[0], product);
210  }
211 }
212 
213 
214 // ===============================
215 // Explicit template instantiation
216 // ===============================
217 
218 template class cuCSCAffineMatrixFunction<float>;
219 template class cuCSCAffineMatrixFunction<double>;
Base class for linear operators. This class serves as interface for all derived classes.
FlagType eigenvalue_relation_known
virtual void transpose_dot(const DataType *vector, DataType *product)
Computes the matrix vector product:
cuCSCAffineMatrixFunction(const DataType *A_data_, const LongIndexType *A_indices_, const LongIndexType *A_index_pointer_, const LongIndexType num_rows_, const LongIndexType num_columns_, const int num_gpu_devices_)
Constructor. Matrix B is assumed to be the identity matrix.
virtual void dot(const DataType *vector, DataType *product)
Computes the matrix vector product:
void initialize_cusparse_handle()
Creates a cusparseHandle_t object, if not created already.
#define ASSERT(condition, message)
Definition: debugging.h:20
int LongIndexType
Definition: types.h:60