imate
C++/CUDA Reference
cuDenseMatrix< DataType > Class Template Reference

#include <cu_dense_matrix.h>

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

Public Member Functions

 cuDenseMatrix ()
 
 cuDenseMatrix (const DataType *A_, const LongIndexType num_rows_, const LongIndexType num_columns_, const FlagType A_is_row_major_, const int num_gpu_devices_)
 
virtual ~cuDenseMatrix ()
 
virtual void dot (const DataType *device_vector, DataType *device_product)
 
virtual void dot_plus (const DataType *device_vector, const DataType alpha, DataType *device_product)
 
virtual void transpose_dot (const DataType *device_vector, DataType *device_product)
 
virtual void transpose_dot_plus (const DataType *device_vector, const DataType alpha, DataType *device_product)
 
- Public Member Functions inherited from cuMatrix< DataType >
 cuMatrix ()
 
 cuMatrix (int num_gpu_devices_)
 
virtual ~cuMatrix ()
 
- Public Member Functions inherited from cuLinearOperator< DataType >
 cuLinearOperator ()
 
 cuLinearOperator (int num_gpu_devices_)
 Constructor with setting num_rows and num_columns. More...
 
virtual ~cuLinearOperator ()
 
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. More...
 
- Public Member Functions inherited from cLinearOperator< DataType >
 cLinearOperator ()
 Default constructor. More...
 
 cLinearOperator (const LongIndexType num_rows_, const LongIndexType num_columns_)
 Constructor with setting num_rows and num_columns. More...
 
virtual ~cLinearOperator ()
 
LongIndexType get_num_rows () const
 
LongIndexType get_num_columns () const
 
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. More...
 
IndexType get_num_parameters () const
 
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. More...
 
- Public Member Functions inherited from cDenseMatrix< DataType >
 cDenseMatrix ()
 
 cDenseMatrix (const DataType *A_, const LongIndexType num_rows_, const LongIndexType num_columns_, const FlagType A_is_row_major_)
 
virtual ~cDenseMatrix ()
 
virtual FlagType is_identity_matrix () const
 Checks whether the matrix is identity. More...
 
- Public Member Functions inherited from cMatrix< DataType >
 cMatrix ()
 Default constructor. More...
 
virtual ~cMatrix ()
 
DataType get_eigenvalue (const DataType *known_parameters, const DataType known_eigenvalue, const DataType *inquiry_parameters) const
 This virtual function is implemented from its pure virtual function of the base class. In this class, this functio has no use and was only implemented so that this class be able to be instantiated (due to the pure virtual function). More...
 

Protected Member Functions

virtual void copy_host_to_device ()
 Copies the member data from the host memory to the device memory. More...
 
- Protected Member Functions inherited from cuLinearOperator< DataType >
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. More...
 
void initialize_cublas_handle ()
 Creates a cublasHandle_t object, if not created already. More...
 
void initialize_cusparse_handle ()
 Creates a cusparseHandle_t object, if not created already. More...
 

Protected Attributes

DataType ** device_A
 
- Protected Attributes inherited from cuLinearOperator< DataType >
int num_gpu_devices
 
bool copied_host_to_device
 
cublasHandle_t * cublas_handle
 
cusparseHandle_t * cusparse_handle
 
- Protected Attributes inherited from cLinearOperator< DataType >
const LongIndexType num_rows
 
const LongIndexType num_columns
 
FlagType eigenvalue_relation_known
 
DataType * parameters
 
IndexType num_parameters
 
- Protected Attributes inherited from cDenseMatrix< DataType >
const DataType * A
 
const FlagType A_is_row_major
 

Detailed Description

template<typename DataType>
class cuDenseMatrix< DataType >

Definition at line 30 of file cu_dense_matrix.h.

Constructor & Destructor Documentation

◆ cuDenseMatrix() [1/2]

template<typename DataType >
cuDenseMatrix< DataType >::cuDenseMatrix

Definition at line 29 of file cu_dense_matrix.cu.

29  :
30  device_A(NULL)
31 {
32 }
DataType ** device_A

◆ cuDenseMatrix() [2/2]

template<typename DataType >
cuDenseMatrix< DataType >::cuDenseMatrix ( const DataType *  A_,
const LongIndexType  num_rows_,
const LongIndexType  num_columns_,
const FlagType  A_is_row_major_,
const int  num_gpu_devices_ 
)

