imate
C++/CUDA Reference
Loading...
Searching...
No Matches
cuLinearOperator< DataType > Class Template Referenceabstract

Base class for linear operators. This class serves as interface for all derived classes. More...

#include <cu_linear_operator.h>

Inheritance diagram for cuLinearOperator< DataType >:
Collaboration diagram for cuLinearOperator< DataType >:

Public Member Functions

 cuLinearOperator ()
 Default constructor.
 
 cuLinearOperator (const int num_gpu_devices_)
 Constructor with setting num_rows and num_columns.
 
virtual ~cuLinearOperator ()
 Destructor.
 
cublasHandle_t get_cublas_handle () const
 This function returns a reference to the cublasHandle_t object. The object will be created, if it is not created already.
 
void set_parameters (DataType *parameters_)
 Sets the scalar parameter this->parameters. Parameter is initialized to NULL. However, before calling dot or transpose_dot functions, the parameters must be set.
 
virtual DataType get_eigenvalue (const DataType *known_parameters, const DataType known_eigenvalue, const DataType *inquiry_parameters) const =0
 
virtual void dot (const DataType *vector, DataType *product)=0
 
virtual void transpose_dot (const DataType *vector, DataType *product)=0
 
- Public Member Functions inherited from cLinearOperatorBase
 cLinearOperatorBase ()
 Default constructor.
 
 cLinearOperatorBase (const LongIndexType num_rows_, const LongIndexType num_columns_)
 Constructor with setting num_rows and num_columns.
 
virtual ~cLinearOperatorBase ()
 Destructor.
 
LongIndexType get_num_rows () const
 Returns the number of rows of the matrix.
 
LongIndexType get_num_columns () const
 Returns the number of columns of the matrix.
 
IndexType get_num_parameters () const
 Returns the number of parameters of the linear operator.
 
FlagType is_eigenvalue_relation_known () const
 Returns a flag that determines whether a relation between the parameters of the operator and its eigenvalue(s) is known.
 
virtual void set_symmetry (FlagType symmetric)=0
 

Protected Member Functions

int query_gpu_devices () const
 Before any numerical computation, this method chechs if any gpu device is available on the machine, or notifies the user if nothing was found.
 
void initialize_cublas_handle ()
 Creates a cublasHandle_t object, if not created already.
 
void initialize_cusparse_handle ()
 Creates a cusparseHandle_t object, if not created already.
 

Protected Attributes

int num_gpu_devices
 
bool copied_host_to_device
 
cublasHandle_t * cublas_handle
 
cusparseHandle_t * cusparse_handle
 
DataType * parameters
 
- Protected Attributes inherited from cLinearOperatorBase
const LongIndexType num_rows
 
const LongIndexType num_columns
 
FlagType eigenvalue_relation_known
 
IndexType num_parameters
 

Detailed Description

template<typename DataType>
class cuLinearOperator< DataType >

Base class for linear operators. This class serves as interface for all derived classes.

The prefix c in this class's name (and its derivatves), stands for the cpp code, intrast to the cu prefix, which stands for the cuda code. Most derived classes have a cuda counterpart.

See also
cuMatrix, cuAffineMatrixFunction, cLinearOperator cLinearOperatorBase

Definition at line 63 of file cu_linear_operator.h.

Constructor & Destructor Documentation

◆ cuLinearOperator() [1/2]

template<typename DataType >
cuLinearOperator< DataType >::cuLinearOperator ( )

Default constructor.

Definition at line 40 of file cu_linear_operator.cu.

40 :
41
42 // Initializer list
45 cublas_handle(NULL),
46 cusparse_handle(NULL)
47{
48 // Check any gpu device exists
49 this->num_gpu_devices = this->query_gpu_devices();
50
51 // Regardless of using dense (cublas) or sparse (cusparse) matrices, the
52 // cublas handle should be initialized, since it is needed for the methods
53 // in cuVectorOperations
55}
cublasHandle_t * cublas_handle
int query_gpu_devices() const
Before any numerical computation, this method chechs if any gpu device is available on the machine,...
cusparseHandle_t * cusparse_handle
void initialize_cublas_handle()
Creates a cublasHandle_t object, if not created already.

References cuLinearOperator< DataType >::initialize_cublas_handle(), cuLinearOperator< DataType >::num_gpu_devices, and cuLinearOperator< DataType >::query_gpu_devices().

Here is the call graph for this function:

