Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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