LLVM OpenMP 20.0.0git
kmp_collapse.h
Go to the documentation of this file.
1/*
2 * kmp_collapse.h -- header for loop collapse feature
3 */
4
5//===----------------------------------------------------------------------===//
6//
7// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8// See https://llvm.org/LICENSE.txt for license information.
9// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef KMP_COLLAPSE_H
14#define KMP_COLLAPSE_H
15
16#include <type_traits>
17
18// Type of the index into the loop nest structures
19// (with values from 0 to less than n from collapse(n))
21
22// Type for combined loop nest space IV:
24
25// Loop has <, <=, etc. as a comparison:
31 comp_greater = 4
32};
33
34// Type of loop IV.
35// Type of bounds and step, after usual promotions
36// are a subset of these types (32 & 64 only):
46};
47
48// Defining loop types to handle special cases
53};
54
55/*!
56 @ingroup WORK_SHARING
57 * Describes the structure for rectangular nested loops.
58 */
59template <typename T> struct bounds_infoXX_template {
60
61 // typedef typename traits_t<T>::unsigned_t UT;
62 typedef typename traits_t<T>::signed_t ST;
63
64 loop_type_t loop_type; // The differentiator
67 // outer_iv should be 0 (or any other less then number of dimentions)
68 // if loop doesn't depend on it (lb1 and ub1 will be 0).
69 // This way we can do multiplication without a check.
71
72 // unions to keep the size constant:
73 union {
74 T lb0;
75 kmp_uint64 lb0_u64; // real type can be signed
76 };
77
78 union {
79 T lb1;
80 kmp_uint64 lb1_u64; // real type can be signed
81 };
82
83 union {
84 T ub0;
85 kmp_uint64 ub0_u64; // real type can be signed
86 };
87
88 union {
89 T ub1;
90 kmp_uint64 ub1_u64; // real type can be signed
91 };
92
93 union {
94 ST step; // signed even if bounds type is unsigned
95 kmp_int64 step_64; // signed
96 };
97
99};
100
101/*!
102 @ingroup WORK_SHARING
103 * Interface struct for rectangular nested loops.
104 * Same size as bounds_infoXX_template.
105 */
107
108 loop_type_t loop_type; // The differentiator
111 // outer_iv should be 0 (or any other less then number of dimentions)
112 // if loop doesn't depend on it (lb1 and ub1 will be 0).
113 // This way we can do multiplication without a check.
115
116 kmp_uint64 lb0_u64; // real type can be signed
117 kmp_uint64 lb1_u64; // real type can be signed
118 kmp_uint64 ub0_u64; // real type can be signed
119 kmp_uint64 ub1_u64; // real type can be signed
121
122 // This is internal, but it's the only internal thing we need
123 // in rectangular case, so let's expose it here:
125};
126
127//-------------------------------------------------------------------------
128// Additional types for internal representation:
129
130// Array for a point in the loop space, in the original space.
131// It's represented in kmp_uint64, but each dimention is calculated in
132// that loop IV type. Also dimentions have to be converted to those types
133// when used in generated code.
135
136// Array: Number of loop iterations on each nesting level to achieve some point,
137// in expanded space or in original space.
138// OMPTODO: move from using iterations to using offsets (iterations multiplied
139// by steps). For those we need to be careful with the types, as step can be
140// negative, but it'll remove multiplications and divisions in several places.
142
143// Internal struct with additional info:
144template <typename T> struct bounds_info_internalXX_template {
145
146 // OMPTODO: should span have type T or should it better be
147 // kmp_uint64/kmp_int64 depending on T sign? (if kmp_uint64/kmp_int64 than
148 // updated bounds should probably also be kmp_uint64/kmp_int64). I'd like to
149 // use big_span_t, if it can be resolved at compile time.
150 typedef
151 typename std::conditional<std::is_signed<T>::value, kmp_int64, kmp_uint64>
153
154 // typedef typename big_span_t span_t;
155 typedef T span_t;
156
157 bounds_infoXX_template<T> b; // possibly adjusted bounds
158
159 // Leaving this as a union in case we'll switch to span_t with different sizes
160 // (depending on T)
161 union {
162 // Smallest possible value of iv (may be smaller than actually possible)
165 };
166
167 // Leaving this as a union in case we'll switch to span_t with different sizes
168 // (depending on T)
169 union {
170 // Biggest possible value of iv (may be bigger than actually possible)
173 };
174
175 // Did we adjust loop bounds (not counting canonicalization)?
177};
178
179// Internal struct with additional info:
181
182 bounds_info_t b; // possibly adjusted bounds
183
184 // Smallest possible value of iv (may be smaller than actually possible)
186
187 // Biggest possible value of iv (may be bigger than actually possible)
189
190 // Did we adjust loop bounds (not counting canonicalization)?
192};
193
194//----------APIs for rectangular loop nests--------------------------------
195
196// Canonicalize loop nest and calculate overall trip count.
197// "bounds_nest" has to be allocated per thread.
198// API will modify original bounds_nest array to bring it to a canonical form
199// (only <= and >=, no !=, <, >). If the original loop nest was already in a
200// canonical form there will be no changes to bounds in bounds_nest array
201// (only trip counts will be calculated).
202// Returns trip count of overall space.
203extern "C" kmp_loop_nest_iv_t
205 /*in/out*/ bounds_info_t *original_bounds_nest,
206 kmp_index_t n);
207
208// Calculate old induction variables corresponding to overall new_iv.
209// Note: original IV will be returned as if it had kmp_uint64 type,
210// will have to be converted to original type in user code.
211// Note: trip counts should be already calculated by
212// __kmpc_process_loop_nest_rectang.
213// OMPTODO: special case 2, 3 nested loops - if it'll be possible to inline
214// that into user code.
215extern "C" void
217 const bounds_info_t *original_bounds_nest,
218 /*out*/ kmp_uint64 *original_ivs,
219 kmp_index_t n);
220
221//----------Init API for non-rectangular loops--------------------------------
222
223// Init API for collapsed loops (static, no chunks defined).
224// "bounds_nest" has to be allocated per thread.
225// API will modify original bounds_nest array to bring it to a canonical form
226// (only <= and >=, no !=, <, >). If the original loop nest was already in a
227// canonical form there will be no changes to bounds in bounds_nest array
228// (only trip counts will be calculated). Internally API will expand the space
229// to parallelogram/parallelepiped, calculate total, calculate bounds for the
230// chunks in terms of the new IV, re-calc them in terms of old IVs (especially
231// important on the left side, to hit the lower bounds and not step over), and
232// pick the correct chunk for this thread (so it will calculate chunks up to the
233// needed one). It could be optimized to calculate just this chunk, potentially
234// a bit less well distributed among threads. It is designed to make sure that
235// threads will receive predictable chunks, deterministically (so that next nest
236// of loops with similar characteristics will get exactly same chunks on same
237// threads).
238// Current contract: chunk_bounds_nest has only lb0 and ub0,
239// lb1 and ub1 are set to 0 and can be ignored. (This may change in the future).
240extern "C" kmp_int32
242 /*in/out*/ bounds_info_t *original_bounds_nest,
243 /*out*/ bounds_info_t *chunk_bounds_nest,
244 kmp_index_t n,
245 /*out*/ kmp_int32 *plastiter);
246
247#endif // KMP_COLLAPSE_H
int64_t kmp_int64
Definition: common.h:10
comparison_t
Definition: kmp_collapse.h:26
@ comp_greater_or_eq
Definition: kmp_collapse.h:28
@ comp_less
Definition: kmp_collapse.h:30
@ comp_greater
Definition: kmp_collapse.h:31
@ comp_not_eq
Definition: kmp_collapse.h:29
@ comp_less_or_eq
Definition: kmp_collapse.h:27
kmp_uint64 * kmp_point_t
Definition: kmp_collapse.h:134
loop_type_t
Definition: kmp_collapse.h:37
@ loop_type_uint8
Definition: kmp_collapse.h:38
@ loop_type_int16
Definition: kmp_collapse.h:41
@ loop_type_int32
Definition: kmp_collapse.h:43
@ loop_type_int64
Definition: kmp_collapse.h:45
@ loop_type_uint32
Definition: kmp_collapse.h:42
@ loop_type_uint16
Definition: kmp_collapse.h:40
@ loop_type_int8
Definition: kmp_collapse.h:39
@ loop_type_uint64
Definition: kmp_collapse.h:44
kmp_int32 __kmpc_for_collapsed_init(ident_t *loc, kmp_int32 gtid, bounds_info_t *original_bounds_nest, bounds_info_t *chunk_bounds_nest, kmp_index_t n, kmp_int32 *plastiter)
kmp_loop_nest_iv_t * kmp_iterations_t
Definition: kmp_collapse.h:141
void __kmpc_calc_original_ivs_rectang(ident_t *loc, kmp_loop_nest_iv_t new_iv, const bounds_info_t *original_bounds_nest, kmp_uint64 *original_ivs, kmp_index_t n)
kmp_int32 kmp_index_t
Definition: kmp_collapse.h:20
nested_loop_type_t
Definition: kmp_collapse.h:49
@ nested_loop_type_unkown
Definition: kmp_collapse.h:50
@ nested_loop_type_lower_triangular_matrix
Definition: kmp_collapse.h:51
@ nested_loop_type_upper_triangular_matrix
Definition: kmp_collapse.h:52
kmp_loop_nest_iv_t __kmpc_process_loop_nest_rectang(ident_t *loc, kmp_int32 gtid, bounds_info_t *original_bounds_nest, kmp_index_t n)
kmp_uint64 kmp_loop_nest_iv_t
Definition: kmp_collapse.h:23
unsigned long long kmp_uint64
int32_t kmp_int32
static id loc
Describes the structure for rectangular nested loops.
Definition: kmp_collapse.h:59
loop_type_t loop_iv_type
Definition: kmp_collapse.h:65
kmp_loop_nest_iv_t trip_count
Definition: kmp_collapse.h:98
comparison_t comparison
Definition: kmp_collapse.h:66
traits_t< T >::signed_t ST
Definition: kmp_collapse.h:62
bounds_infoXX_template< T > b
Definition: kmp_collapse.h:157
std::conditional< std::is_signed< T >::value, kmp_int64, kmp_uint64 > big_span_t
Definition: kmp_collapse.h:152
kmp_uint64 span_biggest_u64
Definition: kmp_collapse.h:188
kmp_uint64 span_smallest_u64
Definition: kmp_collapse.h:185
Interface struct for rectangular nested loops.
Definition: kmp_collapse.h:106
kmp_index_t outer_iv
Definition: kmp_collapse.h:114
kmp_uint64 lb1_u64
Definition: kmp_collapse.h:117
comparison_t comparison
Definition: kmp_collapse.h:110
kmp_loop_nest_iv_t trip_count
Definition: kmp_collapse.h:124
loop_type_t loop_iv_type
Definition: kmp_collapse.h:109
kmp_uint64 ub0_u64
Definition: kmp_collapse.h:118
kmp_uint64 ub1_u64
Definition: kmp_collapse.h:119
kmp_uint64 lb0_u64
Definition: kmp_collapse.h:116
kmp_int64 step_64
Definition: kmp_collapse.h:120
loop_type_t loop_type
Definition: kmp_collapse.h:108