◆ cuLinearOperator() [2/2]

template<typename DataType >
cuLinearOperator< DataType >::cuLinearOperator ( const int  num_gpu_devices_)
explicit

Constructor with setting num_rows and num_columns.

Note
For the classed that are virtually derived (virtual inheritance) from this class, this constructor will never be called. Rather, the default constructor is called by the most derived class. Thus, set the member data directly instead of below.
Parameters
[in]num_gpu_devices_Number of GPU devices to used for parallelization.

Definition at line 73 of file cu_linear_operator.cu.

74 :
75
76 // Initializer list
79 cublas_handle(NULL),
80 cusparse_handle(NULL)
81{
82 // Check any gpu device exists
83 int device_count = this->query_gpu_devices();
84
85 // Set number of gpu devices
86 if (num_gpu_devices_ == 0)
87 {
88 this->num_gpu_devices = device_count;
89 }
90 else if (num_gpu_devices_ > device_count)
91 {
92 std::cerr << "ERROR: Number of requested gpu devices exceeds the " \
93 << "number of available gpu devices. Nummber of detected " \
94 << "devices are " << device_count << " while the " \
95 << "requested number of devices are " << num_gpu_devices_ \
96 << "." << std::endl;
97 abort();
98 }
99 else
100 {
101 this->num_gpu_devices = num_gpu_devices_;
102 }
103
104 // Regardless of using dense (cublas) or sparse (cusparse) matrices, the
105 // cublas handle should be initialized, since it is needed for the methods
106 // in cuVectorOperations
108}

References cuLinearOperator< DataType >::initialize_cublas_handle(), cuLinearOperator< DataType >::num_gpu_devices, and cuLinearOperator< DataType >::query_gpu_devices().

Here is the call graph for this function:

◆ ~cuLinearOperator()

template<typename DataType >
cuLinearOperator< DataType >::~cuLinearOperator ( )
virtual

Destructor.

This function offloads data from GPUs.

Definition at line 120 of file cu_linear_operator.cu.

121{
122 // cublas handle
123 if (this->cublas_handle != NULL)
124 {
125 // Set the number of threads
126 #if defined(USE_OPENMP) && (USE_OPENMP == 1)
128 #endif
129
130 #if defined(USE_OPENMP) && (USE_OPENMP == 1)
131 #pragma omp parallel
132 #endif
133 {
134 // Switch to a device with the same device id as the cpu thread id
135 unsigned int thread_id;
136 #if defined(USE_OPENMP) && (USE_OPENMP == 1)
137 thread_id = omp_get_thread_num();
138 #else
139 thread_id = 0;
140 #endif
141
143
144 cublasStatus_t status = cublasDestroy(
145 this->cublas_handle[thread_id]);
146 assert(status == CUBLAS_STATUS_SUCCESS);
147 }
148
149 // Deallocate arrays of pointers on cpu
150 delete[] this->cublas_handle;
151 this->cublas_handle = NULL;
152 }
153
154 // cusparse handle
155 if (this->cusparse_handle != NULL)
156 {
157 // Set the number of threads
158 #if defined(USE_OPENMP) && (USE_OPENMP == 1)
160 #endif
161
162 #if defined(USE_OPENMP) && (USE_OPENMP == 1)
163 #pragma omp parallel
164 #endif
165 {
166 // Switch to a device with the same device id as the cpu thread id
167 unsigned int thread_id;
168 #if defined(USE_OPENMP) && (USE_OPENMP == 1)
169 thread_id = omp_get_thread_num();
170 #else
171 thread_id = 0;
172 #endif
173
175
176 cusparseStatus_t status = cusparseDestroy(
177 this->cusparse_handle[thread_id]);
178 assert(status == CUSPARSE_STATUS_SUCCESS);
179 }
180
181 // Deallocate arrays of pointers on cpu
182 delete[] this->cusparse_handle;
183 this->cusparse_handle = NULL;
184 }
185}
static void set_device(int device_id)
Sets the current device in multi-gpu applications.
Definition cuda_api.cu:191
void omp_set_num_threads(int num_threads)
int omp_get_thread_num()
cusparseStatus_t cusparseDestroy(cusparseHandle_t handle)
Definition of CUDA's cusparseDestroy function using dynamically loaded cublas library.

References cusparseDestroy(), omp_get_thread_num(), omp_set_num_threads(), and CudaAPI< ArrayType >::set_device().

