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