Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
ReductionOperators.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 <cuda.h>
13 #include "Limits.cuh"
14 #include "MathOperators.cuh"
15 #include "Pair.cuh"
16 
17 namespace faiss { namespace gpu {
18 
19 template <typename T>
20 struct Sum {
21  __device__ inline T operator()(T a, T b) const {
22  return Math<T>::add(a, b);
23  }
24 
25  inline __device__ T identity() const {
26  return Math<T>::zero();
27  }
28 };
29 
30 template <typename T>
31 struct Min {
32  __device__ inline T operator()(T a, T b) const {
33  return Math<T>::lt(a, b) ? a : b;
34  }
35 
36  inline __device__ T identity() const {
37  return Limits<T>::getMax();
38  }
39 };
40 
41 template <typename T>
42 struct Max {
43  __device__ inline T operator()(T a, T b) const {
44  return Math<T>::gt(a, b) ? a : b;
45  }
46 
47  inline __device__ T identity() const {
48  return Limits<T>::getMin();
49  }
50 };
51 
52 /// Used for producing segmented prefix scans; the value of the Pair
53 /// denotes the start of a new segment for the scan
54 template <typename T, typename ReduceOp>
56  inline __device__ SegmentedReduce(const ReduceOp& o)
57  : op(o) {
58  }
59 
60  __device__
61  inline Pair<T, bool>
62  operator()(const Pair<T, bool>& a, const Pair<T, bool>& b) const {
63  return Pair<T, bool>(b.v ? b.k : op(a.k, b.k),
64  a.v || b.v);
65  }
66 
67  inline __device__ Pair<T, bool> identity() const {
68  return Pair<T, bool>(op.identity(), false);
69  }
70 
71  ReduceOp op;
72 };
73 
74 } } // namespace
A simple pair type for CUDA device usage.
Definition: Pair.cuh:20