Alsvinn  0.5.3
The fast FVM simulator with UQ support
vec4.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 #include "alsutils/vec2.hpp"
19 namespace alsutils {
20 
24 template<class T>
25 struct vec4 {
26  T x;
27  T y;
28  T z;
29  T v;
30 
32  : x(0), y(0), z(0), v(0) {
33 
34  }
35  __device__ __host__ vec4(T x, T y, T z, T v)
36  : x(x), y(y), z(z), v(v) {
37  // Empty
38  }
39 
41  x(t), y(t), z(t), v(t) {
42 
43  }
44 
46  : x(x), y(y.x), z(y.y), v(z) {
47  // Empty
48  }
49 
50 
52  : x(other.x), y(other.y), z(other.z), v(other.v) {
53 
54  }
55 
56  template<class S>
58  x = other.x;
59  y = other.y;
60  z = other.z;
61  v = other.v;
62 
63 
64  return *this;
65  }
66  __device__ __host__ bool operator==(const vec4& other) const {
67  return other.x == x && other.y == y && other.z == z && other.v == v;
68  }
69 #if __cplusplus > 199711L || WIN32
70  std::vector<T> toStdVector() {
75  return std::vector<T>({ x, y, z, v });
76  }
77 #endif
78  template<class S>
83  return vec4<S>(S(x), S(y), S(z), S(v));
84  }
85 
89  __device__ __host__ const T& operator[](size_t i) const {
90  // Note: We only store five numbers in this struct, hence this is safe
91  return ((T*)this)[i];
92  }
93 
98  // Note: We only store five numbers in this struct, hence this is safe
99  return ((T*)this)[i];
100  }
101 
105  __device__ __host__ T dot(const vec4<T>& other) const {
106  return x * other.x + y * other.y + z * other.z + v * other.v;
107  }
108 
112  __device__ __host__ static constexpr size_t size() {
113  return 4;
114  }
115 
116  template<class S>
118  x += b.x;
119  y += b.y;
120  z += b.z;
121  v += b.v;
122 
123  return *this;
124  }
125 
126  __host__ std::string str() const;
127 };
128 
134 template<class T>
136  const vec4<T>& b) {
137  return vec4<T>(a.x / b.x, a.y / b.y, a.z / b.z, a.v / b.v);
138 }
139 
144 template<class T>
145 __device__ __host__ inline vec4<T> operator*(T scalar, const vec4<T>& a) {
146  return vec4<T>(a.x * scalar, a.y * scalar, a.z * scalar, a.v * scalar);
147 }
148 
153 template<class T>
155  const vec4<T>& b) {
156  return vec4<T>(a.x - b.x, a.y - b.y, a.z - b.z, a.v - b.v);
157 }
158 
163 template<class T>
164 __device__ __host__ inline vec4<T> operator/(const vec4<T>& a, T scalar) {
165  return vec4<T>(a.x / scalar, a.y / scalar, a.z / scalar, a.v / scalar);
166 }
167 
172 template<class T, class S>
174  const vec4<S>& b) {
175  return vec4<T>(a.x + b.x, a.y + b.y, a.z + b.z.a.v + b.v);
176 }
177 
178 
179 
180 
181 }
182 
183 template<typename T>
184 std::ostream& operator<<(std::ostream& os,
185  const alsutils::vec4<T>& vec) {
186  os << "[";
187 
188  for (int i = 0; i < 4; ++i) {
189  os << vec[i];
190 
191  if (i < 3) {
192  os << ", ";
193  }
194  }
195 
196  os << "]";
197  return os;
198 }
199 
200 template<class T>
201 inline __host__ std::string alsutils::vec4<T>::str() const {
202  std::stringstream ss;
203  ss << *this;
204  return ss.str();
205 }
206 
__device__ __host__ T & operator[](size_t i)
Definition: vec4.hpp:97
__device__ __host__ vec1< T > operator-(const vec1< T > &a, const vec1< T > &b)
Definition: vec1.hpp:140
Definition: vec4.hpp:25
__host__ std::string str() const
Definition: vec4.hpp:201
__device__ __host__ vec4(T t)
Definition: vec4.hpp:40
__device__ __host__ vec1< T > operator*(T scalar, const vec1< T > &a)
Definition: vec1.hpp:131
std::ostream & operator<<(std::ostream &os, const alsutils::vec4< T > &vec)
Definition: vec4.hpp:184
#define __host__
Definition: types.hpp:46
__device__ static __host__ constexpr size_t size()
Definition: vec4.hpp:112
__device__ __host__ vec4(T x, vec2< T > y, T z)
Definition: vec4.hpp:45
Definition: vec2.hpp:24
__device__ __host__ vec4(const vec4< T &> &other)
Definition: vec4.hpp:51
__device__ __host__ bool operator==(const vec4 &other) const
Definition: vec4.hpp:66
__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
__device__ __host__ vec4(T x, T y, T z, T v)
Definition: vec4.hpp:35
T y
Definition: vec4.hpp:27
__device__ __host__ vec4 & operator=(const vec4< S > &other)
Definition: vec4.hpp:57
T v
Definition: vec4.hpp:29
#define __device__
Definition: types.hpp:45
__device__ __host__ T dot(const vec4< T > &other) const
Definition: vec4.hpp:105
T x
Definition: vec4.hpp:26
__device__ __host__ vec1< T > operator/(const vec1< T > &a, const vec1< T > &b)
Definition: vec1.hpp:121
T z
Definition: vec4.hpp:28
__device__ __host__ vec4< T > & operator+=(const vec4< S > &b)
Definition: vec4.hpp:117
__device__ __host__ vec4()
Definition: vec4.hpp:31
__device__ __host__ vec4< S > convert()
Definition: vec4.hpp:82
__device__ __host__ const T & operator[](size_t i) const
Definition: vec4.hpp:89