aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/cxxsupp/openmp/kmp_sched.cpp
blob: 798ed0e206343a246ac2513b41d330bbb7cf897e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
/* 
 * kmp_sched.c -- static scheduling -- iteration initialization 
 */ 
 
 
//===----------------------------------------------------------------------===// 
// 
//                     The LLVM Compiler Infrastructure 
// 
// This file is dual licensed under the MIT and the University of Illinois Open 
// Source Licenses. See LICENSE.txt for details. 
// 
//===----------------------------------------------------------------------===// 
 
 
/* 
 * Static scheduling initialization. 
 * 
 * NOTE: team->t.t_nproc is a constant inside of any dispatch loop, however 
 *       it may change values between parallel regions.  __kmp_max_nth 
 *       is the largest value __kmp_nth may take, 1 is the smallest. 
 * 
 */ 
 
#include "kmp.h" 
#include "kmp_i18n.h" 
#include "kmp_str.h" 
#include "kmp_error.h" 
#include "kmp_stats.h" 
#include "kmp_itt.h" 
 
#if OMPT_SUPPORT 
#include "ompt-specific.h" 
#endif 
 
// template for type limits 
template< typename T > 
struct i_maxmin { 
    static const T mx; 
    static const T mn; 
}; 
template<> 
struct i_maxmin< int > { 
    static const int mx = 0x7fffffff; 
    static const int mn = 0x80000000; 
}; 
template<> 
struct i_maxmin< unsigned int > { 
    static const unsigned int mx = 0xffffffff; 
    static const unsigned int mn = 0x00000000; 
}; 
template<> 
struct i_maxmin< long long > { 
    static const long long mx = 0x7fffffffffffffffLL; 
    static const long long mn = 0x8000000000000000LL; 
}; 
template<> 
struct i_maxmin< unsigned long long > { 
    static const unsigned long long mx = 0xffffffffffffffffLL; 
    static const unsigned long long mn = 0x0000000000000000LL; 
}; 
//------------------------------------------------------------------------- 
#ifdef KMP_DEBUG 
//------------------------------------------------------------------------- 
// template for debug prints specification ( d, u, lld, llu ) 
    char const * traits_t< int >::spec = "d"; 
    char const * traits_t< unsigned int >::spec = "u"; 
    char const * traits_t< long long >::spec = "lld"; 
    char const * traits_t< unsigned long long >::spec = "llu"; 
//------------------------------------------------------------------------- 
#endif 
 
template< typename T > 
static void 
__kmp_for_static_init( 
    ident_t                          *loc, 
    kmp_int32                         global_tid, 
    kmp_int32                         schedtype, 
    kmp_int32                        *plastiter, 
    T                                *plower, 
    T                                *pupper, 
    typename traits_t< T >::signed_t *pstride, 
    typename traits_t< T >::signed_t  incr, 
    typename traits_t< T >::signed_t  chunk 
) { 
    KMP_COUNT_BLOCK(OMP_FOR_static); 
    KMP_TIME_BLOCK (FOR_static_scheduling); 
 
    typedef typename traits_t< T >::unsigned_t  UT; 
    typedef typename traits_t< T >::signed_t    ST; 
    /*  this all has to be changed back to TID and such.. */ 
    kmp_int32   gtid = global_tid;
    kmp_uint32  tid;
    kmp_uint32  nth;
    UT          trip_count;
    kmp_team_t *team;
    kmp_info_t *th = __kmp_threads[ gtid ];
 
#if OMPT_SUPPORT && OMPT_TRACE 
    ompt_team_info_t *team_info = NULL;  
    ompt_task_info_t *task_info = NULL;  
 
    if (ompt_enabled) { 
        // Only fully initialize variables needed by OMPT if OMPT is enabled. 
        team_info = __ompt_get_teaminfo(0, NULL); 
        task_info = __ompt_get_taskinfo(0); 
    } 
#endif 
 
    KMP_DEBUG_ASSERT( plastiter && plower && pupper && pstride ); 
    KE_TRACE( 10, ("__kmpc_for_static_init called (%d)\n", global_tid)); 
    #ifdef KMP_DEBUG 
    { 
        const char * buff; 
        // create format specifiers before the debug output 
        buff = __kmp_str_format( 
            "__kmpc_for_static_init: T#%%d sched=%%d liter=%%d iter=(%%%s," \ 
            " %%%s, %%%s) incr=%%%s chunk=%%%s signed?<%s>\n", 
            traits_t< T >::spec, traits_t< T >::spec, traits_t< ST >::spec, 
            traits_t< ST >::spec, traits_t< ST >::spec, traits_t< T >::spec ); 
        KD_TRACE(100, ( buff, global_tid, schedtype, *plastiter, 
            *plower, *pupper, *pstride, incr, chunk ) ); 
        __kmp_str_free( &buff ); 
    } 
    #endif 
 
    if ( __kmp_env_consistency_check ) { 
        __kmp_push_workshare( global_tid, ct_pdo, loc ); 
        if ( incr == 0 ) { 
            __kmp_error_construct( kmp_i18n_msg_CnsLoopIncrZeroProhibited, ct_pdo, loc ); 
        } 
    } 
    /* special handling for zero-trip loops */ 
    if ( incr > 0 ? (*pupper < *plower) : (*plower < *pupper) ) { 
        if( plastiter != NULL ) 
            *plastiter = FALSE; 
        /* leave pupper and plower set to entire iteration space */ 
        *pstride = incr;   /* value should never be used */ 
	//        *plower = *pupper - incr;   // let compiler bypass the illegal loop (like for(i=1;i<10;i--))  THIS LINE CAUSED shape2F/h_tests_1.f TO HAVE A FAILURE ON A ZERO-TRIP LOOP (lower=1,\ 
	  upper=0,stride=1) - JPH June 23, 2009. 
        #ifdef KMP_DEBUG 
        { 
            const char * buff; 
            // create format specifiers before the debug output 
            buff = __kmp_str_format( 
                "__kmpc_for_static_init:(ZERO TRIP) liter=%%d lower=%%%s upper=%%%s stride = %%%s signed?<%s>, loc = %%s\n", 
                traits_t< T >::spec, traits_t< T >::spec, traits_t< ST >::spec, traits_t< T >::spec ); 
            KD_TRACE(100, ( buff, *plastiter, *plower, *pupper, *pstride, loc->psource ) ); 
            __kmp_str_free( &buff ); 
        } 
        #endif 
        KE_TRACE( 10, ("__kmpc_for_static_init: T#%d return\n", global_tid ) ); 
 
#if OMPT_SUPPORT && OMPT_TRACE 
        if (ompt_enabled && 
            ompt_callbacks.ompt_callback(ompt_event_loop_begin)) { 
            ompt_callbacks.ompt_callback(ompt_event_loop_begin)( 
                team_info->parallel_id, task_info->task_id, 
                team_info->microtask); 
        } 
#endif 
        KMP_COUNT_VALUE (FOR_static_iterations, 0); 
        return; 
    } 
 
    #if OMP_40_ENABLED 
    if ( schedtype > kmp_ord_upper ) { 
        // we are in DISTRIBUTE construct 
        schedtype += kmp_sch_static - kmp_distribute_static;      // AC: convert to usual schedule type 
        tid  = th->th.th_team->t.t_master_tid; 
        team = th->th.th_team->t.t_parent; 
    } else 
    #endif 
    { 
        tid  = __kmp_tid_from_gtid( global_tid ); 
        team = th->th.th_team; 
    } 
 
    /* determine if "for" loop is an active worksharing construct */ 
    if ( team -> t.t_serialized ) { 
        /* serialized parallel, each thread executes whole iteration space */ 
        if( plastiter != NULL ) 
            *plastiter = TRUE; 
        /* leave pupper and plower set to entire iteration space */ 
        *pstride = (incr > 0) ? (*pupper - *plower + 1) : (-(*plower - *pupper + 1)); 
 
        #ifdef KMP_DEBUG 
        { 
            const char * buff; 
            // create format specifiers before the debug output 
            buff = __kmp_str_format( 
                "__kmpc_for_static_init: (serial) liter=%%d lower=%%%s upper=%%%s stride = %%%s\n", 
                traits_t< T >::spec, traits_t< T >::spec, traits_t< ST >::spec ); 
            KD_TRACE(100, ( buff, *plastiter, *plower, *pupper, *pstride ) ); 
            __kmp_str_free( &buff ); 
        } 
        #endif 
        KE_TRACE( 10, ("__kmpc_for_static_init: T#%d return\n", global_tid ) ); 
 
#if OMPT_SUPPORT && OMPT_TRACE 
        if (ompt_enabled && 
            ompt_callbacks.ompt_callback(ompt_event_loop_begin)) { 
            ompt_callbacks.ompt_callback(ompt_event_loop_begin)( 
                team_info->parallel_id, task_info->task_id, 
                team_info->microtask); 
        } 
#endif 
        return; 
    } 
    nth = team->t.t_nproc; 
    if ( nth == 1 ) { 
        if( plastiter != NULL ) 
            *plastiter = TRUE; 
        *pstride = (incr > 0) ? (*pupper - *plower + 1) : (-(*plower - *pupper + 1)); 
        #ifdef KMP_DEBUG 
        { 
            const char * buff; 
            // create format specifiers before the debug output 
            buff = __kmp_str_format( 
                "__kmpc_for_static_init: (serial) liter=%%d lower=%%%s upper=%%%s stride = %%%s\n", 
                traits_t< T >::spec, traits_t< T >::spec, traits_t< ST >::spec ); 
            KD_TRACE(100, ( buff, *plastiter, *plower, *pupper, *pstride ) ); 
            __kmp_str_free( &buff ); 
        } 
        #endif 
        KE_TRACE( 10, ("__kmpc_for_static_init: T#%d return\n", global_tid ) ); 
 
#if OMPT_SUPPORT && OMPT_TRACE 
        if (ompt_enabled && 
            ompt_callbacks.ompt_callback(ompt_event_loop_begin)) { 
            ompt_callbacks.ompt_callback(ompt_event_loop_begin)( 
                team_info->parallel_id, task_info->task_id, 
                team_info->microtask); 
        } 
#endif 
        return; 
    } 
 
    /* compute trip count */ 
    if ( incr == 1 ) { 
        trip_count = *pupper - *plower + 1; 
    } else if (incr == -1) { 
        trip_count = *plower - *pupper + 1; 
    } else { 
        if ( incr > 1 ) {  // the check is needed for unsigned division when incr < 0 
            trip_count = (*pupper - *plower) / incr + 1; 
        } else { 
            trip_count = (*plower - *pupper) / ( -incr ) + 1; 
        } 
    } 
 
    if ( __kmp_env_consistency_check ) { 
        /* tripcount overflow? */ 
        if ( trip_count == 0 && *pupper != *plower ) { 
            __kmp_error_construct( kmp_i18n_msg_CnsIterationRangeTooLarge, ct_pdo, loc ); 
        } 
    } 
    KMP_COUNT_VALUE (FOR_static_iterations, trip_count); 
 
    /* compute remaining parameters */ 
    switch ( schedtype ) { 
    case kmp_sch_static: 
        { 
            if ( trip_count < nth ) { 
                KMP_DEBUG_ASSERT( 
                    __kmp_static == kmp_sch_static_greedy || \ 
                    __kmp_static == kmp_sch_static_balanced 
                ); // Unknown static scheduling type. 
                if ( tid < trip_count ) { 
                    *pupper = *plower = *plower + tid * incr; 
                } else { 
                    *plower = *pupper + incr; 
                } 
                if( plastiter != NULL ) 
                    *plastiter = ( tid == trip_count - 1 ); 
            } else { 
                if ( __kmp_static == kmp_sch_static_balanced ) { 
                    UT small_chunk = trip_count / nth;
                    UT extras = trip_count % nth;
                    *plower += incr * ( tid * small_chunk + ( tid < extras ? tid : extras ) ); 
                    *pupper = *plower + small_chunk * incr - ( tid < extras ? 0 : incr ); 
                    if( plastiter != NULL ) 
                        *plastiter = ( tid == nth - 1 ); 
                } else { 
                    T big_chunk_inc_count = ( trip_count/nth +
                                                     ( ( trip_count % nth ) ? 1 : 0) ) * incr; 
                    T old_upper = *pupper;
 
                    KMP_DEBUG_ASSERT( __kmp_static == kmp_sch_static_greedy ); 
                        // Unknown static scheduling type. 
 
                    *plower += tid * big_chunk_inc_count; 
                    *pupper = *plower + big_chunk_inc_count - incr; 
                    if ( incr > 0 ) { 
                        if( *pupper < *plower ) 
                            *pupper = i_maxmin< T >::mx; 
                        if( plastiter != NULL ) 
                            *plastiter = *plower <= old_upper && *pupper > old_upper - incr; 
                        if ( *pupper > old_upper ) *pupper = old_upper; // tracker C73258 
                    } else { 
                        if( *pupper > *plower ) 
                            *pupper = i_maxmin< T >::mn; 
                        if( plastiter != NULL ) 
                            *plastiter = *plower >= old_upper && *pupper < old_upper - incr; 
                        if ( *pupper < old_upper ) *pupper = old_upper; // tracker C73258 
                    } 
                } 
            } 
            break; 
        } 
    case kmp_sch_static_chunked: 
        { 
            ST span;
            if ( chunk < 1 ) { 
                chunk = 1; 
            } 
            span = chunk * incr; 
            *pstride = span * nth; 
            *plower = *plower + (span * tid); 
            *pupper = *plower + span - incr; 
            if( plastiter != NULL ) 
                *plastiter = (tid == ((trip_count - 1)/( UT )chunk) % nth); 
            break; 
        } 
    default: 
        KMP_ASSERT2( 0, "__kmpc_for_static_init: unknown scheduling type" ); 
        break; 
    } 
 