Here is the call graph for this function:

Member Function Documentation

◆ dot()

template<typename DataType >
virtual void cuLinearOperator< DataType >::dot ( const DataType *  vector,
DataType *  product 
)
pure virtual

◆ get_cublas_handle()

template<typename DataType >
cublasHandle_t cuLinearOperator< DataType >::get_cublas_handle ( ) const

This function returns a reference to the cublasHandle_t object. The object will be created, if it is not created already.

The cublasHandle is needed for the client code (slq method) for vector operations on GPU. However, in this class, the cublasHandle_t might not be needed by it self if the derived class is a sparse matrix, becase the sparse matrix needs only cusparseHandle_t. In case if the cublasHandle_t is not created, it will be created for the purpose of the client codes.

Returns
A void pointer to the cublasHandle_t instance.

Definition at line 205 of file cu_linear_operator.cu.

206{
207 // Get device id
208 int device_id = CudaAPI<DataType>::get_device();
209
210 return this->cublas_handle[device_id];
211}
static int get_device()
Gets the current device in multi-gpu applications.
Definition cuda_api.cu:209

References CudaAPI< ArrayType >::get_device().

Referenced by cu_golub_kahn_bidiagonalization(), and cu_lanczos_tridiagonalization().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_eigenvalue()

template<typename DataType >
virtual DataType cuLinearOperator< DataType >::get_eigenvalue ( const DataType *  known_parameters,
const DataType  known_eigenvalue,
const DataType *  inquiry_parameters 
) const
pure virtual

Implemented in cuAffineMatrixFunction< DataType >, and cuMatrix< DataType >.

Referenced by cuTraceEstimator< DataType >::_cu_stochastic_lanczos_quadrature().

Here is the caller graph for this function:

◆ initialize_cublas_handle()

template<typename DataType >
void cuLinearOperator< DataType >::initialize_cublas_handle ( )
protected

Creates a cublasHandle_t object, if not created already.

Definition at line 222 of file cu_linear_operator.cu.

223{
224 if (this->cublas_handle == NULL)
225 {
226 // Allocate pointers for each gpu device
227 this->cublas_handle = new cublasHandle_t[this->num_gpu_devices];
228
229 // Set the number of threads
230 #if defined(USE_OPENMP) && (USE_OPENMP == 1)
232 #endif
233
234 #if defined(USE_OPENMP) && (USE_OPENMP == 1)
235 #pragma omp parallel
236 #endif
237 {
238 // Switch to a device with the same device id as the cpu thread id
239 unsigned int thread_id;
240 #if defined(USE_OPENMP) && (USE_OPENMP == 1)
241 thread_id = omp_get_thread_num();
242 #else
243 thread_id = 0;
244 #endif
245
247
248 cublasStatus_t status_create = cublasCreate(
249 &this->cublas_handle[thread_id]);
250 assert(status_create == CUBLAS_STATUS_SUCCESS);
251
252 // Set tensor core whenever possible (usually for cublasXgemm)
253 cublasStatus_t status_set = cublasSetMathMode(
254 this->cublas_handle[thread_id], CUBLAS_TENSOR_OP_MATH);
255 assert(status_set == CUBLAS_STATUS_SUCCESS);
256 }
257 }
258}
cublasStatus_t cublasSetMathMode(cublasHandle_t handle, cublasMath_t mode)
Definition of CUDA's cublasSetmathMode function using dynamically loaded cublas library.

References cublasSetMathMode(), omp_get_thread_num(), omp_set_num_threads(), and CudaAPI< ArrayType >::set_device().

Referenced by cuDenseAffineMatrixFunction< DataType >::cuDenseAffineMatrixFunction(), cuDenseAffineMatrixFunction< DataType >::cuDenseAffineMatrixFunction(), cuDenseMatrix< DataType >::cuDenseMatrix(), cuLinearOperator< DataType >::cuLinearOperator(), and cuLinearOperator< DataType >::cuLinearOperator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initialize_cusparse_handle()

template<typename DataType >
void cuLinearOperator< DataType >::initialize_cusparse_handle ( )
protected

Creates a cusparseHandle_t object, if not created already.

Definition at line 269 of file cu_linear_operator.cu.

