DFT-EFE
 
Loading...
Searching...
No Matches
DeviceTypeConfigHalfPrec.cu.h
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (c) 2017-2025 The Regents of the University of Michigan and DFT-FE
4// authors.
5//
6// This file is part of the DFT-FE code.
7//
8// The DFT-FE code is free software; you can use it, redistribute
9// it, and/or modify it under the terms of the GNU Lesser General
10// Public License as published by the Free Software Foundation; either
11// version 2.1 of the License, or (at your option) any later version.
12// The full text of the license can be found in the file LICENSE at
13// the top level of the DFT-FE distribution.
14//
15// ---------------------------------------------------------------------
16//
17#ifndef dftefeDeviceTypeConfigHalfPrec_cuh
18#define dftefeDeviceTypeConfigHalfPrec_cuh
19#include <complex>
20#include <cuComplex.h>
21#include <cuda_fp16.h>
22namespace dftefe
23{
24 namespace utils
25 {
26 typedef __nv_bfloat16 __device_bfloat16;
27 typedef __nv_bfloat162 __device_bfloat162;
28
29 __forceinline__ __device__ void
30 copyValue(__device_bfloat16 *a, const float b)
31 {
32 *a = __float2bfloat16(b);
33 }
34
35 __forceinline__ __device__ void
36 copyValue(__device_bfloat16 *a, const double b)
37 {
38 *a = __float2bfloat16((float)b);
39 }
40
41 __forceinline__ __device__ void
42 copyValue(__device_bfloat162 *a, const cuFloatComplex b)
43 {
44 a->x = __float2bfloat16(b.x);
45 a->y = __float2bfloat16(b.y);
46 }
47
48 __forceinline__ __device__ void
49 copyValue(__device_bfloat162 *a, const cuDoubleComplex b)
50 {
51 a->x = __float2bfloat16((float)b.x);
52 a->y = __float2bfloat16((float)b.y);
53 }
54
55 __forceinline__ __device__ void
56 copyValue(float *a, const __device_bfloat16 b)
57 {
58 *a = __bfloat162float(b);
59 }
60
61 __forceinline__ __device__ void
62 copyValue(double *a, const __device_bfloat16 b)
63 {
64 *a = (double)__bfloat162float(b);
65 }
66
67 __forceinline__ __device__ void
68 copyValue(cuFloatComplex *a, const __device_bfloat162 b)
69 {
70 a->x = __bfloat162float(b.x);
71 a->y = __bfloat162float(b.y);
72 }
73
74 __forceinline__ __device__ void
75 copyValue(cuDoubleComplex *a, const __device_bfloat162 b)
76 {
77 a->x = (double)__bfloat162float(b.x);
78 a->y = (double)__bfloat162float(b.y);
79 }
80
81 __forceinline__ __device__ float
83 {
84 return a.x;
85 }
86
87 __forceinline__ __device__ float
89 {
90 return a.y;
91 }
92
93 // uint16_t saves bits only
94 // not for arithmetic operations
95
98 {
99 return __device_bfloat16{__nv_bfloat16_raw{a}};
100 }
101
102 inline __device_bfloat16 *
104 {
105 return reinterpret_cast<__device_bfloat16 *>(a);
106 }
107
108 inline const __device_bfloat16 *
110 {
111 return reinterpret_cast<const __device_bfloat16 *>(a);
112 }
113
114 inline __device_bfloat162
115 makeDataTypeDeviceCompatible(std::complex<uint16_t> a)
116 {
117 return __device_bfloat162{__device_bfloat16{__nv_bfloat16_raw{a.real()}},
118 __device_bfloat16{__nv_bfloat16_raw{a.imag()}}};
119 }
120
121 inline __device_bfloat162 *
122 makeDataTypeDeviceCompatible(std::complex<uint16_t> *a)
123 {
124 return reinterpret_cast<__device_bfloat162 *>(a);
125 }
126
127 inline const __device_bfloat162 *
128 makeDataTypeDeviceCompatible(const std::complex<uint16_t> *a)
129 {
130 return reinterpret_cast<const __device_bfloat162 *>(a);
131 }
132
133 } // namespace utils
134} // namespace dftefe
135
136#endif // dftefeDeviceTypeConfigHalfPrec_cuh
__device_bfloat16 __float2bfloat16(float f)
Definition: DeviceTypeConfigHalfPrec.sycl.h:41
__nv_bfloat162 __device_bfloat162
Definition: DeviceTypeConfigHalfPrec.cu.h:27
__forceinline__ __device__ void copyValue(double *a, const double b)
Definition: DeviceDataTypeOverloads.cu.h:53
float __bfloat162float(__device_bfloat16 bf16)
Definition: DeviceTypeConfigHalfPrec.sycl.h:33
__forceinline__ __device__ double imagPartDevice(double a)
Definition: DeviceDataTypeOverloads.cu.h:156
__nv_bfloat16 __device_bfloat16
Definition: DeviceTypeConfigHalfPrec.cu.h:26
__forceinline__ __device__ double realPartDevice(double a)
Definition: DeviceDataTypeOverloads.cu.h:130
int * makeDataTypeDeviceCompatible(int *a)
Definition: DeviceDataTypeOverloads.cu.h:595
dealii includes
Definition: AtomFieldDataSpherical.cpp:31