HIP: Heterogenous-computing Interface for Portability
hip_cooperative_groups_helper.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2015 - present Advanced Micro Devices, Inc. All rights reserved.
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
31 #ifndef HIP_INCLUDE_HIP_HCC_DETAIL_HIP_COOPERATIVE_GROUPS_HELPER_H
32 #define HIP_INCLUDE_HIP_HCC_DETAIL_HIP_COOPERATIVE_GROUPS_HELPER_H
33 
34 #if __cplusplus
36 #include <hip/hcc_detail/device_functions.h>
37 
38 #if !defined(__align__)
39 #define __align__(x) __attribute__((aligned(x)))
40 #endif
41 
42 #if !defined(__CG_QUALIFIER__)
43 #define __CG_QUALIFIER__ __device__ __forceinline__
44 #endif
45 
46 #if !defined(__CG_STATIC_QUALIFIER__)
47 #define __CG_STATIC_QUALIFIER__ __device__ static __forceinline__
48 #endif
49 
50 #if !defined(WAVEFRONT_SIZE)
51 #define WAVEFRONT_SIZE 64
52 #endif
53 
54 namespace cooperative_groups {
55 
56 namespace internal {
57 
60 typedef enum {
61  cg_invalid,
62  cg_multi_grid,
63  cg_grid
64 } group_type;
65 
69 namespace multi_grid {
70 
71 __CG_STATIC_QUALIFIER__ uint32_t num_grids() {
72  return (uint32_t)__ockl_multi_grid_num_grids();
73 }
74 
75 __CG_STATIC_QUALIFIER__ uint32_t grid_rank() {
76  return (uint32_t)__ockl_multi_grid_grid_rank();
77 }
78 
79 __CG_STATIC_QUALIFIER__ uint32_t size() {
80  return (uint32_t)__ockl_multi_grid_size();
81 }
82 
83 __CG_STATIC_QUALIFIER__ uint32_t thread_rank() {
84  return (uint32_t)__ockl_multi_grid_thread_rank();
85 }
86 
87 __CG_STATIC_QUALIFIER__ bool is_valid() {
88  return (bool)__ockl_multi_grid_is_valid();
89 }
90 
91 __CG_STATIC_QUALIFIER__ void sync() {
92  __ockl_multi_grid_sync();
93 }
94 
95 } // namespace multi_grid
96 
100 namespace grid {
101 
102 __CG_STATIC_QUALIFIER__ uint32_t size() {
103  return (uint32_t)((hipBlockDim_z * hipGridDim_z) *
104  (hipBlockDim_y * hipGridDim_y) *
105  (hipBlockDim_x * hipGridDim_x));
106 }
107 
108 __CG_STATIC_QUALIFIER__ uint32_t thread_rank() {
109  // Compute global id of the workgroup to which the current thread belongs to
110  uint32_t blkIdx =
111  (uint32_t)((hipBlockIdx_z * hipGridDim_y * hipGridDim_x) +
112  (hipBlockIdx_y * hipGridDim_x) +
113  (hipBlockIdx_x));
114 
115  // Compute total number of threads being passed to reach current workgroup
116  // within grid
117  uint32_t num_threads_till_current_workgroup =
118  (uint32_t)(blkIdx * (hipBlockDim_x * hipBlockDim_y * hipBlockDim_z));
119 
120  // Compute thread local rank within current workgroup
121  uint32_t local_thread_rank =
122  (uint32_t)((hipThreadIdx_z * hipBlockDim_y * hipBlockDim_x) +
123  (hipThreadIdx_y * hipBlockDim_x) +
124  (hipThreadIdx_x));
125 
126  return (num_threads_till_current_workgroup + local_thread_rank);
127 }
128 
129 __CG_STATIC_QUALIFIER__ bool is_valid() {
130  return (bool)__ockl_grid_is_valid();
131 }
132 
133 __CG_STATIC_QUALIFIER__ void sync() {
134  __ockl_grid_sync();
135 }
136 
137 } // namespace grid
138 
139 } // namespace internal
140 
141 } // namespace cooperative_groups
142 
143 #endif // __cplusplus
144 #endif // HIP_INCLUDE_HIP_HCC_DETAIL_HIP_COOPERATIVE_GROUPS_HELPER_H
Contains C function APIs for HIP runtime. This file does not use any HCC builtin or special language ...