StatMech
Loading...
Searching...
No Matches
mytypes.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifdef _OPENMP
4 #define EIGEN_DONT_PARALLELIZE
5#endif
6
7#if __has_include(<mkl_types.h>)
8 #ifndef MKL
9 #define MKL
10 #endif
11 #ifndef EIGEN_USE_MKL_ALL
12 #define EIGEN_USE_MKL_ALL
13 #endif
14#else
15 #if __has_include(<Accelerate/Accelerate.h>)
16 #ifndef ACCELERATE
17 #define ACCELERATE
18 #endif
19 #endif
20#endif
21
22#define EIGEN_DEFAULT_IO_FORMAT \
23 Eigen::IOFormat(Eigen::StreamPrecision, 0, ", ", ";\n", " ", "", "[", ";\n]")
24#ifdef MKL
25 #define EIGEN_USE_MKL_ALL
26#endif
27
28// Define macros for the architecture information
29#if defined(__NVCC__) && defined(MAGMA) && defined(MKL) && !defined(CPU)
30 #if !defined(CPU) && !defined(GPU)
31 #define GPU
32 #endif
33 #if defined(CPU) && !defined(DOUBLE)
34 #define DOUBLE
35 #endif
36 #ifdef DOUBLE
37typedef double Real_t;
38 #else
39 #ifndef FLOAT
40 #define FLOAT
41 #endif
42typedef float Real_t;
43 #endif
44
45#else
46 #ifndef CPU
47 #define CPU
48 #endif
49 #undef GPU
50 #undef FLOAT
51 #ifndef DOUBLE
52 #define DOUBLE
53 #endif
54typedef double Real_t;
55 #define __host__
56 #define __device__
57 #define __global__
58 #define __constant__
59#endif // (END) defined(__NVCC__) && defined(MAGMA) && defined(MKL)
60
61// Define complex number types
62#ifdef GPU
63 #include <cuda/std/complex>
64 #include <magma_types.h>
65class libFloatComplex : public magmaFloatComplex {
66 public:
67 using magmaFloatComplex::magmaFloatComplex;
68 template<typename RealScalar>
70 this->x = static_cast<float>(real);
71 this->y = static_cast<float>(imag);
72 }
73 __host__ __device__ float& real() { return this->x; }
74 __host__ __device__ float real() const { return this->x; }
75 __host__ __device__ float& imag() { return this->y; }
76 __host__ __device__ float imag() const { return this->y; }
77};
78class libDoubleComplex : public magmaDoubleComplex {
79 public:
80 using magmaDoubleComplex::magmaDoubleComplex;
81 template<typename RealScalar>
83 this->x = static_cast<double>(real);
84 this->y = static_cast<double>(imag);
85 }
86 __host__ __device__ double& real() { return this->x; }
87 __host__ __device__ double real() const { return this->x; }
88 __host__ __device__ double& imag() { return this->y; }
89 __host__ __device__ double imag() const { return this->y; }
90};
91template<typename T>
92using myComplex = cuda::std::complex<T>;
93using myFloatComplex = cuda::std::complex<float>;
94using myDoubleComplex = cuda::std::complex<double>;
95
96#elif !defined(MKL) && defined(ACCELERATE)
97 #include <complex>
98 #include <Accelerate/Accelerate.h>
99class libFloatComplex : public __CLPK_complex {
100 public:
101 using __CLPK_complex::__CLPK_complex;
102 __host__ __device__ float& real() { return this->r; }
103 __host__ __device__ float real() const { return this->r; }
104 __host__ __device__ float& imag() { return this->i; }
105 __host__ __device__ float imag() const { return this->i; }
106};
107class libDoubleComplex : public __CLPK_doublecomplex {
108 public:
109 using __CLPK_doublecomplex::__CLPK_doublecomplex;
110 __host__ __device__ double& real() { return this->r; }
111 __host__ __device__ double real() const { return this->r; }
112 __host__ __device__ double& imag() { return this->i; }
113 __host__ __device__ double imag() const { return this->i; }
114};
115template<typename T>
116using myComplex = std::complex<T>;
117using myFloatComplex = std::complex<float>;
118using myDoubleComplex = std::complex<double>;
119
120#else
121 #include <complex>
122using libFloatComplex = std::complex<float>;
123using libDoubleComplex = std::complex<double>;
124template<typename T>
125using myComplex = std::complex<T>;
128#endif // (END) GPU
129
130template<class T = Real_t>
131using complex = std::conditional_t<
132 std::is_same_v<T, float>, myFloatComplex,
133 std::conditional_t<std::is_same_v<T, double>, myDoubleComplex, myComplex<T>> >;
134template<class T>
136 template<class U, void* dummy = (&U::imag, nullptr)>
137 static std::true_type test(U*);
138 static std::false_type test(...);
139
140 public:
141 static constexpr bool value = decltype(test((T*)nullptr))::value;
142};
143template<class T>
144inline constexpr bool is_complex_v = is_complex<T>::value;
145
146template<class T = Real_t>
147class Complex_t : public complex<T> {
148 public:
149 using complex<T>::complex;
150
151 __host__ __device__ Complex_t(complex<T> const& z) : complex<T>(z) {}
152
153 template<class U>
154 __host__ __device__ Complex_t(Complex_t<U> const& z) : complex<T>(z.real(), z.imag()) {}
155
156#if defined(GPU) && defined(MAGMA)
157 __host__ __device__ Complex_t(magmaFloatComplex const& z) : complex<T>(z.x, z.y) {}
158 __host__ __device__ Complex_t(magmaDoubleComplex const& z) : complex<T>(z.x, z.y) {}
159#endif
160#if defined(GPU) || defined(ACCELERATE)
161 __host__ __device__ Complex_t(libFloatComplex const& z) : complex<T>(z.real(), z.imag()) {}
162 __host__ __device__ Complex_t(libDoubleComplex const& z) : complex<T>(z.real(), z.imag()) {}
163 __host__ __device__ operator libFloatComplex() const {
164 return libFloatComplex(this->real(), this->imag());
165 }
166 __host__ __device__ operator libDoubleComplex() const {
167 return libDoubleComplex(this->real(), this->imag());
168 }
169#endif // (END) defined(GPU) || defined(ACCELERATE)
170
171}; // (END) class Complex_t
172#undef MKL_Complex8
173#undef MKL_Complex16
174#define MKL_Complex8 Complex_t<float>
175#define MKL_Complex16 Complex_t<double>
176
177#ifdef GPU
178using cuda::std::conj;
179using cuda::std::exp;
180using cuda::std::fabs;
181using cuda::std::imag;
182using cuda::std::real;
183#else
184using std::conj;
185using std::exp;
186using std::fabs;
187using std::imag;
188using std::real;
189#endif // (END) #ifdef GPU
190
191#if defined(GPU) || defined(ACCELERATE)
192__host__ __device__ static inline Complex_t<float> exp(libFloatComplex const& z) {
193 return exp(complex<float>(z.real(), z.imag()));
194}
195__host__ __device__ static inline Complex_t<double> exp(libDoubleComplex const& z) {
196 return exp(complex<double>(z.real(), z.imag()));
197}
198#endif // (END) #if defined(GPU) || defined(ACCELERATE)
199
200template<class T = Real_t>
201__constant__ constexpr Complex_t<T> ComplexZero = {0, 0};
202template<class T = Real_t>
203__constant__ constexpr Complex_t<T> ComplexOne = {1, 0};
204template<class T = Real_t>
205__constant__ constexpr Complex_t<T> ComplexI = {0, 1};
206
207#include <iostream>
208#include <sstream>
209#include <iomanip>
210#include <Eigen/Core>
211namespace Eigen {
212 template<typename T>
213 struct NumTraits<Complex_t<T>> : GenericNumTraits<Complex_t<T>> {
214 typedef T Real;
215 typedef typename NumTraits<T>::Literal Literal;
216 enum {
217 IsComplex = 1,
218 RequireInitialization = NumTraits<Real>::RequireInitialization,
219 ReadCost = 2 * NumTraits<Real>::ReadCost,
220 AddCost = 2 * NumTraits<Real>::AddCost,
221 MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
222 };
223
224 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline Real epsilon() {
225 return NumTraits<Real>::epsilon();
226 }
227 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline Real dummy_precision() {
228 return NumTraits<Real>::dummy_precision();
229 }
230 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline int digits10() {
231 return NumTraits<Real>::digits10();
232 }
233 };
234
235 template<typename charT, typename traits, typename RealType>
236 std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& strm,
237 const Complex_t<RealType>& c) {
238 constexpr size_t precision = 4;
239 constexpr size_t realWidth = precision + 7;
240 constexpr size_t imagWidth = realWidth;
241 constexpr size_t width = realWidth + imagWidth + 2;
242 constexpr double epsilon = 1.0e-8;
243 std::stringstream buff("");
244 buff << std::showpos << std::scientific << std::setprecision(precision);
245 if(std::abs(c.real()) < epsilon && std::abs(c.imag()) < 1.0e-8)
246 buff << 0;
247 else if(std::abs(c.real()) > epsilon && std::abs(c.imag()) < epsilon) { buff << c.real(); }
248 else if(std::abs(c.real()) < epsilon && std::abs(c.imag()) > epsilon) {
249 buff << c.imag() << "*i";
250 }
251 else { buff << c.real() << c.imag() << "*i"; }
252 strm << std::setw(width) << buff.str();
253 return strm;
254 }
255} // namespace Eigen
256// (END) Define complex number types
257
258// Class for GPU configs and traits
259template<class T>
260class on_GPU {
261 template<class U, void* dummy = (&U::onGPU, nullptr)>
262 static std::true_type test(U*);
263 static std::false_type test(...);
264
265 public:
266 static constexpr bool value = decltype(test((T*)nullptr))::value;
267};
268template<class T>
269inline constexpr bool on_GPU_v = on_GPU<T>::value;
270
271#ifdef GPU
273 private:
276 size_t m_shared;
277 magma_queue_t m_queue;
278
279 public:
281 GPUconfig(dim3 A, dim3 B, size_t C = 0, magma_queue_t D = NULL)
282 : m_dimGrid(A), m_dimBlock(B), m_shared(C), m_queue(D) {}
283 GPUconfig(dim3 A, dim3 B, magma_queue_t D)
284 : m_dimGrid(A), m_dimBlock(B), m_shared(), m_queue(D) {}
285
286 void dimGrid(int x, int y, int z) {
287 m_dimGrid.x = x;
288 m_dimGrid.y = y;
289 m_dimGrid.z = z;
290 }
291 void dimBlock(int x, int y, int z) {
292 m_dimBlock.x = x;
293 m_dimBlock.y = y;
294 m_dimBlock.z = z;
295 }
296 void shared(size_t x) { m_shared = x; }
297 void queue(magma_queue_t x) { m_queue = x; }
298
299 dim3 dimGrid() const { return m_dimGrid; }
300 dim3 dimBlock() const { return m_dimBlock; }
301 size_t shared() const { return m_shared; }
302 magma_queue_t queue() const { return m_queue; }
303 cudaStream_t stream() const { return magma_queue_get_cuda_stream(m_queue); }
304};
305
306 #ifndef CHECK
307 #include <iostream>
308 #define CHECK(err) \
309 do { \
310 magma_int_t err_ = (err); \
311 if(err_ != 0) { \
312 std::cerr << "Error: " << #err << "\nfailed at " << __FILE__ << ":" \
313 << __LINE__ << ": error " << (long long)err_ << ": " \
314 << magma_strerror(err_) << std::endl; \
315 std::exit(EXIT_FAILURE); \
316 } \
317 } while(0);
318 #endif // ifndef CHECK
319 #ifndef cuCHECK
320 #define cuCHECK(call) \
321 { \
322 const cudaError_t error = call; \
323 if(error != cudaSuccess) { \
324 printf("Error: %s:%d, ", __FILE__, __LINE__); \
325 printf("code:%d, reason: %s\n", error, cudaGetErrorString(error)); \
326 cudaDeviceSynchronize(); \
327 assert(false); \
328 } \
329 };
330 #endif // ifndef cuCHECK
331#endif // (END) #ifdef GPU, Class for GPU configs and traits
332
333// #ifdef GPU
334// #include <thrust/host_vector.h>
335// #include <thrust/device_vector.h>
336// template<class T>
337// using host_vector = thrust::host_vector<T>;
338// template<class T>
339// using device_vector = thrust::device_vector<T>;
340// #else
341// template<class T>
342// using host_vector = std::vector<T>;
343// #endif
344
345#ifdef MKL
346 #include "mkl.h"
347 #ifndef zgemm_
348 #define zgemm_ zgemm
349 #endif
350 #ifndef zhemv_
351 #define zhemv_ zhemv
352 #endif
353 #ifndef zdotc_
354 #define zdotc_ zdotc
355 #endif
356#else
357 #define MKL_INT int
358#endif // (END) #ifdef MKL
359typedef MKL_INT Integer_t;
Real_t RealScalar
Definition Ensemble.cuh:14
Definition mytypes.hpp:147
__host__ __device__ Complex_t(magmaDoubleComplex const &z)
Definition mytypes.hpp:158
__host__ __device__ Complex_t(magmaFloatComplex const &z)
Definition mytypes.hpp:157
__host__ __device__ Complex_t(complex< T > const &z)
Definition mytypes.hpp:151
__host__ __device__ Complex_t(libDoubleComplex const &z)
Definition mytypes.hpp:162
__host__ __device__ Complex_t(Complex_t< U > const &z)
Definition mytypes.hpp:154
__host__ __device__ Complex_t(libFloatComplex const &z)
Definition mytypes.hpp:161
Definition mytypes.hpp:272
size_t m_shared
Definition mytypes.hpp:276
magma_queue_t m_queue
Definition mytypes.hpp:277
void dimBlock(int x, int y, int z)
Definition mytypes.hpp:291
dim3 m_dimGrid
Definition mytypes.hpp:274
dim3 dimGrid() const
Definition mytypes.hpp:299
dim3 dimBlock() const
Definition mytypes.hpp:300
GPUconfig(dim3 A, dim3 B, size_t C=0, magma_queue_t D=NULL)
Definition mytypes.hpp:281
void queue(magma_queue_t x)
Definition mytypes.hpp:297
magma_queue_t queue() const
Definition mytypes.hpp:302
void shared(size_t x)
Definition mytypes.hpp:296
dim3 m_dimBlock
Definition mytypes.hpp:275
size_t shared() const
Definition mytypes.hpp:301
GPUconfig(dim3 A, dim3 B, magma_queue_t D)
Definition mytypes.hpp:283
GPUconfig()
Definition mytypes.hpp:280
void dimGrid(int x, int y, int z)
Definition mytypes.hpp:286
cudaStream_t stream() const
Definition mytypes.hpp:303
Definition mytypes.hpp:135
static std::true_type test(U *)
static std::false_type test(...)
static constexpr bool value
Definition mytypes.hpp:141
Definition mytypes.hpp:78
__host__ __device__ double & imag()
Definition mytypes.hpp:88
__host__ __device__ double real() const
Definition mytypes.hpp:87
__host__ __device__ double imag() const
Definition mytypes.hpp:89
__host__ __device__ double & real()
Definition mytypes.hpp:86
__host__ __device__ libDoubleComplex(RealScalar real, RealScalar imag)
Definition mytypes.hpp:82
Definition mytypes.hpp:65
__host__ __device__ float imag() const
Definition mytypes.hpp:76
__host__ __device__ libFloatComplex(RealScalar real, RealScalar imag)
Definition mytypes.hpp:69
__host__ __device__ float & real()
Definition mytypes.hpp:73
__host__ __device__ float & imag()
Definition mytypes.hpp:75
__host__ __device__ float real() const
Definition mytypes.hpp:74
Definition mytypes.hpp:260
static constexpr bool value
Definition mytypes.hpp:266
static std::true_type test(U *)
static std::false_type test(...)
cuda::std::complex< double > myDoubleComplex
Definition mytypes.hpp:94
constexpr bool is_complex_v
Definition mytypes.hpp:144
double Real_t
Definition mytypes.hpp:37
MKL_INT Integer_t
Definition mytypes.hpp:359
__constant__ constexpr Complex_t< T > ComplexOne
Definition mytypes.hpp:203
__constant__ constexpr Complex_t< T > ComplexI
Definition mytypes.hpp:205
cuda::std::complex< float > myFloatComplex
Definition mytypes.hpp:93
__constant__ constexpr Complex_t< T > ComplexZero
Definition mytypes.hpp:201
cuda::std::complex< T > myComplex
Definition mytypes.hpp:92
constexpr bool on_GPU_v
Definition mytypes.hpp:269
std::conditional_t< std::is_same_v< T, float >, myFloatComplex, std::conditional_t< std::is_same_v< T, double >, myDoubleComplex, myComplex< T > > > complex
Definition mytypes.hpp:133
Definition OperatorSpace_test.cpp:3
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &strm, const std::complex< double > &c)
Definition OperatorSpace_test.cpp:6
T Real
Definition mytypes.hpp:214
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int digits10()
Definition mytypes.hpp:230
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR Real dummy_precision()
Definition mytypes.hpp:227
NumTraits< T >::Literal Literal
Definition mytypes.hpp:215
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR Real epsilon()
Definition mytypes.hpp:224
std::stringstream buff("")