Definition at line 40 of file cu_dense_matrix.cu.

45  :
46 
47  // Base class constructor
48  cLinearOperator<DataType>(num_rows_, num_columns_),
49  cDenseMatrix<DataType>(A_, num_rows_, num_columns_, A_is_row_major_),
50  cuMatrix<DataType>(num_gpu_devices_),
51 
52  // Initializer list
53  device_A(NULL)
54 {
56  this->copy_host_to_device();
57 }
Base class for linear operators. This class serves as interface for all derived classes.
virtual void copy_host_to_device()
Copies the member data from the host memory to the device memory.
void initialize_cublas_handle()
Creates a cublasHandle_t object, if not created already.
Base class for constant matrices.
Definition: cu_matrix.h:41

References cuDenseMatrix< DataType >::copy_host_to_device(), and cuLinearOperator< DataType >::initialize_cublas_handle().

Here is the call graph for this function:

◆ ~cuDenseMatrix()

template<typename DataType >
cuDenseMatrix< DataType >::~cuDenseMatrix
virtual

Definition at line 66 of file cu_dense_matrix.cu.

67 {
68  // Member objects exist if the second constructor was called.
69  if (this->copied_host_to_device)
70  {
71  // Deallocate arrays of data on gpu
72  for (int device_id = 0; device_id < this->num_gpu_devices; ++device_id)
73  {
74  // Switch to a device
76 
77  // Deallocate
78  CudaInterface<DataType>::del(this->device_A[device_id]);
79  }
80 
81  delete[] this->device_A;
82  this->device_A = NULL;
83  }
84 }
static void del(void *device_array)
Deletes memory on gpu device if its pointer is not NULL, then sets the pointer to NULL.
static void set_device(int device_id)
Sets the current device in multi-gpu applications.

References CudaInterface< ArrayType >::del(), and CudaInterface< ArrayType >::set_device().

Here is the call graph for this function:

Member Function Documentation

◆ copy_host_to_device()

template<typename DataType >
void cuDenseMatrix< DataType >::copy_host_to_device
protectedvirtual

Copies the member data from the host memory to the device memory.

Implements cuMatrix< DataType >.

Definition at line 95 of file cu_dense_matrix.cu.

96 {
97  if (!this->copied_host_to_device)
98  {
99  // Set the number of threads
100  omp_set_num_threads(this->num_gpu_devices);
101 
102  // Create array of pointers for data on each gpu device
103  this->device_A = new DataType*[this->num_gpu_devices];
104 
105  // Size of data
106  LongIndexType A_size = this->num_rows * this->num_columns;
107 
108  #pragma omp parallel
109  {
110  // Switch to a device with the same device id as the cpu thread id
111  unsigned int thread_id = omp_get_thread_num();
113 
114  // Allocate device memory and copy data from host
115  CudaInterface<DataType>::alloc(this->device_A[thread_id], A_size);
117  this->device_A[thread_id]);
118  }
119 
120  // Flag to prevent reinitialization
121  this->copied_host_to_device = true;
122  }
123 }
static ArrayType * alloc(const LongIndexType array_size)
Allocates memory on gpu device. This function creates a pointer and returns it.
static void copy_to_device(const ArrayType *host_array, const LongIndexType array_size, ArrayType *device_array)
Copies memory on host to device memory.
const DataType * A
const LongIndexType num_rows
const LongIndexType num_columns
int LongIndexType
Definition: types.h:60

References CudaInterface< ArrayType >::alloc(), CudaInterface< ArrayType >::copy_to_device(), and CudaInterface< ArrayType >::set_device().

Referenced by cuDenseMatrix< DataType >::cuDenseMatrix().

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

◆ dot()

template<typename DataType >
void cuDenseMatrix< DataType >::dot ( const DataType *  device_vector,
DataType *  device_product 
)
virtual

Reimplemented from cDenseMatrix< DataType >.

Definition at line 131 of file cu_dense_matrix.cu.