#if USE_ITT_BUILD 
    // Report loop metadata 
    if ( KMP_MASTER_TID(tid) && __itt_metadata_add_ptr && __kmp_forkjoin_frames_mode == 3 && 
#if OMP_40_ENABLED 
        th->th.th_teams_microtask == NULL && 
#endif 
        team->t.t_active_level == 1 ) 
    { 
        kmp_uint64 cur_chunk = chunk; 
        // Calculate chunk in case it was not specified; it is specified for kmp_sch_static_chunked 
        if ( schedtype == kmp_sch_static ) { 
            cur_chunk = trip_count / nth + ( ( trip_count % nth ) ? 1 : 0); 
        } 
        // 0 - "static" schedule 
        __kmp_itt_metadata_loop(loc, 0, trip_count, cur_chunk); 
    } 
#endif 
    #ifdef KMP_DEBUG 
    { 
        const char * buff; 
        // create format specifiers before the debug output 
        buff = __kmp_str_format( 
            "__kmpc_for_static_init: liter=%%d lower=%%%s upper=%%%s stride = %%%s signed?<%s>\n", 
            traits_t< T >::spec, traits_t< T >::spec, traits_t< ST >::spec, traits_t< T >::spec ); 
        KD_TRACE(100, ( buff, *plastiter, *plower, *pupper, *pstride ) ); 
        __kmp_str_free( &buff ); 
    } 
    #endif 
    KE_TRACE( 10, ("__kmpc_for_static_init: T#%d return\n", global_tid ) ); 
 
