imate
C++/CUDA Reference
Loading...
Searching...
No Matches
c_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 _C_BASIC_ALGEBRA_C_VECTOR_OPERATIONS_H_
13#define _C_BASIC_ALGEBRA_C_VECTOR_OPERATIONS_H_
14
15// =======
16// Headers
17// =======
18
19#include "../_definitions/types.h" // LongIndexType
20
21// Restrict qualifier
22#if defined(_MSC_VER)
23 #define RESTRICT __restrict
24#elif defined(__INTEL_COMPILER)
25 #define RESTRICT __restrict
26#elif defined(__CUDA__) || defined(__GNUC__) || defined(__clang__)
27 #define RESTRICT __restrict__
28#else
29 #define RESTRICT
30#endif
31
32
33// =================
34// Vector Operations
35// =================
36
44
45template <typename DataType>
47{
48 public:
49
50 // copy vector
51 static void copy_vector(
52 const DataType* RESTRICT input_vector,
53 const LongIndexType vector_size,
54 DataType* RESTRICT output_vector);
55
56 // copy scaled vector
57 static void copy_scaled_vector(
58 const DataType* RESTRICT input_vector,
59 const LongIndexType vector_size,
60 const DataType scale,
61 DataType* RESTRICT output_vector);
62
63 // subtract scaled vector
64 static void subtract_scaled_vector(
65 const DataType* RESTRICT input_vector,
66 const LongIndexType vector_size,
67 const DataType scale,
68 DataType* RESTRICT output_vector);
69
70 // inner product
71 static DataType inner_product(
72 const DataType* RESTRICT vector1,
73 const DataType* RESTRICT vector2,
74 const LongIndexType vector_size);
75
76 // euclidean norm
77 static DataType euclidean_norm(
78 const DataType* RESTRICT vector,
79 const LongIndexType vector_size);
80
81 // normalize vector in place
82 static DataType normalize_vector_in_place(
83 DataType* RESTRICT vector,
84 const LongIndexType vector_size);
85
86 // normalize vector and copy
87 static DataType normalize_vector_and_copy(
88 const DataType* RESTRICT vector,
89 const LongIndexType vector_size,
90 DataType* RESTRICT output_vector);
91};
92
93#endif // _C_BASIC_ALGEBRA_C_VECTOR_OPERATIONS_H_
#define RESTRICT
A static class for vector operations, similar to level-1 operations of the BLAS library....
static void copy_scaled_vector(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 DataType inner_product(const DataType *RESTRICT vector1, const DataType *RESTRICT vector2, const LongIndexType vector_size)
Computes Euclidean inner product of two vectors.
static DataType normalize_vector_and_copy(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(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 normalize_vector_in_place(DataType *RESTRICT vector, const LongIndexType vector_size)
Normalizes a vector based on Euclidean 2-norm. The result is written in-place.
static void subtract_scaled_vector(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 euclidean_norm(const DataType *RESTRICT vector, const LongIndexType vector_size)
Computes the Euclidean norm of a 1D array.
int LongIndexType
Definition types.h:60