49 #include "kmp_error.h"
52 template<
typename T >
58 struct i_maxmin< int > {
59 static const int mx = 0x7fffffff;
60 static const int mn = 0x80000000;
63 struct i_maxmin< unsigned int > {
64 static const unsigned int mx = 0xffffffff;
65 static const unsigned int mn = 0x00000000;
68 struct i_maxmin< long long > {
69 static const long long mx = 0x7fffffffffffffffLL;
70 static const long long mn = 0x8000000000000000LL;
73 struct i_maxmin< unsigned long long > {
74 static const unsigned long long mx = 0xffffffffffffffffLL;
75 static const unsigned long long mn = 0x0000000000000000LL;
81 char const * traits_t< int >::spec =
"d";
82 char const * traits_t< unsigned int >::spec =
"u";
83 char const * traits_t< long long >::spec =
"lld";
84 char const * traits_t< unsigned long long >::spec =
"llu";
88 template<
typename T >
90 __kmp_for_static_init(
97 typename traits_t< T >::signed_t *pstride,
98 typename traits_t< T >::signed_t incr,
99 typename traits_t< T >::signed_t chunk
101 typedef typename traits_t< T >::unsigned_t UT;
102 typedef typename traits_t< T >::signed_t ST;
104 register kmp_int32 gtid = global_tid;
105 register kmp_uint32 tid;
106 register kmp_uint32 nth;
107 register UT trip_count;
108 register kmp_team_t *team;
110 KE_TRACE( 10, (
"__kmpc_for_static_init called (%d)\n", global_tid));
115 buff = __kmp_str_format(
116 "__kmpc_for_static_init: T#%%d sched=%%d liter=%%d iter=(%%%s," \
117 " %%%s, %%%s) incr=%%%s chunk=%%%s signed?<%s>\n",
118 traits_t< T >::spec, traits_t< T >::spec, traits_t< ST >::spec,
119 traits_t< ST >::spec, traits_t< ST >::spec, traits_t< T >::spec );
120 KD_TRACE(100, ( buff, global_tid, schedtype, *plastiter,
121 *plower, *pupper, *pstride, incr, chunk ) );
122 __kmp_str_free( &buff );
126 if ( __kmp_env_consistency_check ) {
127 __kmp_push_workshare( global_tid, ct_pdo, loc );
129 __kmp_error_construct( kmp_i18n_msg_CnsLoopIncrZeroProhibited, ct_pdo, loc );
134 if ( incr > 0 ? (*pupper < *plower) : (*plower < *pupper) ) {
144 buff = __kmp_str_format(
145 "__kmpc_for_static_init:(ZERO TRIP) liter=%%d lower=%%%s upper=%%%s stride = %%%s signed?<%s>, loc = %%s\n",
146 traits_t< T >::spec, traits_t< T >::spec, traits_t< ST >::spec, traits_t< T >::spec );
147 KD_TRACE(100, ( buff, *plastiter, *plower, *pupper, *pstride, loc->
psource ) );
148 __kmp_str_free( &buff );
151 KE_TRACE( 10, (
"__kmpc_for_static_init: T#%d return\n", global_tid ) );
159 tid = __kmp_threads[ gtid ]->th.th_team->t.t_master_tid;
160 team = __kmp_threads[ gtid ]->th.th_team->t.t_parent;
164 tid = __kmp_tid_from_gtid( global_tid );
165 team = __kmp_threads[ gtid ]->th.th_team;
169 if ( team -> t.t_serialized ) {
173 *pstride = (incr > 0) ? (*pupper - *plower + 1) : (-(*plower - *pupper + 1));
179 buff = __kmp_str_format(
180 "__kmpc_for_static_init: (serial) liter=%%d lower=%%%s upper=%%%s stride = %%%s\n",
181 traits_t< T >::spec, traits_t< T >::spec, traits_t< ST >::spec );
182 KD_TRACE(100, ( buff, *plastiter, *plower, *pupper, *pstride ) );
183 __kmp_str_free( &buff );
186 KE_TRACE( 10, (
"__kmpc_for_static_init: T#%d return\n", global_tid ) );
189 nth = team->t.t_nproc;
197 buff = __kmp_str_format(
198 "__kmpc_for_static_init: (serial) liter=%%d lower=%%%s upper=%%%s stride = %%%s\n",
199 traits_t< T >::spec, traits_t< T >::spec, traits_t< ST >::spec );
200 KD_TRACE(100, ( buff, *plastiter, *plower, *pupper, *pstride ) );
201 __kmp_str_free( &buff );
204 KE_TRACE( 10, (
"__kmpc_for_static_init: T#%d return\n", global_tid ) );
210 trip_count = *pupper - *plower + 1;
211 }
else if (incr == -1) {
212 trip_count = *plower - *pupper + 1;
215 trip_count = (*pupper - *plower) / incr + 1;
217 trip_count = (*plower - *pupper) / ( -incr ) + 1;
220 if ( __kmp_env_consistency_check ) {
222 if ( trip_count == 0 && *pupper != *plower ) {
223 __kmp_error_construct( kmp_i18n_msg_CnsIterationRangeTooLarge, ct_pdo, loc );
228 switch ( schedtype ) {
231 if ( trip_count < nth ) {
233 __kmp_static == kmp_sch_static_greedy || \
234 __kmp_static == kmp_sch_static_balanced
236 if ( tid < trip_count ) {
237 *pupper = *plower = *plower + tid * incr;
239 *plower = *pupper + incr;
241 *plastiter = ( tid == trip_count - 1 );
243 if ( __kmp_static == kmp_sch_static_balanced ) {
244 register UT small_chunk = trip_count / nth;
245 register UT extras = trip_count % nth;
246 *plower += incr * ( tid * small_chunk + ( tid < extras ? tid : extras ) );
247 *pupper = *plower + small_chunk * incr - ( tid < extras ? 0 : incr );
248 *plastiter = ( tid == nth - 1 );
250 register T big_chunk_inc_count = ( trip_count/nth +
251 ( ( trip_count % nth ) ? 1 : 0) ) * incr;
252 register T old_upper = *pupper;
254 KMP_DEBUG_ASSERT( __kmp_static == kmp_sch_static_greedy );
257 *plower += tid * big_chunk_inc_count;
258 *pupper = *plower + big_chunk_inc_count - incr;
260 if ( *pupper < *plower ) {
261 *pupper = i_maxmin< T >::mx;
263 *plastiter = *plower <= old_upper && *pupper > old_upper - incr;
264 if ( *pupper > old_upper ) *pupper = old_upper;
266 if ( *pupper > *plower ) {
267 *pupper = i_maxmin< T >::mn;
269 *plastiter = *plower >= old_upper && *pupper < old_upper - incr;
270 if ( *pupper < old_upper ) *pupper = old_upper;
276 case kmp_sch_static_chunked:
283 *pstride = span * nth;
284 *plower = *plower + (span * tid);
285 *pupper = *plower + span - incr;
288 kmp_int32 lasttid = ((trip_count - 1) / ( UT )chunk) % nth;
289 *plastiter = (tid == lasttid);
294 KMP_ASSERT2( 0,
"__kmpc_for_static_init: unknown scheduling type" );
302 buff = __kmp_str_format(
303 "__kmpc_for_static_init: liter=%%d lower=%%%s upper=%%%s stride = %%%s signed?<%s>\n",
304 traits_t< T >::spec, traits_t< T >::spec, traits_t< ST >::spec, traits_t< T >::spec );
305 KD_TRACE(100, ( buff, *plastiter, *plower, *pupper, *pstride ) );
306 __kmp_str_free( &buff );
309 KE_TRACE( 10, (
"__kmpc_for_static_init: T#%d return\n", global_tid ) );
338 kmp_int32 *plower, kmp_int32 *pupper,
339 kmp_int32 *pstride, kmp_int32 incr, kmp_int32 chunk )
341 __kmp_for_static_init< kmp_int32 >(
342 loc, gtid, schedtype, plastiter, plower, pupper, pstride, incr, chunk );
350 kmp_uint32 *plower, kmp_uint32 *pupper,
351 kmp_int32 *pstride, kmp_int32 incr, kmp_int32 chunk )
353 __kmp_for_static_init< kmp_uint32 >(
354 loc, gtid, schedtype, plastiter, plower, pupper, pstride, incr, chunk );
362 kmp_int64 *plower, kmp_int64 *pupper,
363 kmp_int64 *pstride, kmp_int64 incr, kmp_int64 chunk )
365 __kmp_for_static_init< kmp_int64 >(
366 loc, gtid, schedtype, plastiter, plower, pupper, pstride, incr, chunk );
374 kmp_uint64 *plower, kmp_uint64 *pupper,
375 kmp_int64 *pstride, kmp_int64 incr, kmp_int64 chunk )
377 __kmp_for_static_init< kmp_uint64 >(
378 loc, gtid, schedtype, plastiter, plower, pupper, pstride, incr, chunk );