270{
271 if (this->cusparse_handle == NULL)
272 {
273 // Allocate pointers for each gpu device
274 this->cusparse_handle = new cusparseHandle_t[this->num_gpu_devices];
275
276 // Set the number of threads
277 #if defined(USE_OPENMP) && (USE_OPENMP == 1)
279 #endif
280
281 #if defined(USE_OPENMP) && (USE_OPENMP == 1)
282 #pragma omp parallel
283 #endif
284 {
285 // Switch to a device with the same device id as the cpu thread id
286 unsigned int thread_id;
287 #if defined(USE_OPENMP) && (USE_OPENMP == 1)
288 thread_id = omp_get_thread_num();
289 #else
290 thread_id = 0;
291 #endif
292
294
295 cusparseStatus_t status = cusparseCreate(
296 &this->cusparse_handle[thread_id]);
297 assert(status == CUSPARSE_STATUS_SUCCESS);
298 }
299 }
300}
cusparseStatus_t cusparseCreate(cusparseHandle_t *handle)
Definition of CUDA's cusparseCreate function using dynamically loaded cublas library.

References cusparseCreate(), omp_get_thread_num(), omp_set_num_threads(), and CudaAPI< ArrayType >::set_device().

Referenced by cuCSCAffineMatrixFunction< DataType >::cuCSCAffineMatrixFunction(), cuCSCAffineMatrixFunction< DataType >::cuCSCAffineMatrixFunction(), cuCSCMatrix< DataType >::cuCSCMatrix(), cuCSRAffineMatrixFunction< DataType >::cuCSRAffineMatrixFunction(), cuCSRAffineMatrixFunction< DataType >::cuCSRAffineMatrixFunction(), and cuCSRMatrix< DataType >::cuCSRMatrix().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ query_gpu_devices()

template<typename DataType >
int cuLinearOperator< DataType >::query_gpu_devices ( ) const
protected

Before any numerical computation, this method chechs if any gpu device is available on the machine, or notifies the user if nothing was found.

Returns
Number of gpu available devices.

Definition at line 314 of file cu_linear_operator.cu.

315{
316 int device_count = 0;
317 cudaError_t error = cudaGetDeviceCount(&device_count);
318
319 // Error code 38 means no cuda-capable device was detected.
320 if ((error != cudaSuccess) || (device_count < 1))
321 {
322 std::cerr << "ERROR: No cuda-capable GPU device was detected on " \
323 << "this machine. If a cuda-capable GPU device exists, " \
324 << "install its cuda driver. Alternatively, set " \
325 << "'gpu=False' to use cpu instead." \
326 << std::endl;
327
328 abort();
329 }
330
331 return device_count;
332}
cudaError_t cudaGetDeviceCount(int *count)
Definition of CUDA's cudaGetDeviceCount function using dynamically loaded cudart library.

References cudaGetDeviceCount().

Referenced by cuLinearOperator< DataType >::cuLinearOperator(), and cuLinearOperator< DataType >::cuLinearOperator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_parameters()

template<typename DataType >
void cuLinearOperator< DataType >::set_parameters ( DataType *  parameters_)

Sets the scalar parameter this->parameters. Parameter is initialized to NULL. However, before calling dot or transpose_dot functions, the parameters must be set.

Parameters
[in]parameters_A pointer to the scalar or array of parameters.

Definition at line 347 of file cu_linear_operator.cu.

348{
349 this->parameters = parameters_;
350}

Referenced by cuTraceEstimator< DataType >::_cu_stochastic_lanczos_quadrature().

Here is the caller graph for this function:

◆ transpose_dot()

template<typename DataType >
virtual void cuLinearOperator< DataType >::transpose_dot ( const DataType *  vector,
DataType *  product 
)
pure virtual

Member Data Documentation

◆ copied_host_to_device

template<typename DataType >
bool cuLinearOperator< DataType >::copied_host_to_device
protected

Definition at line 100 of file cu_linear_operator.h.

◆ cublas_handle

template<typename DataType >
cublasHandle_t* cuLinearOperator< DataType >::cublas_handle
protected

Definition at line 101 of file cu_linear_operator.h.

◆ cusparse_handle

template<typename DataType >
cusparseHandle_t* cuLinearOperator< DataType >::cusparse_handle
protected

Definition at line 102 of file cu_linear_operator.h.

◆ num_gpu_devices

◆ parameters

template<typename DataType >
DataType* cuLinearOperator< DataType >::parameters
protected

Definition at line 103 of file cu_linear_operator.h.


The documentation for this class was generated from the following files: