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