imate
C++/CUDA Reference
Loading...
Searching...
No Matches
cu_vector_operations.h
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#ifndef _CU_BASIC_ALGEBRA_CU_VECTOR_OPERATIONS_H_
13#define _CU_BASIC_ALGEBRA_CU_VECTOR_OPERATIONS_H_
14
15// =======
16// Headers
17// =======
18
19#include "../_definitions/types.h" // LongIndexType
20
21// Avoid CUBLAS numeration value not handled in switch [-Wswitch-enum] warning
22#ifdef _MSC_VER
23 #pragma warning(push, 0) // Suppress all warnings from the followings
24 #include <cublas_v2.h>
25 #pragma warning(pop) // Restore previous warning level
26#elif defined(__INTEL_LLVM_COMPILER) || defined(__INTEL_COMPILER)
27 #pragma warning(push, 0)
28 #include <cublas_v2.h>
29 #pragma warning(pop)
30#elif defined(__GNUC__) || defined(__clang__)
31 #pragma GCC diagnostic push
32 #pragma GCC diagnostic ignored "-Wswitch-enum"
33 #include <cublas_v2.h>
34 #pragma GCC diagnostic pop
35#else
36 #include <cublas_v2.h>
37#endif
38
39// Restrict qualifier
40#if defined(_MSC_VER)
41 #define RESTRICT __restrict
42#elif defined(__INTEL_COMPILER)
43 #define RESTRICT __restrict
44#elif defined(__CUDA__) || defined(__GNUC__) || defined(__clang__)
45 #define RESTRICT __restrict__
46#else
47 #define RESTRICT
48#endif
49
50
51// =================
52// Vector Operations
53// =================
54
62
63template <typename DataType>
65{
66 public:
67
68 // copy vector
69 static void copy_vector(
70 cublasHandle_t cublas_handle,
71 const DataType* RESTRICT input_vector,
72 const LongIndexType vector_size,
73 DataType* RESTRICT output_vector);
74
75 // copy scaled vector
76 static void copy_scaled_vector(
77 cublasHandle_t cublas_handle,
78 const DataType* RESTRICT input_vector,
79 const LongIndexType vector_size,
80 const DataType scale,
81 DataType* RESTRICT output_vector);
82
83 // subtract scaled vector
84 static void subtract_scaled_vector(
85 cublasHandle_t cublas_handle,
86 const DataType* RESTRICT input_vector,
87 const LongIndexType vector_size,
88 const DataType scale,
89 DataType* RESTRICT output_vector);
90
91 // inner product
92 static DataType inner_product(
93 cublasHandle_t cublas_handle,
94 const DataType* RESTRICT vector1,
95 const DataType* RESTRICT vector2,
96 const LongIndexType vector_size);
97
98 // euclidean norm
99 static DataType euclidean_norm(
100 cublasHandle_t cublas_handle,
101 const DataType* RESTRICT vector,
102 const LongIndexType vector_size);
103
104 // normalize vector in place
105 static DataType normalize_vector_in_place(
106 cublasHandle_t cublas_handle,
107 DataType* RESTRICT vector,
108 const LongIndexType vector_size);
109
110 // normalize vector and copy
111 static DataType normalize_vector_and_copy(
112 cublasHandle_t cublas_handle,
113 const DataType* RESTRICT vector,
114 const LongIndexType vector_size,
115 DataType* RESTRICT output_vector);
116};
117
118#endif // _CU_BASIC_ALGEBRA_CU_VECTOR_OPERATIONS_H_
A static class for vector operations, similar to level-1 operations of the BLAS library....
static DataType normalize_vector_in_place(cublasHandle_t cublas_handle, DataType *RESTRICT vector, const LongIndexType vector_size)
Normalizes a vector based on Euclidean 2-norm. The result is written in-place.
static void copy_scaled_vector(cublasHandle_t cublas_handle, const DataType *RESTRICT input_vector, const LongIndexType vector_size, const DataType scale, DataType *RESTRICT output_vector)
Scales a vector and stores to a new vector.
static void subtract_scaled_vector(cublasHandle_t cublas_handle, const DataType *RESTRICT input_vector, const LongIndexType vector_size, const DataType scale, DataType *RESTRICT output_vector)
Subtracts the scaled input vector from the output vector.
static DataType normalize_vector_and_copy(cublasHandle_t cublas_handle, const DataType *RESTRICT vector, const LongIndexType vector_size, DataType *RESTRICT output_vector)
Normalizes a vector based on Euclidean 2-norm. The result is written into another vector.
static void copy_vector(cublasHandle_t cublas_handle, const DataType *RESTRICT input_vector, const LongIndexType vector_size, DataType *RESTRICT output_vector)
Copies a vector to a new vector. Result is written in-place.
static DataType inner_product(cublasHandle_t cublas_handle, const DataType *RESTRICT vector1, const DataType *RESTRICT vector2, const LongIndexType vector_size)
Computes Euclidean inner product of two vectors.
static DataType euclidean_norm(cublasHandle_t cublas_handle, const DataType *RESTRICT vector, const LongIndexType vector_size)
Computes the Euclidean 2-norm of a 1D array.
#define RESTRICT
int LongIndexType
Definition types.h:60