Alsvinn  0.5.3
The fast FVM simulator with UQ support
vec1.hpp
Go to the documentation of this file.
1 /* Copyright (c) 2018 ETH Zurich, Kjetil Olsen Lye
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 #pragma once
17 #include <vector>
18 namespace alsutils {
19 
23 template<class T>
24 struct vec1 {
25  T x;
26 
28  : x(0) {
29 
30  }
32  : x(x) {
33  // Empty
34  }
35 
36  __device__ __host__ operator T() const {
37  return x;
38  }
39 
41  : x(other.x) {
42 
43  }
44 
45 
46  template<class S>
48  x = other.x;
49 
50  return *this;
51  }
52 
53  __device__ __host__ bool operator==(const vec1& other) const {
54  return other.x == x;
55  }
56 #if __cplusplus > 199711L || WIN32
57  std::vector<T> toStdVector() {
62  return std::vector<T>({ x});
63  }
64 #endif
65  template<class S>
70  return vec1<S>(S(x));
71  }
72 
76  __device__ __host__ const T& operator[](size_t i) const {
77  // Note: We only store three numbers in this struct, hence this is safe
78  return ((T*)this)[i];
79  }
80 
85  // Note: We only store three numbers in this struct, hence this is safe
86  return ((T*)this)[i];
87  }
88 
92  __device__ __host__ T dot(const vec1<T>& other) const {
93  return x * other.x;
94  }
95 
99  __device__ __host__ static constexpr size_t size() {
100  return 1;
101  }
102 
104  return abs(x);
105  }
106 
107  template<class S>
109  x += b.x;
110 
111  return *this;
112  }
113 };
114 
120 template<class T>
122  const vec1<T>& b) {
123  return vec1<T>(a.x / b.x);
124 }
125 
130 template<class T>
131 __device__ __host__ inline vec1<T> operator*(T scalar, const vec1<T>& a) {
132  return vec1<T>(a.x * scalar);
133 }
134 
139 template<class T>
141  const vec1<T>& b) {
142  return vec1<T>(a.x - b.x);
143 }
144 
149 template<class T>
150 __device__ __host__ inline vec1<T> operator/(const vec1<T>& a, T scalar) {
151  return vec1<T>(a.x / scalar);
152 }
153 
158 template<class T, class S>
160  const vec1<S>& b) {
161  return vec1<T>(a.x + b.x);
162 }
163 }
__device__ __host__ vec1< T > operator-(const vec1< T > &a, const vec1< T > &b)
Definition: vec1.hpp:140
__device__ __host__ T & operator[](size_t i)
Definition: vec1.hpp:84
__device__ __host__ vec1< T > operator*(T scalar, const vec1< T > &a)
Definition: vec1.hpp:131
#define __host__
Definition: types.hpp:46
__device__ __host__ vec1< S > convert() const
Definition: vec1.hpp:69
__device__ __host__ vec1< T > operator+(const vec1< T > &a, const vec1< S > &b)
Definition: vec1.hpp:159
Various utilities for mpi and cuda.
Definition: Factory.hpp:3
Definition: vec1.hpp:24
__device__ static __host__ constexpr size_t size()
Definition: vec1.hpp:99
__device__ __host__ vec1(T x)
Definition: vec1.hpp:31
__device__ __host__ const T & operator[](size_t i) const
Definition: vec1.hpp:76
#define __device__
Definition: types.hpp:45
T x
Definition: vec1.hpp:25
__device__ __host__ vec1 & operator=(const vec1< S > &other)
Definition: vec1.hpp:47
__device__ __host__ vec1< T > operator/(const vec1< T > &a, const vec1< T > &b)
Definition: vec1.hpp:121
__device__ __host__ T dot(const vec1< T > &other) const
Definition: vec1.hpp:92
__device__ __host__ vec1()
Definition: vec1.hpp:27
__device__ __host__ vec1(const vec1< T &> &other)
Definition: vec1.hpp:40
__device__ __host__ vec1< T > & operator+=(const vec1< S > &b)
Definition: vec1.hpp:108
__device__ __host__ bool operator==(const vec1 &other) const
Definition: vec1.hpp:53
__device__ __host__ T norm() const
Definition: vec1.hpp:103