Line data Source code
1 : // Copyright (C) 2015 Technische Universitaet Muenchen
2 : // This file is part of the Mamico project. For conditions of distribution
3 : // and use, please see the copyright notice in Mamico's main folder, or at
4 : // www5.in.tum.de/mamico
5 : #ifndef _TARCH_LA_VECTOR_H_
6 : #define _TARCH_LA_VECTOR_H_
7 : #include "tarch/TarchDefinitions.h"
8 : #include <cmath>
9 : #include <iostream>
10 : #include <type_traits>
11 : #include <Kokkos_Core.hpp>
12 :
13 : namespace tarch {
14 : namespace la {
15 : template <int size, class T> class Vector;
16 : }
17 : } // namespace tarch
18 :
19 : /** light-weight implementation of a vector class.
20 : * @tparam size Size
21 : * @tparam T Data type
22 : * @author Philipp Neumann
23 : */
24 :
25 3024 : template <int size, class T> class tarch::la::Vector {
26 : private:
27 : T _entries[size];
28 :
29 : public:
30 11120494 : KOKKOS_FUNCTION Vector() {}
31 : /** @brief init. vector with a scalar value.
32 : * @param t scalar value for the initialization
33 : */
34 :
35 488124558 : KOKKOS_FUNCTION Vector(const T& t) {
36 1600073230 : for (int i = 0; i < size; i++) {
37 1474618874 : _entries[i] = t;
38 : }
39 : }
40 : /** @brief special constructor for 2D left empty for general purpose vector
41 : * @param t0 scalar value for the initialization
42 : * @param t1 scalar value for the initialization
43 : */
44 224 : KOKKOS_FUNCTION Vector(const T& t0, const T& t1) {
45 : static_assert(size == 2, "ERROR Vector(t0,t1) only valid for 2D vectors!");
46 224 : _entries[0] = t0;
47 192 : _entries[1] = t1;
48 32 : }
49 : /** @brief special constructor for 3D; left empty for general purpose vector
50 : * @param t0 scalar value for the initialization
51 : * @param t1 scalar value for the initialization
52 : * @param t2 scalar value for the initialization
53 : */
54 4304192 : KOKKOS_FUNCTION Vector(const T& t0, const T& t1, const T& t2) {
55 : static_assert(size == 3, "ERROR Vector(t0,t1,t2) only valid for 3D vectors!");
56 4304200 : _entries[0] = t0;
57 4304200 : _entries[1] = t1;
58 4255647 : _entries[2] = t2;
59 40708 : }
60 : /** @brief constructor init. vector from vector
61 : * @param v Vector for the initialization
62 : */
63 32295284 : KOKKOS_FUNCTION Vector(const Vector<size, T>& v) {
64 289784298 : for (int i = 0; i < size; i++) {
65 230593100 : _entries[i] = v[i];
66 : }
67 : }
68 : /** @brief assigns a value to all vector entries.
69 : * @param t scalar value for the assignment
70 : */
71 : KOKKOS_FUNCTION void assign(const T& t) {
72 : for (int i = 0; i < size; i++) {
73 : _entries[i] = t;
74 : }
75 : }
76 : /** @brief operator overloading for vector assignment
77 : * @param v Vector for the assignment
78 : */
79 4267008 : KOKKOS_FUNCTION Vector<size, T>& operator=(const Vector<size, T>& v) {
80 18129996 : for (int i = 0; i < size; i++) {
81 13602212 : _entries[i] = v[i];
82 : }
83 : return *this;
84 : }
85 : /** @brief operator overloading; access to vector entries; both () and [] are
86 : * allowed
87 : * @param i index
88 : */
89 86628238 : KOKKOS_FUNCTION T& operator[](int i) {
90 : #if (TARCH_DEBUG == TARCH_YES)
91 11848838 : if (i < 0 || i > size - 1) {
92 : Kokkos::abort("ERROR Vector T& operator[]: i out of range!\n");
93 : }
94 : #endif
95 58280 : return _entries[i];
96 : }
97 : /** @brief operator overloading; access to vector entries; both () and [] are
98 : * allowed; !!! Attention: const
99 : * @param i index
100 : */
101 288790550 : KOKKOS_FUNCTION const T& operator[](int i) const {
102 : #if (TARCH_DEBUG == TARCH_YES)
103 35313202 : if (i < 0 || i > size - 1) {
104 : Kokkos::abort("ERROR Vector const T& operator[]: i out of range!\n");
105 : }
106 : #endif
107 4600 : return _entries[i];
108 : }
109 : /** @brief operator overloading; add a vector to this existing one (this)
110 : * @param v vector that has to be added
111 : */
112 0 : KOKKOS_FUNCTION Vector<size, T>& operator+=(const Vector<size, T>& v) {
113 0 : for (int i = 0; i < size; i++) {
114 0 : _entries[i] += v[i];
115 : }
116 : return *this;
117 : }
118 : /** @brief operator overloading; subtracts a vector from the existing one
119 : * (this)
120 : * @param v vector that has to be subtracted
121 : */
122 40432 : KOKKOS_FUNCTION Vector<size, T>& operator-=(const Vector<size, T>& v) {
123 141512 : for (int i = 0; i < size; i++) {
124 121296 : _entries[i] -= v[i];
125 : }
126 : return *this;
127 : }
128 :
129 : /** cast Vector<size, T> to Vector<size, convert_to_T> */
130 : template <class convert_to_T> explicit KOKKOS_FUNCTION operator Vector<size, convert_to_T>() const {
131 : Vector<size, convert_to_T> ans;
132 103392 : for (unsigned int i = 0; i < size; i++) {
133 77544 : ans[i] = static_cast<convert_to_T>(_entries[i]);
134 : }
135 : return ans;
136 : }
137 : };
138 :
139 : #include "VectorOperations.cpph"
140 : #endif // _TARCH_LA_VECTOR_H_
|