imate
C++/CUDA Reference
cu_dense_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_,
32  const FlagType A_is_row_major_,
33  const LongIndexType num_rows_,
34  const LongIndexType num_columns_,
35  const int num_gpu_devices_):
36 
37  // Base class constructor
38  cLinearOperator<DataType>(num_rows_, num_columns_),
39 
40  // Initializer list
41  A(A_, num_rows_, num_columns_, A_is_row_major_, num_gpu_devices_)
42 {
43  // This constructor is called assuming B is identity
44  this->B_is_identity = true;
45 
46  // When B is identity, the eigenvalues of A+tB are known for any t
47  this->eigenvalue_relation_known = 1;
48 
49  // Set gpu device
51 }
52 
53 
54 // =============
55 // constructor 2
56 // =============
57 
58 template <typename DataType>
60  const DataType* A_,
61  const FlagType A_is_row_major_,
62  const LongIndexType num_rows_,
63  const LongIndexType num_columns_,
64  const DataType* B_,
65  const FlagType B_is_row_major_,
66  const int num_gpu_devices_):
67 
68  // Base class constructor
69  cLinearOperator<DataType>(num_rows_, num_columns_),
70 
71  // Initializer list
72  A(A_, num_rows_, num_columns_, A_is_row_major_, num_gpu_devices_),
73  B(B_, num_rows_, num_columns_, B_is_row_major_, num_gpu_devices_)
74 {
75  // Matrix B is assumed to be non-zero. Check if it is identity or generic
76  if (this->B.is_identity_matrix())
77  {
78  this->B_is_identity = true;
79  this->eigenvalue_relation_known = 1;
80  }
81 
82  // Set gpu device
84 }
85 
86 
87 // ==========
88 // destructor
89 // ==========
90 
91 template <typename DataType>
93 {
94 }
95 
96 
97 // ===
98 // dot
99 // ===
100 
116 
117 template <typename DataType>
119  const DataType* vector,
120  DataType* product)
121 {
122  // Matrix A times vector
123  this->A.dot(vector, product);
124  LongIndexType min_vector_size;
125 
126  // Matrix B times vector to be added to the product
127  if (this->B_is_identity)
128  {
129  // Check parameter is set
130  ASSERT((this->parameters != NULL), "Parameter is not set.");
131 
132  // Find minimum of the number of rows and columns
133  min_vector_size = \
134  (this->num_rows < this->num_columns) ? \
135  this->num_rows : this->num_columns;
136 
137  // Adding input vector to product
138  this->_add_scaled_vector(vector, min_vector_size,
139  this->parameters[0], product);
140  }
141  else
142  {
143  // Check parameter is set
144  ASSERT((this->parameters != NULL), "Parameter is not set.");
145 
146  // Adding parameter times B times input vector to the product
147  this->B.dot_plus(vector, this->parameters[0], product);
148  }
149 }
150 
151 
152 // =============
153 // transpose dot
154 // =============
155 
172 
173 template <typename DataType>
175  const DataType* vector,
176  DataType* product)
177 {
178  // Matrix A times vector
179  this->A.transpose_dot(vector, product);
180  LongIndexType min_vector_size;
181 
182  // Matrix B times vector to be added to the product
183  if (this->B_is_identity)
184  {
185  // Check parameter is set
186  ASSERT((this->parameters != NULL), "Parameter is not set.");
187 
188  // Find minimum of the number of rows and columns
189  min_vector_size = \
190  (this->num_rows < this->num_columns) ? \
191  this->num_rows : this->num_columns;
192 
193  // Adding input vector to product
194  this->_add_scaled_vector(vector, min_vector_size,
195  this->parameters[0], product);
196  }
197  else
198  {
199  // Check parameter is set
200  ASSERT((this->parameters != NULL), "Parameter is not set.");
201 
202  // Adding "parameter * B * input vector" to the product
203  this->B.transpose_dot_plus(vector, this->parameters[0], product);
204  }
205 }
206 
207 
208 // ===============================
209 // Explicit template instantiation
210 // ===============================
211 
Base class for linear operators. This class serves as interface for all derived classes.
FlagType eigenvalue_relation_known
virtual void dot(const DataType *vector, DataType *product)
Computes the matrix vector product:
cuDenseAffineMatrixFunction(const DataType *A_, const FlagType A_is_row_major_, const LongIndexType num_rows_, const LongIndexType num_colums_, const int num_gpu_devices_)
Constructor. Matrix B is assumed to be the identity matrix.
virtual void transpose_dot(const DataType *vector, DataType *product)
Computes the matrix vector product:
void initialize_cublas_handle()
Creates a cublasHandle_t object, if not created already.
#define ASSERT(condition, message)
Definition: debugging.h:20
int LongIndexType
Definition: types.h:60
int FlagType
Definition: types.h:68