DFT-EFE
 
Loading...
Searching...
No Matches
DeviceTypeConfigHalfPrec.sycl.h
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (c) 2017-2022 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_syclh
18#define dftefeDeviceTypeConfigHalfPrec_syclh
19
20#include <complex>
21#include <cstdint>
22#include <sycl/sycl.hpp>
23#include <sycl/ext/oneapi/bfloat16.hpp>
24
25namespace dftefe
26{
27 namespace utils
28 {
29 typedef sycl::ext::oneapi::bfloat16 __device_bfloat16;
30 typedef std::complex<sycl::ext::oneapi::bfloat16> __device_bfloat162;
31
32 inline float
34 {
35 uint16_t bits = sycl::bit_cast<uint16_t>(bf16);
36 uint32_t float_bits = (bits << 16);
37 return sycl::bit_cast<float>(float_bits);
38 }
39
42 {
43 uint32_t float_bits = sycl::bit_cast<uint32_t>(f);
44 uint32_t rounding_bias = ((float_bits >> 16) & 1) + 0x7FFF;
45 uint32_t rounded_bits = float_bits + rounding_bias;
46 uint16_t bf16_bits = (rounded_bits >> 16) & 0xFFFF;
47 return sycl::bit_cast<__device_bfloat16>(bf16_bits);
48 }
49
52 {
53 return sycl::bit_cast<__device_bfloat16>(a);
54 }
55
56 inline void
57 copyValue(__device_bfloat16 *a, const float b)
58 {
59 *a = __float2bfloat16(b);
60 }
61
62 inline void
63 copyValue(__device_bfloat16 *a, const double b)
64 {
65 *a = __float2bfloat16((float)b);
66 }
67
68 inline void
69 copyValue(__device_bfloat162 *a, const std::complex<float> &b)
70 {
72 __float2bfloat16(b.imag()));
73 }
74
75 inline void
76 copyValue(__device_bfloat162 *a, const std::complex<double> &b)
77 {
78 *a = __device_bfloat162(__float2bfloat16((float)(b.real())),
79 __float2bfloat16((float)(b.imag())));
80 }
81
82 inline void
83 copyValue(float *a, const __device_bfloat16 &b)
84 {
85 *a = __bfloat162float(b);
86 }
87
88 inline void
89 copyValue(double *a, const __device_bfloat16 &b)
90 {
91 *a = (double)__bfloat162float(b);
92 }
93
94 inline void
95 copyValue(std::complex<float> *a, const __device_bfloat162 &b)
96 {
97 *a = std::complex<float>(__bfloat162float(b.real()),
98 __bfloat162float(b.imag()));
99 }
100
101 inline void
102 copyValue(std::complex<double> *a, const __device_bfloat162 &b)
103 {
104 *a = std::complex<double>((double)__bfloat162float(b.real()),
105 (double)__bfloat162float(b.imag()));
106 }
107
108 inline float
110 {
111 return a.real();
112 }
113
114 inline float
116 {
117 return a.imag();
118 }
119
120 // uint16_t saves bits only
121 // not for arithmetic operations
122
123 inline __device_bfloat16
125 {
127 }
128
129 inline __device_bfloat16 *
131 {
132 return reinterpret_cast<__device_bfloat16 *>(a);
133 }
134
135 inline const __device_bfloat16 *
136 makeDataTypeDeviceCompatible(const uint16_t *a)
137 {
138 return reinterpret_cast<const __device_bfloat16 *>(a);
139 }
140
141 inline __device_bfloat162
142 makeDataTypeDeviceCompatible(std::complex<uint16_t> a)
143 {
145 __sycl_bfloat16_raw(a.imag())};
146 }
147
148 inline __device_bfloat162 *
149 makeDataTypeDeviceCompatible(std::complex<uint16_t> *a)
150 {
151 return reinterpret_cast<__device_bfloat162 *>(a);
152 }
153
154 inline const __device_bfloat162 *
155 makeDataTypeDeviceCompatible(const std::complex<uint16_t> *a)
156 {
157 return reinterpret_cast<const __device_bfloat162 *>(a);
158 }
159
160 } // namespace utils
161} // namespace dftefe
162
163#endif // dftefeDeviceTypeConfigHalfPrec_syclh
__device_bfloat16 __float2bfloat16(float f)
Definition: DeviceTypeConfigHalfPrec.sycl.h:41
__nv_bfloat162 __device_bfloat162
Definition: DeviceTypeConfigHalfPrec.cu.h:27
__device_bfloat16 __sycl_bfloat16_raw(uint16_t a)
Definition: DeviceTypeConfigHalfPrec.sycl.h:51
__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