ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ReductionOps.cuh
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <cuda.h>
11 
12 #include "core/nns/kernel/Limits.cuh"
13 #include "core/nns/kernel/Pair.cuh"
14 
15 namespace cloudViewer {
16 namespace core {
17 
18 template <typename T>
19 struct Sum {
20  __device__ inline T operator()(T a, T b) const { return a + b; }
21 
22  inline __device__ T identity() const { return 0.0; }
23 };
24 
25 template <typename T>
26 struct Min {
27  __device__ inline T operator()(T a, T b) const { return a < b ? a : b; }
28 
29  inline __device__ T identity() const { return Limits<T>::getMax(); }
30 };
31 
32 template <typename T>
33 struct Max {
34  __device__ inline T operator()(T a, T b) const { return a > b ? a : b; }
35 
36  inline __device__ T identity() const { return Limits<T>::getMin(); }
37 };
38 
39 /// Used for producing segmented prefix scans; the value of the Pair
40 /// denotes the start of a new segment for the scan
41 template <typename T, typename ReduceOp>
42 struct SegmentedReduce {
43  inline __device__ SegmentedReduce(const ReduceOp& o) : op(o) {}
44 
45  __device__ inline Pair<T, bool> operator()(const Pair<T, bool>& a,
46  const Pair<T, bool>& b) const {
47  return Pair<T, bool>(b.v ? b.k : op(a.k, b.k), a.v || b.v);
48  }
49 
50  inline __device__ Pair<T, bool> identity() const {
51  return Pair<T, bool>(op.identity(), false);
52  }
53 
54  ReduceOp op;
55 };
56 
57 } // namespace core
58 } // namespace cloudViewer