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 half h;
43 h.x = v;
44 return h;
45 }
46
47 template <>
48 struct Limits<half> {
49 static __device__ __host__ inline half getMin() {
50 return kGetHalf(0xfbffU);
51 }
52 static __device__ __host__ inline half getMax() {
53 return kGetHalf(0x7bffU);
54 }
55 };
56
57 #endif // FAISS_USE_FLOAT16
58
59 constexpr int kIntMax = std::numeric_limits<int>::max();
60 constexpr int kIntMin = std::numeric_limits<int>::lowest();
61
62 template <>
63 struct Limits<int> {
64 static __device__ __host__ inline int getMin() {
65 return kIntMin;
66 }
67 static __device__ __host__ inline int getMax() {
68 return kIntMax;
69 }
70 };
71
72 template<typename K, typename V>
73 struct Limits<Pair<K, V>> {
74 static __device__ __host__ inline Pair<K, V> getMin() {
75 return Pair<K, V>(Limits<K>::getMin(), Limits<V>::getMin());
76 }
77
78 static __device__ __host__ inline Pair<K, V> getMax() {
79 return Pair<K, V>(Limits<K>::getMax(), Limits<V>::getMax());
80 }
81 };
82
83 } } // namespace
faiss::gpu::Pair
A simple pair type for CUDA device usage.
Definition: Pair.cuh:21
faiss::gpu::Limits
Definition: Limits.cuh:20