#if OMPT_SUPPORT && OMPT_TRACE 
    if (ompt_enabled && 
        ompt_callbacks.ompt_callback(ompt_event_loop_begin)) { 
        ompt_callbacks.ompt_callback(ompt_event_loop_begin)( 
            team_info->parallel_id, task_info->task_id, team_info->microtask); 
    } 
#endif 
 
    return; 
} 
 
template< typename T > 
static void 
__kmp_dist_for_static_init( 
    ident_t                          *loc, 
    kmp_int32                         gtid, 
    kmp_int32                         schedule, 
    kmp_int32                        *plastiter, 
    T                                *plower, 
    T                                *pupper, 
    T                                *pupperDist, 
    typename traits_t< T >::signed_t *pstride, 
    typename traits_t< T >::signed_t  incr, 
    typename traits_t< T >::signed_t  chunk 
) { 
    KMP_COUNT_BLOCK(OMP_DISTRIBUTE); 
    typedef typename traits_t< T >::unsigned_t  UT; 
    typedef typename traits_t< T >::signed_t    ST; 
    kmp_uint32  tid;
    kmp_uint32  nth;
    kmp_uint32  team_id;
    kmp_uint32  nteams;
    UT          trip_count;
    kmp_team_t *team;
    kmp_info_t * th; 
 
    KMP_DEBUG_ASSERT( plastiter && plower && pupper && pupperDist && pstride ); 
    KE_TRACE( 10, ("__kmpc_dist_for_static_init called (%d)\n", gtid)); 
    #ifdef KMP_DEBUG 
    { 
        const char * buff; 
        // create format specifiers before the debug output 
        buff = __kmp_str_format( 
            "__kmpc_dist_for_static_init: T#%%d schedLoop=%%d liter=%%d "\ 
            "iter=(%%%s, %%%s, %%%s) chunk=%%%s signed?<%s>\n", 
            traits_t< T >::spec, traits_t< T >::spec, traits_t< ST >::spec, 
            traits_t< ST >::spec, traits_t< T >::spec ); 
        KD_TRACE(100, ( buff, gtid, schedule, *plastiter, 
                       *plower, *pupper, incr, chunk ) ); 
        __kmp_str_free( &buff ); 
    } 
    #endif 
 
    if( __kmp_env_consistency_check ) { 
        __kmp_push_workshare( gtid, ct_pdo, loc ); 
        if( incr == 0 ) { 
            __kmp_error_construct( kmp_i18n_msg_CnsLoopIncrZeroProhibited, ct_pdo, loc ); 
        } 
        if( incr > 0 ? (*pupper < *plower) : (*plower < *pupper) ) { 
            // The loop is illegal. 
            // Some zero-trip loops maintained by compiler, e.g.: 
            //   for(i=10;i<0;++i) // lower >= upper - run-time check 
            //   for(i=0;i>10;--i) // lower <= upper - run-time check 
            //   for(i=0;i>10;++i) // incr > 0       - compile-time check 
            //   for(i=10;i<0;--i) // incr < 0       - compile-time check 
            // Compiler does not check the following illegal loops: 
            //   for(i=0;i<10;i+=incr) // where incr<0 
            //   for(i=10;i>0;i-=incr) // where incr<0 
            __kmp_error_construct( kmp_i18n_msg_CnsLoopIncrIllegal, ct_pdo, loc ); 
        } 
    } 
    tid = __kmp_tid_from_gtid( gtid ); 
    th = __kmp_threads[gtid]; 
    nth = th->th.th_team_nproc; 
    team = th->th.th_team; 
    #if OMP_40_ENABLED 
    KMP_DEBUG_ASSERT(th->th.th_teams_microtask);   // we are in the teams construct 
    nteams = th->th.th_teams_size.nteams; 
    #endif 
    team_id = team->t.t_master_tid; 
    KMP_DEBUG_ASSERT(nteams == team->t.t_parent->t.t_nproc); 
 
    // compute global trip count 
    if( incr == 1 ) { 
        trip_count = *pupper - *plower + 1; 
    } else if(incr == -1) { 
        trip_count = *plower - *pupper + 1; 
    } else { 
        trip_count = (ST)(*pupper - *plower) / incr + 1; // cast to signed to cover incr<0 case 
    } 
 
    *pstride = *pupper - *plower;  // just in case (can be unused) 
    if( trip_count <= nteams ) { 
        KMP_DEBUG_ASSERT( 
            __kmp_static == kmp_sch_static_greedy || \ 
            __kmp_static == kmp_sch_static_balanced 
        ); // Unknown static scheduling type. 
        // only masters of some teams get single iteration, other threads get nothing 
        if( team_id < trip_count && tid == 0 ) { 
            *pupper = *pupperDist = *plower = *plower + team_id * incr; 
        } else { 
            *pupperDist = *pupper; 
            *plower = *pupper + incr; // compiler should skip loop body 
        } 
        if( plastiter != NULL ) 
            *plastiter = ( tid == 0 && team_id == trip_count - 1 ); 
    } else { 
        // Get the team's chunk first (each team gets at most one chunk) 
        if( __kmp_static == kmp_sch_static_balanced ) { 
            UT chunkD = trip_count / nteams;
            UT extras = trip_count % nteams;
            *plower += incr * ( team_id * chunkD + ( team_id < extras ? team_id : extras ) ); 
            *pupperDist = *plower + chunkD * incr - ( team_id < extras ? 0 : incr ); 
            if( plastiter != NULL ) 
                *plastiter = ( team_id == nteams - 1 ); 
        } else { 
            T chunk_inc_count =
                ( trip_count / nteams + ( ( trip_count % nteams ) ? 1 : 0) ) * incr; 
            T upper = *pupper;
            KMP_DEBUG_ASSERT( __kmp_static == kmp_sch_static_greedy ); 
                // Unknown static scheduling type. 
            *plower += team_id * chunk_inc_count; 
            *pupperDist = *plower + chunk_inc_count - incr; 
            // Check/correct bounds if needed 
            if( incr > 0 ) { 
                if( *pupperDist < *plower ) 
                    *pupperDist = i_maxmin< T >::mx; 
                if( plastiter != NULL ) 
                    *plastiter = *plower <= upper && *pupperDist > upper - incr; 
                if( *pupperDist > upper ) 
                    *pupperDist = upper; // tracker C73258 
                if( *plower > *pupperDist ) { 
                    *pupper = *pupperDist;  // no iterations available for the team 
                    goto end; 
                } 
            } else { 
                if( *pupperDist > *plower ) 
                    *pupperDist = i_maxmin< T >::mn; 
                if( plastiter != NULL ) 
                    *plastiter = *plower >= upper && *pupperDist < upper - incr; 
                if( *pupperDist < upper ) 
                    *pupperDist = upper; // tracker C73258 
                if( *plower < *pupperDist ) { 
                    *pupper = *pupperDist;  // no iterations available for the team 
                    goto end; 
                } 
            } 
        } 
        // Get the parallel loop chunk now (for thread) 
        // compute trip count for team's chunk 
        if( incr == 1 ) { 
            trip_count = *pupperDist - *plower + 1; 
        } else if(incr == -1) { 
            trip_count = *plower - *pupperDist + 1; 
        } else { 
            trip_count = (ST)(*pupperDist - *plower) / incr + 1; 
        } 
        KMP_DEBUG_ASSERT( trip_count ); 
        switch( schedule ) { 
        case kmp_sch_static: 
        { 
            if( trip_count <= nth ) { 
                KMP_DEBUG_ASSERT( 
                    __kmp_static == kmp_sch_static_greedy || \ 
                    __kmp_static == kmp_sch_static_balanced 
                ); // Unknown static scheduling type. 
                if( tid < trip_count ) 
                    *pupper = *plower = *plower + tid * incr; 
                else 
                    *plower = *pupper + incr; // no iterations available 
                if( plastiter != NULL ) 
                    if( *plastiter != 0 && !( tid == trip_count - 1 ) ) 
                        *plastiter = 0; 
            } else { 
                if( __kmp_static == kmp_sch_static_balanced ) { 
                    UT chunkL = trip_count / nth;
                    UT extras = trip_count % nth;
                    *plower += incr * (tid * chunkL + (tid < extras ? tid : extras)); 
                    *pupper = *plower + chunkL * incr - (tid < extras ? 0 : incr); 
                    if( plastiter != NULL ) 
                        if( *plastiter != 0 && !( tid == nth - 1 ) ) 
                            *plastiter = 0; 
                } else { 
                    T chunk_inc_count =
                        ( trip_count / nth + ( ( trip_count % nth ) ? 1 : 0) ) * incr; 
                    T upper = *pupperDist;
                    KMP_DEBUG_ASSERT( __kmp_static == kmp_sch_static_greedy ); 
                        // Unknown static scheduling type. 
                    *plower += tid * chunk_inc_count; 
                    *pupper = *plower + chunk_inc_count - incr; 
                    if( incr > 0 ) { 
                        if( *pupper < *plower ) 
                            *pupper = i_maxmin< T >::mx; 
                        if( plastiter != NULL ) 
                            if( *plastiter != 0 && !(*plower <= upper && *pupper > upper - incr) ) 
                                *plastiter = 0; 
                        if( *pupper > upper ) 
                            *pupper = upper;//tracker C73258 
                    } else { 
                        if( *pupper > *plower ) 
                            *pupper = i_maxmin< T >::mn; 
                        if( plastiter != NULL ) 
                            if( *plastiter != 0 && !(*plower >= upper && *pupper < upper - incr) ) 
                                *plastiter = 0; 
                        if( *pupper < upper ) 
                            *pupper = upper;//tracker C73258 
                    } 
                } 
            } 
            break; 
        } 
        case kmp_sch_static_chunked: 
        { 
            ST span;
            if( chunk < 1 ) 
                chunk = 1; 
            span = chunk * incr; 
            *pstride = span * nth; 
            *plower = *plower + (span * tid); 
            *pupper = *plower + span - incr; 
            if( plastiter != NULL ) 
                if( *plastiter != 0 && !(tid == ((trip_count - 1) / ( UT )chunk) % nth) ) 
                    *plastiter = 0; 
            break; 
        } 
        default: 
            KMP_ASSERT2( 0, "__kmpc_dist_for_static_init: unknown loop scheduling type" ); 
            break; 
        } 
    } 
    end:; 
    #ifdef KMP_DEBUG 
    { 
        const char * buff; 
        // create format specifiers before the debug output 
        buff = __kmp_str_format( 
            "__kmpc_dist_for_static_init: last=%%d lo=%%%s up=%%%s upDist=%%%s "\ 
            "stride=%%%s signed?<%s>\n", 
            traits_t< T >::spec, traits_t< T >::spec, traits_t< T >::spec, 
            traits_t< ST >::spec, traits_t< T >::spec ); 
        KD_TRACE(100, ( buff, *plastiter, *plower, *pupper, *pupperDist, *pstride ) ); 
        __kmp_str_free( &buff ); 
    } 
    #endif 
    KE_TRACE( 10, ("__kmpc_dist_for_static_init: T#%d return\n", gtid ) ); 
    return; 
} 
 
