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

A static class for vector operations, similar to level-1 operations of the BLAS library. This class acts as a templated namespace, where all member methods are public and static. More...

#include <cu_vector_operations.h>

Static Public Member Functions

static void copy_vector (cublasHandle_t cublas_handle, const DataType *input_vector, const LongIndexType vector_size, DataType *output_vector)
 Copies a vector to a new vector. Result is written in-place. More...
 
static void copy_scaled_vector (cublasHandle_t cublas_handle, const DataType *input_vector, const LongIndexType vector_size, const DataType scale, DataType *output_vector)
 Scales a vector and stores to a new vector. More...
 
static void subtract_scaled_vector (cublasHandle_t cublas_handle, const DataType *input_vector, const LongIndexType vector_size, const DataType scale, DataType *output_vector)
 Subtracts the scaled input vector from the output vector. More...
 
static DataType inner_product (cublasHandle_t cublas_handle, const DataType *vector1, const DataType *vector2, const LongIndexType vector_size)
 Computes Euclidean inner product of two vectors. More...
 
static DataType euclidean_norm (cublasHandle_t cublas_handle, const DataType *vector, const LongIndexType vector_size)
 Computes the Euclidean 2-norm of a 1D array. More...
 
static DataType normalize_vector_in_place (cublasHandle_t cublas_handle, DataType *vector, const LongIndexType vector_size)
 Normalizes a vector based on Euclidean 2-norm. The result is written in-place. More...
 
static DataType normalize_vector_and_copy (cublasHandle_t cublas_handle, const DataType *vector, const LongIndexType vector_size, DataType *output_vector)
 Normalizes a vector based on Euclidean 2-norm. The result is written into another vector. More...
 

Detailed Description

template<typename DataType>
class cuVectorOperations< DataType >

A static class for vector operations, similar to level-1 operations of the BLAS library. This class acts as a templated namespace, where all member methods are public and static.

See also
MatrixOperations

Definition at line 36 of file cu_vector_operations.h.

Member Function Documentation

◆ copy_scaled_vector()

template<typename DataType >
void cuVectorOperations< DataType >::copy_scaled_vector ( cublasHandle_t  cublas_handle,
const DataType *  input_vector,
const LongIndexType  vector_size,
const DataType  scale,
DataType *  output_vector 
)
static

Scales a vector and stores to a new vector.

Parameters
[in]cublas_handleThe cuBLAS object handle.
[in]input_vectorA 1D array
[in]vector_sizeLength of vector array
[in]scaleScale coefficient to the input vector. If this is equal to one, the function effectively becomes the same as copy_vector.
[out]output_vectorOutput vector (written in place).

Definition at line 73 of file cu_vector_operations.cu.

79 {
80  cublasStatus_t status;
81  int incx = 1;
82  int incy = 1;
83 
84  // Copy input to output vector
85  status = cublas_interface::cublasXcopy(cublas_handle, vector_size,
86  input_vector, incx,
87  output_vector, incy);
88 
89  assert(status == CUBLAS_STATUS_SUCCESS);
90 
91  // Scale outpu vector
92  status = cublas_interface::cublasXscal(cublas_handle, vector_size, &scale,
93  output_vector, incy);
94 
95  assert(status == CUBLAS_STATUS_SUCCESS);
96 }
cublasStatus_t cublasXscal(cublasHandle_t handle, int n, const DataType *alpha, DataType *x, int incx)
cublasStatus_t cublasXcopy(cublasHandle_t handle, int n, const DataType *x, int incx, DataType *y, int incy)

References cublas_interface::cublasXcopy(), and cublas_interface::cublasXscal().

Referenced by cu_lanczos_tridiagonalization(), and cuVectorOperations< DataType >::normalize_vector_and_copy().

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

◆ copy_vector()

template<typename DataType >
void cuVectorOperations< DataType >::copy_vector ( cublasHandle_t  cublas_handle,
const DataType *  input_vector,
const LongIndexType  vector_size,
DataType *  output_vector 
)
static

