Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
ConversionOperators.cuh
1 /**
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 
9 #pragma once
10 
11 #include <cuda.h>
12 #include "../../Index.h"
13 #include "Float16.cuh"
14 
15 namespace faiss { namespace gpu {
16 
17 //
18 // Conversion utilities
19 //
20 
21 struct IntToIdxType {
22  inline __device__ faiss::Index::idx_t operator()(int v) const {
23  return (faiss::Index::idx_t) v;
24  }
25 };
26 
27 template <typename T>
28 struct ConvertTo {
29 };
30 
31 template <>
32 struct ConvertTo<float> {
33  static inline __device__ float to(float v) { return v; }
34 #ifdef FAISS_USE_FLOAT16
35  static inline __device__ float to(half v) { return __half2float(v); }
36 #endif
37 };
38 
39 template <>
40 struct ConvertTo<float2> {
41  static inline __device__ float2 to(float2 v) { return v; }
42 #ifdef FAISS_USE_FLOAT16
43  static inline __device__ float2 to(half2 v) { return __half22float2(v); }
44 #endif
45 };
46 
47 template <>
48 struct ConvertTo<float4> {
49  static inline __device__ float4 to(float4 v) { return v; }
50 #ifdef FAISS_USE_FLOAT16
51  static inline __device__ float4 to(Half4 v) { return half4ToFloat4(v); }
52 #endif
53 };
54 
55 #ifdef FAISS_USE_FLOAT16
56 template <>
57 struct ConvertTo<half> {
58  static inline __device__ half to(float v) { return __float2half(v); }
59  static inline __device__ half to(half v) { return v; }
60 };
61 
62 template <>
63 struct ConvertTo<half2> {
64  static inline __device__ half2 to(float2 v) { return __float22half2_rn(v); }
65  static inline __device__ half2 to(half2 v) { return v; }
66 };
67 
68 template <>
69 struct ConvertTo<Half4> {
70  static inline __device__ Half4 to(float4 v) { return float4ToHalf4(v); }
71  static inline __device__ Half4 to(Half4 v) { return v; }
72 };
73 #endif
74 
75 
76 } } // namespace
long idx_t
all indices are this type
Definition: Index.h:62