template< typename T > 
static void 
__kmp_team_static_init( 
    ident_t                          *loc, 
    kmp_int32                         gtid, 
    kmp_int32                        *p_last, 
    T                                *p_lb, 
    T                                *p_ub, 
    typename traits_t< T >::signed_t *p_st, 
    typename traits_t< T >::signed_t  incr, 
    typename traits_t< T >::signed_t  chunk 
) { 
    // The routine returns the first chunk distributed to the team and 
    // stride for next chunks calculation. 
    // Last iteration flag set for the team that will execute 
    // the last iteration of the loop. 
    // The routine is called for dist_schedue(static,chunk) only. 
    typedef typename traits_t< T >::unsigned_t  UT; 
    typedef typename traits_t< T >::signed_t    ST; 
    kmp_uint32  team_id; 
    kmp_uint32  nteams; 
    UT          trip_count; 
    T           lower; 
    T           upper; 
    ST          span; 
    kmp_team_t *team; 
    kmp_info_t *th; 
 
    KMP_DEBUG_ASSERT( p_last && p_lb && p_ub && p_st ); 
    KE_TRACE( 10, ("__kmp_team_static_init called (%d)\n", gtid)); 
    #ifdef KMP_DEBUG 
    { 
        const char * buff; 
        // create format specifiers before the debug output 
        buff = __kmp_str_format( "__kmp_team_static_init enter: T#%%d liter=%%d "\ 
            "iter=(%%%s, %%%s, %%%s) chunk %%%s; signed?<%s>\n", 
            traits_t< T >::spec, traits_t< T >::spec, traits_t< ST >::spec, 
            traits_t< ST >::spec, traits_t< T >::spec ); 
        KD_TRACE(100, ( buff, gtid, *p_last, *p_lb, *p_ub, *p_st, chunk ) ); 
        __kmp_str_free( &buff ); 
    } 
    #endif 
 
    lower = *p_lb; 
    upper = *p_ub; 
    if( __kmp_env_consistency_check ) { 
        if( incr == 0 ) { 
            __kmp_error_construct( kmp_i18n_msg_CnsLoopIncrZeroProhibited, ct_pdo, loc ); 
        } 
        if( incr > 0 ? (upper < lower) : (lower < upper) ) { 
            // The loop is illegal. 
            // Some zero-trip loops maintained by compiler, e.g.: 
            //   for(i=10;i<0;++i) // lower >= upper - run-time check 
            //   for(i=0;i>10;--i) // lower <= upper - run-time check 
            //   for(i=0;i>10;++i) // incr > 0       - compile-time check 
            //   for(i=10;i<0;--i) // incr < 0       - compile-time check 
            // Compiler does not check the following illegal loops: 
            //   for(i=0;i<10;i+=incr) // where incr<0 
            //   for(i=10;i>0;i-=incr) // where incr<0 
            __kmp_error_construct( kmp_i18n_msg_CnsLoopIncrIllegal, ct_pdo, loc ); 
        } 
    } 
    th = __kmp_threads[gtid]; 
    team = th->th.th_team; 
    #if OMP_40_ENABLED 
    KMP_DEBUG_ASSERT(th->th.th_teams_microtask);   // we are in the teams construct 
    nteams = th->th.th_teams_size.nteams; 
    #endif 
    team_id = team->t.t_master_tid; 
    KMP_DEBUG_ASSERT(nteams == team->t.t_parent->t.t_nproc); 
 
    // compute trip count 
    if( incr == 1 ) { 
        trip_count = upper - lower + 1; 
    } else if(incr == -1) { 
        trip_count = lower - upper + 1; 
    } else { 
        trip_count = (ST)(upper - lower) / incr + 1; // cast to signed to cover incr<0 case 
    } 
    if( chunk < 1 ) 
        chunk = 1; 
    span = chunk * incr; 
    *p_st = span * nteams; 
    *p_lb = lower + (span * team_id); 
    *p_ub = *p_lb + span - incr; 
    if ( p_last != NULL ) 
        *p_last = (team_id == ((trip_count - 1)/(UT)chunk) % nteams); 
    // Correct upper bound if needed 
    if( incr > 0 ) { 
        if( *p_ub < *p_lb ) // overflow? 
            *p_ub = i_maxmin< T >::mx; 
        if( *p_ub > upper ) 
            *p_ub = upper; // tracker C73258 
    } else {   // incr < 0 
        if( *p_ub > *p_lb ) 
            *p_ub = i_maxmin< T >::mn; 
        if( *p_ub < upper ) 
            *p_ub = upper; // tracker C73258 
    } 
    #ifdef KMP_DEBUG 
    { 
        const char * buff; 
        // create format specifiers before the debug output 
        buff = __kmp_str_format( "__kmp_team_static_init exit: T#%%d team%%u liter=%%d "\ 
            "iter=(%%%s, %%%s, %%%s) chunk %%%s\n", 
            traits_t< T >::spec, traits_t< T >::spec, traits_t< ST >::spec, 
            traits_t< ST >::spec ); 
        KD_TRACE(100, ( buff, gtid, team_id, *p_last, *p_lb, *p_ub, *p_st, chunk ) ); 
        __kmp_str_free( &buff ); 
    } 
    #endif 
} 
 