Copies a vector to a new vector. Result is written in-place.

Parameters
[in]cublas_handleThe cuBLAS object handle.
[in]input_vectorA 1D array
[in]vector_sizeLength of vector array
[out]output_vectorOutput vector (written in place).

Definition at line 38 of file cu_vector_operations.cu.

43 {
44  int incx = 1;
45  int incy = 1;
46 
47  cublasStatus_t status = cublas_interface::cublasXcopy(
48  cublas_handle, vector_size, input_vector, incx, output_vector,
49  incy);
50 
51  assert(status == CUBLAS_STATUS_SUCCESS);
52 }

References cublas_interface::cublasXcopy().

Here is the call graph for this function:

◆ euclidean_norm()

template<typename DataType >
DataType cuVectorOperations< DataType >::euclidean_norm ( cublasHandle_t  cublas_handle,
const DataType *  vector,
const LongIndexType  vector_size 
)
static

Computes the Euclidean 2-norm of a 1D array.

Parameters
[in]cublas_handleThe cuBLAS object handle.
[in]vectorA pointer to 1D array
[in]vector_sizeLength of the array
Returns
Euclidean norm

Definition at line 201 of file cu_vector_operations.cu.

205 {
206  DataType norm;
207  int incx = 1;
208 
209  cublasStatus_t status = cublas_interface::cublasXnrm2(
210  cublas_handle, vector_size, vector, incx, &norm);
211 
212  assert(status == CUBLAS_STATUS_SUCCESS);
213 
214  return norm;
215 }
cublasStatus_t cublasXnrm2(cublasHandle_t handle, int n, const DataType *x, int incx, DataType *result)

References cublas_interface::cublasXnrm2().

Referenced by cu_lanczos_tridiagonalization(), cuOrthogonalization< DataType >::gram_schmidt_process(), cuVectorOperations< DataType >::normalize_vector_and_copy(), cuVectorOperations< DataType >::normalize_vector_in_place(), and cuOrthogonalization< DataType >::orthogonalize_vectors().

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

◆ inner_product()

template<typename DataType >
DataType cuVectorOperations< DataType >::inner_product ( cublasHandle_t  cublas_handle,
const DataType *  vector1,
const DataType *  vector2,
const LongIndexType  vector_size 
)
static

Computes Euclidean inner product of two vectors.

Parameters
[in]cublas_handleThe cuBLAS object handle.
[in]vector11D array
[in]vector21D array
[in]vector_sizeLength of array
Returns
Inner product of two vectors.

Definition at line 166 of file cu_vector_operations.cu.

171 {
172  DataType inner_prod;
173  int incx = 1;
174  int incy = 1;
175 
176  cublasStatus_t status = cublas_interface::cublasXdot(
177  cublas_handle, vector_size, vector1, incx, vector2, incy,
178  &inner_prod);
179 
180  assert(status == CUBLAS_STATUS_SUCCESS);
181 
182  return inner_prod;
183 }
cublasStatus_t cublasXdot(cublasHandle_t handle, int n, const DataType *x, int incx, const DataType *y, int incy, DataType *result)

References cublas_interface::cublasXdot().

Referenced by cu_lanczos_tridiagonalization(), cuOrthogonalization< DataType >::gram_schmidt_process(), and cuOrthogonalization< DataType >::orthogonalize_vectors().

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

◆ normalize_vector_and_copy()

template<typename DataType >
DataType cuVectorOperations< DataType >::normalize_vector_and_copy ( cublasHandle_t  cublas_handle,
const DataType *  vector,
const LongIndexType  vector_size,
DataType *  output_vector 
)
static

Normalizes a vector based on Euclidean 2-norm. The result is written into another vector.

Parameters
[in]cublas_handleThe cuBLAS object handle.
[in]vectorInput vector.
[in]vector_sizeLength of the input vector
[out]output_vectorOutput vector, which is the normalization of the input vector.
Returns
2-norm of the input vector

