Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
Float16.cu
1 /**
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD+Patents license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 
10 #include "Float16.cuh"
11 #include "nvidia/fp16_emu.cuh"
12 #include <thrust/execution_policy.h>
13 #include <thrust/transform.h>
14 
15 #ifdef FAISS_USE_FLOAT16
16 
17 namespace faiss { namespace gpu {
18 
19 bool getDeviceSupportsFloat16Math(int device) {
20  const auto& prop = getDeviceProperties(device);
21 
22  return (prop.major >= 6 ||
23  (prop.major == 5 && prop.minor >= 3));
24 }
25 
26 struct FloatToHalf {
27  __device__ half operator()(float v) const { return __float2half(v); }
28 };
29 
30 struct HalfToFloat {
31  __device__ float operator()(half v) const { return __half2float(v); }
32 };
33 
34 void runConvertToFloat16(half* out,
35  const float* in,
36  size_t num,
37  cudaStream_t stream) {
38  thrust::transform(thrust::cuda::par.on(stream),
39  in, in + num, out, FloatToHalf());
40 }
41 
42 void runConvertToFloat32(float* out,
43  const half* in,
44  size_t num,
45  cudaStream_t stream) {
46  thrust::transform(thrust::cuda::par.on(stream),
47  in, in + num, out, HalfToFloat());
48 }
49 
50 __half hostFloat2Half(float a) {
51 #if CUDA_VERSION >= 9000
52  __half_raw raw;
53  raw.x = cpu_float2half_rn(a).x;
54  return __half(raw);
55 #else
56  __half h;
57  h.x = cpu_float2half_rn(a).x;
58  return h;
59 #endif
60 }
61 
62 } } // namespace
63 
64 #endif // FAISS_USE_FLOAT16