/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipcub/checkouts/docs-5.5.1/hipcub/include/hipcub/backend/rocprim/block/block_reduce.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipcub/checkouts/docs-5.5.1/hipcub/include/hipcub/backend/rocprim/block/block_reduce.hpp Source File#

hipCUB: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-hipcub/checkouts/docs-5.5.1/hipcub/include/hipcub/backend/rocprim/block/block_reduce.hpp Source File
block_reduce.hpp
1 /******************************************************************************
2  * Copyright (c) 2010-2011, Duane Merrill. All rights reserved.
3  * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
4  * Modifications Copyright (c) 2017-2020, Advanced Micro Devices, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the NVIDIA CORPORATION nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  ******************************************************************************/
29 
30 #ifndef HIPCUB_ROCPRIM_BLOCK_BLOCK_REDUCE_HPP_
31 #define HIPCUB_ROCPRIM_BLOCK_BLOCK_REDUCE_HPP_
32 
33 #include <type_traits>
34 
35 #include <rocprim/block/block_reduce.hpp>
36 
37 BEGIN_HIPCUB_NAMESPACE
38 
39 namespace detail
40 {
41  inline constexpr
42  typename std::underlying_type<::rocprim::block_reduce_algorithm>::type
43  to_BlockReduceAlgorithm_enum(::rocprim::block_reduce_algorithm v)
44  {
45  using utype = std::underlying_type<::rocprim::block_reduce_algorithm>::type;
46  return static_cast<utype>(v);
47  }
48 }
49 
50 enum BlockReduceAlgorithm
51 {
52  BLOCK_REDUCE_RAKING_COMMUTATIVE_ONLY
53  = detail::to_BlockReduceAlgorithm_enum(::rocprim::block_reduce_algorithm::raking_reduce_commutative_only),
54  BLOCK_REDUCE_RAKING
55  = detail::to_BlockReduceAlgorithm_enum(::rocprim::block_reduce_algorithm::raking_reduce),
56  BLOCK_REDUCE_WARP_REDUCTIONS
57  = detail::to_BlockReduceAlgorithm_enum(::rocprim::block_reduce_algorithm::using_warp_reduce)
58 };
59 
60 template<
61  typename T,
62  int BLOCK_DIM_X,
63  BlockReduceAlgorithm ALGORITHM = BLOCK_REDUCE_WARP_REDUCTIONS,
64  int BLOCK_DIM_Y = 1,
65  int BLOCK_DIM_Z = 1,
66  int ARCH = HIPCUB_ARCH /* ignored */
67 >
69  : private ::rocprim::block_reduce<
70  T,
71  BLOCK_DIM_X,
72  static_cast<::rocprim::block_reduce_algorithm>(ALGORITHM),
73  BLOCK_DIM_Y,
74  BLOCK_DIM_Z
75  >
76 {
77  static_assert(
78  BLOCK_DIM_X * BLOCK_DIM_Y * BLOCK_DIM_Z > 0,
79  "BLOCK_DIM_X * BLOCK_DIM_Y * BLOCK_DIM_Z must be greater than 0"
80  );
81 
82  using base_type =
83  typename ::rocprim::block_reduce<
84  T,
85  BLOCK_DIM_X,
86  static_cast<::rocprim::block_reduce_algorithm>(ALGORITHM),
87  BLOCK_DIM_Y,
88  BLOCK_DIM_Z
89  >;
90 
91  // Reference to temporary storage (usually shared memory)
92  typename base_type::storage_type& temp_storage_;
93 
94 public:
95  using TempStorage = typename base_type::storage_type;
96 
97  HIPCUB_DEVICE inline
98  BlockReduce() : temp_storage_(private_storage())
99  {
100  }
101 
102  HIPCUB_DEVICE inline
103  BlockReduce(TempStorage& temp_storage) : temp_storage_(temp_storage)
104  {
105  }
106 
107  HIPCUB_DEVICE inline
108  T Sum(T input)
109  {
110  base_type::reduce(input, input, temp_storage_);
111  return input;
112  }
113 
114  HIPCUB_DEVICE inline
115  T Sum(T input, int valid_items)
116  {
117  base_type::reduce(input, input, valid_items, temp_storage_);
118  return input;
119  }
120 
121  template<int ITEMS_PER_THREAD>
122  HIPCUB_DEVICE inline
123  T Sum(T(&input)[ITEMS_PER_THREAD])
124  {
125  T output;
126  base_type::reduce(input, output, temp_storage_);
127  return output;
128  }
129 
130  template<typename ReduceOp>
131  HIPCUB_DEVICE inline
132  T Reduce(T input, ReduceOp reduce_op)
133  {
134  base_type::reduce(input, input, temp_storage_, reduce_op);
135  return input;
136  }
137 
138  template<typename ReduceOp>
139  HIPCUB_DEVICE inline
140  T Reduce(T input, ReduceOp reduce_op, int valid_items)
141  {
142  base_type::reduce(input, input, valid_items, temp_storage_, reduce_op);
143  return input;
144  }
145 
146  template<int ITEMS_PER_THREAD, typename ReduceOp>
147  HIPCUB_DEVICE inline
148  T Reduce(T(&input)[ITEMS_PER_THREAD], ReduceOp reduce_op)
149  {
150  T output;
151  base_type::reduce(input, output, temp_storage_, reduce_op);
152  return output;
153  }
154 
155 private:
156  HIPCUB_DEVICE inline
157  TempStorage& private_storage()
158  {
159  HIPCUB_SHARED_MEMORY TempStorage private_storage;
160  return private_storage;
161  }
162 };
163 
164 END_HIPCUB_NAMESPACE
165 
166 #endif // HIPCUB_ROCPRIM_BLOCK_BLOCK_REDUCE_HPP_
Definition: block_reduce.hpp:76