rocPRIM
ordered_block_id.hpp
1 // Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef ROCPRIM_DEVICE_DETAIL_ORDERED_BLOCK_ID_HPP_
22 #define ROCPRIM_DEVICE_DETAIL_ORDERED_BLOCK_ID_HPP_
23 
24 #include <type_traits>
25 #include <limits>
26 
27 #include "../../detail/temp_storage.hpp"
28 #include "../../detail/various.hpp"
29 #include "../../intrinsics.hpp"
30 #include "../../types.hpp"
31 
32 BEGIN_ROCPRIM_NAMESPACE
33 
34 namespace detail
35 {
36 
37 // Helper struct for generating ordered unique ids for blocks in a grid.
38 template<class T /* id type */ = unsigned int>
40 {
41  static_assert(std::is_integral<T>::value, "T must be integer");
42  using id_type = T;
43 
44  // shared memory temporary storage type
45  struct storage_type
46  {
47  id_type id;
48  };
49 
50  ROCPRIM_HOST static inline
51  ordered_block_id create(id_type * id)
52  {
53  ordered_block_id ordered_id;
54  ordered_id.id = id;
55  return ordered_id;
56  }
57 
58  ROCPRIM_HOST static inline
59  size_t get_storage_size()
60  {
61  return sizeof(id_type);
62  }
63 
64  ROCPRIM_HOST static inline detail::temp_storage::layout get_temp_storage_layout()
65  {
66  return detail::temp_storage::layout{get_storage_size(), alignof(id_type)};
67  }
68 
69  ROCPRIM_DEVICE ROCPRIM_INLINE
70  void reset()
71  {
72  *id = static_cast<id_type>(0);
73  }
74 
75  ROCPRIM_DEVICE ROCPRIM_INLINE
76  id_type get(unsigned int tid, storage_type& storage)
77  {
78  if(tid == 0)
79  {
80  storage.id = ::rocprim::detail::atomic_add(this->id, 1);
81  }
83  return storage.id;
84  }
85 
86  id_type* id;
87 };
88 
89 } // end of detail namespace
90 
91 END_ROCPRIM_NAMESPACE
92 
93 #endif // ROCPRIM_DEVICE_DETAIL_ORDERED_BLOCK_ID_HPP_
This value-structure describes the required layout of some piece of temporary memory, which includes the required size and the required alignment.
Definition: temp_storage.hpp:47
Deprecated: Configuration of device-level scan primitives.
Definition: block_histogram.hpp:62
ROCPRIM_DEVICE ROCPRIM_INLINE void syncthreads()
Synchronize all threads in a block (tile)
Definition: thread.hpp:216
Definition: ordered_block_id.hpp:39
Definition: ordered_block_id.hpp:45