1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD+Patents license found in the
* LICENSE file in the root directory of this source tree.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#include <cuda.h>
#include "Limits.cuh"
#include "MathOperators.cuh"
#include "Pair.cuh"
namespace faiss { namespace gpu {
template <typename T>
struct Sum {
__device__ inline T operator()(T a, T b) const {
return Math<T>::add(a, b);
}
inline __device__ T identity() const {
return Math<T>::zero();
}
};
template <typename T>
struct Min {
__device__ inline T operator()(T a, T b) const {
return Math<T>::lt(a, b) ? a : b;
}
inline __device__ T identity() const {
return Limits<T>::getMax();
}
};
template <typename T>
struct Max {
__device__ inline T operator()(T a, T b) const {
return Math<T>::gt(a, b) ? a : b;
}
inline __device__ T identity() const {
return Limits<T>::getMin();
}
};
/// Used for producing segmented prefix scans; the value of the Pair
/// denotes the start of a new segment for the scan
template <typename T, typename ReduceOp>
struct SegmentedReduce {
inline __device__ SegmentedReduce(const ReduceOp& o)
: op(o) {
}
__device__
inline Pair<T, bool>
operator()(const Pair<T, bool>& a, const Pair<T, bool>& b) const {
return Pair<T, bool>(b.v ? b.k : op(a.k, b.k),
a.v || b.v);
}
inline __device__ Pair<T, bool> identity() const {
return Pair<T, bool>(op.identity(), false);
}
ReduceOp op;
};
} } // namespace