134 {
135  assert(this->copied_host_to_device);
136 
137  // Get device id
138  int device_id = CudaInterface<DataType>::get_device();
139 
141  this->cublas_handle[device_id],
142  this->device_A[device_id],
143  device_vector,
144  this->num_rows,
145  this->num_columns,
146  this->A_is_row_major,
147  device_product);
148 }
static int get_device()
Gets the current device in multi-gpu applications.
const FlagType A_is_row_major
cublasHandle_t * cublas_handle
static void dense_matvec(cublasHandle_t cublas_handle, const DataType *A, const DataType *b, const LongIndexType num_rows, const LongIndexType num_columns, const FlagType A_is_row_major, DataType *c)
Computes the matrix vector multiplication where is a dense matrix.

References cuMatrixOperations< DataType >::dense_matvec(), and CudaInterface< ArrayType >::get_device().

Here is the call graph for this function:

◆ dot_plus()

template<typename DataType >
void cuDenseMatrix< DataType >::dot_plus ( const DataType *  device_vector,
const DataType  alpha,
DataType *  device_product 
)
virtual

Reimplemented from cDenseMatrix< DataType >.

Definition at line 156 of file cu_dense_matrix.cu.

160 {
161  assert(this->copied_host_to_device);
162 
163  // Get device id
164  int device_id = CudaInterface<DataType>::get_device();
165 
167  this->cublas_handle[device_id],
168  this->device_A[device_id],
169  device_vector,
170  alpha,
171  this->num_rows,
172  this->num_columns,
173  this->A_is_row_major,
174  device_product);
175 }
static void dense_matvec_plus(cublasHandle_t cublas_handle, const DataType *A, const DataType *b, const DataType alpha, const LongIndexType num_rows, const LongIndexType num_columns, const FlagType A_is_row_major, DataType *c)
Computes the operation where is a dense matrix.

References cuMatrixOperations< DataType >::dense_matvec_plus(), and CudaInterface< ArrayType >::get_device().

Here is the call graph for this function:

◆ transpose_dot()

template<typename DataType >
void cuDenseMatrix< DataType >::transpose_dot ( const DataType *  device_vector,
DataType *  device_product 
)
virtual

Reimplemented from cDenseMatrix< DataType >.

Definition at line 183 of file cu_dense_matrix.cu.

186 {
187  assert(this->copied_host_to_device);
188 
189  // Get device id
190  int device_id = CudaInterface<DataType>::get_device();
191 
193  this->cublas_handle[device_id],
194  this->device_A[device_id],
195  device_vector,
196  this->num_rows,
197  this->num_columns,
198  this->A_is_row_major,
199  device_product);
200 }
static void dense_transposed_matvec(cublasHandle_t cublas_handle, const DataType *A, const DataType *b, const LongIndexType num_rows, const LongIndexType num_columns, const FlagType A_is_row_major, DataType *c)
Computes matrix vector multiplication where is dense, and is the transpose of the matrix .

References cuMatrixOperations< DataType >::dense_transposed_matvec(), and CudaInterface< ArrayType >::get_device().

Here is the call graph for this function:

◆ transpose_dot_plus()

template<typename DataType >
void cuDenseMatrix< DataType >::transpose_dot_plus ( const DataType *  device_vector,
const DataType  alpha,
DataType *  device_product 
)
virtual

Reimplemented from cDenseMatrix< DataType >.

Definition at line 208 of file cu_dense_matrix.cu.

212 {
213  assert(this->copied_host_to_device);
214 
215  // Get device id
216  int device_id = CudaInterface<DataType>::get_device();
217 
219  this->cublas_handle[device_id],
220  this->device_A[device_id],
221  device_vector,
222  alpha,
223  this->num_rows,
224  this->num_columns,
225  this->A_is_row_major,
226  device_product);
227 }
static void dense_transposed_matvec_plus(cublasHandle_t cublas_handle, const DataType *A, const DataType *b, const DataType alpha, const LongIndexType num_rows, const LongIndexType num_columns, const FlagType A_is_row_major, DataType *c)
Computes where is dense, and is the transpose of the matrix .

References cuMatrixOperations< DataType >::dense_transposed_matvec_plus(), and CudaInterface< ArrayType >::get_device().

Here is the call graph for this function:

Member Data Documentation

◆ device_A

template<typename DataType >
DataType** cuDenseMatrix< DataType >::device_A
protected

Definition at line 72 of file cu_dense_matrix.h.


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