Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
Limits.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 "Float16.cuh"
12 #include "Pair.cuh"
13 #include <limits>
14
15 namespace faiss { namespace gpu {
16
17 template <typename T>
18 struct Limits {
19 };
20
21 // Unfortunately we can't use constexpr because there is no
22 // constexpr constructor for half
23 // FIXME: faiss CPU uses +/-FLT_MAX instead of +/-infinity
24 constexpr float kFloatMax = std::numeric_limits<float>::max();
25 constexpr float kFloatMin = std::numeric_limits<float>::lowest();
26
27 template <>
28 struct Limits<float> {
29 static __device__ __host__ inline float getMin() {
30 return kFloatMin;
31 }
32 static __device__ __host__ inline float getMax() {
33 return kFloatMax;
34 }
35 };
36
37 #ifdef FAISS_USE_FLOAT16
38
39 inline __device__ __host__ half kGetHalf(unsigned short v) {
40 #if CUDA_VERSION >= 9000
41 __half_raw h;
42 h.x = v;
43 return __half(h);
44 #else
45 half h;
46 h.x = v;
47 return h;
48 #endif
49 }
50
51 template <>
52 struct Limits<half> {
53 static __device__ __host__ inline half getMin() {
54 return kGetHalf(0xfbffU);
55 }
56 static __device__ __host__ inline half getMax() {
57 return kGetHalf(0x7bffU);
58 }
59 };
60
61 #endif // FAISS_USE_FLOAT16
62
63 constexpr int kIntMax = std::numeric_limits<int>::max();
64 constexpr int kIntMin = std::numeric_limits<int>::lowest();
65
66 template <>
67 struct Limits<int> {
68 static __device__ __host__ inline int getMin() {
69 return kIntMin;
70 }
71 static __device__ __host__ inline int getMax() {
72 return kIntMax;
73 }
74 };
75
76 template<typename K, typename V>
77 struct Limits<Pair<K, V>> {
78 static __device__ __host__ inline Pair<K, V> getMin() {
79 return Pair<K, V>(Limits<K>::getMin(), Limits<V>::getMin());
80 }
81
82 static __device__ __host__ inline Pair<K, V> getMax() {
83 return Pair<K, V>(Limits<K>::getMax(), Limits<V>::getMax());
84 }
85 };
86
87 } } // namespace
faiss::gpu::Pair
A simple pair type for CUDA device usage.
Definition: Pair.cuh:19
faiss::gpu::Limits
Definition: Limits.cuh:18