Definition at line 273 of file cu_vector_operations.cu.

278 {
279  // Norm of vector
281  cublas_handle, vector, vector_size);
282 
283  // Normalize to output
284  DataType scale = 1.0 / norm;
286  vector_size, scale,
287  output_vector);
288 
289  return norm;
290 }
static void copy_scaled_vector(cublasHandle_t cublas_handle, const DataType *input_vector, const LongIndexType vector_size, const DataType scale, DataType *output_vector)
Scales a vector and stores to a new vector.
static DataType euclidean_norm(cublasHandle_t cublas_handle, const DataType *vector, const LongIndexType vector_size)
Computes the Euclidean 2-norm of a 1D array.

References cuVectorOperations< DataType >::copy_scaled_vector(), and cuVectorOperations< DataType >::euclidean_norm().

Here is the call graph for this function:

◆ normalize_vector_in_place()

template<typename DataType >
DataType cuVectorOperations< DataType >::normalize_vector_in_place ( cublasHandle_t  cublas_handle,
DataType *  vector,
const LongIndexType  vector_size 
)
static

Normalizes a vector based on Euclidean 2-norm. The result is written in-place.

Parameters
[in]cublas_handleThe cuBLAS object handle.
[in,out]vectorInput vector to be normalized in-place.
[in]vector_sizeLength of the input vector
Returns
2-Norm of the input vector (before normalization)

Definition at line 234 of file cu_vector_operations.cu.

238 {
239  // Norm of vector
241  cublas_handle, vector, vector_size);
242 
243  // Normalize in place
244  DataType scale = 1.0 / norm;
245  int incx = 1;
246  cublasStatus_t status = cublas_interface::cublasXscal(
247  cublas_handle, vector_size, &scale, vector, incx);
248 
249  assert(status == CUBLAS_STATUS_SUCCESS);
250 
251  return norm;
252 }

References cublas_interface::cublasXscal(), and cuVectorOperations< DataType >::euclidean_norm().

Referenced by cu_golub_kahn_bidiagonalization().

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

◆ subtract_scaled_vector()

template<typename DataType >
void cuVectorOperations< DataType >::subtract_scaled_vector ( cublasHandle_t  cublas_handle,
const DataType *  input_vector,
const LongIndexType  vector_size,
const DataType  scale,
DataType *  output_vector 
)
static

Subtracts the scaled input vector from the output vector.

Performs the following operation:

\[ \boldsymbol{b} = \boldsymbol{b} - c \boldsymbol{a}, \]

where

  • \( \boldsymbol{a} \) is the input vector,
  • \( c \) is a scalar scale to the input vector, and
  • \( \boldsymbol{b} \) is the output vector that is written in-place.
Parameters
[in]cublas_handleThe cuBLAS object handle.
[in]input_vectorA 1D array
[in]vector_sizeLength of vector array
[in]scaleScale coefficient to the input vector.
[in,out]output_vectorOutput vector (written in place).

Definition at line 126 of file cu_vector_operations.cu.

132 {
133  if (scale == 0.0)
134  {
135  return;
136  }
137 
138  int incx = 1;
139  int incy = 1;
140 
141  DataType neg_scale = -scale;
142  cublasStatus_t status = cublas_interface::cublasXaxpy(
143  cublas_handle, vector_size, &neg_scale, input_vector, incx,
144  output_vector, incy);
145 
146  assert(status == CUBLAS_STATUS_SUCCESS);
147 }
cublasStatus_t cublasXaxpy(cublasHandle_t handle, int n, const DataType *alpha, const DataType *x, int incx, DataType *y, int incy)

References cublas_interface::cublasXaxpy().

Referenced by cuAffineMatrixFunction< DataType >::_add_scaled_vector(), cu_golub_kahn_bidiagonalization(), cu_lanczos_tridiagonalization(), cuOrthogonalization< DataType >::gram_schmidt_process(), and cuOrthogonalization< DataType >::orthogonalize_vectors().

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

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