//-------------------------------------------------------------------------------------- 
extern "C" { 
 
/*! 
@ingroup WORK_SHARING 
@param    loc       Source code location 
@param    gtid      Global thread id of this thread 
@param    schedtype  Scheduling type 
@param    plastiter Pointer to the "last iteration" flag 
@param    plower    Pointer to the lower bound 
@param    pupper    Pointer to the upper bound 
@param    pstride   Pointer to the stride 
@param    incr      Loop increment 
@param    chunk     The chunk size 
 
Each of the four functions here are identical apart from the argument types. 
 
The functions compute the upper and lower bounds and stride to be used for the set of iterations 
to be executed by the current thread from the statically scheduled loop that is described by the 
initial values of the bounds, stride, increment and chunk size. 
 
@{ 
*/ 
void 
__kmpc_for_static_init_4( ident_t *loc, kmp_int32 gtid, kmp_int32 schedtype, kmp_int32 *plastiter, 
                      kmp_int32 *plower, kmp_int32 *pupper, 
                      kmp_int32 *pstride, kmp_int32 incr, kmp_int32 chunk ) 
{ 
    __kmp_for_static_init< kmp_int32 >( 
                      loc, gtid, schedtype, plastiter, plower, pupper, pstride, incr, chunk ); 
} 
 
/*! 
 See @ref __kmpc_for_static_init_4 
 */ 
void 
__kmpc_for_static_init_4u( ident_t *loc, kmp_int32 gtid, kmp_int32 schedtype, kmp_int32 *plastiter, 
                      kmp_uint32 *plower, kmp_uint32 *pupper, 
                      kmp_int32 *pstride, kmp_int32 incr, kmp_int32 chunk ) 
{ 
    __kmp_for_static_init< kmp_uint32 >( 
                      loc, gtid, schedtype, plastiter, plower, pupper, pstride, incr, chunk ); 
} 
 
/*! 
 See @ref __kmpc_for_static_init_4 
 */ 
void 
__kmpc_for_static_init_8( ident_t *loc, kmp_int32 gtid, kmp_int32 schedtype, kmp_int32 *plastiter, 
                      kmp_int64 *plower, kmp_int64 *pupper, 
                      kmp_int64 *pstride, kmp_int64 incr, kmp_int64 chunk ) 
{ 
    __kmp_for_static_init< kmp_int64 >( 
                      loc, gtid, schedtype, plastiter, plower, pupper, pstride, incr, chunk ); 
} 
 
/*! 
 See @ref __kmpc_for_static_init_4 
 */ 
void 
__kmpc_for_static_init_8u( ident_t *loc, kmp_int32 gtid, kmp_int32 schedtype, kmp_int32 *plastiter, 
                      kmp_uint64 *plower, kmp_uint64 *pupper, 
                      kmp_int64 *pstride, kmp_int64 incr, kmp_int64 chunk ) 
{ 
    __kmp_for_static_init< kmp_uint64 >( 
                      loc, gtid, schedtype, plastiter, plower, pupper, pstride, incr, chunk ); 
} 
/*! 
@} 
*/ 
 
/*! 
@ingroup WORK_SHARING 
@param    loc       Source code location 
@param    gtid      Global thread id of this thread 
@param    schedule  Scheduling type for the parallel loop 
@param    plastiter Pointer to the "last iteration" flag 
@param    plower    Pointer to the lower bound 
@param    pupper    Pointer to the upper bound of loop chunk 
@param    pupperD   Pointer to the upper bound of dist_chunk 
@param    pstride   Pointer to the stride for parallel loop 
@param    incr      Loop increment 
@param    chunk     The chunk size for the parallel loop 
 
Each of the four functions here are identical apart from the argument types. 
 
The functions compute the upper and lower bounds and strides to be used for the set of iterations 
to be executed by the current thread from the statically scheduled loop that is described by the 
initial values of the bounds, strides, increment and chunks for parallel loop and distribute 
constructs. 
 
@{ 
*/ 
void 
__kmpc_dist_for_static_init_4( 
    ident_t *loc, kmp_int32 gtid, kmp_int32 schedule, kmp_int32 *plastiter, 
    kmp_int32 *plower, kmp_int32 *pupper, kmp_int32 *pupperD, 
    kmp_int32 *pstride, kmp_int32 incr, kmp_int32 chunk ) 
{ 
    __kmp_dist_for_static_init< kmp_int32 >( 
        loc, gtid, schedule, plastiter, plower, pupper, pupperD, pstride, incr, chunk ); 
} 
 
/*! 
 See @ref __kmpc_dist_for_static_init_4 
 */ 
void 
__kmpc_dist_for_static_init_4u( 
    ident_t *loc, kmp_int32 gtid, kmp_int32 schedule, kmp_int32 *plastiter, 
    kmp_uint32 *plower, kmp_uint32 *pupper, kmp_uint32 *pupperD, 
    kmp_int32 *pstride, kmp_int32 incr, kmp_int32 chunk ) 
{ 
    __kmp_dist_for_static_init< kmp_uint32 >( 
        loc, gtid, schedule, plastiter, plower, pupper, pupperD, pstride, incr, chunk ); 
} 
 
/*! 
 See @ref __kmpc_dist_for_static_init_4 
 */ 
void 
__kmpc_dist_for_static_init_8( 
    ident_t *loc, kmp_int32 gtid, kmp_int32 schedule, kmp_int32 *plastiter, 
    kmp_int64 *plower, kmp_int64 *pupper, kmp_int64 *pupperD, 
    kmp_int64 *pstride, kmp_int64 incr, kmp_int64 chunk ) 
{ 
    __kmp_dist_for_static_init< kmp_int64 >( 
        loc, gtid, schedule, plastiter, plower, pupper, pupperD, pstride, incr, chunk ); 
} 
 
/*! 
 See @ref __kmpc_dist_for_static_init_4 
 */ 
void 
__kmpc_dist_for_static_init_8u( 
    ident_t *loc, kmp_int32 gtid, kmp_int32 schedule, kmp_int32 *plastiter, 
    kmp_uint64 *plower, kmp_uint64 *pupper, kmp_uint64 *pupperD, 
    kmp_int64 *pstride, kmp_int64 incr, kmp_int64 chunk ) 
{ 
    __kmp_dist_for_static_init< kmp_uint64 >( 
        loc, gtid, schedule, plastiter, plower, pupper, pupperD, pstride, incr, chunk ); 
} 
/*! 
@} 
*/ 
 
//----------------------------------------------------------------------------------------- 
// Auxiliary routines for Distribute Parallel Loop construct implementation 
//    Transfer call to template< type T > 
//    __kmp_team_static_init( ident_t *loc, int gtid, 
//        int *p_last, T *lb, T *ub, ST *st, ST incr, ST chunk ) 
 
/*! 
@ingroup WORK_SHARING 
@{ 
@param loc Source location 
@param gtid Global thread id 
@param p_last pointer to last iteration flag 
@param p_lb  pointer to Lower bound 
@param p_ub  pointer to Upper bound 
@param p_st  Step (or increment if you prefer) 
@param incr  Loop increment 
@param chunk The chunk size to block with 
 
The functions compute the upper and lower bounds and stride to be used for the set of iterations 
to be executed by the current team from the statically scheduled loop that is described by the 
initial values of the bounds, stride, increment and chunk for the distribute construct as part of 
composite distribute parallel loop construct. 
These functions are all identical apart from the types of the arguments. 
*/ 
 
void 
__kmpc_team_static_init_4( 
    ident_t *loc, kmp_int32 gtid, kmp_int32 *p_last, 
    kmp_int32 *p_lb, kmp_int32 *p_ub, kmp_int32 *p_st, kmp_int32 incr, kmp_int32 chunk ) 
{ 
    KMP_DEBUG_ASSERT( __kmp_init_serial ); 
    __kmp_team_static_init< kmp_int32 >( loc, gtid, p_last, p_lb, p_ub, p_st, incr, chunk ); 
} 
 
/*! 
 See @ref __kmpc_team_static_init_4 
 */ 
void 
__kmpc_team_static_init_4u( 
    ident_t *loc, kmp_int32 gtid, kmp_int32 *p_last, 
    kmp_uint32 *p_lb, kmp_uint32 *p_ub, kmp_int32 *p_st, kmp_int32 incr, kmp_int32 chunk ) 
{ 
    KMP_DEBUG_ASSERT( __kmp_init_serial ); 
    __kmp_team_static_init< kmp_uint32 >( loc, gtid, p_last, p_lb, p_ub, p_st, incr, chunk ); 
} 
 
/*! 
 See @ref __kmpc_team_static_init_4 
 */ 
void 
__kmpc_team_static_init_8( 
    ident_t *loc, kmp_int32 gtid, kmp_int32 *p_last, 
    kmp_int64 *p_lb, kmp_int64 *p_ub, kmp_int64 *p_st, kmp_int64 incr, kmp_int64 chunk ) 
{ 
    KMP_DEBUG_ASSERT( __kmp_init_serial ); 
    __kmp_team_static_init< kmp_int64 >( loc, gtid, p_last, p_lb, p_ub, p_st, incr, chunk ); 
} 
 
/*! 
 See @ref __kmpc_team_static_init_4 
 */ 
void 
__kmpc_team_static_init_8u( 
    ident_t *loc, kmp_int32 gtid, kmp_int32 *p_last, 
    kmp_uint64 *p_lb, kmp_uint64 *p_ub, kmp_int64 *p_st, kmp_int64 incr, kmp_int64 chunk ) 
{ 
    KMP_DEBUG_ASSERT( __kmp_init_serial ); 
    __kmp_team_static_init< kmp_uint64 >( loc, gtid, p_last, p_lb, p_ub, p_st, incr, chunk ); 
} 
/*! 
@} 
*/ 
 
} // extern "C"