Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1 | /* |
Jonathan Peyton | de4749b | 2016-12-14 23:01:24 +0000 | [diff] [blame] | 2 | * kmp_runtime.cpp -- KPTS runtime support library |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3 | */ |
| 4 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5 | //===----------------------------------------------------------------------===// |
| 6 | // |
| 7 | // The LLVM Compiler Infrastructure |
| 8 | // |
| 9 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 10 | // Source Licenses. See LICENSE.txt for details. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 14 | #include "kmp.h" |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 15 | #include "kmp_affinity.h" |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 16 | #include "kmp_atomic.h" |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 17 | #include "kmp_environment.h" |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 18 | #include "kmp_error.h" |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 19 | #include "kmp_i18n.h" |
| 20 | #include "kmp_io.h" |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 21 | #include "kmp_itt.h" |
| 22 | #include "kmp_settings.h" |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 23 | #include "kmp_stats.h" |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 24 | #include "kmp_str.h" |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 25 | #include "kmp_wait_release.h" |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 26 | #include "kmp_wrapper_getpid.h" |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 27 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 28 | #if OMPT_SUPPORT |
| 29 | #include "ompt-specific.h" |
| 30 | #endif |
| 31 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 32 | /* these are temporary issues to be dealt with */ |
| 33 | #define KMP_USE_PRCTL 0 |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 34 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 35 | #if KMP_OS_WINDOWS |
| 36 | #include <process.h> |
| 37 | #endif |
| 38 | |
Jonas Hahnfeld | 50fed04 | 2016-11-07 15:58:36 +0000 | [diff] [blame] | 39 | #include "tsan_annotations.h" |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 40 | |
| 41 | #if defined(KMP_GOMP_COMPAT) |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 42 | char const __kmp_version_alt_comp[] = |
| 43 | KMP_VERSION_PREFIX "alternative compiler support: yes"; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 44 | #endif /* defined(KMP_GOMP_COMPAT) */ |
| 45 | |
| 46 | char const __kmp_version_omp_api[] = KMP_VERSION_PREFIX "API version: " |
Jonathan Peyton | e844a54 | 2017-03-06 22:07:40 +0000 | [diff] [blame] | 47 | #if OMP_50_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 48 | "5.0 (201611)"; |
Jonathan Peyton | e844a54 | 2017-03-06 22:07:40 +0000 | [diff] [blame] | 49 | #elif OMP_45_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 50 | "4.5 (201511)"; |
Jonathan Peyton | 74f3ffc | 2016-09-30 15:50:14 +0000 | [diff] [blame] | 51 | #elif OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 52 | "4.0 (201307)"; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 53 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 54 | "3.1 (201107)"; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 55 | #endif |
| 56 | |
| 57 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 58 | char const __kmp_version_lock[] = |
| 59 | KMP_VERSION_PREFIX "lock type: run time selectable"; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 60 | #endif /* KMP_DEBUG */ |
| 61 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 62 | #define KMP_MIN(x, y) ((x) < (y) ? (x) : (y)) |
Jim Cownie | 181b4bb | 2013-12-23 17:28:57 +0000 | [diff] [blame] | 63 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 64 | /* ------------------------------------------------------------------------ */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 65 | |
| 66 | kmp_info_t __kmp_monitor; |
| 67 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 68 | /* Forward declarations */ |
| 69 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 70 | void __kmp_cleanup(void); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 71 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 72 | static void __kmp_initialize_info(kmp_info_t *, kmp_team_t *, int tid, |
| 73 | int gtid); |
| 74 | static void __kmp_initialize_team(kmp_team_t *team, int new_nproc, |
| 75 | kmp_internal_control_t *new_icvs, |
| 76 | ident_t *loc); |
Jonathan Peyton | 2321d57 | 2015-06-08 19:25:25 +0000 | [diff] [blame] | 77 | #if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 78 | static void __kmp_partition_places(kmp_team_t *team, |
| 79 | int update_master_only = 0); |
Jonathan Peyton | 2321d57 | 2015-06-08 19:25:25 +0000 | [diff] [blame] | 80 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 81 | static void __kmp_do_serial_initialize(void); |
| 82 | void __kmp_fork_barrier(int gtid, int tid); |
| 83 | void __kmp_join_barrier(int gtid); |
| 84 | void __kmp_setup_icv_copy(kmp_team_t *team, int new_nproc, |
| 85 | kmp_internal_control_t *new_icvs, ident_t *loc); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 86 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 87 | #ifdef USE_LOAD_BALANCE |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 88 | static int __kmp_load_balance_nproc(kmp_root_t *root, int set_nproc); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 89 | #endif |
| 90 | |
| 91 | static int __kmp_expand_threads(int nWish, int nNeed); |
Jonathan Peyton | 2321d57 | 2015-06-08 19:25:25 +0000 | [diff] [blame] | 92 | #if KMP_OS_WINDOWS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 93 | static int __kmp_unregister_root_other_thread(int gtid); |
Jonathan Peyton | 2321d57 | 2015-06-08 19:25:25 +0000 | [diff] [blame] | 94 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 95 | static void __kmp_unregister_library(void); // called by __kmp_internal_end() |
| 96 | static void __kmp_reap_thread(kmp_info_t *thread, int is_root); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 97 | static kmp_info_t *__kmp_thread_pool_insert_pt = NULL; |
| 98 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 99 | /* Calculate the identifier of the current thread */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 100 | /* fast (and somewhat portable) way to get unique identifier of executing |
| 101 | thread. Returns KMP_GTID_DNE if we haven't been assigned a gtid. */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 102 | int __kmp_get_global_thread_id() { |
| 103 | int i; |
| 104 | kmp_info_t **other_threads; |
| 105 | size_t stack_data; |
| 106 | char *stack_addr; |
| 107 | size_t stack_size; |
| 108 | char *stack_base; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 109 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 110 | KA_TRACE( |
| 111 | 1000, |
| 112 | ("*** __kmp_get_global_thread_id: entering, nproc=%d all_nproc=%d\n", |
| 113 | __kmp_nth, __kmp_all_nth)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 114 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 115 | /* JPH - to handle the case where __kmpc_end(0) is called immediately prior to |
| 116 | a parallel region, made it return KMP_GTID_DNE to force serial_initialize |
| 117 | by caller. Had to handle KMP_GTID_DNE at all call-sites, or else guarantee |
| 118 | __kmp_init_gtid for this to work. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 119 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 120 | if (!TCR_4(__kmp_init_gtid)) |
| 121 | return KMP_GTID_DNE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 122 | |
| 123 | #ifdef KMP_TDATA_GTID |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 124 | if (TCR_4(__kmp_gtid_mode) >= 3) { |
| 125 | KA_TRACE(1000, ("*** __kmp_get_global_thread_id: using TDATA\n")); |
| 126 | return __kmp_gtid; |
| 127 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 128 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 129 | if (TCR_4(__kmp_gtid_mode) >= 2) { |
| 130 | KA_TRACE(1000, ("*** __kmp_get_global_thread_id: using keyed TLS\n")); |
| 131 | return __kmp_gtid_get_specific(); |
| 132 | } |
| 133 | KA_TRACE(1000, ("*** __kmp_get_global_thread_id: using internal alg.\n")); |
| 134 | |
| 135 | stack_addr = (char *)&stack_data; |
| 136 | other_threads = __kmp_threads; |
| 137 | |
| 138 | /* ATT: The code below is a source of potential bugs due to unsynchronized |
| 139 | access to __kmp_threads array. For example: |
| 140 | 1. Current thread loads other_threads[i] to thr and checks it, it is |
| 141 | non-NULL. |
| 142 | 2. Current thread is suspended by OS. |
| 143 | 3. Another thread unregisters and finishes (debug versions of free() |
| 144 | may fill memory with something like 0xEF). |
| 145 | 4. Current thread is resumed. |
| 146 | 5. Current thread reads junk from *thr. |
| 147 | TODO: Fix it. --ln */ |
| 148 | |
| 149 | for (i = 0; i < __kmp_threads_capacity; i++) { |
| 150 | |
| 151 | kmp_info_t *thr = (kmp_info_t *)TCR_SYNC_PTR(other_threads[i]); |
| 152 | if (!thr) |
| 153 | continue; |
| 154 | |
| 155 | stack_size = (size_t)TCR_PTR(thr->th.th_info.ds.ds_stacksize); |
| 156 | stack_base = (char *)TCR_PTR(thr->th.th_info.ds.ds_stackbase); |
| 157 | |
| 158 | /* stack grows down -- search through all of the active threads */ |
| 159 | |
| 160 | if (stack_addr <= stack_base) { |
| 161 | size_t stack_diff = stack_base - stack_addr; |
| 162 | |
| 163 | if (stack_diff <= stack_size) { |
| 164 | /* The only way we can be closer than the allocated */ |
| 165 | /* stack size is if we are running on this thread. */ |
| 166 | KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() == i); |
| 167 | return i; |
| 168 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 169 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 170 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 171 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 172 | /* get specific to try and determine our gtid */ |
| 173 | KA_TRACE(1000, |
| 174 | ("*** __kmp_get_global_thread_id: internal alg. failed to find " |
| 175 | "thread, using TLS\n")); |
| 176 | i = __kmp_gtid_get_specific(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 177 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 178 | /*fprintf( stderr, "=== %d\n", i ); */ /* GROO */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 179 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 180 | /* if we havn't been assigned a gtid, then return code */ |
| 181 | if (i < 0) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 182 | return i; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 183 | |
| 184 | /* dynamically updated stack window for uber threads to avoid get_specific |
| 185 | call */ |
| 186 | if (!TCR_4(other_threads[i]->th.th_info.ds.ds_stackgrow)) { |
| 187 | KMP_FATAL(StackOverflow, i); |
| 188 | } |
| 189 | |
| 190 | stack_base = (char *)other_threads[i]->th.th_info.ds.ds_stackbase; |
| 191 | if (stack_addr > stack_base) { |
| 192 | TCW_PTR(other_threads[i]->th.th_info.ds.ds_stackbase, stack_addr); |
| 193 | TCW_PTR(other_threads[i]->th.th_info.ds.ds_stacksize, |
| 194 | other_threads[i]->th.th_info.ds.ds_stacksize + stack_addr - |
| 195 | stack_base); |
| 196 | } else { |
| 197 | TCW_PTR(other_threads[i]->th.th_info.ds.ds_stacksize, |
| 198 | stack_base - stack_addr); |
| 199 | } |
| 200 | |
| 201 | /* Reprint stack bounds for ubermaster since they have been refined */ |
| 202 | if (__kmp_storage_map) { |
| 203 | char *stack_end = (char *)other_threads[i]->th.th_info.ds.ds_stackbase; |
| 204 | char *stack_beg = stack_end - other_threads[i]->th.th_info.ds.ds_stacksize; |
| 205 | __kmp_print_storage_map_gtid(i, stack_beg, stack_end, |
| 206 | other_threads[i]->th.th_info.ds.ds_stacksize, |
| 207 | "th_%d stack (refinement)", i); |
| 208 | } |
| 209 | return i; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 212 | int __kmp_get_global_thread_id_reg() { |
| 213 | int gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 214 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 215 | if (!__kmp_init_serial) { |
| 216 | gtid = KMP_GTID_DNE; |
| 217 | } else |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 218 | #ifdef KMP_TDATA_GTID |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 219 | if (TCR_4(__kmp_gtid_mode) >= 3) { |
| 220 | KA_TRACE(1000, ("*** __kmp_get_global_thread_id_reg: using TDATA\n")); |
| 221 | gtid = __kmp_gtid; |
| 222 | } else |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 223 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 224 | if (TCR_4(__kmp_gtid_mode) >= 2) { |
| 225 | KA_TRACE(1000, ("*** __kmp_get_global_thread_id_reg: using keyed TLS\n")); |
| 226 | gtid = __kmp_gtid_get_specific(); |
| 227 | } else { |
| 228 | KA_TRACE(1000, |
| 229 | ("*** __kmp_get_global_thread_id_reg: using internal alg.\n")); |
| 230 | gtid = __kmp_get_global_thread_id(); |
| 231 | } |
| 232 | |
| 233 | /* we must be a new uber master sibling thread */ |
| 234 | if (gtid == KMP_GTID_DNE) { |
| 235 | KA_TRACE(10, |
| 236 | ("__kmp_get_global_thread_id_reg: Encountered new root thread. " |
| 237 | "Registering a new gtid.\n")); |
| 238 | __kmp_acquire_bootstrap_lock(&__kmp_initz_lock); |
| 239 | if (!__kmp_init_serial) { |
| 240 | __kmp_do_serial_initialize(); |
| 241 | gtid = __kmp_gtid_get_specific(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 242 | } else { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 243 | gtid = __kmp_register_root(FALSE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 244 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 245 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
| 246 | /*__kmp_printf( "+++ %d\n", gtid ); */ /* GROO */ |
| 247 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 248 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 249 | KMP_DEBUG_ASSERT(gtid >= 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 250 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 251 | return gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* caller must hold forkjoin_lock */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 255 | void __kmp_check_stack_overlap(kmp_info_t *th) { |
| 256 | int f; |
| 257 | char *stack_beg = NULL; |
| 258 | char *stack_end = NULL; |
| 259 | int gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 260 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 261 | KA_TRACE(10, ("__kmp_check_stack_overlap: called\n")); |
| 262 | if (__kmp_storage_map) { |
| 263 | stack_end = (char *)th->th.th_info.ds.ds_stackbase; |
| 264 | stack_beg = stack_end - th->th.th_info.ds.ds_stacksize; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 265 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 266 | gtid = __kmp_gtid_from_thread(th); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 267 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 268 | if (gtid == KMP_GTID_MONITOR) { |
| 269 | __kmp_print_storage_map_gtid( |
| 270 | gtid, stack_beg, stack_end, th->th.th_info.ds.ds_stacksize, |
| 271 | "th_%s stack (%s)", "mon", |
| 272 | (th->th.th_info.ds.ds_stackgrow) ? "initial" : "actual"); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 273 | } else { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 274 | __kmp_print_storage_map_gtid( |
| 275 | gtid, stack_beg, stack_end, th->th.th_info.ds.ds_stacksize, |
| 276 | "th_%d stack (%s)", gtid, |
| 277 | (th->th.th_info.ds.ds_stackgrow) ? "initial" : "actual"); |
| 278 | } |
| 279 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 280 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 281 | /* No point in checking ubermaster threads since they use refinement and |
| 282 | * cannot overlap */ |
| 283 | gtid = __kmp_gtid_from_thread(th); |
| 284 | if (__kmp_env_checks == TRUE && !KMP_UBER_GTID(gtid)) { |
| 285 | KA_TRACE(10, |
| 286 | ("__kmp_check_stack_overlap: performing extensive checking\n")); |
| 287 | if (stack_beg == NULL) { |
| 288 | stack_end = (char *)th->th.th_info.ds.ds_stackbase; |
| 289 | stack_beg = stack_end - th->th.th_info.ds.ds_stacksize; |
| 290 | } |
| 291 | |
| 292 | for (f = 0; f < __kmp_threads_capacity; f++) { |
| 293 | kmp_info_t *f_th = (kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[f]); |
| 294 | |
| 295 | if (f_th && f_th != th) { |
| 296 | char *other_stack_end = |
| 297 | (char *)TCR_PTR(f_th->th.th_info.ds.ds_stackbase); |
| 298 | char *other_stack_beg = |
| 299 | other_stack_end - (size_t)TCR_PTR(f_th->th.th_info.ds.ds_stacksize); |
| 300 | if ((stack_beg > other_stack_beg && stack_beg < other_stack_end) || |
| 301 | (stack_end > other_stack_beg && stack_end < other_stack_end)) { |
| 302 | |
| 303 | /* Print the other stack values before the abort */ |
| 304 | if (__kmp_storage_map) |
| 305 | __kmp_print_storage_map_gtid( |
| 306 | -1, other_stack_beg, other_stack_end, |
| 307 | (size_t)TCR_PTR(f_th->th.th_info.ds.ds_stacksize), |
| 308 | "th_%d stack (overlapped)", __kmp_gtid_from_thread(f_th)); |
| 309 | |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 310 | __kmp_fatal(KMP_MSG(StackOverlap), KMP_HNT(ChangeStackLimit), |
| 311 | __kmp_msg_null); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | KA_TRACE(10, ("__kmp_check_stack_overlap: returning\n")); |
| 317 | } |
| 318 | |
| 319 | /* ------------------------------------------------------------------------ */ |
| 320 | |
| 321 | void __kmp_infinite_loop(void) { |
| 322 | static int done = FALSE; |
| 323 | |
| 324 | while (!done) { |
| 325 | KMP_YIELD(1); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | #define MAX_MESSAGE 512 |
| 330 | |
| 331 | void __kmp_print_storage_map_gtid(int gtid, void *p1, void *p2, size_t size, |
| 332 | char const *format, ...) { |
| 333 | char buffer[MAX_MESSAGE]; |
| 334 | va_list ap; |
| 335 | |
| 336 | va_start(ap, format); |
| 337 | KMP_SNPRINTF(buffer, sizeof(buffer), "OMP storage map: %p %p%8lu %s\n", p1, |
| 338 | p2, (unsigned long)size, format); |
| 339 | __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock); |
| 340 | __kmp_vprintf(kmp_err, buffer, ap); |
| 341 | #if KMP_PRINT_DATA_PLACEMENT |
| 342 | int node; |
| 343 | if (gtid >= 0) { |
| 344 | if (p1 <= p2 && (char *)p2 - (char *)p1 == size) { |
| 345 | if (__kmp_storage_map_verbose) { |
| 346 | node = __kmp_get_host_node(p1); |
| 347 | if (node < 0) /* doesn't work, so don't try this next time */ |
| 348 | __kmp_storage_map_verbose = FALSE; |
| 349 | else { |
| 350 | char *last; |
| 351 | int lastNode; |
| 352 | int localProc = __kmp_get_cpu_from_gtid(gtid); |
| 353 | |
| 354 | const int page_size = KMP_GET_PAGE_SIZE(); |
| 355 | |
| 356 | p1 = (void *)((size_t)p1 & ~((size_t)page_size - 1)); |
| 357 | p2 = (void *)(((size_t)p2 - 1) & ~((size_t)page_size - 1)); |
| 358 | if (localProc >= 0) |
| 359 | __kmp_printf_no_lock(" GTID %d localNode %d\n", gtid, |
| 360 | localProc >> 1); |
| 361 | else |
| 362 | __kmp_printf_no_lock(" GTID %d\n", gtid); |
| 363 | #if KMP_USE_PRCTL |
| 364 | /* The more elaborate format is disabled for now because of the prctl |
| 365 | * hanging bug. */ |
| 366 | do { |
| 367 | last = p1; |
| 368 | lastNode = node; |
| 369 | /* This loop collates adjacent pages with the same host node. */ |
| 370 | do { |
| 371 | (char *)p1 += page_size; |
| 372 | } while (p1 <= p2 && (node = __kmp_get_host_node(p1)) == lastNode); |
| 373 | __kmp_printf_no_lock(" %p-%p memNode %d\n", last, (char *)p1 - 1, |
| 374 | lastNode); |
| 375 | } while (p1 <= p2); |
| 376 | #else |
| 377 | __kmp_printf_no_lock(" %p-%p memNode %d\n", p1, |
| 378 | (char *)p1 + (page_size - 1), |
| 379 | __kmp_get_host_node(p1)); |
| 380 | if (p1 < p2) { |
| 381 | __kmp_printf_no_lock(" %p-%p memNode %d\n", p2, |
| 382 | (char *)p2 + (page_size - 1), |
| 383 | __kmp_get_host_node(p2)); |
| 384 | } |
| 385 | #endif |
| 386 | } |
| 387 | } |
| 388 | } else |
| 389 | __kmp_printf_no_lock(" %s\n", KMP_I18N_STR(StorageMapWarning)); |
| 390 | } |
| 391 | #endif /* KMP_PRINT_DATA_PLACEMENT */ |
| 392 | __kmp_release_bootstrap_lock(&__kmp_stdio_lock); |
| 393 | } |
| 394 | |
| 395 | void __kmp_warn(char const *format, ...) { |
| 396 | char buffer[MAX_MESSAGE]; |
| 397 | va_list ap; |
| 398 | |
| 399 | if (__kmp_generate_warnings == kmp_warnings_off) { |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | va_start(ap, format); |
| 404 | |
| 405 | KMP_SNPRINTF(buffer, sizeof(buffer), "OMP warning: %s\n", format); |
| 406 | __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock); |
| 407 | __kmp_vprintf(kmp_err, buffer, ap); |
| 408 | __kmp_release_bootstrap_lock(&__kmp_stdio_lock); |
| 409 | |
| 410 | va_end(ap); |
| 411 | } |
| 412 | |
| 413 | void __kmp_abort_process() { |
| 414 | // Later threads may stall here, but that's ok because abort() will kill them. |
| 415 | __kmp_acquire_bootstrap_lock(&__kmp_exit_lock); |
| 416 | |
| 417 | if (__kmp_debug_buf) { |
| 418 | __kmp_dump_debug_buffer(); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 419 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 420 | |
| 421 | if (KMP_OS_WINDOWS) { |
| 422 | // Let other threads know of abnormal termination and prevent deadlock |
| 423 | // if abort happened during library initialization or shutdown |
| 424 | __kmp_global.g.g_abort = SIGABRT; |
| 425 | |
| 426 | /* On Windows* OS by default abort() causes pop-up error box, which stalls |
| 427 | nightly testing. Unfortunately, we cannot reliably suppress pop-up error |
| 428 | boxes. _set_abort_behavior() works well, but this function is not |
| 429 | available in VS7 (this is not problem for DLL, but it is a problem for |
| 430 | static OpenMP RTL). SetErrorMode (and so, timelimit utility) does not |
| 431 | help, at least in some versions of MS C RTL. |
| 432 | |
| 433 | It seems following sequence is the only way to simulate abort() and |
| 434 | avoid pop-up error box. */ |
| 435 | raise(SIGABRT); |
| 436 | _exit(3); // Just in case, if signal ignored, exit anyway. |
| 437 | } else { |
| 438 | abort(); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 439 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 440 | |
| 441 | __kmp_infinite_loop(); |
| 442 | __kmp_release_bootstrap_lock(&__kmp_exit_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 443 | |
| 444 | } // __kmp_abort_process |
| 445 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 446 | void __kmp_abort_thread(void) { |
| 447 | // TODO: Eliminate g_abort global variable and this function. |
| 448 | // In case of abort just call abort(), it will kill all the threads. |
| 449 | __kmp_infinite_loop(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 450 | } // __kmp_abort_thread |
| 451 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 452 | /* Print out the storage map for the major kmp_info_t thread data structures |
| 453 | that are allocated together. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 454 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 455 | static void __kmp_print_thread_storage_map(kmp_info_t *thr, int gtid) { |
| 456 | __kmp_print_storage_map_gtid(gtid, thr, thr + 1, sizeof(kmp_info_t), "th_%d", |
| 457 | gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 458 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 459 | __kmp_print_storage_map_gtid(gtid, &thr->th.th_info, &thr->th.th_team, |
| 460 | sizeof(kmp_desc_t), "th_%d.th_info", gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 461 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 462 | __kmp_print_storage_map_gtid(gtid, &thr->th.th_local, &thr->th.th_pri_head, |
| 463 | sizeof(kmp_local_t), "th_%d.th_local", gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 464 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 465 | __kmp_print_storage_map_gtid( |
| 466 | gtid, &thr->th.th_bar[0], &thr->th.th_bar[bs_last_barrier], |
| 467 | sizeof(kmp_balign_t) * bs_last_barrier, "th_%d.th_bar", gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 468 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 469 | __kmp_print_storage_map_gtid(gtid, &thr->th.th_bar[bs_plain_barrier], |
| 470 | &thr->th.th_bar[bs_plain_barrier + 1], |
| 471 | sizeof(kmp_balign_t), "th_%d.th_bar[plain]", |
| 472 | gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 473 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 474 | __kmp_print_storage_map_gtid(gtid, &thr->th.th_bar[bs_forkjoin_barrier], |
| 475 | &thr->th.th_bar[bs_forkjoin_barrier + 1], |
| 476 | sizeof(kmp_balign_t), "th_%d.th_bar[forkjoin]", |
| 477 | gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 478 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 479 | #if KMP_FAST_REDUCTION_BARRIER |
| 480 | __kmp_print_storage_map_gtid(gtid, &thr->th.th_bar[bs_reduction_barrier], |
| 481 | &thr->th.th_bar[bs_reduction_barrier + 1], |
| 482 | sizeof(kmp_balign_t), "th_%d.th_bar[reduction]", |
| 483 | gtid); |
| 484 | #endif // KMP_FAST_REDUCTION_BARRIER |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 487 | /* Print out the storage map for the major kmp_team_t team data structures |
| 488 | that are allocated together. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 489 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 490 | static void __kmp_print_team_storage_map(const char *header, kmp_team_t *team, |
| 491 | int team_id, int num_thr) { |
| 492 | int num_disp_buff = team->t.t_max_nproc > 1 ? __kmp_dispatch_num_buffers : 2; |
| 493 | __kmp_print_storage_map_gtid(-1, team, team + 1, sizeof(kmp_team_t), "%s_%d", |
| 494 | header, team_id); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 495 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 496 | __kmp_print_storage_map_gtid(-1, &team->t.t_bar[0], |
| 497 | &team->t.t_bar[bs_last_barrier], |
| 498 | sizeof(kmp_balign_team_t) * bs_last_barrier, |
| 499 | "%s_%d.t_bar", header, team_id); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 500 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 501 | __kmp_print_storage_map_gtid(-1, &team->t.t_bar[bs_plain_barrier], |
| 502 | &team->t.t_bar[bs_plain_barrier + 1], |
| 503 | sizeof(kmp_balign_team_t), "%s_%d.t_bar[plain]", |
| 504 | header, team_id); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 505 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 506 | __kmp_print_storage_map_gtid(-1, &team->t.t_bar[bs_forkjoin_barrier], |
| 507 | &team->t.t_bar[bs_forkjoin_barrier + 1], |
| 508 | sizeof(kmp_balign_team_t), |
| 509 | "%s_%d.t_bar[forkjoin]", header, team_id); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 510 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 511 | #if KMP_FAST_REDUCTION_BARRIER |
| 512 | __kmp_print_storage_map_gtid(-1, &team->t.t_bar[bs_reduction_barrier], |
| 513 | &team->t.t_bar[bs_reduction_barrier + 1], |
| 514 | sizeof(kmp_balign_team_t), |
| 515 | "%s_%d.t_bar[reduction]", header, team_id); |
| 516 | #endif // KMP_FAST_REDUCTION_BARRIER |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 517 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 518 | __kmp_print_storage_map_gtid( |
| 519 | -1, &team->t.t_dispatch[0], &team->t.t_dispatch[num_thr], |
| 520 | sizeof(kmp_disp_t) * num_thr, "%s_%d.t_dispatch", header, team_id); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 521 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 522 | __kmp_print_storage_map_gtid( |
| 523 | -1, &team->t.t_threads[0], &team->t.t_threads[num_thr], |
| 524 | sizeof(kmp_info_t *) * num_thr, "%s_%d.t_threads", header, team_id); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 525 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 526 | __kmp_print_storage_map_gtid(-1, &team->t.t_disp_buffer[0], |
| 527 | &team->t.t_disp_buffer[num_disp_buff], |
| 528 | sizeof(dispatch_shared_info_t) * num_disp_buff, |
| 529 | "%s_%d.t_disp_buffer", header, team_id); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 530 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 531 | __kmp_print_storage_map_gtid(-1, &team->t.t_taskq, &team->t.t_copypriv_data, |
| 532 | sizeof(kmp_taskq_t), "%s_%d.t_taskq", header, |
| 533 | team_id); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static void __kmp_init_allocator() {} |
| 537 | static void __kmp_fini_allocator() {} |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 538 | |
| 539 | /* ------------------------------------------------------------------------ */ |
| 540 | |
Jonathan Peyton | 9901699 | 2015-05-26 17:32:53 +0000 | [diff] [blame] | 541 | #ifdef KMP_DYNAMIC_LIB |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 542 | #if KMP_OS_WINDOWS |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 543 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 544 | static void __kmp_reset_lock(kmp_bootstrap_lock_t *lck) { |
| 545 | // TODO: Change to __kmp_break_bootstrap_lock(). |
| 546 | __kmp_init_bootstrap_lock(lck); // make the lock released |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 549 | static void __kmp_reset_locks_on_process_detach(int gtid_req) { |
| 550 | int i; |
| 551 | int thread_count; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 552 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 553 | // PROCESS_DETACH is expected to be called by a thread that executes |
| 554 | // ProcessExit() or FreeLibrary(). OS terminates other threads (except the one |
| 555 | // calling ProcessExit or FreeLibrary). So, it might be safe to access the |
| 556 | // __kmp_threads[] without taking the forkjoin_lock. However, in fact, some |
| 557 | // threads can be still alive here, although being about to be terminated. The |
| 558 | // threads in the array with ds_thread==0 are most suspicious. Actually, it |
| 559 | // can be not safe to access the __kmp_threads[]. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 560 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 561 | // TODO: does it make sense to check __kmp_roots[] ? |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 562 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 563 | // Let's check that there are no other alive threads registered with the OMP |
| 564 | // lib. |
| 565 | while (1) { |
| 566 | thread_count = 0; |
| 567 | for (i = 0; i < __kmp_threads_capacity; ++i) { |
| 568 | if (!__kmp_threads) |
| 569 | continue; |
| 570 | kmp_info_t *th = __kmp_threads[i]; |
| 571 | if (th == NULL) |
| 572 | continue; |
| 573 | int gtid = th->th.th_info.ds.ds_gtid; |
| 574 | if (gtid == gtid_req) |
| 575 | continue; |
| 576 | if (gtid < 0) |
| 577 | continue; |
| 578 | DWORD exit_val; |
| 579 | int alive = __kmp_is_thread_alive(th, &exit_val); |
| 580 | if (alive) { |
| 581 | ++thread_count; |
| 582 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 583 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 584 | if (thread_count == 0) |
| 585 | break; // success |
| 586 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 587 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 588 | // Assume that I'm alone. Now it might be safe to check and reset locks. |
| 589 | // __kmp_forkjoin_lock and __kmp_stdio_lock are expected to be reset. |
| 590 | __kmp_reset_lock(&__kmp_forkjoin_lock); |
| 591 | #ifdef KMP_DEBUG |
| 592 | __kmp_reset_lock(&__kmp_stdio_lock); |
| 593 | #endif // KMP_DEBUG |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 596 | BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpReserved) { |
| 597 | //__kmp_acquire_bootstrap_lock( &__kmp_initz_lock ); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 598 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 599 | switch (fdwReason) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 600 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 601 | case DLL_PROCESS_ATTACH: |
| 602 | KA_TRACE(10, ("DllMain: PROCESS_ATTACH\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 603 | |
| 604 | return TRUE; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 605 | |
| 606 | case DLL_PROCESS_DETACH: |
| 607 | KA_TRACE(10, ("DllMain: PROCESS_DETACH T#%d\n", __kmp_gtid_get_specific())); |
| 608 | |
| 609 | if (lpReserved != NULL) { |
| 610 | // lpReserved is used for telling the difference: |
| 611 | // lpReserved == NULL when FreeLibrary() was called, |
| 612 | // lpReserved != NULL when the process terminates. |
| 613 | // When FreeLibrary() is called, worker threads remain alive. So they will |
| 614 | // release the forkjoin lock by themselves. When the process terminates, |
| 615 | // worker threads disappear triggering the problem of unreleased forkjoin |
| 616 | // lock as described below. |
| 617 | |
| 618 | // A worker thread can take the forkjoin lock. The problem comes up if |
| 619 | // that worker thread becomes dead before it releases the forkjoin lock. |
| 620 | // The forkjoin lock remains taken, while the thread executing |
| 621 | // DllMain()->PROCESS_DETACH->__kmp_internal_end_library() below will try |
| 622 | // to take the forkjoin lock and will always fail, so that the application |
| 623 | // will never finish [normally]. This scenario is possible if |
| 624 | // __kmpc_end() has not been executed. It looks like it's not a corner |
| 625 | // case, but common cases: |
| 626 | // - the main function was compiled by an alternative compiler; |
| 627 | // - the main function was compiled by icl but without /Qopenmp |
| 628 | // (application with plugins); |
| 629 | // - application terminates by calling C exit(), Fortran CALL EXIT() or |
| 630 | // Fortran STOP. |
| 631 | // - alive foreign thread prevented __kmpc_end from doing cleanup. |
| 632 | // |
| 633 | // This is a hack to work around the problem. |
| 634 | // TODO: !!! figure out something better. |
| 635 | __kmp_reset_locks_on_process_detach(__kmp_gtid_get_specific()); |
| 636 | } |
| 637 | |
| 638 | __kmp_internal_end_library(__kmp_gtid_get_specific()); |
| 639 | |
| 640 | return TRUE; |
| 641 | |
| 642 | case DLL_THREAD_ATTACH: |
| 643 | KA_TRACE(10, ("DllMain: THREAD_ATTACH\n")); |
| 644 | |
| 645 | /* if we want to register new siblings all the time here call |
| 646 | * __kmp_get_gtid(); */ |
| 647 | return TRUE; |
| 648 | |
| 649 | case DLL_THREAD_DETACH: |
| 650 | KA_TRACE(10, ("DllMain: THREAD_DETACH T#%d\n", __kmp_gtid_get_specific())); |
| 651 | |
| 652 | __kmp_internal_end_thread(__kmp_gtid_get_specific()); |
| 653 | return TRUE; |
| 654 | } |
| 655 | |
| 656 | return TRUE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 659 | #endif /* KMP_OS_WINDOWS */ |
Jonathan Peyton | 9901699 | 2015-05-26 17:32:53 +0000 | [diff] [blame] | 660 | #endif /* KMP_DYNAMIC_LIB */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 661 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 662 | /* Change the library type to "status" and return the old type */ |
| 663 | /* called from within initialization routines where __kmp_initz_lock is held */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 664 | int __kmp_change_library(int status) { |
| 665 | int old_status; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 666 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 667 | old_status = __kmp_yield_init & |
| 668 | 1; // check whether KMP_LIBRARY=throughput (even init count) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 669 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 670 | if (status) { |
| 671 | __kmp_yield_init |= 1; // throughput => turnaround (odd init count) |
| 672 | } else { |
| 673 | __kmp_yield_init &= ~1; // turnaround => throughput (even init count) |
| 674 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 675 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 676 | return old_status; // return previous setting of whether |
| 677 | // KMP_LIBRARY=throughput |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 680 | /* __kmp_parallel_deo -- Wait until it's our turn. */ |
| 681 | void __kmp_parallel_deo(int *gtid_ref, int *cid_ref, ident_t *loc_ref) { |
| 682 | int gtid = *gtid_ref; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 683 | #ifdef BUILD_PARALLEL_ORDERED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 684 | kmp_team_t *team = __kmp_team_from_gtid(gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 685 | #endif /* BUILD_PARALLEL_ORDERED */ |
| 686 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 687 | if (__kmp_env_consistency_check) { |
| 688 | if (__kmp_threads[gtid]->th.th_root->r.r_active) |
Andrey Churbanov | 5c56fb5 | 2015-02-20 18:05:17 +0000 | [diff] [blame] | 689 | #if KMP_USE_DYNAMIC_LOCK |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 690 | __kmp_push_sync(gtid, ct_ordered_in_parallel, loc_ref, NULL, 0); |
Andrey Churbanov | 5c56fb5 | 2015-02-20 18:05:17 +0000 | [diff] [blame] | 691 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 692 | __kmp_push_sync(gtid, ct_ordered_in_parallel, loc_ref, NULL); |
Andrey Churbanov | 5c56fb5 | 2015-02-20 18:05:17 +0000 | [diff] [blame] | 693 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 694 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 695 | #ifdef BUILD_PARALLEL_ORDERED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 696 | if (!team->t.t_serialized) { |
| 697 | KMP_MB(); |
| 698 | KMP_WAIT_YIELD(&team->t.t_ordered.dt.t_value, __kmp_tid_from_gtid(gtid), |
| 699 | KMP_EQ, NULL); |
| 700 | KMP_MB(); |
| 701 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 702 | #endif /* BUILD_PARALLEL_ORDERED */ |
| 703 | } |
| 704 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 705 | /* __kmp_parallel_dxo -- Signal the next task. */ |
| 706 | void __kmp_parallel_dxo(int *gtid_ref, int *cid_ref, ident_t *loc_ref) { |
| 707 | int gtid = *gtid_ref; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 708 | #ifdef BUILD_PARALLEL_ORDERED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 709 | int tid = __kmp_tid_from_gtid(gtid); |
| 710 | kmp_team_t *team = __kmp_team_from_gtid(gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 711 | #endif /* BUILD_PARALLEL_ORDERED */ |
| 712 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 713 | if (__kmp_env_consistency_check) { |
| 714 | if (__kmp_threads[gtid]->th.th_root->r.r_active) |
| 715 | __kmp_pop_sync(gtid, ct_ordered_in_parallel, loc_ref); |
| 716 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 717 | #ifdef BUILD_PARALLEL_ORDERED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 718 | if (!team->t.t_serialized) { |
| 719 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 720 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 721 | /* use the tid of the next thread in this team */ |
| 722 | /* TODO replace with general release procedure */ |
| 723 | team->t.t_ordered.dt.t_value = ((tid + 1) % team->t.t_nproc); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 724 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 725 | #if OMPT_SUPPORT && OMPT_BLAME |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 726 | if (ompt_enabled && |
| 727 | ompt_callbacks.ompt_callback(ompt_event_release_ordered)) { |
| 728 | /* accept blame for "ordered" waiting */ |
| 729 | kmp_info_t *this_thread = __kmp_threads[gtid]; |
| 730 | ompt_callbacks.ompt_callback(ompt_event_release_ordered)( |
| 731 | this_thread->th.ompt_thread_info.wait_id); |
| 732 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 733 | #endif |
| 734 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 735 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 736 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 737 | #endif /* BUILD_PARALLEL_ORDERED */ |
| 738 | } |
| 739 | |
| 740 | /* ------------------------------------------------------------------------ */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 741 | /* The BARRIER for a SINGLE process section is always explicit */ |
| 742 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 743 | int __kmp_enter_single(int gtid, ident_t *id_ref, int push_ws) { |
| 744 | int status; |
| 745 | kmp_info_t *th; |
| 746 | kmp_team_t *team; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 747 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 748 | if (!TCR_4(__kmp_init_parallel)) |
| 749 | __kmp_parallel_initialize(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 750 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 751 | th = __kmp_threads[gtid]; |
| 752 | team = th->th.th_team; |
| 753 | status = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 754 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 755 | th->th.th_ident = id_ref; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 756 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 757 | if (team->t.t_serialized) { |
| 758 | status = 1; |
| 759 | } else { |
| 760 | kmp_int32 old_this = th->th.th_local.this_construct; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 761 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 762 | ++th->th.th_local.this_construct; |
| 763 | /* try to set team count to thread count--success means thread got the |
| 764 | single block */ |
| 765 | /* TODO: Should this be acquire or release? */ |
| 766 | if (team->t.t_construct == old_this) { |
| 767 | status = KMP_COMPARE_AND_STORE_ACQ32(&team->t.t_construct, old_this, |
| 768 | th->th.th_local.this_construct); |
| 769 | } |
Andrey Churbanov | 51aecb8 | 2015-05-06 19:22:36 +0000 | [diff] [blame] | 770 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 771 | if (__itt_metadata_add_ptr && __kmp_forkjoin_frames_mode == 3 && |
| 772 | KMP_MASTER_GTID(gtid) && |
Andrey Churbanov | 51aecb8 | 2015-05-06 19:22:36 +0000 | [diff] [blame] | 773 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 774 | th->th.th_teams_microtask == NULL && |
Andrey Churbanov | 51aecb8 | 2015-05-06 19:22:36 +0000 | [diff] [blame] | 775 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 776 | team->t.t_active_level == |
| 777 | 1) { // Only report metadata by master of active team at level 1 |
| 778 | __kmp_itt_metadata_single(id_ref); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 779 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 780 | #endif /* USE_ITT_BUILD */ |
| 781 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 782 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 783 | if (__kmp_env_consistency_check) { |
| 784 | if (status && push_ws) { |
| 785 | __kmp_push_workshare(gtid, ct_psingle, id_ref); |
| 786 | } else { |
| 787 | __kmp_check_workshare(gtid, ct_psingle, id_ref); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 788 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 789 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 790 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 791 | if (status) { |
| 792 | __kmp_itt_single_start(gtid); |
| 793 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 794 | #endif /* USE_ITT_BUILD */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 795 | return status; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 798 | void __kmp_exit_single(int gtid) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 799 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 800 | __kmp_itt_single_end(gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 801 | #endif /* USE_ITT_BUILD */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 802 | if (__kmp_env_consistency_check) |
| 803 | __kmp_pop_workshare(gtid, ct_psingle, NULL); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 806 | /* determine if we can go parallel or must use a serialized parallel region and |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 807 | * how many threads we can use |
| 808 | * set_nproc is the number of threads requested for the team |
| 809 | * returns 0 if we should serialize or only use one thread, |
| 810 | * otherwise the number of threads to use |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 811 | * The forkjoin lock is held by the caller. */ |
| 812 | static int __kmp_reserve_threads(kmp_root_t *root, kmp_team_t *parent_team, |
| 813 | int master_tid, int set_nthreads |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 814 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 815 | , |
| 816 | int enter_teams |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 817 | #endif /* OMP_40_ENABLED */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 818 | ) { |
| 819 | int capacity; |
| 820 | int new_nthreads; |
| 821 | KMP_DEBUG_ASSERT(__kmp_init_serial); |
| 822 | KMP_DEBUG_ASSERT(root && parent_team); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 823 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 824 | // If dyn-var is set, dynamically adjust the number of desired threads, |
| 825 | // according to the method specified by dynamic_mode. |
| 826 | new_nthreads = set_nthreads; |
| 827 | if (!get__dynamic_2(parent_team, master_tid)) { |
| 828 | ; |
| 829 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 830 | #ifdef USE_LOAD_BALANCE |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 831 | else if (__kmp_global.g.g_dynamic_mode == dynamic_load_balance) { |
| 832 | new_nthreads = __kmp_load_balance_nproc(root, set_nthreads); |
| 833 | if (new_nthreads == 1) { |
| 834 | KC_TRACE(10, ("__kmp_reserve_threads: T#%d load balance reduced " |
| 835 | "reservation to 1 thread\n", |
| 836 | master_tid)); |
| 837 | return 1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 838 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 839 | if (new_nthreads < set_nthreads) { |
| 840 | KC_TRACE(10, ("__kmp_reserve_threads: T#%d load balance reduced " |
| 841 | "reservation to %d threads\n", |
| 842 | master_tid, new_nthreads)); |
| 843 | } |
| 844 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 845 | #endif /* USE_LOAD_BALANCE */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 846 | else if (__kmp_global.g.g_dynamic_mode == dynamic_thread_limit) { |
| 847 | new_nthreads = __kmp_avail_proc - __kmp_nth + |
| 848 | (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc); |
| 849 | if (new_nthreads <= 1) { |
| 850 | KC_TRACE(10, ("__kmp_reserve_threads: T#%d thread limit reduced " |
| 851 | "reservation to 1 thread\n", |
| 852 | master_tid)); |
| 853 | return 1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 854 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 855 | if (new_nthreads < set_nthreads) { |
| 856 | KC_TRACE(10, ("__kmp_reserve_threads: T#%d thread limit reduced " |
| 857 | "reservation to %d threads\n", |
| 858 | master_tid, new_nthreads)); |
| 859 | } else { |
| 860 | new_nthreads = set_nthreads; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 861 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 862 | } else if (__kmp_global.g.g_dynamic_mode == dynamic_random) { |
| 863 | if (set_nthreads > 2) { |
| 864 | new_nthreads = __kmp_get_random(parent_team->t.t_threads[master_tid]); |
| 865 | new_nthreads = (new_nthreads % set_nthreads) + 1; |
| 866 | if (new_nthreads == 1) { |
| 867 | KC_TRACE(10, ("__kmp_reserve_threads: T#%d dynamic random reduced " |
| 868 | "reservation to 1 thread\n", |
| 869 | master_tid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 870 | return 1; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 871 | } |
| 872 | if (new_nthreads < set_nthreads) { |
| 873 | KC_TRACE(10, ("__kmp_reserve_threads: T#%d dynamic random reduced " |
| 874 | "reservation to %d threads\n", |
| 875 | master_tid, new_nthreads)); |
| 876 | } |
| 877 | } |
| 878 | } else { |
| 879 | KMP_ASSERT(0); |
| 880 | } |
| 881 | |
Jonathan Peyton | f439246 | 2017-07-27 20:58:41 +0000 | [diff] [blame] | 882 | // Respect KMP_ALL_THREADS/KMP_DEVICE_THREAD_LIMIT. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 883 | if (__kmp_nth + new_nthreads - |
| 884 | (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) > |
| 885 | __kmp_max_nth) { |
| 886 | int tl_nthreads = __kmp_max_nth - __kmp_nth + |
| 887 | (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc); |
| 888 | if (tl_nthreads <= 0) { |
| 889 | tl_nthreads = 1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 892 | // If dyn-var is false, emit a 1-time warning. |
| 893 | if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) { |
| 894 | __kmp_reserve_warn = 1; |
| 895 | __kmp_msg(kmp_ms_warning, |
| 896 | KMP_MSG(CantFormThrTeam, set_nthreads, tl_nthreads), |
| 897 | KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null); |
| 898 | } |
| 899 | if (tl_nthreads == 1) { |
Jonathan Peyton | f439246 | 2017-07-27 20:58:41 +0000 | [diff] [blame] | 900 | KC_TRACE(10, ("__kmp_reserve_threads: T#%d KMP_DEVICE_THREAD_LIMIT " |
| 901 | "reduced reservation to 1 thread\n", |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 902 | master_tid)); |
| 903 | return 1; |
| 904 | } |
Jonathan Peyton | f439246 | 2017-07-27 20:58:41 +0000 | [diff] [blame] | 905 | KC_TRACE(10, ("__kmp_reserve_threads: T#%d KMP_DEVICE_THREAD_LIMIT reduced " |
| 906 | "reservation to %d threads\n", |
| 907 | master_tid, tl_nthreads)); |
| 908 | new_nthreads = tl_nthreads; |
| 909 | } |
| 910 | |
| 911 | // Respect OMP_THREAD_LIMIT |
| 912 | if (root->r.r_cg_nthreads + new_nthreads - |
| 913 | (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) > |
| 914 | __kmp_cg_max_nth) { |
| 915 | int tl_nthreads = __kmp_cg_max_nth - root->r.r_cg_nthreads + |
| 916 | (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc); |
| 917 | if (tl_nthreads <= 0) { |
| 918 | tl_nthreads = 1; |
| 919 | } |
| 920 | |
| 921 | // If dyn-var is false, emit a 1-time warning. |
| 922 | if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) { |
| 923 | __kmp_reserve_warn = 1; |
| 924 | __kmp_msg(kmp_ms_warning, |
| 925 | KMP_MSG(CantFormThrTeam, set_nthreads, tl_nthreads), |
| 926 | KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null); |
| 927 | } |
| 928 | if (tl_nthreads == 1) { |
| 929 | KC_TRACE(10, ("__kmp_reserve_threads: T#%d OMP_THREAD_LIMIT " |
| 930 | "reduced reservation to 1 thread\n", |
| 931 | master_tid)); |
| 932 | return 1; |
| 933 | } |
| 934 | KC_TRACE(10, ("__kmp_reserve_threads: T#%d OMP_THREAD_LIMIT reduced " |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 935 | "reservation to %d threads\n", |
| 936 | master_tid, tl_nthreads)); |
| 937 | new_nthreads = tl_nthreads; |
| 938 | } |
| 939 | |
| 940 | // Check if the threads array is large enough, or needs expanding. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 941 | // See comment in __kmp_register_root() about the adjustment if |
| 942 | // __kmp_threads[0] == NULL. |
| 943 | capacity = __kmp_threads_capacity; |
| 944 | if (TCR_PTR(__kmp_threads[0]) == NULL) { |
| 945 | --capacity; |
| 946 | } |
| 947 | if (__kmp_nth + new_nthreads - |
| 948 | (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) > |
| 949 | capacity) { |
| 950 | // Expand the threads array. |
| 951 | int slotsRequired = __kmp_nth + new_nthreads - |
| 952 | (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc) - |
| 953 | capacity; |
| 954 | int slotsAdded = __kmp_expand_threads(slotsRequired, slotsRequired); |
| 955 | if (slotsAdded < slotsRequired) { |
| 956 | // The threads array was not expanded enough. |
| 957 | new_nthreads -= (slotsRequired - slotsAdded); |
| 958 | KMP_ASSERT(new_nthreads >= 1); |
| 959 | |
| 960 | // If dyn-var is false, emit a 1-time warning. |
| 961 | if (!get__dynamic_2(parent_team, master_tid) && (!__kmp_reserve_warn)) { |
| 962 | __kmp_reserve_warn = 1; |
| 963 | if (__kmp_tp_cached) { |
| 964 | __kmp_msg(kmp_ms_warning, |
| 965 | KMP_MSG(CantFormThrTeam, set_nthreads, new_nthreads), |
| 966 | KMP_HNT(Set_ALL_THREADPRIVATE, __kmp_tp_capacity), |
| 967 | KMP_HNT(PossibleSystemLimitOnThreads), __kmp_msg_null); |
| 968 | } else { |
| 969 | __kmp_msg(kmp_ms_warning, |
| 970 | KMP_MSG(CantFormThrTeam, set_nthreads, new_nthreads), |
| 971 | KMP_HNT(SystemLimitOnThreads), __kmp_msg_null); |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | |
Jonathan Peyton | 642688b | 2017-06-01 16:46:36 +0000 | [diff] [blame] | 977 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 978 | if (new_nthreads == 1) { |
| 979 | KC_TRACE(10, |
| 980 | ("__kmp_reserve_threads: T#%d serializing team after reclaiming " |
| 981 | "dead roots and rechecking; requested %d threads\n", |
| 982 | __kmp_get_gtid(), set_nthreads)); |
Jonathan Peyton | 642688b | 2017-06-01 16:46:36 +0000 | [diff] [blame] | 983 | } else { |
| 984 | KC_TRACE(10, ("__kmp_reserve_threads: T#%d allocating %d threads; requested" |
| 985 | " %d threads\n", |
| 986 | __kmp_get_gtid(), new_nthreads, set_nthreads)); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 987 | } |
Jonathan Peyton | 642688b | 2017-06-01 16:46:36 +0000 | [diff] [blame] | 988 | #endif // KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 989 | return new_nthreads; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 992 | /* Allocate threads from the thread pool and assign them to the new team. We are |
| 993 | assured that there are enough threads available, because we checked on that |
| 994 | earlier within critical section forkjoin */ |
| 995 | static void __kmp_fork_team_threads(kmp_root_t *root, kmp_team_t *team, |
| 996 | kmp_info_t *master_th, int master_gtid) { |
| 997 | int i; |
| 998 | int use_hot_team; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 999 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1000 | KA_TRACE(10, ("__kmp_fork_team_threads: new_nprocs = %d\n", team->t.t_nproc)); |
| 1001 | KMP_DEBUG_ASSERT(master_gtid == __kmp_get_gtid()); |
| 1002 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1003 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1004 | /* first, let's setup the master thread */ |
| 1005 | master_th->th.th_info.ds.ds_tid = 0; |
| 1006 | master_th->th.th_team = team; |
| 1007 | master_th->th.th_team_nproc = team->t.t_nproc; |
| 1008 | master_th->th.th_team_master = master_th; |
| 1009 | master_th->th.th_team_serialized = FALSE; |
| 1010 | master_th->th.th_dispatch = &team->t.t_dispatch[0]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1011 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1012 | /* make sure we are not the optimized hot team */ |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1013 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1014 | use_hot_team = 0; |
| 1015 | kmp_hot_team_ptr_t *hot_teams = master_th->th.th_hot_teams; |
| 1016 | if (hot_teams) { // hot teams array is not allocated if |
| 1017 | // KMP_HOT_TEAMS_MAX_LEVEL=0 |
| 1018 | int level = team->t.t_active_level - 1; // index in array of hot teams |
| 1019 | if (master_th->th.th_teams_microtask) { // are we inside the teams? |
| 1020 | if (master_th->th.th_teams_size.nteams > 1) { |
| 1021 | ++level; // level was not increased in teams construct for |
| 1022 | // team_of_masters |
| 1023 | } |
| 1024 | if (team->t.t_pkfn != (microtask_t)__kmp_teams_master && |
| 1025 | master_th->th.th_teams_level == team->t.t_level) { |
| 1026 | ++level; // level was not increased in teams construct for |
| 1027 | // team_of_workers before the parallel |
| 1028 | } // team->t.t_level will be increased inside parallel |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1029 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1030 | if (level < __kmp_hot_teams_max_level) { |
| 1031 | if (hot_teams[level].hot_team) { |
| 1032 | // hot team has already been allocated for given level |
| 1033 | KMP_DEBUG_ASSERT(hot_teams[level].hot_team == team); |
| 1034 | use_hot_team = 1; // the team is ready to use |
| 1035 | } else { |
| 1036 | use_hot_team = 0; // AC: threads are not allocated yet |
| 1037 | hot_teams[level].hot_team = team; // remember new hot team |
| 1038 | hot_teams[level].hot_team_nth = team->t.t_nproc; |
| 1039 | } |
| 1040 | } else { |
| 1041 | use_hot_team = 0; |
| 1042 | } |
| 1043 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1044 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1045 | use_hot_team = team == root->r.r_hot_team; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1046 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1047 | if (!use_hot_team) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1048 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1049 | /* install the master thread */ |
| 1050 | team->t.t_threads[0] = master_th; |
| 1051 | __kmp_initialize_info(master_th, team, 0, master_gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1052 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1053 | /* now, install the worker threads */ |
| 1054 | for (i = 1; i < team->t.t_nproc; i++) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1055 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1056 | /* fork or reallocate a new thread and install it in team */ |
| 1057 | kmp_info_t *thr = __kmp_allocate_thread(root, team, i); |
| 1058 | team->t.t_threads[i] = thr; |
| 1059 | KMP_DEBUG_ASSERT(thr); |
| 1060 | KMP_DEBUG_ASSERT(thr->th.th_team == team); |
| 1061 | /* align team and thread arrived states */ |
| 1062 | KA_TRACE(20, ("__kmp_fork_team_threads: T#%d(%d:%d) init arrived " |
| 1063 | "T#%d(%d:%d) join =%llu, plain=%llu\n", |
| 1064 | __kmp_gtid_from_tid(0, team), team->t.t_id, 0, |
| 1065 | __kmp_gtid_from_tid(i, team), team->t.t_id, i, |
| 1066 | team->t.t_bar[bs_forkjoin_barrier].b_arrived, |
| 1067 | team->t.t_bar[bs_plain_barrier].b_arrived)); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1068 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1069 | thr->th.th_teams_microtask = master_th->th.th_teams_microtask; |
| 1070 | thr->th.th_teams_level = master_th->th.th_teams_level; |
| 1071 | thr->th.th_teams_size = master_th->th.th_teams_size; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1072 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1073 | { // Initialize threads' barrier data. |
| 1074 | int b; |
| 1075 | kmp_balign_t *balign = team->t.t_threads[i]->th.th_bar; |
| 1076 | for (b = 0; b < bs_last_barrier; ++b) { |
| 1077 | balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived; |
| 1078 | KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG); |
Jonathan Peyton | 8fbb49a | 2015-07-09 18:16:58 +0000 | [diff] [blame] | 1079 | #if USE_DEBUGGER |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1080 | balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived; |
Jonathan Peyton | 8fbb49a | 2015-07-09 18:16:58 +0000 | [diff] [blame] | 1081 | #endif |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1082 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1083 | } |
| 1084 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1085 | |
Alp Toker | 98758b0 | 2014-03-02 04:12:06 +0000 | [diff] [blame] | 1086 | #if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1087 | __kmp_partition_places(team); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1088 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1089 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1090 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1091 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1094 | #if KMP_ARCH_X86 || KMP_ARCH_X86_64 |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1095 | // Propagate any changes to the floating point control registers out to the team |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1096 | // We try to avoid unnecessary writes to the relevant cache line in the team |
| 1097 | // structure, so we don't make changes unless they are needed. |
| 1098 | inline static void propagateFPControl(kmp_team_t *team) { |
| 1099 | if (__kmp_inherit_fp_control) { |
| 1100 | kmp_int16 x87_fpu_control_word; |
| 1101 | kmp_uint32 mxcsr; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1102 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1103 | // Get master values of FPU control flags (both X87 and vector) |
| 1104 | __kmp_store_x87_fpu_control_word(&x87_fpu_control_word); |
| 1105 | __kmp_store_mxcsr(&mxcsr); |
| 1106 | mxcsr &= KMP_X86_MXCSR_MASK; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1107 | |
Jonathan Peyton | 94a114f | 2017-10-20 19:30:57 +0000 | [diff] [blame] | 1108 | // There is no point looking at t_fp_control_saved here. |
| 1109 | // If it is TRUE, we still have to update the values if they are different |
| 1110 | // from those we now have. If it is FALSE we didn't save anything yet, but |
| 1111 | // our objective is the same. We have to ensure that the values in the team |
| 1112 | // are the same as those we have. |
| 1113 | // So, this code achieves what we need whether or not t_fp_control_saved is |
| 1114 | // true. By checking whether the value needs updating we avoid unnecessary |
| 1115 | // writes that would put the cache-line into a written state, causing all |
| 1116 | // threads in the team to have to read it again. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1117 | KMP_CHECK_UPDATE(team->t.t_x87_fpu_control_word, x87_fpu_control_word); |
| 1118 | KMP_CHECK_UPDATE(team->t.t_mxcsr, mxcsr); |
| 1119 | // Although we don't use this value, other code in the runtime wants to know |
| 1120 | // whether it should restore them. So we must ensure it is correct. |
| 1121 | KMP_CHECK_UPDATE(team->t.t_fp_control_saved, TRUE); |
| 1122 | } else { |
| 1123 | // Similarly here. Don't write to this cache-line in the team structure |
| 1124 | // unless we have to. |
| 1125 | KMP_CHECK_UPDATE(team->t.t_fp_control_saved, FALSE); |
| 1126 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1129 | // Do the opposite, setting the hardware registers to the updated values from |
| 1130 | // the team. |
| 1131 | inline static void updateHWFPControl(kmp_team_t *team) { |
| 1132 | if (__kmp_inherit_fp_control && team->t.t_fp_control_saved) { |
| 1133 | // Only reset the fp control regs if they have been changed in the team. |
| 1134 | // the parallel region that we are exiting. |
| 1135 | kmp_int16 x87_fpu_control_word; |
| 1136 | kmp_uint32 mxcsr; |
| 1137 | __kmp_store_x87_fpu_control_word(&x87_fpu_control_word); |
| 1138 | __kmp_store_mxcsr(&mxcsr); |
| 1139 | mxcsr &= KMP_X86_MXCSR_MASK; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1140 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1141 | if (team->t.t_x87_fpu_control_word != x87_fpu_control_word) { |
| 1142 | __kmp_clear_x87_fpu_status_word(); |
| 1143 | __kmp_load_x87_fpu_control_word(&team->t.t_x87_fpu_control_word); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1144 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1145 | |
| 1146 | if (team->t.t_mxcsr != mxcsr) { |
| 1147 | __kmp_load_mxcsr(&team->t.t_mxcsr); |
| 1148 | } |
| 1149 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1150 | } |
| 1151 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1152 | #define propagateFPControl(x) ((void)0) |
| 1153 | #define updateHWFPControl(x) ((void)0) |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1154 | #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */ |
| 1155 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1156 | static void __kmp_alloc_argv_entries(int argc, kmp_team_t *team, |
| 1157 | int realloc); // forward declaration |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1158 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1159 | /* Run a parallel region that has been serialized, so runs only in a team of the |
| 1160 | single master thread. */ |
| 1161 | void __kmp_serialized_parallel(ident_t *loc, kmp_int32 global_tid) { |
| 1162 | kmp_info_t *this_thr; |
| 1163 | kmp_team_t *serial_team; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1164 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1165 | KC_TRACE(10, ("__kmpc_serialized_parallel: called by T#%d\n", global_tid)); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1166 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1167 | /* Skip all this code for autopar serialized loops since it results in |
| 1168 | unacceptable overhead */ |
| 1169 | if (loc != NULL && (loc->flags & KMP_IDENT_AUTOPAR)) |
| 1170 | return; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1171 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1172 | if (!TCR_4(__kmp_init_parallel)) |
| 1173 | __kmp_parallel_initialize(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1174 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1175 | this_thr = __kmp_threads[global_tid]; |
| 1176 | serial_team = this_thr->th.th_serial_team; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1177 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1178 | /* utilize the serialized team held by this thread */ |
| 1179 | KMP_DEBUG_ASSERT(serial_team); |
| 1180 | KMP_MB(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1181 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1182 | if (__kmp_tasking_mode != tskm_immediate_exec) { |
| 1183 | KMP_DEBUG_ASSERT( |
| 1184 | this_thr->th.th_task_team == |
| 1185 | this_thr->th.th_team->t.t_task_team[this_thr->th.th_task_state]); |
| 1186 | KMP_DEBUG_ASSERT(serial_team->t.t_task_team[this_thr->th.th_task_state] == |
| 1187 | NULL); |
| 1188 | KA_TRACE(20, ("__kmpc_serialized_parallel: T#%d pushing task_team %p / " |
| 1189 | "team %p, new task_team = NULL\n", |
| 1190 | global_tid, this_thr->th.th_task_team, this_thr->th.th_team)); |
| 1191 | this_thr->th.th_task_team = NULL; |
| 1192 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1193 | |
| 1194 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1195 | kmp_proc_bind_t proc_bind = this_thr->th.th_set_proc_bind; |
| 1196 | if (this_thr->th.th_current_task->td_icvs.proc_bind == proc_bind_false) { |
| 1197 | proc_bind = proc_bind_false; |
| 1198 | } else if (proc_bind == proc_bind_default) { |
| 1199 | // No proc_bind clause was specified, so use the current value |
| 1200 | // of proc-bind-var for this parallel region. |
| 1201 | proc_bind = this_thr->th.th_current_task->td_icvs.proc_bind; |
| 1202 | } |
| 1203 | // Reset for next parallel region |
| 1204 | this_thr->th.th_set_proc_bind = proc_bind_default; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1205 | #endif /* OMP_40_ENABLED */ |
| 1206 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1207 | if (this_thr->th.th_team != serial_team) { |
| 1208 | // Nested level will be an index in the nested nthreads array |
| 1209 | int level = this_thr->th.th_team->t.t_level; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1210 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1211 | if (serial_team->t.t_serialized) { |
| 1212 | /* this serial team was already used |
| 1213 | TODO increase performance by making this locks more specific */ |
| 1214 | kmp_team_t *new_team; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1215 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1216 | __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1217 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1218 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1219 | ompt_parallel_id_t ompt_parallel_id = __ompt_parallel_id_new(global_tid); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1220 | #endif |
| 1221 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1222 | new_team = __kmp_allocate_team(this_thr->th.th_root, 1, 1, |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1223 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1224 | ompt_parallel_id, |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1225 | #endif |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1226 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1227 | proc_bind, |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1228 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1229 | &this_thr->th.th_current_task->td_icvs, |
| 1230 | 0 USE_NESTED_HOT_ARG(NULL)); |
| 1231 | __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); |
| 1232 | KMP_ASSERT(new_team); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1233 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1234 | /* setup new serialized team and install it */ |
| 1235 | new_team->t.t_threads[0] = this_thr; |
| 1236 | new_team->t.t_parent = this_thr->th.th_team; |
| 1237 | serial_team = new_team; |
| 1238 | this_thr->th.th_serial_team = serial_team; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1239 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1240 | KF_TRACE( |
| 1241 | 10, |
| 1242 | ("__kmpc_serialized_parallel: T#%d allocated new serial team %p\n", |
| 1243 | global_tid, serial_team)); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1244 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1245 | /* TODO the above breaks the requirement that if we run out of resources, |
| 1246 | then we can still guarantee that serialized teams are ok, since we may |
| 1247 | need to allocate a new one */ |
| 1248 | } else { |
| 1249 | KF_TRACE( |
| 1250 | 10, |
| 1251 | ("__kmpc_serialized_parallel: T#%d reusing cached serial team %p\n", |
| 1252 | global_tid, serial_team)); |
| 1253 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1254 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1255 | /* we have to initialize this serial team */ |
| 1256 | KMP_DEBUG_ASSERT(serial_team->t.t_threads); |
| 1257 | KMP_DEBUG_ASSERT(serial_team->t.t_threads[0] == this_thr); |
| 1258 | KMP_DEBUG_ASSERT(this_thr->th.th_team != serial_team); |
| 1259 | serial_team->t.t_ident = loc; |
| 1260 | serial_team->t.t_serialized = 1; |
| 1261 | serial_team->t.t_nproc = 1; |
| 1262 | serial_team->t.t_parent = this_thr->th.th_team; |
| 1263 | serial_team->t.t_sched = this_thr->th.th_team->t.t_sched; |
| 1264 | this_thr->th.th_team = serial_team; |
| 1265 | serial_team->t.t_master_tid = this_thr->th.th_info.ds.ds_tid; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1266 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1267 | KF_TRACE(10, ("__kmpc_serialized_parallel: T#d curtask=%p\n", global_tid, |
| 1268 | this_thr->th.th_current_task)); |
| 1269 | KMP_ASSERT(this_thr->th.th_current_task->td_flags.executing == 1); |
| 1270 | this_thr->th.th_current_task->td_flags.executing = 0; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1271 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1272 | __kmp_push_current_task_to_thread(this_thr, serial_team, 0); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1273 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1274 | /* TODO: GEH: do ICVs work for nested serialized teams? Don't we need an |
| 1275 | implicit task for each serialized task represented by |
| 1276 | team->t.t_serialized? */ |
| 1277 | copy_icvs(&this_thr->th.th_current_task->td_icvs, |
| 1278 | &this_thr->th.th_current_task->td_parent->td_icvs); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1279 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1280 | // Thread value exists in the nested nthreads array for the next nested |
| 1281 | // level |
| 1282 | if (__kmp_nested_nth.used && (level + 1 < __kmp_nested_nth.used)) { |
| 1283 | this_thr->th.th_current_task->td_icvs.nproc = |
| 1284 | __kmp_nested_nth.nth[level + 1]; |
| 1285 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1286 | |
| 1287 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1288 | if (__kmp_nested_proc_bind.used && |
| 1289 | (level + 1 < __kmp_nested_proc_bind.used)) { |
| 1290 | this_thr->th.th_current_task->td_icvs.proc_bind = |
| 1291 | __kmp_nested_proc_bind.bind_types[level + 1]; |
| 1292 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1293 | #endif /* OMP_40_ENABLED */ |
| 1294 | |
Jonathan Peyton | 8fbb49a | 2015-07-09 18:16:58 +0000 | [diff] [blame] | 1295 | #if USE_DEBUGGER |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1296 | serial_team->t.t_pkfn = (microtask_t)(~0); // For the debugger. |
Jonathan Peyton | 8fbb49a | 2015-07-09 18:16:58 +0000 | [diff] [blame] | 1297 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1298 | this_thr->th.th_info.ds.ds_tid = 0; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1299 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1300 | /* set thread cache values */ |
| 1301 | this_thr->th.th_team_nproc = 1; |
| 1302 | this_thr->th.th_team_master = this_thr; |
| 1303 | this_thr->th.th_team_serialized = 1; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1304 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1305 | serial_team->t.t_level = serial_team->t.t_parent->t.t_level + 1; |
| 1306 | serial_team->t.t_active_level = serial_team->t.t_parent->t.t_active_level; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1307 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1308 | propagateFPControl(serial_team); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1309 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1310 | /* check if we need to allocate dispatch buffers stack */ |
| 1311 | KMP_DEBUG_ASSERT(serial_team->t.t_dispatch); |
| 1312 | if (!serial_team->t.t_dispatch->th_disp_buffer) { |
| 1313 | serial_team->t.t_dispatch->th_disp_buffer = |
| 1314 | (dispatch_private_info_t *)__kmp_allocate( |
| 1315 | sizeof(dispatch_private_info_t)); |
| 1316 | } |
| 1317 | this_thr->th.th_dispatch = serial_team->t.t_dispatch; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1318 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1319 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1320 | ompt_parallel_id_t ompt_parallel_id = __ompt_parallel_id_new(global_tid); |
| 1321 | __ompt_team_assign_id(serial_team, ompt_parallel_id); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1322 | #endif |
| 1323 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1324 | KMP_MB(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1325 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1326 | } else { |
| 1327 | /* this serialized team is already being used, |
| 1328 | * that's fine, just add another nested level */ |
| 1329 | KMP_DEBUG_ASSERT(this_thr->th.th_team == serial_team); |
| 1330 | KMP_DEBUG_ASSERT(serial_team->t.t_threads); |
| 1331 | KMP_DEBUG_ASSERT(serial_team->t.t_threads[0] == this_thr); |
| 1332 | ++serial_team->t.t_serialized; |
| 1333 | this_thr->th.th_team_serialized = serial_team->t.t_serialized; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1334 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1335 | // Nested level will be an index in the nested nthreads array |
| 1336 | int level = this_thr->th.th_team->t.t_level; |
| 1337 | // Thread value exists in the nested nthreads array for the next nested |
| 1338 | // level |
| 1339 | if (__kmp_nested_nth.used && (level + 1 < __kmp_nested_nth.used)) { |
| 1340 | this_thr->th.th_current_task->td_icvs.nproc = |
| 1341 | __kmp_nested_nth.nth[level + 1]; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1342 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1343 | serial_team->t.t_level++; |
| 1344 | KF_TRACE(10, ("__kmpc_serialized_parallel: T#%d increasing nesting level " |
| 1345 | "of serial team %p to %d\n", |
| 1346 | global_tid, serial_team, serial_team->t.t_level)); |
| 1347 | |
| 1348 | /* allocate/push dispatch buffers stack */ |
| 1349 | KMP_DEBUG_ASSERT(serial_team->t.t_dispatch); |
| 1350 | { |
| 1351 | dispatch_private_info_t *disp_buffer = |
| 1352 | (dispatch_private_info_t *)__kmp_allocate( |
| 1353 | sizeof(dispatch_private_info_t)); |
| 1354 | disp_buffer->next = serial_team->t.t_dispatch->th_disp_buffer; |
| 1355 | serial_team->t.t_dispatch->th_disp_buffer = disp_buffer; |
| 1356 | } |
| 1357 | this_thr->th.th_dispatch = serial_team->t.t_dispatch; |
| 1358 | |
| 1359 | KMP_MB(); |
| 1360 | } |
Olga Malysheva | dbdcfa1 | 2017-04-04 13:56:50 +0000 | [diff] [blame] | 1361 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1362 | KMP_CHECK_UPDATE(serial_team->t.t_cancel_request, cancel_noreq); |
Olga Malysheva | dbdcfa1 | 2017-04-04 13:56:50 +0000 | [diff] [blame] | 1363 | #endif |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1364 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1365 | if (__kmp_env_consistency_check) |
| 1366 | __kmp_push_parallel(global_tid, NULL); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1367 | } |
Jim Cownie | 181b4bb | 2013-12-23 17:28:57 +0000 | [diff] [blame] | 1368 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1369 | /* most of the work for a fork */ |
| 1370 | /* return true if we really went parallel, false if serialized */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1371 | int __kmp_fork_call(ident_t *loc, int gtid, |
| 1372 | enum fork_context_e call_context, // Intel, GNU, ... |
| 1373 | kmp_int32 argc, |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1374 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1375 | void *unwrapped_task, |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1376 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1377 | microtask_t microtask, launch_t invoker, |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1378 | /* TODO: revert workaround for Intel(R) 64 tracker #96 */ |
Andrey Churbanov | cbda868 | 2015-01-13 14:43:35 +0000 | [diff] [blame] | 1379 | #if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1380 | va_list *ap |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1381 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1382 | va_list ap |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1383 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1384 | ) { |
| 1385 | void **argv; |
| 1386 | int i; |
| 1387 | int master_tid; |
| 1388 | int master_this_cons; |
| 1389 | kmp_team_t *team; |
| 1390 | kmp_team_t *parent_team; |
| 1391 | kmp_info_t *master_th; |
| 1392 | kmp_root_t *root; |
| 1393 | int nthreads; |
| 1394 | int master_active; |
| 1395 | int master_set_numthreads; |
| 1396 | int level; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1397 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1398 | int active_level; |
| 1399 | int teams_level; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1400 | #endif |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1401 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1402 | kmp_hot_team_ptr_t **p_hot_teams; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1403 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1404 | { // KMP_TIME_BLOCK |
Jonathan Peyton | 5375fe8 | 2016-11-14 21:13:44 +0000 | [diff] [blame] | 1405 | KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(KMP_fork_call); |
Jonathan Peyton | 45be450 | 2015-08-11 21:36:41 +0000 | [diff] [blame] | 1406 | KMP_COUNT_VALUE(OMP_PARALLEL_args, argc); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1407 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1408 | KA_TRACE(20, ("__kmp_fork_call: enter T#%d\n", gtid)); |
| 1409 | if (__kmp_stkpadding > 0 && __kmp_root[gtid] != NULL) { |
| 1410 | /* Some systems prefer the stack for the root thread(s) to start with */ |
| 1411 | /* some gap from the parent stack to prevent false sharing. */ |
| 1412 | void *dummy = KMP_ALLOCA(__kmp_stkpadding); |
| 1413 | /* These 2 lines below are so this does not get optimized out */ |
| 1414 | if (__kmp_stkpadding > KMP_MAX_STKPADDING) |
| 1415 | __kmp_stkpadding += (short)((kmp_int64)dummy); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1416 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1417 | |
| 1418 | /* initialize if needed */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1419 | KMP_DEBUG_ASSERT( |
| 1420 | __kmp_init_serial); // AC: potentially unsafe, not in sync with shutdown |
| 1421 | if (!TCR_4(__kmp_init_parallel)) |
| 1422 | __kmp_parallel_initialize(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1423 | |
| 1424 | /* setup current data */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1425 | master_th = __kmp_threads[gtid]; // AC: potentially unsafe, not in sync with |
| 1426 | // shutdown |
| 1427 | parent_team = master_th->th.th_team; |
| 1428 | master_tid = master_th->th.th_info.ds.ds_tid; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1429 | master_this_cons = master_th->th.th_local.this_construct; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1430 | root = master_th->th.th_root; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1431 | master_active = root->r.r_active; |
| 1432 | master_set_numthreads = master_th->th.th_set_nproc; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1433 | |
| 1434 | #if OMPT_SUPPORT |
| 1435 | ompt_parallel_id_t ompt_parallel_id; |
| 1436 | ompt_task_id_t ompt_task_id; |
| 1437 | ompt_frame_t *ompt_frame; |
| 1438 | ompt_task_id_t my_task_id; |
| 1439 | ompt_parallel_id_t my_parallel_id; |
| 1440 | |
Jonathan Peyton | b68a85d | 2015-09-21 18:11:22 +0000 | [diff] [blame] | 1441 | if (ompt_enabled) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1442 | ompt_parallel_id = __ompt_parallel_id_new(gtid); |
| 1443 | ompt_task_id = __ompt_get_task_id_internal(0); |
| 1444 | ompt_frame = __ompt_get_task_frame_internal(0); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1445 | } |
| 1446 | #endif |
| 1447 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1448 | // Nested level will be an index in the nested nthreads array |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1449 | level = parent_team->t.t_level; |
| 1450 | // used to launch non-serial teams even if nested is not allowed |
| 1451 | active_level = parent_team->t.t_active_level; |
Jonathan Peyton | c76f9f0 | 2016-06-21 19:12:07 +0000 | [diff] [blame] | 1452 | #if OMP_40_ENABLED |
Jonathan Peyton | 642688b | 2017-06-01 16:46:36 +0000 | [diff] [blame] | 1453 | // needed to check nesting inside the teams |
| 1454 | teams_level = master_th->th.th_teams_level; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1455 | #endif |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1456 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1457 | p_hot_teams = &master_th->th.th_hot_teams; |
| 1458 | if (*p_hot_teams == NULL && __kmp_hot_teams_max_level > 0) { |
| 1459 | *p_hot_teams = (kmp_hot_team_ptr_t *)__kmp_allocate( |
| 1460 | sizeof(kmp_hot_team_ptr_t) * __kmp_hot_teams_max_level); |
| 1461 | (*p_hot_teams)[0].hot_team = root->r.r_hot_team; |
Jonathan Peyton | 642688b | 2017-06-01 16:46:36 +0000 | [diff] [blame] | 1462 | // it is either actual or not needed (when active_level > 0) |
| 1463 | (*p_hot_teams)[0].hot_team_nth = 1; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1464 | } |
| 1465 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1466 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1467 | #if OMPT_SUPPORT |
Jonathan Peyton | b68a85d | 2015-09-21 18:11:22 +0000 | [diff] [blame] | 1468 | if (ompt_enabled && |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1469 | ompt_callbacks.ompt_callback(ompt_event_parallel_begin)) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1470 | int team_size = master_set_numthreads; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1471 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1472 | ompt_callbacks.ompt_callback(ompt_event_parallel_begin)( |
| 1473 | ompt_task_id, ompt_frame, ompt_parallel_id, team_size, unwrapped_task, |
| 1474 | OMPT_INVOKER(call_context)); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1475 | } |
| 1476 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1477 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1478 | master_th->th.th_ident = loc; |
| 1479 | |
| 1480 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1481 | if (master_th->th.th_teams_microtask && ap && |
| 1482 | microtask != (microtask_t)__kmp_teams_master && level == teams_level) { |
| 1483 | // AC: This is start of parallel that is nested inside teams construct. |
| 1484 | // The team is actual (hot), all workers are ready at the fork barrier. |
| 1485 | // No lock needed to initialize the team a bit, then free workers. |
| 1486 | parent_team->t.t_ident = loc; |
| 1487 | __kmp_alloc_argv_entries(argc, parent_team, TRUE); |
| 1488 | parent_team->t.t_argc = argc; |
| 1489 | argv = (void **)parent_team->t.t_argv; |
| 1490 | for (i = argc - 1; i >= 0; --i) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1491 | /* TODO: revert workaround for Intel(R) 64 tracker #96 */ |
Andrey Churbanov | cbda868 | 2015-01-13 14:43:35 +0000 | [diff] [blame] | 1492 | #if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1493 | *argv++ = va_arg(*ap, void *); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1494 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1495 | *argv++ = va_arg(ap, void *); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1496 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1497 | // Increment our nested depth levels, but not increase the serialization |
| 1498 | if (parent_team == master_th->th.th_serial_team) { |
| 1499 | // AC: we are in serialized parallel |
| 1500 | __kmpc_serialized_parallel(loc, gtid); |
| 1501 | KMP_DEBUG_ASSERT(parent_team->t.t_serialized > 1); |
| 1502 | // AC: need this in order enquiry functions work |
| 1503 | // correctly, will restore at join time |
| 1504 | parent_team->t.t_serialized--; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1505 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1506 | void *dummy; |
| 1507 | void **exit_runtime_p; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1508 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1509 | ompt_lw_taskteam_t lw_taskteam; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1510 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1511 | if (ompt_enabled) { |
| 1512 | __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid, unwrapped_task, |
| 1513 | ompt_parallel_id); |
| 1514 | lw_taskteam.ompt_task_info.task_id = __ompt_task_id_new(gtid); |
| 1515 | exit_runtime_p = |
| 1516 | &(lw_taskteam.ompt_task_info.frame.exit_runtime_frame); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1517 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1518 | __ompt_lw_taskteam_link(&lw_taskteam, master_th); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1519 | |
| 1520 | #if OMPT_TRACE |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1521 | /* OMPT implicit task begin */ |
| 1522 | my_task_id = lw_taskteam.ompt_task_info.task_id; |
| 1523 | my_parallel_id = parent_team->t.ompt_team_info.parallel_id; |
| 1524 | if (ompt_callbacks.ompt_callback(ompt_event_implicit_task_begin)) { |
| 1525 | ompt_callbacks.ompt_callback(ompt_event_implicit_task_begin)( |
| 1526 | my_parallel_id, my_task_id); |
| 1527 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1528 | #endif |
| 1529 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1530 | /* OMPT state */ |
| 1531 | master_th->th.ompt_thread_info.state = ompt_state_work_parallel; |
| 1532 | } else { |
| 1533 | exit_runtime_p = &dummy; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1534 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1535 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1536 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1537 | { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1538 | KMP_TIME_PARTITIONED_BLOCK(OMP_parallel); |
| 1539 | KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK); |
| 1540 | __kmp_invoke_microtask(microtask, gtid, 0, argc, parent_team->t.t_argv |
| 1541 | #if OMPT_SUPPORT |
| 1542 | , |
| 1543 | exit_runtime_p |
| 1544 | #endif |
| 1545 | ); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1546 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1547 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1548 | #if OMPT_SUPPORT |
| 1549 | *exit_runtime_p = NULL; |
| 1550 | if (ompt_enabled) { |
| 1551 | #if OMPT_TRACE |
| 1552 | lw_taskteam.ompt_task_info.frame.exit_runtime_frame = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1553 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1554 | if (ompt_callbacks.ompt_callback(ompt_event_implicit_task_end)) { |
| 1555 | ompt_callbacks.ompt_callback(ompt_event_implicit_task_end)( |
| 1556 | ompt_parallel_id, ompt_task_id); |
| 1557 | } |
| 1558 | |
| 1559 | __ompt_lw_taskteam_unlink(master_th); |
| 1560 | // reset clear the task id only after unlinking the task |
| 1561 | lw_taskteam.ompt_task_info.task_id = ompt_task_id_none; |
| 1562 | #endif |
| 1563 | |
| 1564 | if (ompt_callbacks.ompt_callback(ompt_event_parallel_end)) { |
| 1565 | ompt_callbacks.ompt_callback(ompt_event_parallel_end)( |
| 1566 | ompt_parallel_id, ompt_task_id, OMPT_INVOKER(call_context)); |
| 1567 | } |
| 1568 | master_th->th.ompt_thread_info.state = ompt_state_overhead; |
| 1569 | } |
| 1570 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1571 | return TRUE; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | parent_team->t.t_pkfn = microtask; |
| 1575 | #if OMPT_SUPPORT |
| 1576 | parent_team->t.ompt_team_info.microtask = unwrapped_task; |
| 1577 | #endif |
| 1578 | parent_team->t.t_invoke = invoker; |
| 1579 | KMP_TEST_THEN_INC32((kmp_int32 *)&root->r.r_in_parallel); |
| 1580 | parent_team->t.t_active_level++; |
| 1581 | parent_team->t.t_level++; |
| 1582 | |
| 1583 | /* Change number of threads in the team if requested */ |
| 1584 | if (master_set_numthreads) { // The parallel has num_threads clause |
| 1585 | if (master_set_numthreads < master_th->th.th_teams_size.nth) { |
| 1586 | // AC: only can reduce number of threads dynamically, can't increase |
| 1587 | kmp_info_t **other_threads = parent_team->t.t_threads; |
| 1588 | parent_team->t.t_nproc = master_set_numthreads; |
| 1589 | for (i = 0; i < master_set_numthreads; ++i) { |
| 1590 | other_threads[i]->th.th_team_nproc = master_set_numthreads; |
| 1591 | } |
| 1592 | // Keep extra threads hot in the team for possible next parallels |
| 1593 | } |
| 1594 | master_th->th.th_set_nproc = 0; |
| 1595 | } |
| 1596 | |
| 1597 | #if USE_DEBUGGER |
| 1598 | if (__kmp_debugging) { // Let debugger override number of threads. |
| 1599 | int nth = __kmp_omp_num_threads(loc); |
Jonathan Peyton | 642688b | 2017-06-01 16:46:36 +0000 | [diff] [blame] | 1600 | if (nth > 0) { // 0 means debugger doesn't want to change num threads |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1601 | master_set_numthreads = nth; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1602 | } |
| 1603 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1604 | #endif |
| 1605 | |
| 1606 | KF_TRACE(10, ("__kmp_fork_call: before internal fork: root=%p, team=%p, " |
| 1607 | "master_th=%p, gtid=%d\n", |
| 1608 | root, parent_team, master_th, gtid)); |
| 1609 | __kmp_internal_fork(loc, gtid, parent_team); |
| 1610 | KF_TRACE(10, ("__kmp_fork_call: after internal fork: root=%p, team=%p, " |
| 1611 | "master_th=%p, gtid=%d\n", |
| 1612 | root, parent_team, master_th, gtid)); |
| 1613 | |
| 1614 | /* Invoke microtask for MASTER thread */ |
| 1615 | KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) invoke microtask = %p\n", gtid, |
| 1616 | parent_team->t.t_id, parent_team->t.t_pkfn)); |
| 1617 | |
| 1618 | { |
| 1619 | KMP_TIME_PARTITIONED_BLOCK(OMP_parallel); |
| 1620 | KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK); |
| 1621 | if (!parent_team->t.t_invoke(gtid)) { |
| 1622 | KMP_ASSERT2(0, "cannot invoke microtask for MASTER thread"); |
| 1623 | } |
| 1624 | } |
| 1625 | KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) done microtask = %p\n", gtid, |
| 1626 | parent_team->t.t_id, parent_team->t.t_pkfn)); |
| 1627 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 1628 | |
| 1629 | KA_TRACE(20, ("__kmp_fork_call: parallel exit T#%d\n", gtid)); |
| 1630 | |
| 1631 | return TRUE; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1632 | } // Parallel closely nested in teams construct |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1633 | #endif /* OMP_40_ENABLED */ |
| 1634 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1635 | #if KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1636 | if (__kmp_tasking_mode != tskm_immediate_exec) { |
| 1637 | KMP_DEBUG_ASSERT(master_th->th.th_task_team == |
| 1638 | parent_team->t.t_task_team[master_th->th.th_task_state]); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1639 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1640 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1641 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1642 | if (parent_team->t.t_active_level >= |
| 1643 | master_th->th.th_current_task->td_icvs.max_active_levels) { |
| 1644 | nthreads = 1; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1645 | } else { |
Andrey Churbanov | 92effc4 | 2015-08-18 10:08:27 +0000 | [diff] [blame] | 1646 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1647 | int enter_teams = ((ap == NULL && active_level == 0) || |
| 1648 | (ap && teams_level > 0 && teams_level == level)); |
Andrey Churbanov | 92effc4 | 2015-08-18 10:08:27 +0000 | [diff] [blame] | 1649 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1650 | nthreads = |
| 1651 | master_set_numthreads |
| 1652 | ? master_set_numthreads |
| 1653 | : get__nproc_2( |
| 1654 | parent_team, |
| 1655 | master_tid); // TODO: get nproc directly from current task |
Andrey Churbanov | 92effc4 | 2015-08-18 10:08:27 +0000 | [diff] [blame] | 1656 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1657 | // Check if we need to take forkjoin lock? (no need for serialized |
| 1658 | // parallel out of teams construct). This code moved here from |
| 1659 | // __kmp_reserve_threads() to speedup nested serialized parallels. |
| 1660 | if (nthreads > 1) { |
| 1661 | if ((!get__nested(master_th) && (root->r.r_in_parallel |
Andrey Churbanov | 92effc4 | 2015-08-18 10:08:27 +0000 | [diff] [blame] | 1662 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1663 | && !enter_teams |
Andrey Churbanov | 92effc4 | 2015-08-18 10:08:27 +0000 | [diff] [blame] | 1664 | #endif /* OMP_40_ENABLED */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1665 | )) || |
| 1666 | (__kmp_library == library_serial)) { |
Jonathan Peyton | 642688b | 2017-06-01 16:46:36 +0000 | [diff] [blame] | 1667 | KC_TRACE(10, ("__kmp_fork_call: T#%d serializing team; requested %d" |
| 1668 | " threads\n", |
| 1669 | gtid, nthreads)); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1670 | nthreads = 1; |
Andrey Churbanov | 92effc4 | 2015-08-18 10:08:27 +0000 | [diff] [blame] | 1671 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1672 | } |
| 1673 | if (nthreads > 1) { |
| 1674 | /* determine how many new threads we can use */ |
| 1675 | __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1676 | nthreads = __kmp_reserve_threads( |
| 1677 | root, parent_team, master_tid, nthreads |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1678 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1679 | /* AC: If we execute teams from parallel region (on host), then |
| 1680 | teams should be created but each can only have 1 thread if |
| 1681 | nesting is disabled. If teams called from serial region, then |
| 1682 | teams and their threads should be created regardless of the |
| 1683 | nesting setting. */ |
| 1684 | , |
| 1685 | enter_teams |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1686 | #endif /* OMP_40_ENABLED */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1687 | ); |
| 1688 | if (nthreads == 1) { |
| 1689 | // Free lock for single thread execution here; for multi-thread |
| 1690 | // execution it will be freed later after team of threads created |
| 1691 | // and initialized |
| 1692 | __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); |
Andrey Churbanov | 92effc4 | 2015-08-18 10:08:27 +0000 | [diff] [blame] | 1693 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1694 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1695 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1696 | KMP_DEBUG_ASSERT(nthreads > 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1697 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1698 | // If we temporarily changed the set number of threads then restore it now |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1699 | master_th->th.th_set_nproc = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1700 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1701 | /* create a serialized parallel region? */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1702 | if (nthreads == 1) { |
| 1703 | /* josh todo: hypothetical question: what do we do for OS X*? */ |
| 1704 | #if KMP_OS_LINUX && \ |
| 1705 | (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) |
| 1706 | void *args[argc]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1707 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1708 | void **args = (void **)KMP_ALLOCA(argc * sizeof(void *)); |
| 1709 | #endif /* KMP_OS_LINUX && ( KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || \ |
| 1710 | KMP_ARCH_AARCH64) */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1711 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1712 | KA_TRACE(20, |
| 1713 | ("__kmp_fork_call: T#%d serializing parallel region\n", gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1714 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1715 | __kmpc_serialized_parallel(loc, gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1716 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1717 | if (call_context == fork_context_intel) { |
| 1718 | /* TODO this sucks, use the compiler itself to pass args! :) */ |
| 1719 | master_th->th.th_serial_team->t.t_ident = loc; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1720 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1721 | if (!ap) { |
| 1722 | // revert change made in __kmpc_serialized_parallel() |
| 1723 | master_th->th.th_serial_team->t.t_level--; |
| 1724 | // Get args from parent team for teams construct |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1725 | |
| 1726 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1727 | void *dummy; |
| 1728 | void **exit_runtime_p; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1729 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1730 | ompt_lw_taskteam_t lw_taskteam; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1731 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1732 | if (ompt_enabled) { |
| 1733 | __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid, |
| 1734 | unwrapped_task, ompt_parallel_id); |
| 1735 | lw_taskteam.ompt_task_info.task_id = __ompt_task_id_new(gtid); |
| 1736 | exit_runtime_p = |
| 1737 | &(lw_taskteam.ompt_task_info.frame.exit_runtime_frame); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1738 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1739 | __ompt_lw_taskteam_link(&lw_taskteam, master_th); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1740 | |
| 1741 | #if OMPT_TRACE |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1742 | my_task_id = lw_taskteam.ompt_task_info.task_id; |
| 1743 | if (ompt_callbacks.ompt_callback(ompt_event_implicit_task_begin)) { |
| 1744 | ompt_callbacks.ompt_callback(ompt_event_implicit_task_begin)( |
| 1745 | ompt_parallel_id, my_task_id); |
| 1746 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1747 | #endif |
| 1748 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1749 | /* OMPT state */ |
| 1750 | master_th->th.ompt_thread_info.state = ompt_state_work_parallel; |
| 1751 | } else { |
| 1752 | exit_runtime_p = &dummy; |
| 1753 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1754 | #endif |
| 1755 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1756 | { |
| 1757 | KMP_TIME_PARTITIONED_BLOCK(OMP_parallel); |
| 1758 | KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK); |
| 1759 | __kmp_invoke_microtask(microtask, gtid, 0, argc, |
| 1760 | parent_team->t.t_argv |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1761 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1762 | , |
| 1763 | exit_runtime_p |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1764 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1765 | ); |
| 1766 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1767 | |
| 1768 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1769 | *exit_runtime_p = NULL; |
| 1770 | if (ompt_enabled) { |
| 1771 | lw_taskteam.ompt_task_info.frame.exit_runtime_frame = NULL; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1772 | |
| 1773 | #if OMPT_TRACE |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1774 | if (ompt_callbacks.ompt_callback(ompt_event_implicit_task_end)) { |
| 1775 | ompt_callbacks.ompt_callback(ompt_event_implicit_task_end)( |
| 1776 | ompt_parallel_id, ompt_task_id); |
| 1777 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1778 | #endif |
| 1779 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1780 | __ompt_lw_taskteam_unlink(master_th); |
| 1781 | // reset clear the task id only after unlinking the task |
| 1782 | lw_taskteam.ompt_task_info.task_id = ompt_task_id_none; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1783 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1784 | if (ompt_callbacks.ompt_callback(ompt_event_parallel_end)) { |
| 1785 | ompt_callbacks.ompt_callback(ompt_event_parallel_end)( |
| 1786 | ompt_parallel_id, ompt_task_id, OMPT_INVOKER(call_context)); |
| 1787 | } |
| 1788 | master_th->th.ompt_thread_info.state = ompt_state_overhead; |
| 1789 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1790 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1791 | } else if (microtask == (microtask_t)__kmp_teams_master) { |
| 1792 | KMP_DEBUG_ASSERT(master_th->th.th_team == |
| 1793 | master_th->th.th_serial_team); |
| 1794 | team = master_th->th.th_team; |
| 1795 | // team->t.t_pkfn = microtask; |
| 1796 | team->t.t_invoke = invoker; |
| 1797 | __kmp_alloc_argv_entries(argc, team, TRUE); |
| 1798 | team->t.t_argc = argc; |
| 1799 | argv = (void **)team->t.t_argv; |
| 1800 | if (ap) { |
| 1801 | for (i = argc - 1; i >= 0; --i) |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1802 | // TODO: revert workaround for Intel(R) 64 tracker #96 |
Andrey Churbanov | cbda868 | 2015-01-13 14:43:35 +0000 | [diff] [blame] | 1803 | #if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1804 | *argv++ = va_arg(*ap, void *); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1805 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1806 | *argv++ = va_arg(ap, void *); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1807 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1808 | } else { |
| 1809 | for (i = 0; i < argc; ++i) |
| 1810 | // Get args from parent team for teams construct |
| 1811 | argv[i] = parent_team->t.t_argv[i]; |
| 1812 | } |
| 1813 | // AC: revert change made in __kmpc_serialized_parallel() |
| 1814 | // because initial code in teams should have level=0 |
| 1815 | team->t.t_level--; |
| 1816 | // AC: call special invoker for outer "parallel" of teams construct |
| 1817 | { |
| 1818 | KMP_TIME_PARTITIONED_BLOCK(OMP_parallel); |
| 1819 | KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK); |
| 1820 | invoker(gtid); |
| 1821 | } |
| 1822 | } else { |
| 1823 | #endif /* OMP_40_ENABLED */ |
| 1824 | argv = args; |
| 1825 | for (i = argc - 1; i >= 0; --i) |
| 1826 | // TODO: revert workaround for Intel(R) 64 tracker #96 |
| 1827 | #if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX |
| 1828 | *argv++ = va_arg(*ap, void *); |
| 1829 | #else |
| 1830 | *argv++ = va_arg(ap, void *); |
| 1831 | #endif |
| 1832 | KMP_MB(); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1833 | |
| 1834 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1835 | void *dummy; |
| 1836 | void **exit_runtime_p; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1837 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1838 | ompt_lw_taskteam_t lw_taskteam; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1839 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1840 | if (ompt_enabled) { |
| 1841 | __ompt_lw_taskteam_init(&lw_taskteam, master_th, gtid, |
| 1842 | unwrapped_task, ompt_parallel_id); |
| 1843 | lw_taskteam.ompt_task_info.task_id = __ompt_task_id_new(gtid); |
| 1844 | exit_runtime_p = |
| 1845 | &(lw_taskteam.ompt_task_info.frame.exit_runtime_frame); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1846 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1847 | __ompt_lw_taskteam_link(&lw_taskteam, master_th); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1848 | |
| 1849 | #if OMPT_TRACE |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1850 | /* OMPT implicit task begin */ |
| 1851 | my_task_id = lw_taskteam.ompt_task_info.task_id; |
| 1852 | my_parallel_id = ompt_parallel_id; |
| 1853 | if (ompt_callbacks.ompt_callback(ompt_event_implicit_task_begin)) { |
| 1854 | ompt_callbacks.ompt_callback(ompt_event_implicit_task_begin)( |
| 1855 | my_parallel_id, my_task_id); |
| 1856 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1857 | #endif |
| 1858 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1859 | /* OMPT state */ |
| 1860 | master_th->th.ompt_thread_info.state = ompt_state_work_parallel; |
| 1861 | } else { |
| 1862 | exit_runtime_p = &dummy; |
| 1863 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1864 | #endif |
| 1865 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1866 | { |
| 1867 | KMP_TIME_PARTITIONED_BLOCK(OMP_parallel); |
| 1868 | KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK); |
| 1869 | __kmp_invoke_microtask(microtask, gtid, 0, argc, args |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1870 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1871 | , |
| 1872 | exit_runtime_p |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1873 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1874 | ); |
| 1875 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1876 | |
| 1877 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1878 | *exit_runtime_p = NULL; |
| 1879 | if (ompt_enabled) { |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1880 | #if OMPT_TRACE |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1881 | lw_taskteam.ompt_task_info.frame.exit_runtime_frame = NULL; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1882 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1883 | if (ompt_callbacks.ompt_callback(ompt_event_implicit_task_end)) { |
| 1884 | ompt_callbacks.ompt_callback(ompt_event_implicit_task_end)( |
| 1885 | my_parallel_id, my_task_id); |
| 1886 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1887 | #endif |
| 1888 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1889 | __ompt_lw_taskteam_unlink(master_th); |
| 1890 | // reset clear the task id only after unlinking the task |
| 1891 | lw_taskteam.ompt_task_info.task_id = ompt_task_id_none; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1892 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1893 | if (ompt_callbacks.ompt_callback(ompt_event_parallel_end)) { |
| 1894 | ompt_callbacks.ompt_callback(ompt_event_parallel_end)( |
| 1895 | ompt_parallel_id, ompt_task_id, OMPT_INVOKER(call_context)); |
| 1896 | } |
| 1897 | master_th->th.ompt_thread_info.state = ompt_state_overhead; |
| 1898 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1899 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1900 | #if OMP_40_ENABLED |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1901 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1902 | #endif /* OMP_40_ENABLED */ |
| 1903 | } else if (call_context == fork_context_gnu) { |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1904 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1905 | ompt_lw_taskteam_t *lwt = |
| 1906 | (ompt_lw_taskteam_t *)__kmp_allocate(sizeof(ompt_lw_taskteam_t)); |
| 1907 | __ompt_lw_taskteam_init(lwt, master_th, gtid, unwrapped_task, |
| 1908 | ompt_parallel_id); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1909 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1910 | lwt->ompt_task_info.task_id = __ompt_task_id_new(gtid); |
| 1911 | lwt->ompt_task_info.frame.exit_runtime_frame = NULL; |
| 1912 | __ompt_lw_taskteam_link(lwt, master_th); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 1913 | #endif |
| 1914 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1915 | // we were called from GNU native code |
| 1916 | KA_TRACE(20, ("__kmp_fork_call: T#%d serial exit\n", gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1917 | return FALSE; |
Andrey Churbanov | c47afcd | 2017-07-03 11:24:08 +0000 | [diff] [blame] | 1918 | } else { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1919 | KMP_ASSERT2(call_context < fork_context_last, |
| 1920 | "__kmp_fork_call: unknown fork_context parameter"); |
| 1921 | } |
| 1922 | |
| 1923 | KA_TRACE(20, ("__kmp_fork_call: T#%d serial exit\n", gtid)); |
| 1924 | KMP_MB(); |
| 1925 | return FALSE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1928 | // GEH: only modify the executing flag in the case when not serialized |
| 1929 | // serialized case is handled in kmpc_serialized_parallel |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1930 | KF_TRACE(10, ("__kmp_fork_call: parent_team_aclevel=%d, master_th=%p, " |
| 1931 | "curtask=%p, curtask_max_aclevel=%d\n", |
| 1932 | parent_team->t.t_active_level, master_th, |
| 1933 | master_th->th.th_current_task, |
| 1934 | master_th->th.th_current_task->td_icvs.max_active_levels)); |
| 1935 | // TODO: GEH - cannot do this assertion because root thread not set up as |
| 1936 | // executing |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1937 | // KMP_ASSERT( master_th->th.th_current_task->td_flags.executing == 1 ); |
| 1938 | master_th->th.th_current_task->td_flags.executing = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1939 | |
| 1940 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1941 | if (!master_th->th.th_teams_microtask || level > teams_level) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1942 | #endif /* OMP_40_ENABLED */ |
| 1943 | { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1944 | /* Increment our nested depth level */ |
| 1945 | KMP_TEST_THEN_INC32((kmp_int32 *)&root->r.r_in_parallel); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1948 | // See if we need to make a copy of the ICVs. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1949 | int nthreads_icv = master_th->th.th_current_task->td_icvs.nproc; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1950 | if ((level + 1 < __kmp_nested_nth.used) && |
| 1951 | (__kmp_nested_nth.nth[level + 1] != nthreads_icv)) { |
| 1952 | nthreads_icv = __kmp_nested_nth.nth[level + 1]; |
| 1953 | } else { |
| 1954 | nthreads_icv = 0; // don't update |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | #if OMP_40_ENABLED |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1958 | // Figure out the proc_bind_policy for the new team. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1959 | kmp_proc_bind_t proc_bind = master_th->th.th_set_proc_bind; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1960 | kmp_proc_bind_t proc_bind_icv = |
| 1961 | proc_bind_default; // proc_bind_default means don't update |
| 1962 | if (master_th->th.th_current_task->td_icvs.proc_bind == proc_bind_false) { |
| 1963 | proc_bind = proc_bind_false; |
| 1964 | } else { |
| 1965 | if (proc_bind == proc_bind_default) { |
| 1966 | // No proc_bind clause specified; use current proc-bind-var for this |
| 1967 | // parallel region |
| 1968 | proc_bind = master_th->th.th_current_task->td_icvs.proc_bind; |
| 1969 | } |
| 1970 | /* else: The proc_bind policy was specified explicitly on parallel clause. |
| 1971 | This overrides proc-bind-var for this parallel region, but does not |
| 1972 | change proc-bind-var. */ |
| 1973 | // Figure the value of proc-bind-var for the child threads. |
| 1974 | if ((level + 1 < __kmp_nested_proc_bind.used) && |
| 1975 | (__kmp_nested_proc_bind.bind_types[level + 1] != |
| 1976 | master_th->th.th_current_task->td_icvs.proc_bind)) { |
| 1977 | proc_bind_icv = __kmp_nested_proc_bind.bind_types[level + 1]; |
| 1978 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1981 | // Reset for next parallel region |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1982 | master_th->th.th_set_proc_bind = proc_bind_default; |
| 1983 | #endif /* OMP_40_ENABLED */ |
| 1984 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1985 | if ((nthreads_icv > 0) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1986 | #if OMP_40_ENABLED |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1987 | || (proc_bind_icv != proc_bind_default) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1988 | #endif /* OMP_40_ENABLED */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1989 | ) { |
| 1990 | kmp_internal_control_t new_icvs; |
| 1991 | copy_icvs(&new_icvs, &master_th->th.th_current_task->td_icvs); |
| 1992 | new_icvs.next = NULL; |
| 1993 | if (nthreads_icv > 0) { |
| 1994 | new_icvs.nproc = nthreads_icv; |
| 1995 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1996 | |
| 1997 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1998 | if (proc_bind_icv != proc_bind_default) { |
| 1999 | new_icvs.proc_bind = proc_bind_icv; |
| 2000 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2001 | #endif /* OMP_40_ENABLED */ |
| 2002 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2003 | /* allocate a new parallel team */ |
| 2004 | KF_TRACE(10, ("__kmp_fork_call: before __kmp_allocate_team\n")); |
| 2005 | team = __kmp_allocate_team(root, nthreads, nthreads, |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2006 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2007 | ompt_parallel_id, |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2008 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2009 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2010 | proc_bind, |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2011 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2012 | &new_icvs, argc USE_NESTED_HOT_ARG(master_th)); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 2013 | } else { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2014 | /* allocate a new parallel team */ |
| 2015 | KF_TRACE(10, ("__kmp_fork_call: before __kmp_allocate_team\n")); |
| 2016 | team = __kmp_allocate_team(root, nthreads, nthreads, |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2017 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2018 | ompt_parallel_id, |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2019 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2020 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2021 | proc_bind, |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2022 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2023 | &master_th->th.th_current_task->td_icvs, |
| 2024 | argc USE_NESTED_HOT_ARG(master_th)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2025 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2026 | KF_TRACE( |
| 2027 | 10, ("__kmp_fork_call: after __kmp_allocate_team - team = %p\n", team)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2028 | |
| 2029 | /* setup the new team */ |
Jonathan Peyton | b044e4f | 2016-05-23 18:01:19 +0000 | [diff] [blame] | 2030 | KMP_CHECK_UPDATE(team->t.t_master_tid, master_tid); |
| 2031 | KMP_CHECK_UPDATE(team->t.t_master_this_cons, master_this_cons); |
| 2032 | KMP_CHECK_UPDATE(team->t.t_ident, loc); |
| 2033 | KMP_CHECK_UPDATE(team->t.t_parent, parent_team); |
| 2034 | KMP_CHECK_UPDATE_SYNC(team->t.t_pkfn, microtask); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2035 | #if OMPT_SUPPORT |
Jonathan Peyton | b044e4f | 2016-05-23 18:01:19 +0000 | [diff] [blame] | 2036 | KMP_CHECK_UPDATE_SYNC(team->t.ompt_team_info.microtask, unwrapped_task); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2037 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2038 | KMP_CHECK_UPDATE(team->t.t_invoke, invoker); // TODO move to root, maybe |
| 2039 | // TODO: parent_team->t.t_level == INT_MAX ??? |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2040 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2041 | if (!master_th->th.th_teams_microtask || level > teams_level) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2042 | #endif /* OMP_40_ENABLED */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2043 | int new_level = parent_team->t.t_level + 1; |
| 2044 | KMP_CHECK_UPDATE(team->t.t_level, new_level); |
| 2045 | new_level = parent_team->t.t_active_level + 1; |
| 2046 | KMP_CHECK_UPDATE(team->t.t_active_level, new_level); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2047 | #if OMP_40_ENABLED |
| 2048 | } else { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2049 | // AC: Do not increase parallel level at start of the teams construct |
| 2050 | int new_level = parent_team->t.t_level; |
| 2051 | KMP_CHECK_UPDATE(team->t.t_level, new_level); |
| 2052 | new_level = parent_team->t.t_active_level; |
| 2053 | KMP_CHECK_UPDATE(team->t.t_active_level, new_level); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2054 | } |
| 2055 | #endif /* OMP_40_ENABLED */ |
Jonathan Peyton | b044e4f | 2016-05-23 18:01:19 +0000 | [diff] [blame] | 2056 | kmp_r_sched_t new_sched = get__sched_2(parent_team, master_tid); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2057 | if (team->t.t_sched.r_sched_type != new_sched.r_sched_type || |
| 2058 | team->t.t_sched.chunk != new_sched.chunk) |
| 2059 | team->t.t_sched = |
| 2060 | new_sched; // set master's schedule as new run-time schedule |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2061 | |
Jonathan Peyton | 45ca5da | 2015-10-19 19:33:38 +0000 | [diff] [blame] | 2062 | #if OMP_40_ENABLED |
Jonathan Peyton | b044e4f | 2016-05-23 18:01:19 +0000 | [diff] [blame] | 2063 | KMP_CHECK_UPDATE(team->t.t_cancel_request, cancel_noreq); |
Jonathan Peyton | 45ca5da | 2015-10-19 19:33:38 +0000 | [diff] [blame] | 2064 | #endif |
| 2065 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 2066 | // Update the floating point rounding in the team if required. |
| 2067 | propagateFPControl(team); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2068 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2069 | if (__kmp_tasking_mode != tskm_immediate_exec) { |
| 2070 | // Set master's task team to team's task team. Unless this is hot team, it |
| 2071 | // should be NULL. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2072 | KMP_DEBUG_ASSERT(master_th->th.th_task_team == |
| 2073 | parent_team->t.t_task_team[master_th->th.th_task_state]); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2074 | KA_TRACE(20, ("__kmp_fork_call: Master T#%d pushing task_team %p / team " |
| 2075 | "%p, new task_team %p / team %p\n", |
| 2076 | __kmp_gtid_from_thread(master_th), |
| 2077 | master_th->th.th_task_team, parent_team, |
| 2078 | team->t.t_task_team[master_th->th.th_task_state], team)); |
Jonathan Peyton | d3f2b94 | 2016-02-09 22:32:41 +0000 | [diff] [blame] | 2079 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2080 | if (active_level || master_th->th.th_task_team) { |
| 2081 | // Take a memo of master's task_state |
| 2082 | KMP_DEBUG_ASSERT(master_th->th.th_task_state_memo_stack); |
| 2083 | if (master_th->th.th_task_state_top >= |
| 2084 | master_th->th.th_task_state_stack_sz) { // increase size |
| 2085 | kmp_uint32 new_size = 2 * master_th->th.th_task_state_stack_sz; |
| 2086 | kmp_uint8 *old_stack, *new_stack; |
| 2087 | kmp_uint32 i; |
| 2088 | new_stack = (kmp_uint8 *)__kmp_allocate(new_size); |
| 2089 | for (i = 0; i < master_th->th.th_task_state_stack_sz; ++i) { |
| 2090 | new_stack[i] = master_th->th.th_task_state_memo_stack[i]; |
| 2091 | } |
| 2092 | for (i = master_th->th.th_task_state_stack_sz; i < new_size; |
| 2093 | ++i) { // zero-init rest of stack |
| 2094 | new_stack[i] = 0; |
| 2095 | } |
| 2096 | old_stack = master_th->th.th_task_state_memo_stack; |
| 2097 | master_th->th.th_task_state_memo_stack = new_stack; |
| 2098 | master_th->th.th_task_state_stack_sz = new_size; |
| 2099 | __kmp_free(old_stack); |
Andrey Churbanov | 6d224db | 2015-02-10 18:37:43 +0000 | [diff] [blame] | 2100 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2101 | // Store master's task_state on stack |
| 2102 | master_th->th |
| 2103 | .th_task_state_memo_stack[master_th->th.th_task_state_top] = |
| 2104 | master_th->th.th_task_state; |
| 2105 | master_th->th.th_task_state_top++; |
| 2106 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 642688b | 2017-06-01 16:46:36 +0000 | [diff] [blame] | 2107 | if (team == master_th->th.th_hot_teams[active_level].hot_team) { |
| 2108 | // Restore master's nested state if nested hot team |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2109 | master_th->th.th_task_state = |
| 2110 | master_th->th |
| 2111 | .th_task_state_memo_stack[master_th->th.th_task_state_top]; |
| 2112 | } else { |
| 2113 | #endif |
| 2114 | master_th->th.th_task_state = 0; |
| 2115 | #if KMP_NESTED_HOT_TEAMS |
| 2116 | } |
| 2117 | #endif |
| 2118 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 2119 | #if !KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2120 | KMP_DEBUG_ASSERT((master_th->th.th_task_team == NULL) || |
| 2121 | (team == root->r.r_hot_team)); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 2122 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2123 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2124 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2125 | KA_TRACE( |
| 2126 | 20, |
| 2127 | ("__kmp_fork_call: T#%d(%d:%d)->(%d:0) created a team of %d threads\n", |
| 2128 | gtid, parent_team->t.t_id, team->t.t_master_tid, team->t.t_id, |
| 2129 | team->t.t_nproc)); |
| 2130 | KMP_DEBUG_ASSERT(team != root->r.r_hot_team || |
| 2131 | (team->t.t_master_tid == 0 && |
| 2132 | (team->t.t_parent == root->r.r_root_team || |
| 2133 | team->t.t_parent->t.t_serialized))); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2134 | KMP_MB(); |
| 2135 | |
| 2136 | /* now, setup the arguments */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2137 | argv = (void **)team->t.t_argv; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2138 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2139 | if (ap) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2140 | #endif /* OMP_40_ENABLED */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2141 | for (i = argc - 1; i >= 0; --i) { |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 2142 | // TODO: revert workaround for Intel(R) 64 tracker #96 |
Andrey Churbanov | cbda868 | 2015-01-13 14:43:35 +0000 | [diff] [blame] | 2143 | #if (KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && KMP_OS_LINUX |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2144 | void *new_argv = va_arg(*ap, void *); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2145 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2146 | void *new_argv = va_arg(ap, void *); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2147 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2148 | KMP_CHECK_UPDATE(*argv, new_argv); |
| 2149 | argv++; |
| 2150 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2151 | #if OMP_40_ENABLED |
| 2152 | } else { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2153 | for (i = 0; i < argc; ++i) { |
| 2154 | // Get args from parent team for teams construct |
| 2155 | KMP_CHECK_UPDATE(argv[i], team->t.t_parent->t.t_argv[i]); |
| 2156 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2157 | } |
| 2158 | #endif /* OMP_40_ENABLED */ |
| 2159 | |
| 2160 | /* now actually fork the threads */ |
Jonathan Peyton | b044e4f | 2016-05-23 18:01:19 +0000 | [diff] [blame] | 2161 | KMP_CHECK_UPDATE(team->t.t_master_active, master_active); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 2162 | if (!root->r.r_active) // Only do assignment if it prevents cache ping-pong |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2163 | root->r.r_active = TRUE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2164 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2165 | __kmp_fork_team_threads(root, team, master_th, gtid); |
| 2166 | __kmp_setup_icv_copy(team, nthreads, |
| 2167 | &master_th->th.th_current_task->td_icvs, loc); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2168 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2169 | #if OMPT_SUPPORT |
| 2170 | master_th->th.ompt_thread_info.state = ompt_state_work_parallel; |
| 2171 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2172 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2173 | __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2174 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2175 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2176 | if (team->t.t_active_level == 1 // only report frames at level 1 |
| 2177 | #if OMP_40_ENABLED |
Andrey Churbanov | 51aecb8 | 2015-05-06 19:22:36 +0000 | [diff] [blame] | 2178 | && !master_th->th.th_teams_microtask // not in teams construct |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2179 | #endif /* OMP_40_ENABLED */ |
| 2180 | ) { |
Andrey Churbanov | 51aecb8 | 2015-05-06 19:22:36 +0000 | [diff] [blame] | 2181 | #if USE_ITT_NOTIFY |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2182 | if ((__itt_frame_submit_v3_ptr || KMP_ITT_DEBUG) && |
| 2183 | (__kmp_forkjoin_frames_mode == 3 || |
| 2184 | __kmp_forkjoin_frames_mode == 1)) { |
| 2185 | kmp_uint64 tmp_time = 0; |
| 2186 | if (__itt_get_timestamp_ptr) |
| 2187 | tmp_time = __itt_get_timestamp(); |
| 2188 | // Internal fork - report frame begin |
| 2189 | master_th->th.th_frame_time = tmp_time; |
| 2190 | if (__kmp_forkjoin_frames_mode == 3) |
| 2191 | team->t.t_region_time = tmp_time; |
Jonathan Peyton | 642688b | 2017-06-01 16:46:36 +0000 | [diff] [blame] | 2192 | } else |
| 2193 | // only one notification scheme (either "submit" or "forking/joined", not both) |
Andrey Churbanov | 51aecb8 | 2015-05-06 19:22:36 +0000 | [diff] [blame] | 2194 | #endif /* USE_ITT_NOTIFY */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2195 | if ((__itt_frame_begin_v3_ptr || KMP_ITT_DEBUG) && |
| 2196 | __kmp_forkjoin_frames && !__kmp_forkjoin_frames_mode) { |
| 2197 | // Mark start of "parallel" region for VTune. |
| 2198 | __kmp_itt_region_forking(gtid, team->t.t_nproc, 0); |
| 2199 | } |
Andrey Churbanov | f6451d9 | 2015-01-16 15:58:03 +0000 | [diff] [blame] | 2200 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2201 | #endif /* USE_ITT_BUILD */ |
| 2202 | |
| 2203 | /* now go on and do the work */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2204 | KMP_DEBUG_ASSERT(team == __kmp_threads[gtid]->th.th_team); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2205 | KMP_MB(); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2206 | KF_TRACE(10, |
| 2207 | ("__kmp_internal_fork : root=%p, team=%p, master_th=%p, gtid=%d\n", |
| 2208 | root, team, master_th, gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2209 | |
| 2210 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2211 | if (__itt_stack_caller_create_ptr) { |
| 2212 | team->t.t_stack_id = |
| 2213 | __kmp_itt_stack_caller_create(); // create new stack stitching id |
| 2214 | // before entering fork barrier |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2215 | } |
| 2216 | #endif /* USE_ITT_BUILD */ |
| 2217 | |
| 2218 | #if OMP_40_ENABLED |
Jonathan Peyton | 642688b | 2017-06-01 16:46:36 +0000 | [diff] [blame] | 2219 | // AC: skip __kmp_internal_fork at teams construct, let only master |
| 2220 | // threads execute |
| 2221 | if (ap) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2222 | #endif /* OMP_40_ENABLED */ |
| 2223 | { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2224 | __kmp_internal_fork(loc, gtid, team); |
| 2225 | KF_TRACE(10, ("__kmp_internal_fork : after : root=%p, team=%p, " |
| 2226 | "master_th=%p, gtid=%d\n", |
| 2227 | root, team, master_th, gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 2230 | if (call_context == fork_context_gnu) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2231 | KA_TRACE(20, ("__kmp_fork_call: parallel exit T#%d\n", gtid)); |
| 2232 | return TRUE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | /* Invoke microtask for MASTER thread */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2236 | KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) invoke microtask = %p\n", gtid, |
| 2237 | team->t.t_id, team->t.t_pkfn)); |
| 2238 | } // END of timer KMP_fork_call block |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2239 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2240 | { |
| 2241 | KMP_TIME_PARTITIONED_BLOCK(OMP_parallel); |
| 2242 | KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK); |
| 2243 | if (!team->t.t_invoke(gtid)) { |
| 2244 | KMP_ASSERT2(0, "cannot invoke microtask for MASTER thread"); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2245 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2246 | } |
| 2247 | KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) done microtask = %p\n", gtid, |
| 2248 | team->t.t_id, team->t.t_pkfn)); |
| 2249 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2250 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2251 | KA_TRACE(20, ("__kmp_fork_call: parallel exit T#%d\n", gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2252 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2253 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2254 | if (ompt_enabled) { |
| 2255 | master_th->th.ompt_thread_info.state = ompt_state_overhead; |
| 2256 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2257 | #endif |
| 2258 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2259 | return TRUE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2262 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2263 | static inline void __kmp_join_restore_state(kmp_info_t *thread, |
| 2264 | kmp_team_t *team) { |
| 2265 | // restore state outside the region |
| 2266 | thread->th.ompt_thread_info.state = |
| 2267 | ((team->t.t_serialized) ? ompt_state_work_serial |
| 2268 | : ompt_state_work_parallel); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2271 | static inline void __kmp_join_ompt(kmp_info_t *thread, kmp_team_t *team, |
| 2272 | ompt_parallel_id_t parallel_id, |
| 2273 | fork_context_e fork_context) { |
| 2274 | ompt_task_info_t *task_info = __ompt_get_taskinfo(0); |
| 2275 | if (ompt_callbacks.ompt_callback(ompt_event_parallel_end)) { |
| 2276 | ompt_callbacks.ompt_callback(ompt_event_parallel_end)( |
| 2277 | parallel_id, task_info->task_id, OMPT_INVOKER(fork_context)); |
| 2278 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2279 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2280 | task_info->frame.reenter_runtime_frame = NULL; |
| 2281 | __kmp_join_restore_state(thread, team); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2282 | } |
| 2283 | #endif |
| 2284 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2285 | void __kmp_join_call(ident_t *loc, int gtid |
Jonathan Peyton | f89fbbb | 2015-08-31 18:15:00 +0000 | [diff] [blame] | 2286 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2287 | , |
| 2288 | enum fork_context_e fork_context |
Jonathan Peyton | f89fbbb | 2015-08-31 18:15:00 +0000 | [diff] [blame] | 2289 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2290 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2291 | , |
| 2292 | int exit_teams |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2293 | #endif /* OMP_40_ENABLED */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2294 | ) { |
| 2295 | KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(KMP_join_call); |
| 2296 | kmp_team_t *team; |
| 2297 | kmp_team_t *parent_team; |
| 2298 | kmp_info_t *master_th; |
| 2299 | kmp_root_t *root; |
| 2300 | int master_active; |
| 2301 | int i; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2302 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2303 | KA_TRACE(20, ("__kmp_join_call: enter T#%d\n", gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2304 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2305 | /* setup current data */ |
| 2306 | master_th = __kmp_threads[gtid]; |
| 2307 | root = master_th->th.th_root; |
| 2308 | team = master_th->th.th_team; |
| 2309 | parent_team = team->t.t_parent; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2310 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2311 | master_th->th.th_ident = loc; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2312 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2313 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2314 | if (ompt_enabled) { |
| 2315 | master_th->th.ompt_thread_info.state = ompt_state_overhead; |
| 2316 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2317 | #endif |
| 2318 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 2319 | #if KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2320 | if (__kmp_tasking_mode != tskm_immediate_exec && !exit_teams) { |
| 2321 | KA_TRACE(20, ("__kmp_join_call: T#%d, old team = %p old task_team = %p, " |
| 2322 | "th_task_team = %p\n", |
| 2323 | __kmp_gtid_from_thread(master_th), team, |
| 2324 | team->t.t_task_team[master_th->th.th_task_state], |
| 2325 | master_th->th.th_task_team)); |
| 2326 | KMP_DEBUG_ASSERT(master_th->th.th_task_team == |
| 2327 | team->t.t_task_team[master_th->th.th_task_state]); |
| 2328 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 2329 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2330 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2331 | if (team->t.t_serialized) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2332 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2333 | if (master_th->th.th_teams_microtask) { |
| 2334 | // We are in teams construct |
| 2335 | int level = team->t.t_level; |
| 2336 | int tlevel = master_th->th.th_teams_level; |
| 2337 | if (level == tlevel) { |
| 2338 | // AC: we haven't incremented it earlier at start of teams construct, |
| 2339 | // so do it here - at the end of teams construct |
| 2340 | team->t.t_level++; |
| 2341 | } else if (level == tlevel + 1) { |
| 2342 | // AC: we are exiting parallel inside teams, need to increment |
| 2343 | // serialization in order to restore it in the next call to |
| 2344 | // __kmpc_end_serialized_parallel |
| 2345 | team->t.t_serialized++; |
| 2346 | } |
Andrey Churbanov | 6d224db | 2015-02-10 18:37:43 +0000 | [diff] [blame] | 2347 | } |
Jonathan Peyton | 441f337 | 2015-09-21 17:24:46 +0000 | [diff] [blame] | 2348 | #endif /* OMP_40_ENABLED */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2349 | __kmpc_end_serialized_parallel(loc, gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2350 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2351 | #if OMPT_SUPPORT |
Jonathan Peyton | b68a85d | 2015-09-21 18:11:22 +0000 | [diff] [blame] | 2352 | if (ompt_enabled) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2353 | __kmp_join_restore_state(master_th, parent_team); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 2354 | } |
| 2355 | #endif |
| 2356 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2357 | return; |
| 2358 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2359 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2360 | master_active = team->t.t_master_active; |
| 2361 | |
| 2362 | #if OMP_40_ENABLED |
| 2363 | if (!exit_teams) |
| 2364 | #endif /* OMP_40_ENABLED */ |
| 2365 | { |
| 2366 | // AC: No barrier for internal teams at exit from teams construct. |
| 2367 | // But there is barrier for external team (league). |
| 2368 | __kmp_internal_join(loc, gtid, team); |
| 2369 | } |
| 2370 | #if OMP_40_ENABLED |
| 2371 | else { |
| 2372 | master_th->th.th_task_state = |
| 2373 | 0; // AC: no tasking in teams (out of any parallel) |
| 2374 | } |
| 2375 | #endif /* OMP_40_ENABLED */ |
| 2376 | |
| 2377 | KMP_MB(); |
| 2378 | |
| 2379 | #if OMPT_SUPPORT |
| 2380 | ompt_parallel_id_t parallel_id = team->t.ompt_team_info.parallel_id; |
| 2381 | #endif |
| 2382 | |
| 2383 | #if USE_ITT_BUILD |
| 2384 | if (__itt_stack_caller_create_ptr) { |
| 2385 | __kmp_itt_stack_caller_destroy( |
| 2386 | (__itt_caller)team->t |
| 2387 | .t_stack_id); // destroy the stack stitching id after join barrier |
| 2388 | } |
| 2389 | |
| 2390 | // Mark end of "parallel" region for VTune. |
| 2391 | if (team->t.t_active_level == 1 |
| 2392 | #if OMP_40_ENABLED |
| 2393 | && !master_th->th.th_teams_microtask /* not in teams construct */ |
| 2394 | #endif /* OMP_40_ENABLED */ |
| 2395 | ) { |
| 2396 | master_th->th.th_ident = loc; |
| 2397 | // only one notification scheme (either "submit" or "forking/joined", not |
| 2398 | // both) |
| 2399 | if ((__itt_frame_submit_v3_ptr || KMP_ITT_DEBUG) && |
| 2400 | __kmp_forkjoin_frames_mode == 3) |
| 2401 | __kmp_itt_frame_submit(gtid, team->t.t_region_time, |
| 2402 | master_th->th.th_frame_time, 0, loc, |
| 2403 | master_th->th.th_team_nproc, 1); |
| 2404 | else if ((__itt_frame_end_v3_ptr || KMP_ITT_DEBUG) && |
| 2405 | !__kmp_forkjoin_frames_mode && __kmp_forkjoin_frames) |
| 2406 | __kmp_itt_region_joined(gtid); |
| 2407 | } // active_level == 1 |
| 2408 | #endif /* USE_ITT_BUILD */ |
| 2409 | |
| 2410 | #if OMP_40_ENABLED |
| 2411 | if (master_th->th.th_teams_microtask && !exit_teams && |
| 2412 | team->t.t_pkfn != (microtask_t)__kmp_teams_master && |
| 2413 | team->t.t_level == master_th->th.th_teams_level + 1) { |
| 2414 | // AC: We need to leave the team structure intact at the end of parallel |
| 2415 | // inside the teams construct, so that at the next parallel same (hot) team |
| 2416 | // works, only adjust nesting levels |
| 2417 | |
| 2418 | /* Decrement our nested depth level */ |
| 2419 | team->t.t_level--; |
| 2420 | team->t.t_active_level--; |
| 2421 | KMP_TEST_THEN_DEC32((kmp_int32 *)&root->r.r_in_parallel); |
| 2422 | |
| 2423 | /* Restore number of threads in the team if needed */ |
| 2424 | if (master_th->th.th_team_nproc < master_th->th.th_teams_size.nth) { |
| 2425 | int old_num = master_th->th.th_team_nproc; |
| 2426 | int new_num = master_th->th.th_teams_size.nth; |
| 2427 | kmp_info_t **other_threads = team->t.t_threads; |
| 2428 | team->t.t_nproc = new_num; |
| 2429 | for (i = 0; i < old_num; ++i) { |
| 2430 | other_threads[i]->th.th_team_nproc = new_num; |
| 2431 | } |
| 2432 | // Adjust states of non-used threads of the team |
| 2433 | for (i = old_num; i < new_num; ++i) { |
| 2434 | // Re-initialize thread's barrier data. |
| 2435 | int b; |
| 2436 | kmp_balign_t *balign = other_threads[i]->th.th_bar; |
| 2437 | for (b = 0; b < bs_last_barrier; ++b) { |
| 2438 | balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived; |
| 2439 | KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG); |
| 2440 | #if USE_DEBUGGER |
| 2441 | balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived; |
| 2442 | #endif |
| 2443 | } |
| 2444 | if (__kmp_tasking_mode != tskm_immediate_exec) { |
| 2445 | // Synchronize thread's task state |
| 2446 | other_threads[i]->th.th_task_state = master_th->th.th_task_state; |
| 2447 | } |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | #if OMPT_SUPPORT |
| 2452 | if (ompt_enabled) { |
| 2453 | __kmp_join_ompt(master_th, parent_team, parallel_id, fork_context); |
| 2454 | } |
| 2455 | #endif |
| 2456 | |
| 2457 | return; |
| 2458 | } |
| 2459 | #endif /* OMP_40_ENABLED */ |
| 2460 | |
| 2461 | /* do cleanup and restore the parent team */ |
| 2462 | master_th->th.th_info.ds.ds_tid = team->t.t_master_tid; |
| 2463 | master_th->th.th_local.this_construct = team->t.t_master_this_cons; |
| 2464 | |
| 2465 | master_th->th.th_dispatch = &parent_team->t.t_dispatch[team->t.t_master_tid]; |
| 2466 | |
| 2467 | /* jc: The following lock has instructions with REL and ACQ semantics, |
| 2468 | separating the parallel user code called in this parallel region |
| 2469 | from the serial user code called after this function returns. */ |
| 2470 | __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock); |
| 2471 | |
| 2472 | #if OMP_40_ENABLED |
| 2473 | if (!master_th->th.th_teams_microtask || |
| 2474 | team->t.t_level > master_th->th.th_teams_level) |
| 2475 | #endif /* OMP_40_ENABLED */ |
| 2476 | { |
| 2477 | /* Decrement our nested depth level */ |
| 2478 | KMP_TEST_THEN_DEC32((kmp_int32 *)&root->r.r_in_parallel); |
| 2479 | } |
| 2480 | KMP_DEBUG_ASSERT(root->r.r_in_parallel >= 0); |
| 2481 | |
| 2482 | #if OMPT_SUPPORT && OMPT_TRACE |
| 2483 | if (ompt_enabled) { |
| 2484 | ompt_task_info_t *task_info = __ompt_get_taskinfo(0); |
| 2485 | if (ompt_callbacks.ompt_callback(ompt_event_implicit_task_end)) { |
| 2486 | ompt_callbacks.ompt_callback(ompt_event_implicit_task_end)( |
| 2487 | parallel_id, task_info->task_id); |
| 2488 | } |
| 2489 | task_info->frame.exit_runtime_frame = NULL; |
| 2490 | task_info->task_id = 0; |
| 2491 | } |
| 2492 | #endif |
| 2493 | |
| 2494 | KF_TRACE(10, ("__kmp_join_call1: T#%d, this_thread=%p team=%p\n", 0, |
| 2495 | master_th, team)); |
| 2496 | __kmp_pop_current_task_from_thread(master_th); |
| 2497 | |
| 2498 | #if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED |
| 2499 | // Restore master thread's partition. |
| 2500 | master_th->th.th_first_place = team->t.t_first_place; |
| 2501 | master_th->th.th_last_place = team->t.t_last_place; |
| 2502 | #endif /* OMP_40_ENABLED */ |
| 2503 | |
| 2504 | updateHWFPControl(team); |
| 2505 | |
| 2506 | if (root->r.r_active != master_active) |
| 2507 | root->r.r_active = master_active; |
| 2508 | |
| 2509 | __kmp_free_team(root, team USE_NESTED_HOT_ARG( |
| 2510 | master_th)); // this will free worker threads |
| 2511 | |
| 2512 | /* this race was fun to find. make sure the following is in the critical |
| 2513 | region otherwise assertions may fail occasionally since the old team may be |
| 2514 | reallocated and the hierarchy appears inconsistent. it is actually safe to |
| 2515 | run and won't cause any bugs, but will cause those assertion failures. it's |
| 2516 | only one deref&assign so might as well put this in the critical region */ |
| 2517 | master_th->th.th_team = parent_team; |
| 2518 | master_th->th.th_team_nproc = parent_team->t.t_nproc; |
| 2519 | master_th->th.th_team_master = parent_team->t.t_threads[0]; |
| 2520 | master_th->th.th_team_serialized = parent_team->t.t_serialized; |
| 2521 | |
| 2522 | /* restore serialized team, if need be */ |
| 2523 | if (parent_team->t.t_serialized && |
| 2524 | parent_team != master_th->th.th_serial_team && |
| 2525 | parent_team != root->r.r_root_team) { |
| 2526 | __kmp_free_team(root, |
| 2527 | master_th->th.th_serial_team USE_NESTED_HOT_ARG(NULL)); |
| 2528 | master_th->th.th_serial_team = parent_team; |
| 2529 | } |
| 2530 | |
| 2531 | if (__kmp_tasking_mode != tskm_immediate_exec) { |
| 2532 | if (master_th->th.th_task_state_top > |
| 2533 | 0) { // Restore task state from memo stack |
| 2534 | KMP_DEBUG_ASSERT(master_th->th.th_task_state_memo_stack); |
| 2535 | // Remember master's state if we re-use this nested hot team |
| 2536 | master_th->th.th_task_state_memo_stack[master_th->th.th_task_state_top] = |
| 2537 | master_th->th.th_task_state; |
| 2538 | --master_th->th.th_task_state_top; // pop |
| 2539 | // Now restore state at this level |
| 2540 | master_th->th.th_task_state = |
| 2541 | master_th->th |
| 2542 | .th_task_state_memo_stack[master_th->th.th_task_state_top]; |
| 2543 | } |
| 2544 | // Copy the task team from the parent team to the master thread |
| 2545 | master_th->th.th_task_team = |
| 2546 | parent_team->t.t_task_team[master_th->th.th_task_state]; |
| 2547 | KA_TRACE(20, |
| 2548 | ("__kmp_join_call: Master T#%d restoring task_team %p / team %p\n", |
| 2549 | __kmp_gtid_from_thread(master_th), master_th->th.th_task_team, |
| 2550 | parent_team)); |
| 2551 | } |
| 2552 | |
| 2553 | // TODO: GEH - cannot do this assertion because root thread not set up as |
| 2554 | // executing |
| 2555 | // KMP_ASSERT( master_th->th.th_current_task->td_flags.executing == 0 ); |
| 2556 | master_th->th.th_current_task->td_flags.executing = 1; |
| 2557 | |
| 2558 | __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); |
| 2559 | |
| 2560 | #if OMPT_SUPPORT |
| 2561 | if (ompt_enabled) { |
| 2562 | __kmp_join_ompt(master_th, parent_team, parallel_id, fork_context); |
| 2563 | } |
| 2564 | #endif |
| 2565 | |
| 2566 | KMP_MB(); |
| 2567 | KA_TRACE(20, ("__kmp_join_call: exit T#%d\n", gtid)); |
| 2568 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2569 | |
| 2570 | /* Check whether we should push an internal control record onto the |
| 2571 | serial team stack. If so, do it. */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2572 | void __kmp_save_internal_controls(kmp_info_t *thread) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2573 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2574 | if (thread->th.th_team != thread->th.th_serial_team) { |
| 2575 | return; |
| 2576 | } |
| 2577 | if (thread->th.th_team->t.t_serialized > 1) { |
| 2578 | int push = 0; |
| 2579 | |
| 2580 | if (thread->th.th_team->t.t_control_stack_top == NULL) { |
| 2581 | push = 1; |
| 2582 | } else { |
| 2583 | if (thread->th.th_team->t.t_control_stack_top->serial_nesting_level != |
| 2584 | thread->th.th_team->t.t_serialized) { |
| 2585 | push = 1; |
| 2586 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2587 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2588 | if (push) { /* push a record on the serial team's stack */ |
| 2589 | kmp_internal_control_t *control = |
| 2590 | (kmp_internal_control_t *)__kmp_allocate( |
| 2591 | sizeof(kmp_internal_control_t)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2592 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2593 | copy_icvs(control, &thread->th.th_current_task->td_icvs); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2594 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2595 | control->serial_nesting_level = thread->th.th_team->t.t_serialized; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2596 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2597 | control->next = thread->th.th_team->t.t_control_stack_top; |
| 2598 | thread->th.th_team->t.t_control_stack_top = control; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2599 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2600 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | /* Changes set_nproc */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2604 | void __kmp_set_num_threads(int new_nth, int gtid) { |
| 2605 | kmp_info_t *thread; |
| 2606 | kmp_root_t *root; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2607 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2608 | KF_TRACE(10, ("__kmp_set_num_threads: new __kmp_nth = %d\n", new_nth)); |
| 2609 | KMP_DEBUG_ASSERT(__kmp_init_serial); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2610 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2611 | if (new_nth < 1) |
| 2612 | new_nth = 1; |
| 2613 | else if (new_nth > __kmp_max_nth) |
| 2614 | new_nth = __kmp_max_nth; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2615 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2616 | KMP_COUNT_VALUE(OMP_set_numthreads, new_nth); |
| 2617 | thread = __kmp_threads[gtid]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2618 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2619 | __kmp_save_internal_controls(thread); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2620 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2621 | set__nproc(thread, new_nth); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2622 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2623 | // If this omp_set_num_threads() call will cause the hot team size to be |
| 2624 | // reduced (in the absence of a num_threads clause), then reduce it now, |
| 2625 | // rather than waiting for the next parallel region. |
| 2626 | root = thread->th.th_root; |
| 2627 | if (__kmp_init_parallel && (!root->r.r_active) && |
| 2628 | (root->r.r_hot_team->t.t_nproc > new_nth) |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 2629 | #if KMP_NESTED_HOT_TEAMS |
| 2630 | && __kmp_hot_teams_max_level && !__kmp_hot_teams_mode |
| 2631 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2632 | ) { |
| 2633 | kmp_team_t *hot_team = root->r.r_hot_team; |
| 2634 | int f; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2635 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2636 | __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2637 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2638 | // Release the extra threads we don't need any more. |
| 2639 | for (f = new_nth; f < hot_team->t.t_nproc; f++) { |
| 2640 | KMP_DEBUG_ASSERT(hot_team->t.t_threads[f] != NULL); |
| 2641 | if (__kmp_tasking_mode != tskm_immediate_exec) { |
| 2642 | // When decreasing team size, threads no longer in the team should unref |
| 2643 | // task team. |
| 2644 | hot_team->t.t_threads[f]->th.th_task_team = NULL; |
| 2645 | } |
| 2646 | __kmp_free_thread(hot_team->t.t_threads[f]); |
| 2647 | hot_team->t.t_threads[f] = NULL; |
| 2648 | } |
| 2649 | hot_team->t.t_nproc = new_nth; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 2650 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2651 | if (thread->th.th_hot_teams) { |
| 2652 | KMP_DEBUG_ASSERT(hot_team == thread->th.th_hot_teams[0].hot_team); |
| 2653 | thread->th.th_hot_teams[0].hot_team_nth = new_nth; |
| 2654 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 2655 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2656 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2657 | __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2658 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2659 | // Update the t_nproc field in the threads that are still active. |
| 2660 | for (f = 0; f < new_nth; f++) { |
| 2661 | KMP_DEBUG_ASSERT(hot_team->t.t_threads[f] != NULL); |
| 2662 | hot_team->t.t_threads[f]->th.th_team_nproc = new_nth; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2663 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2664 | // Special flag in case omp_set_num_threads() call |
| 2665 | hot_team->t.t_size_changed = -1; |
| 2666 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2669 | /* Changes max_active_levels */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2670 | void __kmp_set_max_active_levels(int gtid, int max_active_levels) { |
| 2671 | kmp_info_t *thread; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2672 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2673 | KF_TRACE(10, ("__kmp_set_max_active_levels: new max_active_levels for thread " |
| 2674 | "%d = (%d)\n", |
| 2675 | gtid, max_active_levels)); |
| 2676 | KMP_DEBUG_ASSERT(__kmp_init_serial); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2677 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2678 | // validate max_active_levels |
| 2679 | if (max_active_levels < 0) { |
| 2680 | KMP_WARNING(ActiveLevelsNegative, max_active_levels); |
| 2681 | // We ignore this call if the user has specified a negative value. |
| 2682 | // The current setting won't be changed. The last valid setting will be |
| 2683 | // used. A warning will be issued (if warnings are allowed as controlled by |
| 2684 | // the KMP_WARNINGS env var). |
| 2685 | KF_TRACE(10, ("__kmp_set_max_active_levels: the call is ignored: new " |
| 2686 | "max_active_levels for thread %d = (%d)\n", |
| 2687 | gtid, max_active_levels)); |
| 2688 | return; |
| 2689 | } |
| 2690 | if (max_active_levels <= KMP_MAX_ACTIVE_LEVELS_LIMIT) { |
| 2691 | // it's OK, the max_active_levels is within the valid range: [ 0; |
| 2692 | // KMP_MAX_ACTIVE_LEVELS_LIMIT ] |
| 2693 | // We allow a zero value. (implementation defined behavior) |
| 2694 | } else { |
| 2695 | KMP_WARNING(ActiveLevelsExceedLimit, max_active_levels, |
| 2696 | KMP_MAX_ACTIVE_LEVELS_LIMIT); |
| 2697 | max_active_levels = KMP_MAX_ACTIVE_LEVELS_LIMIT; |
| 2698 | // Current upper limit is MAX_INT. (implementation defined behavior) |
| 2699 | // If the input exceeds the upper limit, we correct the input to be the |
| 2700 | // upper limit. (implementation defined behavior) |
| 2701 | // Actually, the flow should never get here until we use MAX_INT limit. |
| 2702 | } |
| 2703 | KF_TRACE(10, ("__kmp_set_max_active_levels: after validation: new " |
| 2704 | "max_active_levels for thread %d = (%d)\n", |
| 2705 | gtid, max_active_levels)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2706 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2707 | thread = __kmp_threads[gtid]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2708 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2709 | __kmp_save_internal_controls(thread); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2710 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2711 | set__max_active_levels(thread, max_active_levels); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | /* Gets max_active_levels */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2715 | int __kmp_get_max_active_levels(int gtid) { |
| 2716 | kmp_info_t *thread; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2717 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2718 | KF_TRACE(10, ("__kmp_get_max_active_levels: thread %d\n", gtid)); |
| 2719 | KMP_DEBUG_ASSERT(__kmp_init_serial); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2720 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2721 | thread = __kmp_threads[gtid]; |
| 2722 | KMP_DEBUG_ASSERT(thread->th.th_current_task); |
| 2723 | KF_TRACE(10, ("__kmp_get_max_active_levels: thread %d, curtask=%p, " |
| 2724 | "curtask_maxaclevel=%d\n", |
| 2725 | gtid, thread->th.th_current_task, |
| 2726 | thread->th.th_current_task->td_icvs.max_active_levels)); |
| 2727 | return thread->th.th_current_task->td_icvs.max_active_levels; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | /* Changes def_sched_var ICV values (run-time schedule kind and chunk) */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2731 | void __kmp_set_schedule(int gtid, kmp_sched_t kind, int chunk) { |
| 2732 | kmp_info_t *thread; |
| 2733 | // kmp_team_t *team; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2734 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2735 | KF_TRACE(10, ("__kmp_set_schedule: new schedule for thread %d = (%d, %d)\n", |
| 2736 | gtid, (int)kind, chunk)); |
| 2737 | KMP_DEBUG_ASSERT(__kmp_init_serial); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2738 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2739 | // Check if the kind parameter is valid, correct if needed. |
| 2740 | // Valid parameters should fit in one of two intervals - standard or extended: |
| 2741 | // <lower>, <valid>, <upper_std>, <lower_ext>, <valid>, <upper> |
| 2742 | // 2008-01-25: 0, 1 - 4, 5, 100, 101 - 102, 103 |
| 2743 | if (kind <= kmp_sched_lower || kind >= kmp_sched_upper || |
| 2744 | (kind <= kmp_sched_lower_ext && kind >= kmp_sched_upper_std)) { |
| 2745 | // TODO: Hint needs attention in case we change the default schedule. |
| 2746 | __kmp_msg(kmp_ms_warning, KMP_MSG(ScheduleKindOutOfRange, kind), |
| 2747 | KMP_HNT(DefaultScheduleKindUsed, "static, no chunk"), |
| 2748 | __kmp_msg_null); |
| 2749 | kind = kmp_sched_default; |
| 2750 | chunk = 0; // ignore chunk value in case of bad kind |
| 2751 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2752 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2753 | thread = __kmp_threads[gtid]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2754 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2755 | __kmp_save_internal_controls(thread); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2756 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2757 | if (kind < kmp_sched_upper_std) { |
| 2758 | if (kind == kmp_sched_static && chunk < KMP_DEFAULT_CHUNK) { |
| 2759 | // differ static chunked vs. unchunked: chunk should be invalid to |
| 2760 | // indicate unchunked schedule (which is the default) |
| 2761 | thread->th.th_current_task->td_icvs.sched.r_sched_type = kmp_sch_static; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2762 | } else { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2763 | thread->th.th_current_task->td_icvs.sched.r_sched_type = |
| 2764 | __kmp_sch_map[kind - kmp_sched_lower - 1]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2765 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2766 | } else { |
| 2767 | // __kmp_sch_map[ kind - kmp_sched_lower_ext + kmp_sched_upper_std - |
| 2768 | // kmp_sched_lower - 2 ]; |
| 2769 | thread->th.th_current_task->td_icvs.sched.r_sched_type = |
| 2770 | __kmp_sch_map[kind - kmp_sched_lower_ext + kmp_sched_upper_std - |
| 2771 | kmp_sched_lower - 2]; |
| 2772 | } |
Andrey Churbanov | d454c73 | 2017-06-05 17:17:33 +0000 | [diff] [blame] | 2773 | if (kind == kmp_sched_auto || chunk < 1) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2774 | // ignore parameter chunk for schedule auto |
| 2775 | thread->th.th_current_task->td_icvs.sched.chunk = KMP_DEFAULT_CHUNK; |
| 2776 | } else { |
| 2777 | thread->th.th_current_task->td_icvs.sched.chunk = chunk; |
| 2778 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | /* Gets def_sched_var ICV values */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2782 | void __kmp_get_schedule(int gtid, kmp_sched_t *kind, int *chunk) { |
| 2783 | kmp_info_t *thread; |
| 2784 | enum sched_type th_type; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2785 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2786 | KF_TRACE(10, ("__kmp_get_schedule: thread %d\n", gtid)); |
| 2787 | KMP_DEBUG_ASSERT(__kmp_init_serial); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2788 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2789 | thread = __kmp_threads[gtid]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2790 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2791 | th_type = thread->th.th_current_task->td_icvs.sched.r_sched_type; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2792 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2793 | switch (th_type) { |
| 2794 | case kmp_sch_static: |
| 2795 | case kmp_sch_static_greedy: |
| 2796 | case kmp_sch_static_balanced: |
| 2797 | *kind = kmp_sched_static; |
| 2798 | *chunk = 0; // chunk was not set, try to show this fact via zero value |
| 2799 | return; |
| 2800 | case kmp_sch_static_chunked: |
| 2801 | *kind = kmp_sched_static; |
| 2802 | break; |
| 2803 | case kmp_sch_dynamic_chunked: |
| 2804 | *kind = kmp_sched_dynamic; |
| 2805 | break; |
| 2806 | case kmp_sch_guided_chunked: |
| 2807 | case kmp_sch_guided_iterative_chunked: |
| 2808 | case kmp_sch_guided_analytical_chunked: |
| 2809 | *kind = kmp_sched_guided; |
| 2810 | break; |
| 2811 | case kmp_sch_auto: |
| 2812 | *kind = kmp_sched_auto; |
| 2813 | break; |
| 2814 | case kmp_sch_trapezoidal: |
| 2815 | *kind = kmp_sched_trapezoidal; |
| 2816 | break; |
Jonathan Peyton | a1234cf | 2016-10-07 18:01:35 +0000 | [diff] [blame] | 2817 | #if KMP_STATIC_STEAL_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2818 | case kmp_sch_static_steal: |
| 2819 | *kind = kmp_sched_static_steal; |
| 2820 | break; |
Jonathan Peyton | a1234cf | 2016-10-07 18:01:35 +0000 | [diff] [blame] | 2821 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2822 | default: |
| 2823 | KMP_FATAL(UnknownSchedulingType, th_type); |
| 2824 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2825 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2826 | *chunk = thread->th.th_current_task->td_icvs.sched.chunk; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2829 | int __kmp_get_ancestor_thread_num(int gtid, int level) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2830 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2831 | int ii, dd; |
| 2832 | kmp_team_t *team; |
| 2833 | kmp_info_t *thr; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2834 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2835 | KF_TRACE(10, ("__kmp_get_ancestor_thread_num: thread %d %d\n", gtid, level)); |
| 2836 | KMP_DEBUG_ASSERT(__kmp_init_serial); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2837 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2838 | // validate level |
| 2839 | if (level == 0) |
| 2840 | return 0; |
| 2841 | if (level < 0) |
| 2842 | return -1; |
| 2843 | thr = __kmp_threads[gtid]; |
| 2844 | team = thr->th.th_team; |
| 2845 | ii = team->t.t_level; |
| 2846 | if (level > ii) |
| 2847 | return -1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2848 | |
| 2849 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2850 | if (thr->th.th_teams_microtask) { |
| 2851 | // AC: we are in teams region where multiple nested teams have same level |
| 2852 | int tlevel = thr->th.th_teams_level; // the level of the teams construct |
| 2853 | if (level <= |
| 2854 | tlevel) { // otherwise usual algorithm works (will not touch the teams) |
| 2855 | KMP_DEBUG_ASSERT(ii >= tlevel); |
| 2856 | // AC: As we need to pass by the teams league, we need to artificially |
| 2857 | // increase ii |
| 2858 | if (ii == tlevel) { |
| 2859 | ii += 2; // three teams have same level |
| 2860 | } else { |
| 2861 | ii++; // two teams have same level |
| 2862 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2863 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2864 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2865 | #endif |
| 2866 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2867 | if (ii == level) |
| 2868 | return __kmp_tid_from_gtid(gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2869 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2870 | dd = team->t.t_serialized; |
| 2871 | level++; |
| 2872 | while (ii > level) { |
| 2873 | for (dd = team->t.t_serialized; (dd > 0) && (ii > level); dd--, ii--) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2874 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2875 | if ((team->t.t_serialized) && (!dd)) { |
| 2876 | team = team->t.t_parent; |
| 2877 | continue; |
| 2878 | } |
| 2879 | if (ii > level) { |
| 2880 | team = team->t.t_parent; |
| 2881 | dd = team->t.t_serialized; |
| 2882 | ii--; |
| 2883 | } |
| 2884 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2885 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2886 | return (dd > 1) ? (0) : (team->t.t_master_tid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2889 | int __kmp_get_team_size(int gtid, int level) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2890 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2891 | int ii, dd; |
| 2892 | kmp_team_t *team; |
| 2893 | kmp_info_t *thr; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2894 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2895 | KF_TRACE(10, ("__kmp_get_team_size: thread %d %d\n", gtid, level)); |
| 2896 | KMP_DEBUG_ASSERT(__kmp_init_serial); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2897 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2898 | // validate level |
| 2899 | if (level == 0) |
| 2900 | return 1; |
| 2901 | if (level < 0) |
| 2902 | return -1; |
| 2903 | thr = __kmp_threads[gtid]; |
| 2904 | team = thr->th.th_team; |
| 2905 | ii = team->t.t_level; |
| 2906 | if (level > ii) |
| 2907 | return -1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2908 | |
| 2909 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2910 | if (thr->th.th_teams_microtask) { |
| 2911 | // AC: we are in teams region where multiple nested teams have same level |
| 2912 | int tlevel = thr->th.th_teams_level; // the level of the teams construct |
| 2913 | if (level <= |
| 2914 | tlevel) { // otherwise usual algorithm works (will not touch the teams) |
| 2915 | KMP_DEBUG_ASSERT(ii >= tlevel); |
| 2916 | // AC: As we need to pass by the teams league, we need to artificially |
| 2917 | // increase ii |
| 2918 | if (ii == tlevel) { |
| 2919 | ii += 2; // three teams have same level |
| 2920 | } else { |
| 2921 | ii++; // two teams have same level |
| 2922 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2923 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2924 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2925 | #endif |
| 2926 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2927 | while (ii > level) { |
| 2928 | for (dd = team->t.t_serialized; (dd > 0) && (ii > level); dd--, ii--) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2929 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2930 | if (team->t.t_serialized && (!dd)) { |
| 2931 | team = team->t.t_parent; |
| 2932 | continue; |
| 2933 | } |
| 2934 | if (ii > level) { |
| 2935 | team = team->t.t_parent; |
| 2936 | ii--; |
| 2937 | } |
| 2938 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2939 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2940 | return team->t.t_nproc; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2943 | kmp_r_sched_t __kmp_get_schedule_global() { |
| 2944 | // This routine created because pairs (__kmp_sched, __kmp_chunk) and |
| 2945 | // (__kmp_static, __kmp_guided) may be changed by kmp_set_defaults |
| 2946 | // independently. So one can get the updated schedule here. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2947 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2948 | kmp_r_sched_t r_sched; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2949 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2950 | // create schedule from 4 globals: __kmp_sched, __kmp_chunk, __kmp_static, |
| 2951 | // __kmp_guided. __kmp_sched should keep original value, so that user can set |
| 2952 | // KMP_SCHEDULE multiple times, and thus have different run-time schedules in |
| 2953 | // different roots (even in OMP 2.5) |
| 2954 | if (__kmp_sched == kmp_sch_static) { |
| 2955 | r_sched.r_sched_type = __kmp_static; // replace STATIC with more detailed |
| 2956 | // schedule (balanced or greedy) |
| 2957 | } else if (__kmp_sched == kmp_sch_guided_chunked) { |
| 2958 | r_sched.r_sched_type = __kmp_guided; // replace GUIDED with more detailed |
| 2959 | // schedule (iterative or analytical) |
| 2960 | } else { |
| 2961 | r_sched.r_sched_type = |
| 2962 | __kmp_sched; // (STATIC_CHUNKED), or (DYNAMIC_CHUNKED), or other |
| 2963 | } |
| 2964 | |
| 2965 | if (__kmp_chunk < KMP_DEFAULT_CHUNK) { // __kmp_chunk may be wrong here (if it |
| 2966 | // was not ever set) |
| 2967 | r_sched.chunk = KMP_DEFAULT_CHUNK; |
| 2968 | } else { |
| 2969 | r_sched.chunk = __kmp_chunk; |
| 2970 | } |
| 2971 | |
| 2972 | return r_sched; |
| 2973 | } |
| 2974 | |
| 2975 | /* Allocate (realloc == FALSE) * or reallocate (realloc == TRUE) |
| 2976 | at least argc number of *t_argv entries for the requested team. */ |
| 2977 | static void __kmp_alloc_argv_entries(int argc, kmp_team_t *team, int realloc) { |
| 2978 | |
| 2979 | KMP_DEBUG_ASSERT(team); |
| 2980 | if (!realloc || argc > team->t.t_max_argc) { |
| 2981 | |
| 2982 | KA_TRACE(100, ("__kmp_alloc_argv_entries: team %d: needed entries=%d, " |
| 2983 | "current entries=%d\n", |
| 2984 | team->t.t_id, argc, (realloc) ? team->t.t_max_argc : 0)); |
| 2985 | /* if previously allocated heap space for args, free them */ |
| 2986 | if (realloc && team->t.t_argv != &team->t.t_inline_argv[0]) |
| 2987 | __kmp_free((void *)team->t.t_argv); |
| 2988 | |
| 2989 | if (argc <= KMP_INLINE_ARGV_ENTRIES) { |
| 2990 | /* use unused space in the cache line for arguments */ |
| 2991 | team->t.t_max_argc = KMP_INLINE_ARGV_ENTRIES; |
| 2992 | KA_TRACE(100, ("__kmp_alloc_argv_entries: team %d: inline allocate %d " |
| 2993 | "argv entries\n", |
| 2994 | team->t.t_id, team->t.t_max_argc)); |
| 2995 | team->t.t_argv = &team->t.t_inline_argv[0]; |
| 2996 | if (__kmp_storage_map) { |
| 2997 | __kmp_print_storage_map_gtid( |
| 2998 | -1, &team->t.t_inline_argv[0], |
| 2999 | &team->t.t_inline_argv[KMP_INLINE_ARGV_ENTRIES], |
| 3000 | (sizeof(void *) * KMP_INLINE_ARGV_ENTRIES), "team_%d.t_inline_argv", |
| 3001 | team->t.t_id); |
| 3002 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3003 | } else { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3004 | /* allocate space for arguments in the heap */ |
| 3005 | team->t.t_max_argc = (argc <= (KMP_MIN_MALLOC_ARGV_ENTRIES >> 1)) |
| 3006 | ? KMP_MIN_MALLOC_ARGV_ENTRIES |
| 3007 | : 2 * argc; |
| 3008 | KA_TRACE(100, ("__kmp_alloc_argv_entries: team %d: dynamic allocate %d " |
| 3009 | "argv entries\n", |
| 3010 | team->t.t_id, team->t.t_max_argc)); |
| 3011 | team->t.t_argv = |
| 3012 | (void **)__kmp_page_allocate(sizeof(void *) * team->t.t_max_argc); |
| 3013 | if (__kmp_storage_map) { |
| 3014 | __kmp_print_storage_map_gtid(-1, &team->t.t_argv[0], |
| 3015 | &team->t.t_argv[team->t.t_max_argc], |
| 3016 | sizeof(void *) * team->t.t_max_argc, |
| 3017 | "team_%d.t_argv", team->t.t_id); |
| 3018 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3019 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3020 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3023 | static void __kmp_allocate_team_arrays(kmp_team_t *team, int max_nth) { |
| 3024 | int i; |
| 3025 | int num_disp_buff = max_nth > 1 ? __kmp_dispatch_num_buffers : 2; |
| 3026 | team->t.t_threads = |
| 3027 | (kmp_info_t **)__kmp_allocate(sizeof(kmp_info_t *) * max_nth); |
| 3028 | team->t.t_disp_buffer = (dispatch_shared_info_t *)__kmp_allocate( |
| 3029 | sizeof(dispatch_shared_info_t) * num_disp_buff); |
| 3030 | team->t.t_dispatch = |
| 3031 | (kmp_disp_t *)__kmp_allocate(sizeof(kmp_disp_t) * max_nth); |
| 3032 | team->t.t_implicit_task_taskdata = |
| 3033 | (kmp_taskdata_t *)__kmp_allocate(sizeof(kmp_taskdata_t) * max_nth); |
| 3034 | team->t.t_max_nproc = max_nth; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3035 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3036 | /* setup dispatch buffers */ |
| 3037 | for (i = 0; i < num_disp_buff; ++i) { |
| 3038 | team->t.t_disp_buffer[i].buffer_index = i; |
Jonathan Peyton | df6818b | 2016-06-14 17:57:47 +0000 | [diff] [blame] | 3039 | #if OMP_45_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3040 | team->t.t_disp_buffer[i].doacross_buf_idx = i; |
Jonathan Peyton | 71909c5 | 2016-03-02 22:42:06 +0000 | [diff] [blame] | 3041 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3042 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3045 | static void __kmp_free_team_arrays(kmp_team_t *team) { |
| 3046 | /* Note: this does not free the threads in t_threads (__kmp_free_threads) */ |
| 3047 | int i; |
| 3048 | for (i = 0; i < team->t.t_max_nproc; ++i) { |
| 3049 | if (team->t.t_dispatch[i].th_disp_buffer != NULL) { |
| 3050 | __kmp_free(team->t.t_dispatch[i].th_disp_buffer); |
| 3051 | team->t.t_dispatch[i].th_disp_buffer = NULL; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3052 | } |
| 3053 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3054 | __kmp_free(team->t.t_threads); |
| 3055 | __kmp_free(team->t.t_disp_buffer); |
| 3056 | __kmp_free(team->t.t_dispatch); |
| 3057 | __kmp_free(team->t.t_implicit_task_taskdata); |
| 3058 | team->t.t_threads = NULL; |
| 3059 | team->t.t_disp_buffer = NULL; |
| 3060 | team->t.t_dispatch = NULL; |
| 3061 | team->t.t_implicit_task_taskdata = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3064 | static void __kmp_reallocate_team_arrays(kmp_team_t *team, int max_nth) { |
| 3065 | kmp_info_t **oldThreads = team->t.t_threads; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3066 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3067 | __kmp_free(team->t.t_disp_buffer); |
| 3068 | __kmp_free(team->t.t_dispatch); |
| 3069 | __kmp_free(team->t.t_implicit_task_taskdata); |
| 3070 | __kmp_allocate_team_arrays(team, max_nth); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3071 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3072 | KMP_MEMCPY(team->t.t_threads, oldThreads, |
| 3073 | team->t.t_nproc * sizeof(kmp_info_t *)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3074 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3075 | __kmp_free(oldThreads); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3076 | } |
| 3077 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3078 | static kmp_internal_control_t __kmp_get_global_icvs(void) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3079 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3080 | kmp_r_sched_t r_sched = |
| 3081 | __kmp_get_schedule_global(); // get current state of scheduling globals |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3082 | |
| 3083 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3084 | KMP_DEBUG_ASSERT(__kmp_nested_proc_bind.used > 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3085 | #endif /* OMP_40_ENABLED */ |
| 3086 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3087 | kmp_internal_control_t g_icvs = { |
| 3088 | 0, // int serial_nesting_level; //corresponds to value of th_team_serialized |
| 3089 | (kmp_int8)__kmp_dflt_nested, // int nested; //internal control |
| 3090 | // for nested parallelism (per thread) |
| 3091 | (kmp_int8)__kmp_global.g.g_dynamic, // internal control for dynamic |
| 3092 | // adjustment of threads (per thread) |
| 3093 | (kmp_int8)__kmp_env_blocktime, // int bt_set; //internal control for |
| 3094 | // whether blocktime is explicitly set |
| 3095 | __kmp_dflt_blocktime, // int blocktime; //internal control for blocktime |
Jonathan Peyton | e1c7c13 | 2016-10-07 18:12:19 +0000 | [diff] [blame] | 3096 | #if KMP_USE_MONITOR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3097 | __kmp_bt_intervals, // int bt_intervals; //internal control for blocktime |
| 3098 | // intervals |
Jonathan Peyton | e1c7c13 | 2016-10-07 18:12:19 +0000 | [diff] [blame] | 3099 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3100 | __kmp_dflt_team_nth, // int nproc; //internal control for # of threads for |
| 3101 | // next parallel region (per thread) |
| 3102 | // (use a max ub on value if __kmp_parallel_initialize not called yet) |
| 3103 | __kmp_dflt_max_active_levels, // int max_active_levels; //internal control |
| 3104 | // for max_active_levels |
| 3105 | r_sched, // kmp_r_sched_t sched; //internal control for runtime schedule |
| 3106 | // {sched,chunk} pair |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3107 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3108 | __kmp_nested_proc_bind.bind_types[0], |
| 3109 | __kmp_default_device, |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3110 | #endif /* OMP_40_ENABLED */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3111 | NULL // struct kmp_internal_control *next; |
| 3112 | }; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3113 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3114 | return g_icvs; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3117 | static kmp_internal_control_t __kmp_get_x_global_icvs(const kmp_team_t *team) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3118 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3119 | kmp_internal_control_t gx_icvs; |
| 3120 | gx_icvs.serial_nesting_level = |
| 3121 | 0; // probably =team->t.t_serial like in save_inter_controls |
| 3122 | copy_icvs(&gx_icvs, &team->t.t_threads[0]->th.th_current_task->td_icvs); |
| 3123 | gx_icvs.next = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3124 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3125 | return gx_icvs; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3128 | static void __kmp_initialize_root(kmp_root_t *root) { |
| 3129 | int f; |
| 3130 | kmp_team_t *root_team; |
| 3131 | kmp_team_t *hot_team; |
| 3132 | int hot_team_max_nth; |
| 3133 | kmp_r_sched_t r_sched = |
| 3134 | __kmp_get_schedule_global(); // get current state of scheduling globals |
| 3135 | kmp_internal_control_t r_icvs = __kmp_get_global_icvs(); |
| 3136 | KMP_DEBUG_ASSERT(root); |
| 3137 | KMP_ASSERT(!root->r.r_begin); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3138 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3139 | /* setup the root state structure */ |
| 3140 | __kmp_init_lock(&root->r.r_begin_lock); |
| 3141 | root->r.r_begin = FALSE; |
| 3142 | root->r.r_active = FALSE; |
| 3143 | root->r.r_in_parallel = 0; |
| 3144 | root->r.r_blocktime = __kmp_dflt_blocktime; |
| 3145 | root->r.r_nested = __kmp_dflt_nested; |
Jonathan Peyton | f439246 | 2017-07-27 20:58:41 +0000 | [diff] [blame] | 3146 | root->r.r_cg_nthreads = 1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3147 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3148 | /* setup the root team for this task */ |
| 3149 | /* allocate the root team structure */ |
| 3150 | KF_TRACE(10, ("__kmp_initialize_root: before root_team\n")); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 3151 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3152 | root_team = |
| 3153 | __kmp_allocate_team(root, |
| 3154 | 1, // new_nproc |
| 3155 | 1, // max_nproc |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 3156 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3157 | 0, // root parallel id |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 3158 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3159 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3160 | __kmp_nested_proc_bind.bind_types[0], |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3161 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3162 | &r_icvs, |
| 3163 | 0 // argc |
| 3164 | USE_NESTED_HOT_ARG(NULL) // master thread is unknown |
| 3165 | ); |
Jonathan Peyton | 8fbb49a | 2015-07-09 18:16:58 +0000 | [diff] [blame] | 3166 | #if USE_DEBUGGER |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3167 | // Non-NULL value should be assigned to make the debugger display the root |
| 3168 | // team. |
| 3169 | TCW_SYNC_PTR(root_team->t.t_pkfn, (microtask_t)(~0)); |
Jonathan Peyton | 8fbb49a | 2015-07-09 18:16:58 +0000 | [diff] [blame] | 3170 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3171 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3172 | KF_TRACE(10, ("__kmp_initialize_root: after root_team = %p\n", root_team)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3173 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3174 | root->r.r_root_team = root_team; |
| 3175 | root_team->t.t_control_stack_top = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3176 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3177 | /* initialize root team */ |
| 3178 | root_team->t.t_threads[0] = NULL; |
| 3179 | root_team->t.t_nproc = 1; |
| 3180 | root_team->t.t_serialized = 1; |
| 3181 | // TODO???: root_team->t.t_max_active_levels = __kmp_dflt_max_active_levels; |
| 3182 | root_team->t.t_sched.r_sched_type = r_sched.r_sched_type; |
| 3183 | root_team->t.t_sched.chunk = r_sched.chunk; |
| 3184 | KA_TRACE( |
| 3185 | 20, |
| 3186 | ("__kmp_initialize_root: init root team %d arrived: join=%u, plain=%u\n", |
| 3187 | root_team->t.t_id, KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3188 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3189 | /* setup the hot team for this task */ |
| 3190 | /* allocate the hot team structure */ |
| 3191 | KF_TRACE(10, ("__kmp_initialize_root: before hot_team\n")); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 3192 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3193 | hot_team = |
| 3194 | __kmp_allocate_team(root, |
| 3195 | 1, // new_nproc |
| 3196 | __kmp_dflt_team_nth_ub * 2, // max_nproc |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 3197 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3198 | 0, // root parallel id |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 3199 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3200 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3201 | __kmp_nested_proc_bind.bind_types[0], |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3202 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3203 | &r_icvs, |
| 3204 | 0 // argc |
| 3205 | USE_NESTED_HOT_ARG(NULL) // master thread is unknown |
| 3206 | ); |
| 3207 | KF_TRACE(10, ("__kmp_initialize_root: after hot_team = %p\n", hot_team)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3208 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3209 | root->r.r_hot_team = hot_team; |
| 3210 | root_team->t.t_control_stack_top = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3211 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3212 | /* first-time initialization */ |
| 3213 | hot_team->t.t_parent = root_team; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3214 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3215 | /* initialize hot team */ |
| 3216 | hot_team_max_nth = hot_team->t.t_max_nproc; |
| 3217 | for (f = 0; f < hot_team_max_nth; ++f) { |
| 3218 | hot_team->t.t_threads[f] = NULL; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3219 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3220 | hot_team->t.t_nproc = 1; |
| 3221 | // TODO???: hot_team->t.t_max_active_levels = __kmp_dflt_max_active_levels; |
| 3222 | hot_team->t.t_sched.r_sched_type = r_sched.r_sched_type; |
| 3223 | hot_team->t.t_sched.chunk = r_sched.chunk; |
| 3224 | hot_team->t.t_size_changed = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | #ifdef KMP_DEBUG |
| 3228 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3229 | typedef struct kmp_team_list_item { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3230 | kmp_team_p const *entry; |
| 3231 | struct kmp_team_list_item *next; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3232 | } kmp_team_list_item_t; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3233 | typedef kmp_team_list_item_t *kmp_team_list_t; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3234 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3235 | static void __kmp_print_structure_team_accum( // Add team to list of teams. |
| 3236 | kmp_team_list_t list, // List of teams. |
| 3237 | kmp_team_p const *team // Team to add. |
| 3238 | ) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3239 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3240 | // List must terminate with item where both entry and next are NULL. |
| 3241 | // Team is added to the list only once. |
| 3242 | // List is sorted in ascending order by team id. |
| 3243 | // Team id is *not* a key. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3244 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3245 | kmp_team_list_t l; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3246 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3247 | KMP_DEBUG_ASSERT(list != NULL); |
| 3248 | if (team == NULL) { |
| 3249 | return; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3250 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3251 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3252 | __kmp_print_structure_team_accum(list, team->t.t_parent); |
| 3253 | __kmp_print_structure_team_accum(list, team->t.t_next_pool); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3254 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3255 | // Search list for the team. |
| 3256 | l = list; |
| 3257 | while (l->next != NULL && l->entry != team) { |
| 3258 | l = l->next; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3259 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3260 | if (l->next != NULL) { |
| 3261 | return; // Team has been added before, exit. |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3262 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3263 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3264 | // Team is not found. Search list again for insertion point. |
| 3265 | l = list; |
| 3266 | while (l->next != NULL && l->entry->t.t_id <= team->t.t_id) { |
| 3267 | l = l->next; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3268 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3269 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3270 | // Insert team. |
| 3271 | { |
| 3272 | kmp_team_list_item_t *item = (kmp_team_list_item_t *)KMP_INTERNAL_MALLOC( |
| 3273 | sizeof(kmp_team_list_item_t)); |
| 3274 | *item = *l; |
| 3275 | l->entry = team; |
| 3276 | l->next = item; |
| 3277 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3280 | static void __kmp_print_structure_team(char const *title, kmp_team_p const *team |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3281 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3282 | ) { |
| 3283 | __kmp_printf("%s", title); |
| 3284 | if (team != NULL) { |
| 3285 | __kmp_printf("%2x %p\n", team->t.t_id, team); |
| 3286 | } else { |
| 3287 | __kmp_printf(" - (nil)\n"); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3288 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3291 | static void __kmp_print_structure_thread(char const *title, |
| 3292 | kmp_info_p const *thread) { |
| 3293 | __kmp_printf("%s", title); |
| 3294 | if (thread != NULL) { |
| 3295 | __kmp_printf("%2d %p\n", thread->th.th_info.ds.ds_gtid, thread); |
| 3296 | } else { |
| 3297 | __kmp_printf(" - (nil)\n"); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3298 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3299 | } |
| 3300 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3301 | void __kmp_print_structure(void) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3302 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3303 | kmp_team_list_t list; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3304 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3305 | // Initialize list of teams. |
| 3306 | list = |
| 3307 | (kmp_team_list_item_t *)KMP_INTERNAL_MALLOC(sizeof(kmp_team_list_item_t)); |
| 3308 | list->entry = NULL; |
| 3309 | list->next = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3310 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3311 | __kmp_printf("\n------------------------------\nGlobal Thread " |
| 3312 | "Table\n------------------------------\n"); |
| 3313 | { |
| 3314 | int gtid; |
| 3315 | for (gtid = 0; gtid < __kmp_threads_capacity; ++gtid) { |
| 3316 | __kmp_printf("%2d", gtid); |
| 3317 | if (__kmp_threads != NULL) { |
| 3318 | __kmp_printf(" %p", __kmp_threads[gtid]); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3319 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3320 | if (__kmp_root != NULL) { |
| 3321 | __kmp_printf(" %p", __kmp_root[gtid]); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3322 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3323 | __kmp_printf("\n"); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3324 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3325 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3326 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3327 | // Print out __kmp_threads array. |
| 3328 | __kmp_printf("\n------------------------------\nThreads\n--------------------" |
| 3329 | "----------\n"); |
| 3330 | if (__kmp_threads != NULL) { |
| 3331 | int gtid; |
| 3332 | for (gtid = 0; gtid < __kmp_threads_capacity; ++gtid) { |
| 3333 | kmp_info_t const *thread = __kmp_threads[gtid]; |
| 3334 | if (thread != NULL) { |
| 3335 | __kmp_printf("GTID %2d %p:\n", gtid, thread); |
| 3336 | __kmp_printf(" Our Root: %p\n", thread->th.th_root); |
| 3337 | __kmp_print_structure_team(" Our Team: ", thread->th.th_team); |
| 3338 | __kmp_print_structure_team(" Serial Team: ", |
| 3339 | thread->th.th_serial_team); |
| 3340 | __kmp_printf(" Threads: %2d\n", thread->th.th_team_nproc); |
| 3341 | __kmp_print_structure_thread(" Master: ", |
| 3342 | thread->th.th_team_master); |
| 3343 | __kmp_printf(" Serialized?: %2d\n", thread->th.th_team_serialized); |
| 3344 | __kmp_printf(" Set NProc: %2d\n", thread->th.th_set_nproc); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3345 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3346 | __kmp_printf(" Set Proc Bind: %2d\n", thread->th.th_set_proc_bind); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3347 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3348 | __kmp_print_structure_thread(" Next in pool: ", |
| 3349 | thread->th.th_next_pool); |
| 3350 | __kmp_printf("\n"); |
| 3351 | __kmp_print_structure_team_accum(list, thread->th.th_team); |
| 3352 | __kmp_print_structure_team_accum(list, thread->th.th_serial_team); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3353 | } |
| 3354 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3355 | } else { |
| 3356 | __kmp_printf("Threads array is not allocated.\n"); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3357 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3358 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3359 | // Print out __kmp_root array. |
| 3360 | __kmp_printf("\n------------------------------\nUbers\n----------------------" |
| 3361 | "--------\n"); |
| 3362 | if (__kmp_root != NULL) { |
| 3363 | int gtid; |
| 3364 | for (gtid = 0; gtid < __kmp_threads_capacity; ++gtid) { |
| 3365 | kmp_root_t const *root = __kmp_root[gtid]; |
| 3366 | if (root != NULL) { |
| 3367 | __kmp_printf("GTID %2d %p:\n", gtid, root); |
| 3368 | __kmp_print_structure_team(" Root Team: ", root->r.r_root_team); |
| 3369 | __kmp_print_structure_team(" Hot Team: ", root->r.r_hot_team); |
| 3370 | __kmp_print_structure_thread(" Uber Thread: ", |
| 3371 | root->r.r_uber_thread); |
| 3372 | __kmp_printf(" Active?: %2d\n", root->r.r_active); |
| 3373 | __kmp_printf(" Nested?: %2d\n", root->r.r_nested); |
| 3374 | __kmp_printf(" In Parallel: %2d\n", root->r.r_in_parallel); |
| 3375 | __kmp_printf("\n"); |
| 3376 | __kmp_print_structure_team_accum(list, root->r.r_root_team); |
| 3377 | __kmp_print_structure_team_accum(list, root->r.r_hot_team); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3378 | } |
| 3379 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3380 | } else { |
| 3381 | __kmp_printf("Ubers array is not allocated.\n"); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3382 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3383 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3384 | __kmp_printf("\n------------------------------\nTeams\n----------------------" |
| 3385 | "--------\n"); |
| 3386 | while (list->next != NULL) { |
| 3387 | kmp_team_p const *team = list->entry; |
| 3388 | int i; |
| 3389 | __kmp_printf("Team %2x %p:\n", team->t.t_id, team); |
| 3390 | __kmp_print_structure_team(" Parent Team: ", team->t.t_parent); |
| 3391 | __kmp_printf(" Master TID: %2d\n", team->t.t_master_tid); |
| 3392 | __kmp_printf(" Max threads: %2d\n", team->t.t_max_nproc); |
| 3393 | __kmp_printf(" Levels of serial: %2d\n", team->t.t_serialized); |
| 3394 | __kmp_printf(" Number threads: %2d\n", team->t.t_nproc); |
| 3395 | for (i = 0; i < team->t.t_nproc; ++i) { |
| 3396 | __kmp_printf(" Thread %2d: ", i); |
| 3397 | __kmp_print_structure_thread("", team->t.t_threads[i]); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3398 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3399 | __kmp_print_structure_team(" Next in pool: ", team->t.t_next_pool); |
| 3400 | __kmp_printf("\n"); |
| 3401 | list = list->next; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3402 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3403 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3404 | // Print out __kmp_thread_pool and __kmp_team_pool. |
| 3405 | __kmp_printf("\n------------------------------\nPools\n----------------------" |
| 3406 | "--------\n"); |
| 3407 | __kmp_print_structure_thread("Thread pool: ", |
Andrey Churbanov | c47afcd | 2017-07-03 11:24:08 +0000 | [diff] [blame] | 3408 | CCAST(kmp_info_t *, __kmp_thread_pool)); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3409 | __kmp_print_structure_team("Team pool: ", |
Andrey Churbanov | c47afcd | 2017-07-03 11:24:08 +0000 | [diff] [blame] | 3410 | CCAST(kmp_team_t *, __kmp_team_pool)); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3411 | __kmp_printf("\n"); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3412 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3413 | // Free team list. |
| 3414 | while (list != NULL) { |
| 3415 | kmp_team_list_item_t *item = list; |
| 3416 | list = list->next; |
| 3417 | KMP_INTERNAL_FREE(item); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3418 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | #endif |
| 3422 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3423 | //--------------------------------------------------------------------------- |
| 3424 | // Stuff for per-thread fast random number generator |
| 3425 | // Table of primes |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3426 | static const unsigned __kmp_primes[] = { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3427 | 0x9e3779b1, 0xffe6cc59, 0x2109f6dd, 0x43977ab5, 0xba5703f5, 0xb495a877, |
| 3428 | 0xe1626741, 0x79695e6b, 0xbc98c09f, 0xd5bee2b3, 0x287488f9, 0x3af18231, |
| 3429 | 0x9677cd4d, 0xbe3a6929, 0xadc6a877, 0xdcf0674b, 0xbe4d6fe9, 0x5f15e201, |
| 3430 | 0x99afc3fd, 0xf3f16801, 0xe222cfff, 0x24ba5fdb, 0x0620452d, 0x79f149e3, |
| 3431 | 0xc8b93f49, 0x972702cd, 0xb07dd827, 0x6c97d5ed, 0x085a3d61, 0x46eb5ea7, |
| 3432 | 0x3d9910ed, 0x2e687b5b, 0x29609227, 0x6eb081f1, 0x0954c4e1, 0x9d114db9, |
| 3433 | 0x542acfa9, 0xb3e6bd7b, 0x0742d917, 0xe9f3ffa7, 0x54581edb, 0xf2480f45, |
| 3434 | 0x0bb9288f, 0xef1affc7, 0x85fa0ca7, 0x3ccc14db, 0xe6baf34b, 0x343377f7, |
| 3435 | 0x5ca19031, 0xe6d9293b, 0xf0a9f391, 0x5d2e980b, 0xfc411073, 0xc3749363, |
| 3436 | 0xb892d829, 0x3549366b, 0x629750ad, 0xb98294e5, 0x892d9483, 0xc235baf3, |
| 3437 | 0x3d2402a3, 0x6bdef3c9, 0xbec333cd, 0x40c9520f}; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3438 | |
| 3439 | //--------------------------------------------------------------------------- |
| 3440 | // __kmp_get_random: Get a random number using a linear congruential method. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3441 | unsigned short __kmp_get_random(kmp_info_t *thread) { |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 3442 | unsigned x = thread->th.th_x; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3443 | unsigned short r = x >> 16; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3444 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3445 | thread->th.th_x = x * thread->th.th_a + 1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3446 | |
| 3447 | KA_TRACE(30, ("__kmp_get_random: THREAD: %d, RETURN: %u\n", |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3448 | thread->th.th_info.ds.ds_tid, r)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3449 | |
| 3450 | return r; |
| 3451 | } |
| 3452 | //-------------------------------------------------------- |
| 3453 | // __kmp_init_random: Initialize a random number generator |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3454 | void __kmp_init_random(kmp_info_t *thread) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3455 | unsigned seed = thread->th.th_info.ds.ds_tid; |
| 3456 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3457 | thread->th.th_a = |
| 3458 | __kmp_primes[seed % (sizeof(__kmp_primes) / sizeof(__kmp_primes[0]))]; |
| 3459 | thread->th.th_x = (seed + 1) * thread->th.th_a + 1; |
| 3460 | KA_TRACE(30, |
| 3461 | ("__kmp_init_random: THREAD: %u; A: %u\n", seed, thread->th.th_a)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3462 | } |
| 3463 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3464 | #if KMP_OS_WINDOWS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3465 | /* reclaim array entries for root threads that are already dead, returns number |
| 3466 | * reclaimed */ |
| 3467 | static int __kmp_reclaim_dead_roots(void) { |
| 3468 | int i, r = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3469 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3470 | for (i = 0; i < __kmp_threads_capacity; ++i) { |
| 3471 | if (KMP_UBER_GTID(i) && |
| 3472 | !__kmp_still_running((kmp_info_t *)TCR_SYNC_PTR(__kmp_threads[i])) && |
| 3473 | !__kmp_root[i] |
| 3474 | ->r.r_active) { // AC: reclaim only roots died in non-active state |
| 3475 | r += __kmp_unregister_root_other_thread(i); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3476 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3477 | } |
| 3478 | return r; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3479 | } |
| 3480 | #endif |
| 3481 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3482 | /* This function attempts to create free entries in __kmp_threads and |
| 3483 | __kmp_root, and returns the number of free entries generated. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3484 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3485 | For Windows* OS static library, the first mechanism used is to reclaim array |
| 3486 | entries for root threads that are already dead. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3487 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3488 | On all platforms, expansion is attempted on the arrays __kmp_threads_ and |
| 3489 | __kmp_root, with appropriate update to __kmp_threads_capacity. Array |
| 3490 | capacity is increased by doubling with clipping to __kmp_tp_capacity, if |
| 3491 | threadprivate cache array has been created. Synchronization with |
| 3492 | __kmpc_threadprivate_cached is done using __kmp_tp_cached_lock. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3493 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3494 | After any dead root reclamation, if the clipping value allows array expansion |
| 3495 | to result in the generation of a total of nWish free slots, the function does |
| 3496 | that expansion. If not, but the clipping value allows array expansion to |
| 3497 | result in the generation of a total of nNeed free slots, the function does |
| 3498 | that expansion. Otherwise, nothing is done beyond the possible initial root |
| 3499 | thread reclamation. However, if nNeed is zero, a best-effort attempt is made |
| 3500 | to fulfil nWish as far as possible, i.e. the function will attempt to create |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3501 | as many free slots as possible up to nWish. |
| 3502 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3503 | If any argument is negative, the behavior is undefined. */ |
| 3504 | static int __kmp_expand_threads(int nWish, int nNeed) { |
| 3505 | int added = 0; |
| 3506 | int old_tp_cached; |
| 3507 | int __kmp_actual_max_nth; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3508 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3509 | if (nNeed > nWish) /* normalize the arguments */ |
| 3510 | nWish = nNeed; |
Jonathan Peyton | 9901699 | 2015-05-26 17:32:53 +0000 | [diff] [blame] | 3511 | #if KMP_OS_WINDOWS && !defined KMP_DYNAMIC_LIB |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3512 | /* only for Windows static library */ |
| 3513 | /* reclaim array entries for root threads that are already dead */ |
| 3514 | added = __kmp_reclaim_dead_roots(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3515 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3516 | if (nNeed) { |
| 3517 | nNeed -= added; |
| 3518 | if (nNeed < 0) |
| 3519 | nNeed = 0; |
| 3520 | } |
| 3521 | if (nWish) { |
| 3522 | nWish -= added; |
| 3523 | if (nWish < 0) |
| 3524 | nWish = 0; |
| 3525 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3526 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3527 | if (nWish <= 0) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3528 | return added; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3529 | |
| 3530 | while (1) { |
| 3531 | int nTarget; |
| 3532 | int minimumRequiredCapacity; |
| 3533 | int newCapacity; |
| 3534 | kmp_info_t **newThreads; |
| 3535 | kmp_root_t **newRoot; |
| 3536 | |
| 3537 | // Note that __kmp_threads_capacity is not bounded by __kmp_max_nth. If |
| 3538 | // __kmp_max_nth is set to some value less than __kmp_sys_max_nth by the |
Jonathan Peyton | f439246 | 2017-07-27 20:58:41 +0000 | [diff] [blame] | 3539 | // user via KMP_DEVICE_THREAD_LIMIT, then __kmp_threads_capacity may become |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3540 | // > __kmp_max_nth in one of two ways: |
| 3541 | // |
| 3542 | // 1) The initialization thread (gtid = 0) exits. __kmp_threads[0] |
| 3543 | // may not be resused by another thread, so we may need to increase |
Jonathan Peyton | 09244f3 | 2017-07-26 20:07:58 +0000 | [diff] [blame] | 3544 | // __kmp_threads_capacity to __kmp_max_nth + 1. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3545 | // |
| 3546 | // 2) New foreign root(s) are encountered. We always register new foreign |
| 3547 | // roots. This may cause a smaller # of threads to be allocated at |
| 3548 | // subsequent parallel regions, but the worker threads hang around (and |
| 3549 | // eventually go to sleep) and need slots in the __kmp_threads[] array. |
| 3550 | // |
| 3551 | // Anyway, that is the reason for moving the check to see if |
Jonathan Peyton | 09244f3 | 2017-07-26 20:07:58 +0000 | [diff] [blame] | 3552 | // __kmp_max_nth was exceeded into __kmp_reserve_threads() |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3553 | // instead of having it performed here. -BB |
| 3554 | old_tp_cached = __kmp_tp_cached; |
| 3555 | __kmp_actual_max_nth = |
| 3556 | old_tp_cached ? __kmp_tp_capacity : __kmp_sys_max_nth; |
| 3557 | KMP_DEBUG_ASSERT(__kmp_actual_max_nth >= __kmp_threads_capacity); |
| 3558 | |
| 3559 | /* compute expansion headroom to check if we can expand and whether to aim |
| 3560 | for nWish or nNeed */ |
| 3561 | nTarget = nWish; |
| 3562 | if (__kmp_actual_max_nth - __kmp_threads_capacity < nTarget) { |
| 3563 | /* can't fulfil nWish, so try nNeed */ |
| 3564 | if (nNeed) { |
| 3565 | nTarget = nNeed; |
| 3566 | if (__kmp_actual_max_nth - __kmp_threads_capacity < nTarget) { |
| 3567 | /* possible expansion too small -- give up */ |
| 3568 | break; |
| 3569 | } |
| 3570 | } else { |
| 3571 | /* best-effort */ |
| 3572 | nTarget = __kmp_actual_max_nth - __kmp_threads_capacity; |
| 3573 | if (!nTarget) { |
| 3574 | /* can expand at all -- give up */ |
| 3575 | break; |
| 3576 | } |
| 3577 | } |
| 3578 | } |
| 3579 | minimumRequiredCapacity = __kmp_threads_capacity + nTarget; |
| 3580 | |
| 3581 | newCapacity = __kmp_threads_capacity; |
| 3582 | do { |
| 3583 | newCapacity = newCapacity <= (__kmp_actual_max_nth >> 1) |
| 3584 | ? (newCapacity << 1) |
| 3585 | : __kmp_actual_max_nth; |
| 3586 | } while (newCapacity < minimumRequiredCapacity); |
| 3587 | newThreads = (kmp_info_t **)__kmp_allocate( |
| 3588 | (sizeof(kmp_info_t *) + sizeof(kmp_root_t *)) * newCapacity + |
| 3589 | CACHE_LINE); |
| 3590 | newRoot = (kmp_root_t **)((char *)newThreads + |
| 3591 | sizeof(kmp_info_t *) * newCapacity); |
| 3592 | KMP_MEMCPY(newThreads, __kmp_threads, |
| 3593 | __kmp_threads_capacity * sizeof(kmp_info_t *)); |
| 3594 | KMP_MEMCPY(newRoot, __kmp_root, |
| 3595 | __kmp_threads_capacity * sizeof(kmp_root_t *)); |
| 3596 | memset(newThreads + __kmp_threads_capacity, 0, |
| 3597 | (newCapacity - __kmp_threads_capacity) * sizeof(kmp_info_t *)); |
| 3598 | memset(newRoot + __kmp_threads_capacity, 0, |
| 3599 | (newCapacity - __kmp_threads_capacity) * sizeof(kmp_root_t *)); |
| 3600 | |
| 3601 | if (!old_tp_cached && __kmp_tp_cached && newCapacity > __kmp_tp_capacity) { |
| 3602 | /* __kmp_tp_cached has changed, i.e. __kmpc_threadprivate_cached has |
| 3603 | allocated a threadprivate cache while we were allocating the expanded |
| 3604 | array, and our new capacity is larger than the threadprivate cache |
| 3605 | capacity, so we should deallocate the expanded arrays and try again. |
| 3606 | This is the first check of a double-check pair. */ |
| 3607 | __kmp_free(newThreads); |
| 3608 | continue; /* start over and try again */ |
| 3609 | } |
| 3610 | __kmp_acquire_bootstrap_lock(&__kmp_tp_cached_lock); |
| 3611 | if (!old_tp_cached && __kmp_tp_cached && newCapacity > __kmp_tp_capacity) { |
| 3612 | /* Same check as above, but this time with the lock so we can be sure if |
| 3613 | we can succeed. */ |
| 3614 | __kmp_release_bootstrap_lock(&__kmp_tp_cached_lock); |
| 3615 | __kmp_free(newThreads); |
| 3616 | continue; /* start over and try again */ |
| 3617 | } else { |
| 3618 | /* success */ |
| 3619 | // __kmp_free( __kmp_threads ); // ATT: It leads to crash. Need to be |
| 3620 | // investigated. |
| 3621 | *(kmp_info_t * *volatile *)&__kmp_threads = newThreads; |
| 3622 | *(kmp_root_t * *volatile *)&__kmp_root = newRoot; |
| 3623 | added += newCapacity - __kmp_threads_capacity; |
| 3624 | *(volatile int *)&__kmp_threads_capacity = newCapacity; |
| 3625 | __kmp_release_bootstrap_lock(&__kmp_tp_cached_lock); |
| 3626 | break; /* succeeded, so we can exit the loop */ |
| 3627 | } |
| 3628 | } |
| 3629 | return added; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3632 | /* Register the current thread as a root thread and obtain our gtid. We must |
| 3633 | have the __kmp_initz_lock held at this point. Argument TRUE only if are the |
| 3634 | thread that calls from __kmp_do_serial_initialize() */ |
| 3635 | int __kmp_register_root(int initial_thread) { |
| 3636 | kmp_info_t *root_thread; |
| 3637 | kmp_root_t *root; |
| 3638 | int gtid; |
| 3639 | int capacity; |
| 3640 | __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock); |
| 3641 | KA_TRACE(20, ("__kmp_register_root: entered\n")); |
| 3642 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3643 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3644 | /* 2007-03-02: |
| 3645 | If initial thread did not invoke OpenMP RTL yet, and this thread is not an |
| 3646 | initial one, "__kmp_all_nth >= __kmp_threads_capacity" condition does not |
| 3647 | work as expected -- it may return false (that means there is at least one |
| 3648 | empty slot in __kmp_threads array), but it is possible the only free slot |
| 3649 | is #0, which is reserved for initial thread and so cannot be used for this |
| 3650 | one. Following code workarounds this bug. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3651 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3652 | However, right solution seems to be not reserving slot #0 for initial |
| 3653 | thread because: |
| 3654 | (1) there is no magic in slot #0, |
| 3655 | (2) we cannot detect initial thread reliably (the first thread which does |
| 3656 | serial initialization may be not a real initial thread). |
| 3657 | */ |
| 3658 | capacity = __kmp_threads_capacity; |
| 3659 | if (!initial_thread && TCR_PTR(__kmp_threads[0]) == NULL) { |
| 3660 | --capacity; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3661 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3662 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3663 | /* see if there are too many threads */ |
| 3664 | if (__kmp_all_nth >= capacity && !__kmp_expand_threads(1, 1)) { |
| 3665 | if (__kmp_tp_cached) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 3666 | __kmp_fatal(KMP_MSG(CantRegisterNewThread), |
| 3667 | KMP_HNT(Set_ALL_THREADPRIVATE, __kmp_tp_capacity), |
| 3668 | KMP_HNT(PossibleSystemLimitOnThreads), __kmp_msg_null); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3669 | } else { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 3670 | __kmp_fatal(KMP_MSG(CantRegisterNewThread), KMP_HNT(SystemLimitOnThreads), |
| 3671 | __kmp_msg_null); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3672 | } |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3673 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3674 | |
| 3675 | /* find an available thread slot */ |
| 3676 | /* Don't reassign the zero slot since we need that to only be used by initial |
| 3677 | thread */ |
| 3678 | for (gtid = (initial_thread ? 0 : 1); TCR_PTR(__kmp_threads[gtid]) != NULL; |
| 3679 | gtid++) |
| 3680 | ; |
| 3681 | KA_TRACE(1, |
| 3682 | ("__kmp_register_root: found slot in threads array: T#%d\n", gtid)); |
| 3683 | KMP_ASSERT(gtid < __kmp_threads_capacity); |
| 3684 | |
| 3685 | /* update global accounting */ |
| 3686 | __kmp_all_nth++; |
| 3687 | TCW_4(__kmp_nth, __kmp_nth + 1); |
| 3688 | |
| 3689 | // if __kmp_adjust_gtid_mode is set, then we use method #1 (sp search) for low |
| 3690 | // numbers of procs, and method #2 (keyed API call) for higher numbers. |
| 3691 | if (__kmp_adjust_gtid_mode) { |
| 3692 | if (__kmp_all_nth >= __kmp_tls_gtid_min) { |
| 3693 | if (TCR_4(__kmp_gtid_mode) != 2) { |
| 3694 | TCW_4(__kmp_gtid_mode, 2); |
| 3695 | } |
| 3696 | } else { |
| 3697 | if (TCR_4(__kmp_gtid_mode) != 1) { |
| 3698 | TCW_4(__kmp_gtid_mode, 1); |
| 3699 | } |
| 3700 | } |
| 3701 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3702 | |
| 3703 | #ifdef KMP_ADJUST_BLOCKTIME |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3704 | /* Adjust blocktime to zero if necessary */ |
| 3705 | /* Middle initialization might not have occurred yet */ |
| 3706 | if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) { |
| 3707 | if (__kmp_nth > __kmp_avail_proc) { |
| 3708 | __kmp_zero_bt = TRUE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3709 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3710 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3711 | #endif /* KMP_ADJUST_BLOCKTIME */ |
| 3712 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3713 | /* setup this new hierarchy */ |
| 3714 | if (!(root = __kmp_root[gtid])) { |
| 3715 | root = __kmp_root[gtid] = (kmp_root_t *)__kmp_allocate(sizeof(kmp_root_t)); |
| 3716 | KMP_DEBUG_ASSERT(!root->r.r_root_team); |
| 3717 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3718 | |
Jonathan Peyton | 5375fe8 | 2016-11-14 21:13:44 +0000 | [diff] [blame] | 3719 | #if KMP_STATS_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3720 | // Initialize stats as soon as possible (right after gtid assignment). |
| 3721 | __kmp_stats_thread_ptr = __kmp_stats_list->push_back(gtid); |
| 3722 | KMP_START_EXPLICIT_TIMER(OMP_worker_thread_life); |
| 3723 | KMP_SET_THREAD_STATE(SERIAL_REGION); |
| 3724 | KMP_INIT_PARTITIONED_TIMERS(OMP_serial); |
Jonathan Peyton | 5375fe8 | 2016-11-14 21:13:44 +0000 | [diff] [blame] | 3725 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3726 | __kmp_initialize_root(root); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3727 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3728 | /* setup new root thread structure */ |
| 3729 | if (root->r.r_uber_thread) { |
| 3730 | root_thread = root->r.r_uber_thread; |
| 3731 | } else { |
| 3732 | root_thread = (kmp_info_t *)__kmp_allocate(sizeof(kmp_info_t)); |
| 3733 | if (__kmp_storage_map) { |
| 3734 | __kmp_print_thread_storage_map(root_thread, gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3735 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3736 | root_thread->th.th_info.ds.ds_gtid = gtid; |
| 3737 | root_thread->th.th_root = root; |
| 3738 | if (__kmp_env_consistency_check) { |
| 3739 | root_thread->th.th_cons = __kmp_allocate_cons_stack(gtid); |
| 3740 | } |
| 3741 | #if USE_FAST_MEMORY |
| 3742 | __kmp_initialize_fast_memory(root_thread); |
| 3743 | #endif /* USE_FAST_MEMORY */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3744 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3745 | #if KMP_USE_BGET |
| 3746 | KMP_DEBUG_ASSERT(root_thread->th.th_local.bget_data == NULL); |
| 3747 | __kmp_initialize_bget(root_thread); |
| 3748 | #endif |
| 3749 | __kmp_init_random(root_thread); // Initialize random number generator |
| 3750 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 3751 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3752 | /* setup the serial team held in reserve by the root thread */ |
| 3753 | if (!root_thread->th.th_serial_team) { |
| 3754 | kmp_internal_control_t r_icvs = __kmp_get_global_icvs(); |
| 3755 | KF_TRACE(10, ("__kmp_register_root: before serial_team\n")); |
| 3756 | root_thread->th.th_serial_team = |
| 3757 | __kmp_allocate_team(root, 1, 1, |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 3758 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3759 | 0, // root parallel id |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 3760 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3761 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3762 | proc_bind_default, |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3763 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3764 | &r_icvs, 0 USE_NESTED_HOT_ARG(NULL)); |
| 3765 | } |
| 3766 | KMP_ASSERT(root_thread->th.th_serial_team); |
| 3767 | KF_TRACE(10, ("__kmp_register_root: after serial_team = %p\n", |
| 3768 | root_thread->th.th_serial_team)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3769 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3770 | /* drop root_thread into place */ |
| 3771 | TCW_SYNC_PTR(__kmp_threads[gtid], root_thread); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3772 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3773 | root->r.r_root_team->t.t_threads[0] = root_thread; |
| 3774 | root->r.r_hot_team->t.t_threads[0] = root_thread; |
| 3775 | root_thread->th.th_serial_team->t.t_threads[0] = root_thread; |
| 3776 | // AC: the team created in reserve, not for execution (it is unused for now). |
| 3777 | root_thread->th.th_serial_team->t.t_serialized = 0; |
| 3778 | root->r.r_uber_thread = root_thread; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3779 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3780 | /* initialize the thread, get it ready to go */ |
| 3781 | __kmp_initialize_info(root_thread, root->r.r_root_team, 0, gtid); |
| 3782 | TCW_4(__kmp_init_gtid, TRUE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3783 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3784 | /* prepare the master thread for get_gtid() */ |
| 3785 | __kmp_gtid_set_specific(gtid); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 3786 | |
Jonathan Peyton | 7abf9d5 | 2016-05-26 18:19:10 +0000 | [diff] [blame] | 3787 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3788 | __kmp_itt_thread_name(gtid); |
Jonathan Peyton | 7abf9d5 | 2016-05-26 18:19:10 +0000 | [diff] [blame] | 3789 | #endif /* USE_ITT_BUILD */ |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 3790 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3791 | #ifdef KMP_TDATA_GTID |
| 3792 | __kmp_gtid = gtid; |
Jonathan Peyton | 8fbb49a | 2015-07-09 18:16:58 +0000 | [diff] [blame] | 3793 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3794 | __kmp_create_worker(gtid, root_thread, __kmp_stksize); |
| 3795 | KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() == gtid); |
| 3796 | |
| 3797 | KA_TRACE(20, ("__kmp_register_root: T#%d init T#%d(%d:%d) arrived: join=%u, " |
| 3798 | "plain=%u\n", |
| 3799 | gtid, __kmp_gtid_from_tid(0, root->r.r_hot_team), |
| 3800 | root->r.r_hot_team->t.t_id, 0, KMP_INIT_BARRIER_STATE, |
| 3801 | KMP_INIT_BARRIER_STATE)); |
| 3802 | { // Initialize barrier data. |
| 3803 | int b; |
| 3804 | for (b = 0; b < bs_last_barrier; ++b) { |
| 3805 | root_thread->th.th_bar[b].bb.b_arrived = KMP_INIT_BARRIER_STATE; |
| 3806 | #if USE_DEBUGGER |
| 3807 | root_thread->th.th_bar[b].bb.b_worker_arrived = 0; |
| 3808 | #endif |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 3809 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3810 | } |
| 3811 | KMP_DEBUG_ASSERT(root->r.r_hot_team->t.t_bar[bs_forkjoin_barrier].b_arrived == |
| 3812 | KMP_INIT_BARRIER_STATE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3813 | |
Alp Toker | 763b939 | 2014-02-28 09:42:41 +0000 | [diff] [blame] | 3814 | #if KMP_AFFINITY_SUPPORTED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3815 | #if OMP_40_ENABLED |
| 3816 | root_thread->th.th_current_place = KMP_PLACE_UNDEFINED; |
| 3817 | root_thread->th.th_new_place = KMP_PLACE_UNDEFINED; |
| 3818 | root_thread->th.th_first_place = KMP_PLACE_UNDEFINED; |
| 3819 | root_thread->th.th_last_place = KMP_PLACE_UNDEFINED; |
| 3820 | #endif |
Jonathan Peyton | 2f7c077 | 2016-02-25 18:49:52 +0000 | [diff] [blame] | 3821 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3822 | if (TCR_4(__kmp_init_middle)) { |
| 3823 | __kmp_affinity_set_init_mask(gtid, TRUE); |
| 3824 | } |
Alp Toker | 763b939 | 2014-02-28 09:42:41 +0000 | [diff] [blame] | 3825 | #endif /* KMP_AFFINITY_SUPPORTED */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3826 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3827 | __kmp_root_counter++; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3828 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3829 | KMP_MB(); |
| 3830 | __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3831 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3832 | return gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 3835 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3836 | static int __kmp_free_hot_teams(kmp_root_t *root, kmp_info_t *thr, int level, |
| 3837 | const int max_level) { |
| 3838 | int i, n, nth; |
| 3839 | kmp_hot_team_ptr_t *hot_teams = thr->th.th_hot_teams; |
| 3840 | if (!hot_teams || !hot_teams[level].hot_team) { |
| 3841 | return 0; |
| 3842 | } |
| 3843 | KMP_DEBUG_ASSERT(level < max_level); |
| 3844 | kmp_team_t *team = hot_teams[level].hot_team; |
| 3845 | nth = hot_teams[level].hot_team_nth; |
| 3846 | n = nth - 1; // master is not freed |
| 3847 | if (level < max_level - 1) { |
| 3848 | for (i = 0; i < nth; ++i) { |
| 3849 | kmp_info_t *th = team->t.t_threads[i]; |
| 3850 | n += __kmp_free_hot_teams(root, th, level + 1, max_level); |
| 3851 | if (i > 0 && th->th.th_hot_teams) { |
| 3852 | __kmp_free(th->th.th_hot_teams); |
| 3853 | th->th.th_hot_teams = NULL; |
| 3854 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 3855 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3856 | } |
| 3857 | __kmp_free_team(root, team, NULL); |
| 3858 | return n; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 3859 | } |
| 3860 | #endif |
| 3861 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3862 | // Resets a root thread and clear its root and hot teams. |
| 3863 | // Returns the number of __kmp_threads entries directly and indirectly freed. |
| 3864 | static int __kmp_reset_root(int gtid, kmp_root_t *root) { |
| 3865 | kmp_team_t *root_team = root->r.r_root_team; |
| 3866 | kmp_team_t *hot_team = root->r.r_hot_team; |
| 3867 | int n = hot_team->t.t_nproc; |
| 3868 | int i; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3869 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3870 | KMP_DEBUG_ASSERT(!root->r.r_active); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3871 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3872 | root->r.r_root_team = NULL; |
| 3873 | root->r.r_hot_team = NULL; |
| 3874 | // __kmp_free_team() does not free hot teams, so we have to clear r_hot_team |
| 3875 | // before call to __kmp_free_team(). |
| 3876 | __kmp_free_team(root, root_team USE_NESTED_HOT_ARG(NULL)); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 3877 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3878 | if (__kmp_hot_teams_max_level > |
| 3879 | 0) { // need to free nested hot teams and their threads if any |
| 3880 | for (i = 0; i < hot_team->t.t_nproc; ++i) { |
| 3881 | kmp_info_t *th = hot_team->t.t_threads[i]; |
| 3882 | if (__kmp_hot_teams_max_level > 1) { |
| 3883 | n += __kmp_free_hot_teams(root, th, 1, __kmp_hot_teams_max_level); |
| 3884 | } |
| 3885 | if (th->th.th_hot_teams) { |
| 3886 | __kmp_free(th->th.th_hot_teams); |
| 3887 | th->th.th_hot_teams = NULL; |
| 3888 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 3889 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3890 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 3891 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3892 | __kmp_free_team(root, hot_team USE_NESTED_HOT_ARG(NULL)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3893 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3894 | // Before we can reap the thread, we need to make certain that all other |
| 3895 | // threads in the teams that had this root as ancestor have stopped trying to |
| 3896 | // steal tasks. |
| 3897 | if (__kmp_tasking_mode != tskm_immediate_exec) { |
| 3898 | __kmp_wait_to_unref_task_teams(); |
| 3899 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3900 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3901 | #if KMP_OS_WINDOWS |
| 3902 | /* Close Handle of root duplicated in __kmp_create_worker (tr #62919) */ |
| 3903 | KA_TRACE( |
| 3904 | 10, ("__kmp_reset_root: free handle, th = %p, handle = %" KMP_UINTPTR_SPEC |
| 3905 | "\n", |
| 3906 | (LPVOID) & (root->r.r_uber_thread->th), |
| 3907 | root->r.r_uber_thread->th.th_info.ds.ds_thread)); |
| 3908 | __kmp_free_handle(root->r.r_uber_thread->th.th_info.ds.ds_thread); |
| 3909 | #endif /* KMP_OS_WINDOWS */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3910 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 3911 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3912 | if (ompt_enabled && ompt_callbacks.ompt_callback(ompt_event_thread_end)) { |
| 3913 | int gtid = __kmp_get_gtid(); |
| 3914 | __ompt_thread_end(ompt_thread_initial, gtid); |
| 3915 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 3916 | #endif |
| 3917 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3918 | TCW_4(__kmp_nth, |
| 3919 | __kmp_nth - 1); // __kmp_reap_thread will decrement __kmp_all_nth. |
Jonathan Peyton | f439246 | 2017-07-27 20:58:41 +0000 | [diff] [blame] | 3920 | root->r.r_cg_nthreads--; |
| 3921 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3922 | __kmp_reap_thread(root->r.r_uber_thread, 1); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3923 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3924 | // We canot put root thread to __kmp_thread_pool, so we have to reap it istead |
| 3925 | // of freeing. |
| 3926 | root->r.r_uber_thread = NULL; |
| 3927 | /* mark root as no longer in use */ |
| 3928 | root->r.r_begin = FALSE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3929 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3930 | return n; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3933 | void __kmp_unregister_root_current_thread(int gtid) { |
| 3934 | KA_TRACE(1, ("__kmp_unregister_root_current_thread: enter T#%d\n", gtid)); |
| 3935 | /* this lock should be ok, since unregister_root_current_thread is never |
| 3936 | called during an abort, only during a normal close. furthermore, if you |
| 3937 | have the forkjoin lock, you should never try to get the initz lock */ |
| 3938 | __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock); |
| 3939 | if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) { |
| 3940 | KC_TRACE(10, ("__kmp_unregister_root_current_thread: already finished, " |
| 3941 | "exiting T#%d\n", |
| 3942 | gtid)); |
| 3943 | __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); |
| 3944 | return; |
| 3945 | } |
| 3946 | kmp_root_t *root = __kmp_root[gtid]; |
Jim Cownie | 77c2a63 | 2014-09-03 11:34:33 +0000 | [diff] [blame] | 3947 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3948 | KMP_DEBUG_ASSERT(__kmp_threads && __kmp_threads[gtid]); |
| 3949 | KMP_ASSERT(KMP_UBER_GTID(gtid)); |
| 3950 | KMP_ASSERT(root == __kmp_threads[gtid]->th.th_root); |
| 3951 | KMP_ASSERT(root->r.r_active == FALSE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3952 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3953 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3954 | |
Jonathan Peyton | df6818b | 2016-06-14 17:57:47 +0000 | [diff] [blame] | 3955 | #if OMP_45_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3956 | kmp_info_t *thread = __kmp_threads[gtid]; |
| 3957 | kmp_team_t *team = thread->th.th_team; |
| 3958 | kmp_task_team_t *task_team = thread->th.th_task_team; |
Andrey Churbanov | 535b6fa | 2015-05-07 17:41:51 +0000 | [diff] [blame] | 3959 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3960 | // we need to wait for the proxy tasks before finishing the thread |
| 3961 | if (task_team != NULL && task_team->tt.tt_found_proxy_tasks) { |
Jonathan Peyton | 6d247f7 | 2015-09-10 21:33:50 +0000 | [diff] [blame] | 3962 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3963 | // the runtime is shutting down so we won't report any events |
| 3964 | thread->th.ompt_thread_info.state = ompt_state_undefined; |
Jonathan Peyton | 6d247f7 | 2015-09-10 21:33:50 +0000 | [diff] [blame] | 3965 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3966 | __kmp_task_team_wait(thread, team USE_ITT_BUILD_ARG(NULL)); |
| 3967 | } |
Andrey Churbanov | 535b6fa | 2015-05-07 17:41:51 +0000 | [diff] [blame] | 3968 | #endif |
| 3969 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3970 | __kmp_reset_root(gtid, root); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3971 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3972 | /* free up this thread slot */ |
| 3973 | __kmp_gtid_set_specific(KMP_GTID_DNE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3974 | #ifdef KMP_TDATA_GTID |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3975 | __kmp_gtid = KMP_GTID_DNE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3976 | #endif |
| 3977 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3978 | KMP_MB(); |
| 3979 | KC_TRACE(10, |
| 3980 | ("__kmp_unregister_root_current_thread: T#%d unregistered\n", gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3981 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3982 | __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Jonathan Peyton | 2321d57 | 2015-06-08 19:25:25 +0000 | [diff] [blame] | 3985 | #if KMP_OS_WINDOWS |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3986 | /* __kmp_forkjoin_lock must be already held |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3987 | Unregisters a root thread that is not the current thread. Returns the number |
| 3988 | of __kmp_threads entries freed as a result. */ |
| 3989 | static int __kmp_unregister_root_other_thread(int gtid) { |
| 3990 | kmp_root_t *root = __kmp_root[gtid]; |
| 3991 | int r; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3992 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3993 | KA_TRACE(1, ("__kmp_unregister_root_other_thread: enter T#%d\n", gtid)); |
| 3994 | KMP_DEBUG_ASSERT(__kmp_threads && __kmp_threads[gtid]); |
| 3995 | KMP_ASSERT(KMP_UBER_GTID(gtid)); |
| 3996 | KMP_ASSERT(root == __kmp_threads[gtid]->th.th_root); |
| 3997 | KMP_ASSERT(root->r.r_active == FALSE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3998 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 3999 | r = __kmp_reset_root(gtid, root); |
| 4000 | KC_TRACE(10, |
| 4001 | ("__kmp_unregister_root_other_thread: T#%d unregistered\n", gtid)); |
| 4002 | return r; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4003 | } |
Jonathan Peyton | 2321d57 | 2015-06-08 19:25:25 +0000 | [diff] [blame] | 4004 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4005 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4006 | #if KMP_DEBUG |
| 4007 | void __kmp_task_info() { |
| 4008 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4009 | kmp_int32 gtid = __kmp_entry_gtid(); |
| 4010 | kmp_int32 tid = __kmp_tid_from_gtid(gtid); |
| 4011 | kmp_info_t *this_thr = __kmp_threads[gtid]; |
| 4012 | kmp_team_t *steam = this_thr->th.th_serial_team; |
| 4013 | kmp_team_t *team = this_thr->th.th_team; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4014 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4015 | __kmp_printf("__kmp_task_info: gtid=%d tid=%d t_thread=%p team=%p curtask=%p " |
| 4016 | "ptask=%p\n", |
| 4017 | gtid, tid, this_thr, team, this_thr->th.th_current_task, |
| 4018 | team->t.t_implicit_task_taskdata[tid].td_parent); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4019 | } |
| 4020 | #endif // KMP_DEBUG |
| 4021 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4022 | /* TODO optimize with one big memclr, take out what isn't needed, split |
| 4023 | responsibility to workers as much as possible, and delay initialization of |
| 4024 | features as much as possible */ |
| 4025 | static void __kmp_initialize_info(kmp_info_t *this_thr, kmp_team_t *team, |
| 4026 | int tid, int gtid) { |
| 4027 | /* this_thr->th.th_info.ds.ds_gtid is setup in |
| 4028 | kmp_allocate_thread/create_worker. |
| 4029 | this_thr->th.th_serial_team is setup in __kmp_allocate_thread */ |
| 4030 | kmp_info_t *master = team->t.t_threads[0]; |
| 4031 | KMP_DEBUG_ASSERT(this_thr != NULL); |
| 4032 | KMP_DEBUG_ASSERT(this_thr->th.th_serial_team); |
| 4033 | KMP_DEBUG_ASSERT(team); |
| 4034 | KMP_DEBUG_ASSERT(team->t.t_threads); |
| 4035 | KMP_DEBUG_ASSERT(team->t.t_dispatch); |
| 4036 | KMP_DEBUG_ASSERT(master); |
| 4037 | KMP_DEBUG_ASSERT(master->th.th_root); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4038 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4039 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4040 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4041 | TCW_SYNC_PTR(this_thr->th.th_team, team); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4042 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4043 | this_thr->th.th_info.ds.ds_tid = tid; |
| 4044 | this_thr->th.th_set_nproc = 0; |
| 4045 | if (__kmp_tasking_mode != tskm_immediate_exec) |
| 4046 | // When tasking is possible, threads are not safe to reap until they are |
| 4047 | // done tasking; this will be set when tasking code is exited in wait |
| 4048 | this_thr->th.th_reap_state = KMP_NOT_SAFE_TO_REAP; |
| 4049 | else // no tasking --> always safe to reap |
| 4050 | this_thr->th.th_reap_state = KMP_SAFE_TO_REAP; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4051 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4052 | this_thr->th.th_set_proc_bind = proc_bind_default; |
| 4053 | #if KMP_AFFINITY_SUPPORTED |
| 4054 | this_thr->th.th_new_place = this_thr->th.th_current_place; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4055 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4056 | #endif |
| 4057 | this_thr->th.th_root = master->th.th_root; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4058 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4059 | /* setup the thread's cache of the team structure */ |
| 4060 | this_thr->th.th_team_nproc = team->t.t_nproc; |
| 4061 | this_thr->th.th_team_master = master; |
| 4062 | this_thr->th.th_team_serialized = team->t.t_serialized; |
| 4063 | TCW_PTR(this_thr->th.th_sleep_loc, NULL); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4064 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4065 | KMP_DEBUG_ASSERT(team->t.t_implicit_task_taskdata); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4066 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4067 | KF_TRACE(10, ("__kmp_initialize_info1: T#%d:%d this_thread=%p curtask=%p\n", |
| 4068 | tid, gtid, this_thr, this_thr->th.th_current_task)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4069 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4070 | __kmp_init_implicit_task(this_thr->th.th_team_master->th.th_ident, this_thr, |
| 4071 | team, tid, TRUE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4072 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4073 | KF_TRACE(10, ("__kmp_initialize_info2: T#%d:%d this_thread=%p curtask=%p\n", |
| 4074 | tid, gtid, this_thr, this_thr->th.th_current_task)); |
| 4075 | // TODO: Initialize ICVs from parent; GEH - isn't that already done in |
| 4076 | // __kmp_initialize_team()? |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4077 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4078 | /* TODO no worksharing in speculative threads */ |
| 4079 | this_thr->th.th_dispatch = &team->t.t_dispatch[tid]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4080 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4081 | this_thr->th.th_local.this_construct = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4082 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4083 | if (!this_thr->th.th_pri_common) { |
| 4084 | this_thr->th.th_pri_common = |
| 4085 | (struct common_table *)__kmp_allocate(sizeof(struct common_table)); |
| 4086 | if (__kmp_storage_map) { |
| 4087 | __kmp_print_storage_map_gtid( |
| 4088 | gtid, this_thr->th.th_pri_common, this_thr->th.th_pri_common + 1, |
| 4089 | sizeof(struct common_table), "th_%d.th_pri_common\n", gtid); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 4090 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4091 | this_thr->th.th_pri_head = NULL; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 4092 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4093 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4094 | /* Initialize dynamic dispatch */ |
| 4095 | { |
| 4096 | volatile kmp_disp_t *dispatch = this_thr->th.th_dispatch; |
| 4097 | // Use team max_nproc since this will never change for the team. |
| 4098 | size_t disp_size = |
| 4099 | sizeof(dispatch_private_info_t) * |
| 4100 | (team->t.t_max_nproc == 1 ? 1 : __kmp_dispatch_num_buffers); |
| 4101 | KD_TRACE(10, ("__kmp_initialize_info: T#%d max_nproc: %d\n", gtid, |
| 4102 | team->t.t_max_nproc)); |
| 4103 | KMP_ASSERT(dispatch); |
| 4104 | KMP_DEBUG_ASSERT(team->t.t_dispatch); |
| 4105 | KMP_DEBUG_ASSERT(dispatch == &team->t.t_dispatch[tid]); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4106 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4107 | dispatch->th_disp_index = 0; |
Jonathan Peyton | df6818b | 2016-06-14 17:57:47 +0000 | [diff] [blame] | 4108 | #if OMP_45_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4109 | dispatch->th_doacross_buf_idx = 0; |
Jonathan Peyton | 71909c5 | 2016-03-02 22:42:06 +0000 | [diff] [blame] | 4110 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4111 | if (!dispatch->th_disp_buffer) { |
| 4112 | dispatch->th_disp_buffer = |
| 4113 | (dispatch_private_info_t *)__kmp_allocate(disp_size); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4114 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4115 | if (__kmp_storage_map) { |
| 4116 | __kmp_print_storage_map_gtid( |
| 4117 | gtid, &dispatch->th_disp_buffer[0], |
| 4118 | &dispatch->th_disp_buffer[team->t.t_max_nproc == 1 |
| 4119 | ? 1 |
| 4120 | : __kmp_dispatch_num_buffers], |
| 4121 | disp_size, "th_%d.th_dispatch.th_disp_buffer " |
| 4122 | "(team_%d.t_dispatch[%d].th_disp_buffer)", |
| 4123 | gtid, team->t.t_id, gtid); |
| 4124 | } |
| 4125 | } else { |
| 4126 | memset(&dispatch->th_disp_buffer[0], '\0', disp_size); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4129 | dispatch->th_dispatch_pr_current = 0; |
| 4130 | dispatch->th_dispatch_sh_current = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4131 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4132 | dispatch->th_deo_fcn = 0; /* ORDERED */ |
| 4133 | dispatch->th_dxo_fcn = 0; /* END ORDERED */ |
| 4134 | } |
Andrey Churbanov | 6d224db | 2015-02-10 18:37:43 +0000 | [diff] [blame] | 4135 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4136 | this_thr->th.th_next_pool = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4137 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4138 | if (!this_thr->th.th_task_state_memo_stack) { |
| 4139 | size_t i; |
| 4140 | this_thr->th.th_task_state_memo_stack = |
| 4141 | (kmp_uint8 *)__kmp_allocate(4 * sizeof(kmp_uint8)); |
| 4142 | this_thr->th.th_task_state_top = 0; |
| 4143 | this_thr->th.th_task_state_stack_sz = 4; |
| 4144 | for (i = 0; i < this_thr->th.th_task_state_stack_sz; |
| 4145 | ++i) // zero init the stack |
| 4146 | this_thr->th.th_task_state_memo_stack[i] = 0; |
| 4147 | } |
| 4148 | |
| 4149 | KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here); |
| 4150 | KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0); |
| 4151 | |
| 4152 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4155 | /* allocate a new thread for the requesting team. this is only called from |
| 4156 | within a forkjoin critical section. we will first try to get an available |
| 4157 | thread from the thread pool. if none is available, we will fork a new one |
| 4158 | assuming we are able to create a new one. this should be assured, as the |
| 4159 | caller should check on this first. */ |
| 4160 | kmp_info_t *__kmp_allocate_thread(kmp_root_t *root, kmp_team_t *team, |
| 4161 | int new_tid) { |
| 4162 | kmp_team_t *serial_team; |
| 4163 | kmp_info_t *new_thr; |
| 4164 | int new_gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4165 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4166 | KA_TRACE(20, ("__kmp_allocate_thread: T#%d\n", __kmp_get_gtid())); |
| 4167 | KMP_DEBUG_ASSERT(root && team); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4168 | #if !KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4169 | KMP_DEBUG_ASSERT(KMP_MASTER_GTID(__kmp_get_gtid())); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4170 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4171 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4172 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4173 | /* first, try to get one from the thread pool */ |
| 4174 | if (__kmp_thread_pool) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4175 | |
Andrey Churbanov | c47afcd | 2017-07-03 11:24:08 +0000 | [diff] [blame] | 4176 | new_thr = CCAST(kmp_info_t *, __kmp_thread_pool); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4177 | __kmp_thread_pool = (volatile kmp_info_t *)new_thr->th.th_next_pool; |
| 4178 | if (new_thr == __kmp_thread_pool_insert_pt) { |
| 4179 | __kmp_thread_pool_insert_pt = NULL; |
| 4180 | } |
| 4181 | TCW_4(new_thr->th.th_in_pool, FALSE); |
| 4182 | // Don't touch th_active_in_pool or th_active. |
| 4183 | // The worker thread adjusts those flags as it sleeps/awakens. |
| 4184 | __kmp_thread_pool_nth--; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4185 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4186 | KA_TRACE(20, ("__kmp_allocate_thread: T#%d using thread T#%d\n", |
| 4187 | __kmp_get_gtid(), new_thr->th.th_info.ds.ds_gtid)); |
| 4188 | KMP_ASSERT(!new_thr->th.th_team); |
| 4189 | KMP_DEBUG_ASSERT(__kmp_nth < __kmp_threads_capacity); |
| 4190 | KMP_DEBUG_ASSERT(__kmp_thread_pool_nth >= 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4191 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4192 | /* setup the thread structure */ |
| 4193 | __kmp_initialize_info(new_thr, team, new_tid, |
| 4194 | new_thr->th.th_info.ds.ds_gtid); |
| 4195 | KMP_DEBUG_ASSERT(new_thr->th.th_serial_team); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4196 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4197 | TCW_4(__kmp_nth, __kmp_nth + 1); |
Jonathan Peyton | f439246 | 2017-07-27 20:58:41 +0000 | [diff] [blame] | 4198 | root->r.r_cg_nthreads++; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4199 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4200 | new_thr->th.th_task_state = 0; |
| 4201 | new_thr->th.th_task_state_top = 0; |
| 4202 | new_thr->th.th_task_state_stack_sz = 4; |
Andrey Churbanov | 6d224db | 2015-02-10 18:37:43 +0000 | [diff] [blame] | 4203 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4204 | #ifdef KMP_ADJUST_BLOCKTIME |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4205 | /* Adjust blocktime back to zero if necessary */ |
| 4206 | /* Middle initialization might not have occurred yet */ |
| 4207 | if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) { |
| 4208 | if (__kmp_nth > __kmp_avail_proc) { |
| 4209 | __kmp_zero_bt = TRUE; |
| 4210 | } |
| 4211 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4212 | #endif /* KMP_ADJUST_BLOCKTIME */ |
| 4213 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4214 | #if KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4215 | // If thread entered pool via __kmp_free_thread, wait_flag should != |
| 4216 | // KMP_BARRIER_PARENT_FLAG. |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4217 | int b; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4218 | kmp_balign_t *balign = new_thr->th.th_bar; |
| 4219 | for (b = 0; b < bs_last_barrier; ++b) |
| 4220 | KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4221 | #endif |
| 4222 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4223 | KF_TRACE(10, ("__kmp_allocate_thread: T#%d using thread %p T#%d\n", |
| 4224 | __kmp_get_gtid(), new_thr, new_thr->th.th_info.ds.ds_gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4225 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4226 | KMP_MB(); |
| 4227 | return new_thr; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | /* no, well fork a new one */ |
| 4231 | KMP_ASSERT(__kmp_nth == __kmp_all_nth); |
| 4232 | KMP_ASSERT(__kmp_all_nth < __kmp_threads_capacity); |
| 4233 | |
| 4234 | #if KMP_USE_MONITOR |
| 4235 | // If this is the first worker thread the RTL is creating, then also |
| 4236 | // launch the monitor thread. We try to do this as early as possible. |
| 4237 | if (!TCR_4(__kmp_init_monitor)) { |
| 4238 | __kmp_acquire_bootstrap_lock(&__kmp_monitor_lock); |
| 4239 | if (!TCR_4(__kmp_init_monitor)) { |
| 4240 | KF_TRACE(10, ("before __kmp_create_monitor\n")); |
| 4241 | TCW_4(__kmp_init_monitor, 1); |
| 4242 | __kmp_create_monitor(&__kmp_monitor); |
| 4243 | KF_TRACE(10, ("after __kmp_create_monitor\n")); |
| 4244 | #if KMP_OS_WINDOWS |
| 4245 | // AC: wait until monitor has started. This is a fix for CQ232808. |
| 4246 | // The reason is that if the library is loaded/unloaded in a loop with |
| 4247 | // small (parallel) work in between, then there is high probability that |
| 4248 | // monitor thread started after the library shutdown. At shutdown it is |
| 4249 | // too late to cope with the problem, because when the master is in |
| 4250 | // DllMain (process detach) the monitor has no chances to start (it is |
| 4251 | // blocked), and master has no means to inform the monitor that the |
| 4252 | // library has gone, because all the memory which the monitor can access |
| 4253 | // is going to be released/reset. |
| 4254 | while (TCR_4(__kmp_init_monitor) < 2) { |
| 4255 | KMP_YIELD(TRUE); |
| 4256 | } |
| 4257 | KF_TRACE(10, ("after monitor thread has started\n")); |
| 4258 | #endif |
| 4259 | } |
| 4260 | __kmp_release_bootstrap_lock(&__kmp_monitor_lock); |
| 4261 | } |
| 4262 | #endif |
| 4263 | |
| 4264 | KMP_MB(); |
| 4265 | for (new_gtid = 1; TCR_PTR(__kmp_threads[new_gtid]) != NULL; ++new_gtid) { |
| 4266 | KMP_DEBUG_ASSERT(new_gtid < __kmp_threads_capacity); |
| 4267 | } |
| 4268 | |
| 4269 | /* allocate space for it. */ |
| 4270 | new_thr = (kmp_info_t *)__kmp_allocate(sizeof(kmp_info_t)); |
| 4271 | |
| 4272 | TCW_SYNC_PTR(__kmp_threads[new_gtid], new_thr); |
| 4273 | |
| 4274 | if (__kmp_storage_map) { |
| 4275 | __kmp_print_thread_storage_map(new_thr, new_gtid); |
| 4276 | } |
| 4277 | |
| 4278 | // add the reserve serialized team, initialized from the team's master thread |
| 4279 | { |
| 4280 | kmp_internal_control_t r_icvs = __kmp_get_x_global_icvs(team); |
| 4281 | KF_TRACE(10, ("__kmp_allocate_thread: before th_serial/serial_team\n")); |
| 4282 | new_thr->th.th_serial_team = serial_team = |
| 4283 | (kmp_team_t *)__kmp_allocate_team(root, 1, 1, |
| 4284 | #if OMPT_SUPPORT |
| 4285 | 0, // root parallel id |
| 4286 | #endif |
| 4287 | #if OMP_40_ENABLED |
| 4288 | proc_bind_default, |
| 4289 | #endif |
| 4290 | &r_icvs, 0 USE_NESTED_HOT_ARG(NULL)); |
| 4291 | } |
| 4292 | KMP_ASSERT(serial_team); |
| 4293 | serial_team->t.t_serialized = 0; // AC: the team created in reserve, not for |
| 4294 | // execution (it is unused for now). |
| 4295 | serial_team->t.t_threads[0] = new_thr; |
| 4296 | KF_TRACE(10, |
| 4297 | ("__kmp_allocate_thread: after th_serial/serial_team : new_thr=%p\n", |
| 4298 | new_thr)); |
| 4299 | |
| 4300 | /* setup the thread structures */ |
| 4301 | __kmp_initialize_info(new_thr, team, new_tid, new_gtid); |
| 4302 | |
| 4303 | #if USE_FAST_MEMORY |
| 4304 | __kmp_initialize_fast_memory(new_thr); |
| 4305 | #endif /* USE_FAST_MEMORY */ |
| 4306 | |
| 4307 | #if KMP_USE_BGET |
| 4308 | KMP_DEBUG_ASSERT(new_thr->th.th_local.bget_data == NULL); |
| 4309 | __kmp_initialize_bget(new_thr); |
| 4310 | #endif |
| 4311 | |
| 4312 | __kmp_init_random(new_thr); // Initialize random number generator |
| 4313 | |
| 4314 | /* Initialize these only once when thread is grabbed for a team allocation */ |
| 4315 | KA_TRACE(20, |
| 4316 | ("__kmp_allocate_thread: T#%d init go fork=%u, plain=%u\n", |
| 4317 | __kmp_get_gtid(), KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE)); |
| 4318 | |
| 4319 | int b; |
| 4320 | kmp_balign_t *balign = new_thr->th.th_bar; |
| 4321 | for (b = 0; b < bs_last_barrier; ++b) { |
| 4322 | balign[b].bb.b_go = KMP_INIT_BARRIER_STATE; |
| 4323 | balign[b].bb.team = NULL; |
| 4324 | balign[b].bb.wait_flag = KMP_BARRIER_NOT_WAITING; |
| 4325 | balign[b].bb.use_oncore_barrier = 0; |
| 4326 | } |
| 4327 | |
| 4328 | new_thr->th.th_spin_here = FALSE; |
| 4329 | new_thr->th.th_next_waiting = 0; |
| 4330 | |
| 4331 | #if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED |
| 4332 | new_thr->th.th_current_place = KMP_PLACE_UNDEFINED; |
| 4333 | new_thr->th.th_new_place = KMP_PLACE_UNDEFINED; |
| 4334 | new_thr->th.th_first_place = KMP_PLACE_UNDEFINED; |
| 4335 | new_thr->th.th_last_place = KMP_PLACE_UNDEFINED; |
| 4336 | #endif |
| 4337 | |
| 4338 | TCW_4(new_thr->th.th_in_pool, FALSE); |
| 4339 | new_thr->th.th_active_in_pool = FALSE; |
| 4340 | TCW_4(new_thr->th.th_active, TRUE); |
| 4341 | |
| 4342 | /* adjust the global counters */ |
| 4343 | __kmp_all_nth++; |
| 4344 | __kmp_nth++; |
| 4345 | |
Jonathan Peyton | f439246 | 2017-07-27 20:58:41 +0000 | [diff] [blame] | 4346 | root->r.r_cg_nthreads++; |
| 4347 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4348 | // if __kmp_adjust_gtid_mode is set, then we use method #1 (sp search) for low |
| 4349 | // numbers of procs, and method #2 (keyed API call) for higher numbers. |
| 4350 | if (__kmp_adjust_gtid_mode) { |
| 4351 | if (__kmp_all_nth >= __kmp_tls_gtid_min) { |
| 4352 | if (TCR_4(__kmp_gtid_mode) != 2) { |
| 4353 | TCW_4(__kmp_gtid_mode, 2); |
| 4354 | } |
| 4355 | } else { |
| 4356 | if (TCR_4(__kmp_gtid_mode) != 1) { |
| 4357 | TCW_4(__kmp_gtid_mode, 1); |
| 4358 | } |
| 4359 | } |
| 4360 | } |
| 4361 | |
| 4362 | #ifdef KMP_ADJUST_BLOCKTIME |
| 4363 | /* Adjust blocktime back to zero if necessary */ |
| 4364 | /* Middle initialization might not have occurred yet */ |
| 4365 | if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) { |
| 4366 | if (__kmp_nth > __kmp_avail_proc) { |
| 4367 | __kmp_zero_bt = TRUE; |
| 4368 | } |
| 4369 | } |
| 4370 | #endif /* KMP_ADJUST_BLOCKTIME */ |
| 4371 | |
| 4372 | /* actually fork it and create the new worker thread */ |
| 4373 | KF_TRACE( |
| 4374 | 10, ("__kmp_allocate_thread: before __kmp_create_worker: %p\n", new_thr)); |
| 4375 | __kmp_create_worker(new_gtid, new_thr, __kmp_stksize); |
| 4376 | KF_TRACE(10, |
| 4377 | ("__kmp_allocate_thread: after __kmp_create_worker: %p\n", new_thr)); |
| 4378 | |
| 4379 | KA_TRACE(20, ("__kmp_allocate_thread: T#%d forked T#%d\n", __kmp_get_gtid(), |
| 4380 | new_gtid)); |
| 4381 | KMP_MB(); |
| 4382 | return new_thr; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4385 | /* Reinitialize team for reuse. |
| 4386 | The hot team code calls this case at every fork barrier, so EPCC barrier |
| 4387 | test are extremely sensitive to changes in it, esp. writes to the team |
| 4388 | struct, which cause a cache invalidation in all threads. |
| 4389 | IF YOU TOUCH THIS ROUTINE, RUN EPCC C SYNCBENCH ON A BIG-IRON MACHINE!!! */ |
| 4390 | static void __kmp_reinitialize_team(kmp_team_t *team, |
| 4391 | kmp_internal_control_t *new_icvs, |
| 4392 | ident_t *loc) { |
| 4393 | KF_TRACE(10, ("__kmp_reinitialize_team: enter this_thread=%p team=%p\n", |
| 4394 | team->t.t_threads[0], team)); |
| 4395 | KMP_DEBUG_ASSERT(team && new_icvs); |
| 4396 | KMP_DEBUG_ASSERT((!TCR_4(__kmp_init_parallel)) || new_icvs->nproc); |
| 4397 | KMP_CHECK_UPDATE(team->t.t_ident, loc); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4398 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4399 | KMP_CHECK_UPDATE(team->t.t_id, KMP_GEN_TEAM_ID()); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4400 | // Copy ICVs to the master thread's implicit taskdata |
| 4401 | __kmp_init_implicit_task(loc, team->t.t_threads[0], team, 0, FALSE); |
| 4402 | copy_icvs(&team->t.t_implicit_task_taskdata[0].td_icvs, new_icvs); |
Jim Cownie | 181b4bb | 2013-12-23 17:28:57 +0000 | [diff] [blame] | 4403 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4404 | KF_TRACE(10, ("__kmp_reinitialize_team: exit this_thread=%p team=%p\n", |
| 4405 | team->t.t_threads[0], team)); |
Jim Cownie | 181b4bb | 2013-12-23 17:28:57 +0000 | [diff] [blame] | 4406 | } |
| 4407 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4408 | /* Initialize the team data structure. |
| 4409 | This assumes the t_threads and t_max_nproc are already set. |
| 4410 | Also, we don't touch the arguments */ |
| 4411 | static void __kmp_initialize_team(kmp_team_t *team, int new_nproc, |
| 4412 | kmp_internal_control_t *new_icvs, |
| 4413 | ident_t *loc) { |
| 4414 | KF_TRACE(10, ("__kmp_initialize_team: enter: team=%p\n", team)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4415 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4416 | /* verify */ |
| 4417 | KMP_DEBUG_ASSERT(team); |
| 4418 | KMP_DEBUG_ASSERT(new_nproc <= team->t.t_max_nproc); |
| 4419 | KMP_DEBUG_ASSERT(team->t.t_threads); |
| 4420 | KMP_MB(); |
Jim Cownie | 181b4bb | 2013-12-23 17:28:57 +0000 | [diff] [blame] | 4421 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4422 | team->t.t_master_tid = 0; /* not needed */ |
| 4423 | /* team->t.t_master_bar; not needed */ |
| 4424 | team->t.t_serialized = new_nproc > 1 ? 0 : 1; |
| 4425 | team->t.t_nproc = new_nproc; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4426 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4427 | /* team->t.t_parent = NULL; TODO not needed & would mess up hot team */ |
| 4428 | team->t.t_next_pool = NULL; |
| 4429 | /* memset( team->t.t_threads, 0, sizeof(kmp_info_t*)*new_nproc ); would mess |
| 4430 | * up hot team */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4431 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4432 | TCW_SYNC_PTR(team->t.t_pkfn, NULL); /* not needed */ |
| 4433 | team->t.t_invoke = NULL; /* not needed */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4434 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4435 | // TODO???: team->t.t_max_active_levels = new_max_active_levels; |
| 4436 | team->t.t_sched = new_icvs->sched; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4437 | |
| 4438 | #if KMP_ARCH_X86 || KMP_ARCH_X86_64 |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4439 | team->t.t_fp_control_saved = FALSE; /* not needed */ |
| 4440 | team->t.t_x87_fpu_control_word = 0; /* not needed */ |
| 4441 | team->t.t_mxcsr = 0; /* not needed */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4442 | #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */ |
| 4443 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4444 | team->t.t_construct = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4445 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4446 | team->t.t_ordered.dt.t_value = 0; |
| 4447 | team->t.t_master_active = FALSE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4448 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4449 | memset(&team->t.t_taskq, '\0', sizeof(kmp_taskq_t)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4450 | |
| 4451 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4452 | team->t.t_copypriv_data = NULL; /* not necessary, but nice for debugging */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4453 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4454 | team->t.t_copyin_counter = 0; /* for barrier-free copyin implementation */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4455 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4456 | team->t.t_control_stack_top = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4457 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4458 | __kmp_reinitialize_team(team, new_icvs, loc); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4459 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4460 | KMP_MB(); |
| 4461 | KF_TRACE(10, ("__kmp_initialize_team: exit: team=%p\n", team)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
Alp Toker | 98758b0 | 2014-03-02 04:12:06 +0000 | [diff] [blame] | 4464 | #if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4465 | /* Sets full mask for thread and returns old mask, no changes to structures. */ |
| 4466 | static void |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4467 | __kmp_set_thread_affinity_mask_full_tmp(kmp_affin_mask_t *old_mask) { |
| 4468 | if (KMP_AFFINITY_CAPABLE()) { |
| 4469 | int status; |
| 4470 | if (old_mask != NULL) { |
| 4471 | status = __kmp_get_system_affinity(old_mask, TRUE); |
| 4472 | int error = errno; |
| 4473 | if (status != 0) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 4474 | __kmp_fatal(KMP_MSG(ChangeThreadAffMaskError), KMP_ERR(error), |
| 4475 | __kmp_msg_null); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4476 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4477 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4478 | __kmp_set_system_affinity(__kmp_affin_fullMask, TRUE); |
| 4479 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4480 | } |
| 4481 | #endif |
| 4482 | |
Alp Toker | 98758b0 | 2014-03-02 04:12:06 +0000 | [diff] [blame] | 4483 | #if OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4484 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4485 | // __kmp_partition_places() is the heart of the OpenMP 4.0 affinity mechanism. |
| 4486 | // It calculats the worker + master thread's partition based upon the parent |
Alp Toker | 8f2d3f0 | 2014-02-24 10:40:15 +0000 | [diff] [blame] | 4487 | // thread's partition, and binds each worker to a thread in their partition. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4488 | // The master thread's partition should already include its current binding. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4489 | static void __kmp_partition_places(kmp_team_t *team, int update_master_only) { |
| 4490 | // Copy the master thread's place partion to the team struct |
| 4491 | kmp_info_t *master_th = team->t.t_threads[0]; |
| 4492 | KMP_DEBUG_ASSERT(master_th != NULL); |
| 4493 | kmp_proc_bind_t proc_bind = team->t.t_proc_bind; |
| 4494 | int first_place = master_th->th.th_first_place; |
| 4495 | int last_place = master_th->th.th_last_place; |
| 4496 | int masters_place = master_th->th.th_current_place; |
| 4497 | team->t.t_first_place = first_place; |
| 4498 | team->t.t_last_place = last_place; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4499 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4500 | KA_TRACE(20, ("__kmp_partition_places: enter: proc_bind = %d T#%d(%d:0) " |
| 4501 | "bound to place %d partition = [%d,%d]\n", |
| 4502 | proc_bind, __kmp_gtid_from_thread(team->t.t_threads[0]), |
| 4503 | team->t.t_id, masters_place, first_place, last_place)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4504 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4505 | switch (proc_bind) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4506 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4507 | case proc_bind_default: |
| 4508 | // serial teams might have the proc_bind policy set to proc_bind_default. It |
| 4509 | // doesn't matter, as we don't rebind master thread for any proc_bind policy |
| 4510 | KMP_DEBUG_ASSERT(team->t.t_nproc == 1); |
| 4511 | break; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4512 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4513 | case proc_bind_master: { |
| 4514 | int f; |
| 4515 | int n_th = team->t.t_nproc; |
| 4516 | for (f = 1; f < n_th; f++) { |
| 4517 | kmp_info_t *th = team->t.t_threads[f]; |
| 4518 | KMP_DEBUG_ASSERT(th != NULL); |
| 4519 | th->th.th_first_place = first_place; |
| 4520 | th->th.th_last_place = last_place; |
| 4521 | th->th.th_new_place = masters_place; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4522 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4523 | KA_TRACE(100, ("__kmp_partition_places: master: T#%d(%d:%d) place %d " |
| 4524 | "partition = [%d,%d]\n", |
| 4525 | __kmp_gtid_from_thread(team->t.t_threads[f]), team->t.t_id, |
| 4526 | f, masters_place, first_place, last_place)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4527 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4528 | } break; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4529 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4530 | case proc_bind_close: { |
| 4531 | int f; |
| 4532 | int n_th = team->t.t_nproc; |
| 4533 | int n_places; |
| 4534 | if (first_place <= last_place) { |
| 4535 | n_places = last_place - first_place + 1; |
| 4536 | } else { |
| 4537 | n_places = __kmp_affinity_num_masks - first_place + last_place + 1; |
| 4538 | } |
| 4539 | if (n_th <= n_places) { |
| 4540 | int place = masters_place; |
| 4541 | for (f = 1; f < n_th; f++) { |
| 4542 | kmp_info_t *th = team->t.t_threads[f]; |
| 4543 | KMP_DEBUG_ASSERT(th != NULL); |
| 4544 | |
| 4545 | if (place == last_place) { |
| 4546 | place = first_place; |
| 4547 | } else if (place == (int)(__kmp_affinity_num_masks - 1)) { |
| 4548 | place = 0; |
| 4549 | } else { |
| 4550 | place++; |
| 4551 | } |
| 4552 | th->th.th_first_place = first_place; |
| 4553 | th->th.th_last_place = last_place; |
| 4554 | th->th.th_new_place = place; |
| 4555 | |
| 4556 | KA_TRACE(100, ("__kmp_partition_places: close: T#%d(%d:%d) place %d " |
| 4557 | "partition = [%d,%d]\n", |
| 4558 | __kmp_gtid_from_thread(team->t.t_threads[f]), |
| 4559 | team->t.t_id, f, place, first_place, last_place)); |
| 4560 | } |
| 4561 | } else { |
| 4562 | int S, rem, gap, s_count; |
| 4563 | S = n_th / n_places; |
| 4564 | s_count = 0; |
| 4565 | rem = n_th - (S * n_places); |
| 4566 | gap = rem > 0 ? n_places / rem : n_places; |
| 4567 | int place = masters_place; |
| 4568 | int gap_ct = gap; |
| 4569 | for (f = 0; f < n_th; f++) { |
| 4570 | kmp_info_t *th = team->t.t_threads[f]; |
| 4571 | KMP_DEBUG_ASSERT(th != NULL); |
| 4572 | |
| 4573 | th->th.th_first_place = first_place; |
| 4574 | th->th.th_last_place = last_place; |
| 4575 | th->th.th_new_place = place; |
| 4576 | s_count++; |
| 4577 | |
| 4578 | if ((s_count == S) && rem && (gap_ct == gap)) { |
| 4579 | // do nothing, add an extra thread to place on next iteration |
| 4580 | } else if ((s_count == S + 1) && rem && (gap_ct == gap)) { |
| 4581 | // we added an extra thread to this place; move to next place |
| 4582 | if (place == last_place) { |
| 4583 | place = first_place; |
| 4584 | } else if (place == (int)(__kmp_affinity_num_masks - 1)) { |
| 4585 | place = 0; |
| 4586 | } else { |
| 4587 | place++; |
| 4588 | } |
| 4589 | s_count = 0; |
| 4590 | gap_ct = 1; |
| 4591 | rem--; |
| 4592 | } else if (s_count == S) { // place full; don't add extra |
| 4593 | if (place == last_place) { |
| 4594 | place = first_place; |
| 4595 | } else if (place == (int)(__kmp_affinity_num_masks - 1)) { |
| 4596 | place = 0; |
| 4597 | } else { |
| 4598 | place++; |
| 4599 | } |
| 4600 | gap_ct++; |
| 4601 | s_count = 0; |
| 4602 | } |
| 4603 | |
| 4604 | KA_TRACE(100, |
| 4605 | ("__kmp_partition_places: close: T#%d(%d:%d) place %d " |
| 4606 | "partition = [%d,%d]\n", |
| 4607 | __kmp_gtid_from_thread(team->t.t_threads[f]), team->t.t_id, f, |
| 4608 | th->th.th_new_place, first_place, last_place)); |
| 4609 | } |
| 4610 | KMP_DEBUG_ASSERT(place == masters_place); |
| 4611 | } |
| 4612 | } break; |
| 4613 | |
| 4614 | case proc_bind_spread: { |
| 4615 | int f; |
| 4616 | int n_th = team->t.t_nproc; |
| 4617 | int n_places; |
| 4618 | int thidx; |
| 4619 | if (first_place <= last_place) { |
| 4620 | n_places = last_place - first_place + 1; |
| 4621 | } else { |
| 4622 | n_places = __kmp_affinity_num_masks - first_place + last_place + 1; |
| 4623 | } |
| 4624 | if (n_th <= n_places) { |
Paul Osmialowski | a016279 | 2017-08-10 23:04:11 +0000 | [diff] [blame] | 4625 | int place = -1; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4626 | |
Paul Osmialowski | a016279 | 2017-08-10 23:04:11 +0000 | [diff] [blame] | 4627 | if (n_places != static_cast<int>(__kmp_affinity_num_masks)) { |
| 4628 | int S = n_places / n_th; |
| 4629 | int s_count, rem, gap, gap_ct; |
| 4630 | |
| 4631 | place = masters_place; |
| 4632 | rem = n_places - n_th * S; |
| 4633 | gap = rem ? n_th / rem : 1; |
| 4634 | gap_ct = gap; |
| 4635 | thidx = n_th; |
| 4636 | if (update_master_only == 1) |
| 4637 | thidx = 1; |
| 4638 | for (f = 0; f < thidx; f++) { |
| 4639 | kmp_info_t *th = team->t.t_threads[f]; |
| 4640 | KMP_DEBUG_ASSERT(th != NULL); |
| 4641 | |
| 4642 | th->th.th_first_place = place; |
| 4643 | th->th.th_new_place = place; |
| 4644 | s_count = 1; |
| 4645 | while (s_count < S) { |
| 4646 | if (place == last_place) { |
| 4647 | place = first_place; |
| 4648 | } else if (place == (int)(__kmp_affinity_num_masks - 1)) { |
| 4649 | place = 0; |
| 4650 | } else { |
| 4651 | place++; |
| 4652 | } |
| 4653 | s_count++; |
| 4654 | } |
| 4655 | if (rem && (gap_ct == gap)) { |
| 4656 | if (place == last_place) { |
| 4657 | place = first_place; |
| 4658 | } else if (place == (int)(__kmp_affinity_num_masks - 1)) { |
| 4659 | place = 0; |
| 4660 | } else { |
| 4661 | place++; |
| 4662 | } |
| 4663 | rem--; |
| 4664 | gap_ct = 0; |
| 4665 | } |
| 4666 | th->th.th_last_place = place; |
| 4667 | gap_ct++; |
| 4668 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4669 | if (place == last_place) { |
| 4670 | place = first_place; |
| 4671 | } else if (place == (int)(__kmp_affinity_num_masks - 1)) { |
| 4672 | place = 0; |
| 4673 | } else { |
| 4674 | place++; |
| 4675 | } |
Paul Osmialowski | a016279 | 2017-08-10 23:04:11 +0000 | [diff] [blame] | 4676 | |
Jonathan Peyton | 94a114f | 2017-10-20 19:30:57 +0000 | [diff] [blame] | 4677 | KA_TRACE(100, |
| 4678 | ("__kmp_partition_places: spread: T#%d(%d:%d) place %d " |
| 4679 | "partition = [%d,%d], __kmp_affinity_num_masks: %u\n", |
| 4680 | __kmp_gtid_from_thread(team->t.t_threads[f]), team->t.t_id, |
| 4681 | f, th->th.th_new_place, th->th.th_first_place, |
| 4682 | th->th.th_last_place, __kmp_affinity_num_masks)); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4683 | } |
Paul Osmialowski | a016279 | 2017-08-10 23:04:11 +0000 | [diff] [blame] | 4684 | } else { |
| 4685 | /* Having uniform space of available computation places I can create |
| 4686 | T partitions of round(P/T) size and put threads into the first |
| 4687 | place of each partition. */ |
| 4688 | double current = static_cast<double>(masters_place); |
| 4689 | double spacing = |
Jonathan Peyton | 94a114f | 2017-10-20 19:30:57 +0000 | [diff] [blame] | 4690 | (static_cast<double>(n_places + 1) / static_cast<double>(n_th)); |
Paul Osmialowski | a016279 | 2017-08-10 23:04:11 +0000 | [diff] [blame] | 4691 | int first, last; |
| 4692 | kmp_info_t *th; |
| 4693 | |
| 4694 | thidx = n_th + 1; |
| 4695 | if (update_master_only == 1) |
| 4696 | thidx = 1; |
| 4697 | for (f = 0; f < thidx; f++) { |
| 4698 | first = static_cast<int>(current); |
| 4699 | last = static_cast<int>(current + spacing) - 1; |
| 4700 | KMP_DEBUG_ASSERT(last >= first); |
| 4701 | if (first >= n_places) { |
| 4702 | if (masters_place) { |
| 4703 | first -= n_places; |
| 4704 | last -= n_places; |
| 4705 | if (first == (masters_place + 1)) { |
| 4706 | KMP_DEBUG_ASSERT(f == n_th); |
| 4707 | first--; |
| 4708 | } |
| 4709 | if (last == masters_place) { |
| 4710 | KMP_DEBUG_ASSERT(f == (n_th - 1)); |
| 4711 | last--; |
| 4712 | } |
| 4713 | } else { |
| 4714 | KMP_DEBUG_ASSERT(f == n_th); |
| 4715 | first = 0; |
| 4716 | last = 0; |
| 4717 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4718 | } |
Paul Osmialowski | a016279 | 2017-08-10 23:04:11 +0000 | [diff] [blame] | 4719 | if (last >= n_places) { |
| 4720 | last = (n_places - 1); |
| 4721 | } |
| 4722 | place = first; |
| 4723 | current += spacing; |
| 4724 | if (f < n_th) { |
| 4725 | KMP_DEBUG_ASSERT(0 <= first); |
| 4726 | KMP_DEBUG_ASSERT(n_places > first); |
| 4727 | KMP_DEBUG_ASSERT(0 <= last); |
| 4728 | KMP_DEBUG_ASSERT(n_places > last); |
| 4729 | KMP_DEBUG_ASSERT(last_place >= first_place); |
| 4730 | th = team->t.t_threads[f]; |
| 4731 | KMP_DEBUG_ASSERT(th); |
| 4732 | th->th.th_first_place = first; |
| 4733 | th->th.th_new_place = place; |
| 4734 | th->th.th_last_place = last; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4735 | |
Jonathan Peyton | 94a114f | 2017-10-20 19:30:57 +0000 | [diff] [blame] | 4736 | KA_TRACE(100, |
| 4737 | ("__kmp_partition_places: spread: T#%d(%d:%d) place %d " |
| 4738 | "partition = [%d,%d], spacing = %.4f\n", |
| 4739 | __kmp_gtid_from_thread(team->t.t_threads[f]), |
| 4740 | team->t.t_id, f, th->th.th_new_place, |
| 4741 | th->th.th_first_place, th->th.th_last_place, spacing)); |
Paul Osmialowski | a016279 | 2017-08-10 23:04:11 +0000 | [diff] [blame] | 4742 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4743 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4744 | } |
| 4745 | KMP_DEBUG_ASSERT(update_master_only || place == masters_place); |
| 4746 | } else { |
| 4747 | int S, rem, gap, s_count; |
| 4748 | S = n_th / n_places; |
| 4749 | s_count = 0; |
| 4750 | rem = n_th - (S * n_places); |
| 4751 | gap = rem > 0 ? n_places / rem : n_places; |
| 4752 | int place = masters_place; |
| 4753 | int gap_ct = gap; |
| 4754 | thidx = n_th; |
| 4755 | if (update_master_only == 1) |
| 4756 | thidx = 1; |
| 4757 | for (f = 0; f < thidx; f++) { |
| 4758 | kmp_info_t *th = team->t.t_threads[f]; |
| 4759 | KMP_DEBUG_ASSERT(th != NULL); |
| 4760 | |
| 4761 | th->th.th_first_place = place; |
| 4762 | th->th.th_last_place = place; |
| 4763 | th->th.th_new_place = place; |
| 4764 | s_count++; |
| 4765 | |
| 4766 | if ((s_count == S) && rem && (gap_ct == gap)) { |
| 4767 | // do nothing, add an extra thread to place on next iteration |
| 4768 | } else if ((s_count == S + 1) && rem && (gap_ct == gap)) { |
| 4769 | // we added an extra thread to this place; move on to next place |
| 4770 | if (place == last_place) { |
| 4771 | place = first_place; |
| 4772 | } else if (place == (int)(__kmp_affinity_num_masks - 1)) { |
| 4773 | place = 0; |
| 4774 | } else { |
| 4775 | place++; |
| 4776 | } |
| 4777 | s_count = 0; |
| 4778 | gap_ct = 1; |
| 4779 | rem--; |
| 4780 | } else if (s_count == S) { // place is full; don't add extra thread |
| 4781 | if (place == last_place) { |
| 4782 | place = first_place; |
| 4783 | } else if (place == (int)(__kmp_affinity_num_masks - 1)) { |
| 4784 | place = 0; |
| 4785 | } else { |
| 4786 | place++; |
| 4787 | } |
| 4788 | gap_ct++; |
| 4789 | s_count = 0; |
| 4790 | } |
| 4791 | |
| 4792 | KA_TRACE(100, ("__kmp_partition_places: spread: T#%d(%d:%d) place %d " |
| 4793 | "partition = [%d,%d]\n", |
| 4794 | __kmp_gtid_from_thread(team->t.t_threads[f]), |
| 4795 | team->t.t_id, f, th->th.th_new_place, |
| 4796 | th->th.th_first_place, th->th.th_last_place)); |
| 4797 | } |
| 4798 | KMP_DEBUG_ASSERT(update_master_only || place == masters_place); |
| 4799 | } |
| 4800 | } break; |
| 4801 | |
| 4802 | default: |
| 4803 | break; |
| 4804 | } |
| 4805 | |
| 4806 | KA_TRACE(20, ("__kmp_partition_places: exit T#%d\n", team->t.t_id)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
Alp Toker | 98758b0 | 2014-03-02 04:12:06 +0000 | [diff] [blame] | 4809 | #endif /* OMP_40_ENABLED && KMP_AFFINITY_SUPPORTED */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4810 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4811 | /* allocate a new team data structure to use. take one off of the free pool if |
| 4812 | available */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4813 | kmp_team_t * |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4814 | __kmp_allocate_team(kmp_root_t *root, int new_nproc, int max_nproc, |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 4815 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4816 | ompt_parallel_id_t ompt_parallel_id, |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 4817 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4818 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4819 | kmp_proc_bind_t new_proc_bind, |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4820 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4821 | kmp_internal_control_t *new_icvs, |
| 4822 | int argc USE_NESTED_HOT_ARG(kmp_info_t *master)) { |
| 4823 | KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(KMP_allocate_team); |
| 4824 | int f; |
| 4825 | kmp_team_t *team; |
| 4826 | int use_hot_team = !root->r.r_active; |
| 4827 | int level = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4828 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4829 | KA_TRACE(20, ("__kmp_allocate_team: called\n")); |
| 4830 | KMP_DEBUG_ASSERT(new_nproc >= 1 && argc >= 0); |
| 4831 | KMP_DEBUG_ASSERT(max_nproc >= new_nproc); |
| 4832 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4833 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4834 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4835 | kmp_hot_team_ptr_t *hot_teams; |
| 4836 | if (master) { |
| 4837 | team = master->th.th_team; |
| 4838 | level = team->t.t_active_level; |
| 4839 | if (master->th.th_teams_microtask) { // in teams construct? |
| 4840 | if (master->th.th_teams_size.nteams > 1 && |
| 4841 | ( // #teams > 1 |
| 4842 | team->t.t_pkfn == |
| 4843 | (microtask_t)__kmp_teams_master || // inner fork of the teams |
| 4844 | master->th.th_teams_level < |
| 4845 | team->t.t_level)) { // or nested parallel inside the teams |
| 4846 | ++level; // not increment if #teams==1, or for outer fork of the teams; |
| 4847 | // increment otherwise |
| 4848 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4849 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4850 | hot_teams = master->th.th_hot_teams; |
| 4851 | if (level < __kmp_hot_teams_max_level && hot_teams && |
| 4852 | hot_teams[level] |
| 4853 | .hot_team) { // hot team has already been allocated for given level |
| 4854 | use_hot_team = 1; |
| 4855 | } else { |
| 4856 | use_hot_team = 0; |
| 4857 | } |
| 4858 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4859 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4860 | // Optimization to use a "hot" team |
| 4861 | if (use_hot_team && new_nproc > 1) { |
| 4862 | KMP_DEBUG_ASSERT(new_nproc == max_nproc); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4863 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4864 | team = hot_teams[level].hot_team; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4865 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4866 | team = root->r.r_hot_team; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4867 | #endif |
| 4868 | #if KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4869 | if (__kmp_tasking_mode != tskm_immediate_exec) { |
| 4870 | KA_TRACE(20, ("__kmp_allocate_team: hot team task_team[0] = %p " |
| 4871 | "task_team[1] = %p before reinit\n", |
| 4872 | team->t.t_task_team[0], team->t.t_task_team[1])); |
| 4873 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4874 | #endif |
| 4875 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4876 | // Has the number of threads changed? |
| 4877 | /* Let's assume the most common case is that the number of threads is |
| 4878 | unchanged, and put that case first. */ |
| 4879 | if (team->t.t_nproc == new_nproc) { // Check changes in number of threads |
| 4880 | KA_TRACE(20, ("__kmp_allocate_team: reusing hot team\n")); |
| 4881 | // This case can mean that omp_set_num_threads() was called and the hot |
Jonathan Peyton | 642688b | 2017-06-01 16:46:36 +0000 | [diff] [blame] | 4882 | // team size was already reduced, so we check the special flag |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4883 | if (team->t.t_size_changed == -1) { |
| 4884 | team->t.t_size_changed = 1; |
| 4885 | } else { |
| 4886 | KMP_CHECK_UPDATE(team->t.t_size_changed, 0); |
| 4887 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4888 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4889 | // TODO???: team->t.t_max_active_levels = new_max_active_levels; |
| 4890 | kmp_r_sched_t new_sched = new_icvs->sched; |
| 4891 | if (team->t.t_sched.r_sched_type != new_sched.r_sched_type || |
| 4892 | team->t.t_sched.chunk != new_sched.chunk) |
| 4893 | team->t.t_sched = |
| 4894 | new_sched; // set master's schedule as new run-time schedule |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4895 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4896 | __kmp_reinitialize_team(team, new_icvs, |
| 4897 | root->r.r_uber_thread->th.th_ident); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4898 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4899 | KF_TRACE(10, ("__kmp_allocate_team2: T#%d, this_thread=%p team=%p\n", 0, |
| 4900 | team->t.t_threads[0], team)); |
| 4901 | __kmp_push_current_task_to_thread(team->t.t_threads[0], team, 0); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4902 | |
| 4903 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4904 | #if KMP_AFFINITY_SUPPORTED |
| 4905 | if ((team->t.t_size_changed == 0) && |
| 4906 | (team->t.t_proc_bind == new_proc_bind)) { |
| 4907 | if (new_proc_bind == proc_bind_spread) { |
| 4908 | __kmp_partition_places( |
| 4909 | team, 1); // add flag to update only master for spread |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4910 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4911 | KA_TRACE(200, ("__kmp_allocate_team: reusing hot team #%d bindings: " |
| 4912 | "proc_bind = %d, partition = [%d,%d]\n", |
| 4913 | team->t.t_id, new_proc_bind, team->t.t_first_place, |
| 4914 | team->t.t_last_place)); |
| 4915 | } else { |
| 4916 | KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind); |
| 4917 | __kmp_partition_places(team); |
| 4918 | } |
| 4919 | #else |
| 4920 | KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind); |
| 4921 | #endif /* KMP_AFFINITY_SUPPORTED */ |
| 4922 | #endif /* OMP_40_ENABLED */ |
| 4923 | } else if (team->t.t_nproc > new_nproc) { |
| 4924 | KA_TRACE(20, |
| 4925 | ("__kmp_allocate_team: decreasing hot team thread count to %d\n", |
| 4926 | new_nproc)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4927 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4928 | team->t.t_size_changed = 1; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4929 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4930 | if (__kmp_hot_teams_mode == 0) { |
| 4931 | // AC: saved number of threads should correspond to team's value in this |
| 4932 | // mode, can be bigger in mode 1, when hot team has threads in reserve |
| 4933 | KMP_DEBUG_ASSERT(hot_teams[level].hot_team_nth == team->t.t_nproc); |
| 4934 | hot_teams[level].hot_team_nth = new_nproc; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4935 | #endif // KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4936 | /* release the extra threads we don't need any more */ |
| 4937 | for (f = new_nproc; f < team->t.t_nproc; f++) { |
| 4938 | KMP_DEBUG_ASSERT(team->t.t_threads[f]); |
| 4939 | if (__kmp_tasking_mode != tskm_immediate_exec) { |
| 4940 | // When decreasing team size, threads no longer in the team should |
| 4941 | // unref task team. |
| 4942 | team->t.t_threads[f]->th.th_task_team = NULL; |
| 4943 | } |
| 4944 | __kmp_free_thread(team->t.t_threads[f]); |
| 4945 | team->t.t_threads[f] = NULL; |
| 4946 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4947 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4948 | } // (__kmp_hot_teams_mode == 0) |
| 4949 | else { |
| 4950 | // When keeping extra threads in team, switch threads to wait on own |
| 4951 | // b_go flag |
| 4952 | for (f = new_nproc; f < team->t.t_nproc; ++f) { |
| 4953 | KMP_DEBUG_ASSERT(team->t.t_threads[f]); |
| 4954 | kmp_balign_t *balign = team->t.t_threads[f]->th.th_bar; |
| 4955 | for (int b = 0; b < bs_last_barrier; ++b) { |
| 4956 | if (balign[b].bb.wait_flag == KMP_BARRIER_PARENT_FLAG) { |
| 4957 | balign[b].bb.wait_flag = KMP_BARRIER_SWITCH_TO_OWN_FLAG; |
Andrey Churbanov | d6e1d7e | 2016-08-11 13:04:00 +0000 | [diff] [blame] | 4958 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4959 | KMP_CHECK_UPDATE(balign[b].bb.leaf_kids, 0); |
| 4960 | } |
| 4961 | } |
| 4962 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 4963 | #endif // KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4964 | team->t.t_nproc = new_nproc; |
| 4965 | // TODO???: team->t.t_max_active_levels = new_max_active_levels; |
| 4966 | if (team->t.t_sched.r_sched_type != new_icvs->sched.r_sched_type || |
| 4967 | team->t.t_sched.chunk != new_icvs->sched.chunk) |
| 4968 | team->t.t_sched = new_icvs->sched; |
| 4969 | __kmp_reinitialize_team(team, new_icvs, |
| 4970 | root->r.r_uber_thread->th.th_ident); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4971 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4972 | /* update the remaining threads */ |
| 4973 | for (f = 0; f < new_nproc; ++f) { |
| 4974 | team->t.t_threads[f]->th.th_team_nproc = new_nproc; |
| 4975 | } |
| 4976 | // restore the current task state of the master thread: should be the |
| 4977 | // implicit task |
| 4978 | KF_TRACE(10, ("__kmp_allocate_team: T#%d, this_thread=%p team=%p\n", 0, |
| 4979 | team->t.t_threads[0], team)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4980 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4981 | __kmp_push_current_task_to_thread(team->t.t_threads[0], team, 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4982 | |
| 4983 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4984 | for (f = 0; f < team->t.t_nproc; f++) { |
| 4985 | KMP_DEBUG_ASSERT(team->t.t_threads[f] && |
| 4986 | team->t.t_threads[f]->th.th_team_nproc == |
| 4987 | team->t.t_nproc); |
| 4988 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4989 | #endif |
| 4990 | |
| 4991 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4992 | KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind); |
| 4993 | #if KMP_AFFINITY_SUPPORTED |
| 4994 | __kmp_partition_places(team); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 4995 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4996 | #endif |
| 4997 | } else { // team->t.t_nproc < new_nproc |
Alp Toker | 98758b0 | 2014-03-02 04:12:06 +0000 | [diff] [blame] | 4998 | #if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 4999 | kmp_affin_mask_t *old_mask; |
| 5000 | if (KMP_AFFINITY_CAPABLE()) { |
| 5001 | KMP_CPU_ALLOC(old_mask); |
| 5002 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5003 | #endif |
| 5004 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5005 | KA_TRACE(20, |
| 5006 | ("__kmp_allocate_team: increasing hot team thread count to %d\n", |
| 5007 | new_nproc)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5008 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5009 | team->t.t_size_changed = 1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5010 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 5011 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5012 | int avail_threads = hot_teams[level].hot_team_nth; |
| 5013 | if (new_nproc < avail_threads) |
| 5014 | avail_threads = new_nproc; |
| 5015 | kmp_info_t **other_threads = team->t.t_threads; |
| 5016 | for (f = team->t.t_nproc; f < avail_threads; ++f) { |
| 5017 | // Adjust barrier data of reserved threads (if any) of the team |
| 5018 | // Other data will be set in __kmp_initialize_info() below. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5019 | int b; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5020 | kmp_balign_t *balign = other_threads[f]->th.th_bar; |
| 5021 | for (b = 0; b < bs_last_barrier; ++b) { |
| 5022 | balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived; |
| 5023 | KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG); |
Jonathan Peyton | 8fbb49a | 2015-07-09 18:16:58 +0000 | [diff] [blame] | 5024 | #if USE_DEBUGGER |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5025 | balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived; |
Jonathan Peyton | 8fbb49a | 2015-07-09 18:16:58 +0000 | [diff] [blame] | 5026 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5027 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5028 | } |
| 5029 | if (hot_teams[level].hot_team_nth >= new_nproc) { |
| 5030 | // we have all needed threads in reserve, no need to allocate any |
| 5031 | // this only possible in mode 1, cannot have reserved threads in mode 0 |
| 5032 | KMP_DEBUG_ASSERT(__kmp_hot_teams_mode == 1); |
| 5033 | team->t.t_nproc = new_nproc; // just get reserved threads involved |
| 5034 | } else { |
| 5035 | // we may have some threads in reserve, but not enough |
| 5036 | team->t.t_nproc = |
| 5037 | hot_teams[level] |
| 5038 | .hot_team_nth; // get reserved threads involved if any |
| 5039 | hot_teams[level].hot_team_nth = new_nproc; // adjust hot team max size |
| 5040 | #endif // KMP_NESTED_HOT_TEAMS |
| 5041 | if (team->t.t_max_nproc < new_nproc) { |
| 5042 | /* reallocate larger arrays */ |
| 5043 | __kmp_reallocate_team_arrays(team, new_nproc); |
| 5044 | __kmp_reinitialize_team(team, new_icvs, NULL); |
| 5045 | } |
| 5046 | |
| 5047 | #if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED |
| 5048 | /* Temporarily set full mask for master thread before creation of |
| 5049 | workers. The reason is that workers inherit the affinity from master, |
| 5050 | so if a lot of workers are created on the single core quickly, they |
| 5051 | don't get a chance to set their own affinity for a long time. */ |
| 5052 | __kmp_set_thread_affinity_mask_full_tmp(old_mask); |
| 5053 | #endif |
| 5054 | |
| 5055 | /* allocate new threads for the hot team */ |
| 5056 | for (f = team->t.t_nproc; f < new_nproc; f++) { |
| 5057 | kmp_info_t *new_worker = __kmp_allocate_thread(root, team, f); |
| 5058 | KMP_DEBUG_ASSERT(new_worker); |
| 5059 | team->t.t_threads[f] = new_worker; |
| 5060 | |
| 5061 | KA_TRACE(20, |
| 5062 | ("__kmp_allocate_team: team %d init T#%d arrived: " |
| 5063 | "join=%llu, plain=%llu\n", |
| 5064 | team->t.t_id, __kmp_gtid_from_tid(f, team), team->t.t_id, f, |
| 5065 | team->t.t_bar[bs_forkjoin_barrier].b_arrived, |
| 5066 | team->t.t_bar[bs_plain_barrier].b_arrived)); |
| 5067 | |
| 5068 | { // Initialize barrier data for new threads. |
| 5069 | int b; |
| 5070 | kmp_balign_t *balign = new_worker->th.th_bar; |
| 5071 | for (b = 0; b < bs_last_barrier; ++b) { |
| 5072 | balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived; |
| 5073 | KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != |
| 5074 | KMP_BARRIER_PARENT_FLAG); |
| 5075 | #if USE_DEBUGGER |
| 5076 | balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived; |
| 5077 | #endif |
| 5078 | } |
| 5079 | } |
| 5080 | } |
| 5081 | |
| 5082 | #if KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED |
| 5083 | if (KMP_AFFINITY_CAPABLE()) { |
| 5084 | /* Restore initial master thread's affinity mask */ |
| 5085 | __kmp_set_system_affinity(old_mask, TRUE); |
| 5086 | KMP_CPU_FREE(old_mask); |
| 5087 | } |
| 5088 | #endif |
| 5089 | #if KMP_NESTED_HOT_TEAMS |
| 5090 | } // end of check of t_nproc vs. new_nproc vs. hot_team_nth |
| 5091 | #endif // KMP_NESTED_HOT_TEAMS |
| 5092 | /* make sure everyone is syncronized */ |
| 5093 | int old_nproc = team->t.t_nproc; // save old value and use to update only |
| 5094 | // new threads below |
| 5095 | __kmp_initialize_team(team, new_nproc, new_icvs, |
| 5096 | root->r.r_uber_thread->th.th_ident); |
| 5097 | |
| 5098 | /* reinitialize the threads */ |
| 5099 | KMP_DEBUG_ASSERT(team->t.t_nproc == new_nproc); |
| 5100 | for (f = 0; f < team->t.t_nproc; ++f) |
| 5101 | __kmp_initialize_info(team->t.t_threads[f], team, f, |
| 5102 | __kmp_gtid_from_tid(f, team)); |
| 5103 | if (level) { // set th_task_state for new threads in nested hot team |
| 5104 | // __kmp_initialize_info() no longer zeroes th_task_state, so we should |
| 5105 | // only need to set the th_task_state for the new threads. th_task_state |
| 5106 | // for master thread will not be accurate until after this in |
| 5107 | // __kmp_fork_call(), so we look to the master's memo_stack to get the |
| 5108 | // correct value. |
| 5109 | for (f = old_nproc; f < team->t.t_nproc; ++f) |
| 5110 | team->t.t_threads[f]->th.th_task_state = |
| 5111 | team->t.t_threads[0]->th.th_task_state_memo_stack[level]; |
| 5112 | } else { // set th_task_state for new threads in non-nested hot team |
| 5113 | int old_state = |
| 5114 | team->t.t_threads[0]->th.th_task_state; // copy master's state |
| 5115 | for (f = old_nproc; f < team->t.t_nproc; ++f) |
| 5116 | team->t.t_threads[f]->th.th_task_state = old_state; |
| 5117 | } |
| 5118 | |
| 5119 | #ifdef KMP_DEBUG |
| 5120 | for (f = 0; f < team->t.t_nproc; ++f) { |
| 5121 | KMP_DEBUG_ASSERT(team->t.t_threads[f] && |
| 5122 | team->t.t_threads[f]->th.th_team_nproc == |
| 5123 | team->t.t_nproc); |
| 5124 | } |
| 5125 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5126 | |
| 5127 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5128 | KMP_CHECK_UPDATE(team->t.t_proc_bind, new_proc_bind); |
| 5129 | #if KMP_AFFINITY_SUPPORTED |
| 5130 | __kmp_partition_places(team); |
| 5131 | #endif |
| 5132 | #endif |
| 5133 | } // Check changes in number of threads |
| 5134 | |
| 5135 | #if OMP_40_ENABLED |
| 5136 | kmp_info_t *master = team->t.t_threads[0]; |
| 5137 | if (master->th.th_teams_microtask) { |
| 5138 | for (f = 1; f < new_nproc; ++f) { |
| 5139 | // propagate teams construct specific info to workers |
| 5140 | kmp_info_t *thr = team->t.t_threads[f]; |
| 5141 | thr->th.th_teams_microtask = master->th.th_teams_microtask; |
| 5142 | thr->th.th_teams_level = master->th.th_teams_level; |
| 5143 | thr->th.th_teams_size = master->th.th_teams_size; |
| 5144 | } |
| 5145 | } |
| 5146 | #endif /* OMP_40_ENABLED */ |
| 5147 | #if KMP_NESTED_HOT_TEAMS |
| 5148 | if (level) { |
| 5149 | // Sync barrier state for nested hot teams, not needed for outermost hot |
| 5150 | // team. |
| 5151 | for (f = 1; f < new_nproc; ++f) { |
| 5152 | kmp_info_t *thr = team->t.t_threads[f]; |
| 5153 | int b; |
| 5154 | kmp_balign_t *balign = thr->th.th_bar; |
| 5155 | for (b = 0; b < bs_last_barrier; ++b) { |
| 5156 | balign[b].bb.b_arrived = team->t.t_bar[b].b_arrived; |
| 5157 | KMP_DEBUG_ASSERT(balign[b].bb.wait_flag != KMP_BARRIER_PARENT_FLAG); |
| 5158 | #if USE_DEBUGGER |
| 5159 | balign[b].bb.b_worker_arrived = team->t.t_bar[b].b_team_arrived; |
| 5160 | #endif |
| 5161 | } |
| 5162 | } |
| 5163 | } |
| 5164 | #endif // KMP_NESTED_HOT_TEAMS |
| 5165 | |
| 5166 | /* reallocate space for arguments if necessary */ |
| 5167 | __kmp_alloc_argv_entries(argc, team, TRUE); |
| 5168 | KMP_CHECK_UPDATE(team->t.t_argc, argc); |
| 5169 | // The hot team re-uses the previous task team, |
| 5170 | // if untouched during the previous release->gather phase. |
| 5171 | |
| 5172 | KF_TRACE(10, (" hot_team = %p\n", team)); |
| 5173 | |
| 5174 | #if KMP_DEBUG |
| 5175 | if (__kmp_tasking_mode != tskm_immediate_exec) { |
| 5176 | KA_TRACE(20, ("__kmp_allocate_team: hot team task_team[0] = %p " |
| 5177 | "task_team[1] = %p after reinit\n", |
| 5178 | team->t.t_task_team[0], team->t.t_task_team[1])); |
| 5179 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5180 | #endif |
| 5181 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 5182 | #if OMPT_SUPPORT |
| 5183 | __ompt_team_assign_id(team, ompt_parallel_id); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 5184 | #endif |
| 5185 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5186 | KMP_MB(); |
| 5187 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5188 | return team; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
| 5191 | /* next, let's try to take one from the team pool */ |
| 5192 | KMP_MB(); |
Andrey Churbanov | c47afcd | 2017-07-03 11:24:08 +0000 | [diff] [blame] | 5193 | for (team = CCAST(kmp_team_t *, __kmp_team_pool); (team);) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5194 | /* TODO: consider resizing undersized teams instead of reaping them, now |
| 5195 | that we have a resizing mechanism */ |
| 5196 | if (team->t.t_max_nproc >= max_nproc) { |
| 5197 | /* take this team from the team pool */ |
| 5198 | __kmp_team_pool = team->t.t_next_pool; |
| 5199 | |
| 5200 | /* setup the team for fresh use */ |
| 5201 | __kmp_initialize_team(team, new_nproc, new_icvs, NULL); |
| 5202 | |
| 5203 | KA_TRACE(20, ("__kmp_allocate_team: setting task_team[0] %p and " |
| 5204 | "task_team[1] %p to NULL\n", |
| 5205 | &team->t.t_task_team[0], &team->t.t_task_team[1])); |
| 5206 | team->t.t_task_team[0] = NULL; |
| 5207 | team->t.t_task_team[1] = NULL; |
| 5208 | |
| 5209 | /* reallocate space for arguments if necessary */ |
| 5210 | __kmp_alloc_argv_entries(argc, team, TRUE); |
| 5211 | KMP_CHECK_UPDATE(team->t.t_argc, argc); |
| 5212 | |
| 5213 | KA_TRACE( |
| 5214 | 20, ("__kmp_allocate_team: team %d init arrived: join=%u, plain=%u\n", |
| 5215 | team->t.t_id, KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE)); |
| 5216 | { // Initialize barrier data. |
| 5217 | int b; |
| 5218 | for (b = 0; b < bs_last_barrier; ++b) { |
| 5219 | team->t.t_bar[b].b_arrived = KMP_INIT_BARRIER_STATE; |
| 5220 | #if USE_DEBUGGER |
| 5221 | team->t.t_bar[b].b_master_arrived = 0; |
| 5222 | team->t.t_bar[b].b_team_arrived = 0; |
| 5223 | #endif |
| 5224 | } |
| 5225 | } |
| 5226 | |
| 5227 | #if OMP_40_ENABLED |
| 5228 | team->t.t_proc_bind = new_proc_bind; |
| 5229 | #endif |
| 5230 | |
| 5231 | KA_TRACE(20, ("__kmp_allocate_team: using team from pool %d.\n", |
| 5232 | team->t.t_id)); |
| 5233 | |
| 5234 | #if OMPT_SUPPORT |
| 5235 | __ompt_team_assign_id(team, ompt_parallel_id); |
| 5236 | #endif |
| 5237 | |
| 5238 | KMP_MB(); |
| 5239 | |
| 5240 | return team; |
| 5241 | } |
| 5242 | |
Jonathan Peyton | 94a114f | 2017-10-20 19:30:57 +0000 | [diff] [blame] | 5243 | /* reap team if it is too small, then loop back and check the next one */ |
| 5244 | // not sure if this is wise, but, will be redone during the hot-teams |
| 5245 | // rewrite. |
| 5246 | /* TODO: Use technique to find the right size hot-team, don't reap them */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5247 | team = __kmp_reap_team(team); |
| 5248 | __kmp_team_pool = team; |
| 5249 | } |
| 5250 | |
| 5251 | /* nothing available in the pool, no matter, make a new team! */ |
| 5252 | KMP_MB(); |
| 5253 | team = (kmp_team_t *)__kmp_allocate(sizeof(kmp_team_t)); |
| 5254 | |
| 5255 | /* and set it up */ |
| 5256 | team->t.t_max_nproc = max_nproc; |
| 5257 | /* NOTE well, for some reason allocating one big buffer and dividing it up |
| 5258 | seems to really hurt performance a lot on the P4, so, let's not use this */ |
| 5259 | __kmp_allocate_team_arrays(team, max_nproc); |
| 5260 | |
| 5261 | KA_TRACE(20, ("__kmp_allocate_team: making a new team\n")); |
| 5262 | __kmp_initialize_team(team, new_nproc, new_icvs, NULL); |
| 5263 | |
| 5264 | KA_TRACE(20, ("__kmp_allocate_team: setting task_team[0] %p and task_team[1] " |
| 5265 | "%p to NULL\n", |
| 5266 | &team->t.t_task_team[0], &team->t.t_task_team[1])); |
| 5267 | team->t.t_task_team[0] = NULL; // to be removed, as __kmp_allocate zeroes |
| 5268 | // memory, no need to duplicate |
| 5269 | team->t.t_task_team[1] = NULL; // to be removed, as __kmp_allocate zeroes |
| 5270 | // memory, no need to duplicate |
| 5271 | |
| 5272 | if (__kmp_storage_map) { |
| 5273 | __kmp_print_team_storage_map("team", team, team->t.t_id, new_nproc); |
| 5274 | } |
| 5275 | |
| 5276 | /* allocate space for arguments */ |
| 5277 | __kmp_alloc_argv_entries(argc, team, FALSE); |
| 5278 | team->t.t_argc = argc; |
| 5279 | |
| 5280 | KA_TRACE(20, |
| 5281 | ("__kmp_allocate_team: team %d init arrived: join=%u, plain=%u\n", |
| 5282 | team->t.t_id, KMP_INIT_BARRIER_STATE, KMP_INIT_BARRIER_STATE)); |
| 5283 | { // Initialize barrier data. |
| 5284 | int b; |
| 5285 | for (b = 0; b < bs_last_barrier; ++b) { |
| 5286 | team->t.t_bar[b].b_arrived = KMP_INIT_BARRIER_STATE; |
| 5287 | #if USE_DEBUGGER |
| 5288 | team->t.t_bar[b].b_master_arrived = 0; |
| 5289 | team->t.t_bar[b].b_team_arrived = 0; |
| 5290 | #endif |
| 5291 | } |
| 5292 | } |
| 5293 | |
| 5294 | #if OMP_40_ENABLED |
| 5295 | team->t.t_proc_bind = new_proc_bind; |
| 5296 | #endif |
| 5297 | |
| 5298 | #if OMPT_SUPPORT |
| 5299 | __ompt_team_assign_id(team, ompt_parallel_id); |
| 5300 | team->t.ompt_serialized_team_info = NULL; |
| 5301 | #endif |
| 5302 | |
| 5303 | KMP_MB(); |
| 5304 | |
| 5305 | KA_TRACE(20, ("__kmp_allocate_team: done creating a new team %d.\n", |
| 5306 | team->t.t_id)); |
| 5307 | |
| 5308 | return team; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | /* TODO implement hot-teams at all levels */ |
| 5312 | /* TODO implement lazy thread release on demand (disband request) */ |
| 5313 | |
| 5314 | /* free the team. return it to the team pool. release all the threads |
| 5315 | * associated with it */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5316 | void __kmp_free_team(kmp_root_t *root, |
| 5317 | kmp_team_t *team USE_NESTED_HOT_ARG(kmp_info_t *master)) { |
| 5318 | int f; |
| 5319 | KA_TRACE(20, ("__kmp_free_team: T#%d freeing team %d\n", __kmp_get_gtid(), |
| 5320 | team->t.t_id)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5321 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5322 | /* verify state */ |
| 5323 | KMP_DEBUG_ASSERT(root); |
| 5324 | KMP_DEBUG_ASSERT(team); |
| 5325 | KMP_DEBUG_ASSERT(team->t.t_nproc <= team->t.t_max_nproc); |
| 5326 | KMP_DEBUG_ASSERT(team->t.t_threads); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5327 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5328 | int use_hot_team = team == root->r.r_hot_team; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 5329 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5330 | int level; |
| 5331 | kmp_hot_team_ptr_t *hot_teams; |
| 5332 | if (master) { |
| 5333 | level = team->t.t_active_level - 1; |
| 5334 | if (master->th.th_teams_microtask) { // in teams construct? |
| 5335 | if (master->th.th_teams_size.nteams > 1) { |
| 5336 | ++level; // level was not increased in teams construct for |
| 5337 | // team_of_masters |
| 5338 | } |
| 5339 | if (team->t.t_pkfn != (microtask_t)__kmp_teams_master && |
| 5340 | master->th.th_teams_level == team->t.t_level) { |
| 5341 | ++level; // level was not increased in teams construct for |
| 5342 | // team_of_workers before the parallel |
| 5343 | } // team->t.t_level will be increased inside parallel |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 5344 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5345 | hot_teams = master->th.th_hot_teams; |
| 5346 | if (level < __kmp_hot_teams_max_level) { |
| 5347 | KMP_DEBUG_ASSERT(team == hot_teams[level].hot_team); |
| 5348 | use_hot_team = 1; |
| 5349 | } |
| 5350 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 5351 | #endif // KMP_NESTED_HOT_TEAMS |
| 5352 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5353 | /* team is done working */ |
| 5354 | TCW_SYNC_PTR(team->t.t_pkfn, |
| 5355 | NULL); // Important for Debugging Support Library. |
| 5356 | team->t.t_copyin_counter = 0; // init counter for possible reuse |
| 5357 | // Do not reset pointer to parent team to NULL for hot teams. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5358 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5359 | /* if we are non-hot team, release our threads */ |
| 5360 | if (!use_hot_team) { |
| 5361 | if (__kmp_tasking_mode != tskm_immediate_exec) { |
| 5362 | // Wait for threads to reach reapable state |
| 5363 | for (f = 1; f < team->t.t_nproc; ++f) { |
| 5364 | KMP_DEBUG_ASSERT(team->t.t_threads[f]); |
| 5365 | kmp_info_t *th = team->t.t_threads[f]; |
| 5366 | volatile kmp_uint32 *state = &th->th.th_reap_state; |
| 5367 | while (*state != KMP_SAFE_TO_REAP) { |
Andrey Churbanov | 581490e | 2017-02-06 18:53:32 +0000 | [diff] [blame] | 5368 | #if KMP_OS_WINDOWS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5369 | // On Windows a thread can be killed at any time, check this |
| 5370 | DWORD ecode; |
| 5371 | if (!__kmp_is_thread_alive(th, &ecode)) { |
| 5372 | *state = KMP_SAFE_TO_REAP; // reset the flag for dead thread |
| 5373 | break; |
| 5374 | } |
Andrey Churbanov | 581490e | 2017-02-06 18:53:32 +0000 | [diff] [blame] | 5375 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5376 | // first check if thread is sleeping |
| 5377 | kmp_flag_64 fl(&th->th.th_bar[bs_forkjoin_barrier].bb.b_go, th); |
| 5378 | if (fl.is_sleeping()) |
| 5379 | fl.resume(__kmp_gtid_from_thread(th)); |
| 5380 | KMP_CPU_PAUSE(); |
| 5381 | } |
| 5382 | } |
Andrey Churbanov | 581490e | 2017-02-06 18:53:32 +0000 | [diff] [blame] | 5383 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5384 | // Delete task teams |
| 5385 | int tt_idx; |
| 5386 | for (tt_idx = 0; tt_idx < 2; ++tt_idx) { |
| 5387 | kmp_task_team_t *task_team = team->t.t_task_team[tt_idx]; |
| 5388 | if (task_team != NULL) { |
| 5389 | for (f = 0; f < team->t.t_nproc; |
| 5390 | ++f) { // Have all threads unref task teams |
| 5391 | team->t.t_threads[f]->th.th_task_team = NULL; |
| 5392 | } |
| 5393 | KA_TRACE( |
| 5394 | 20, |
| 5395 | ("__kmp_free_team: T#%d deactivating task_team %p on team %d\n", |
| 5396 | __kmp_get_gtid(), task_team, team->t.t_id)); |
Jonathan Peyton | baaccfa | 2015-11-16 22:48:41 +0000 | [diff] [blame] | 5397 | #if KMP_NESTED_HOT_TEAMS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5398 | __kmp_free_task_team(master, task_team); |
Jonathan Peyton | baaccfa | 2015-11-16 22:48:41 +0000 | [diff] [blame] | 5399 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5400 | team->t.t_task_team[tt_idx] = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5401 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5402 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5403 | } |
| 5404 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5405 | // Reset pointer to parent team only for non-hot teams. |
| 5406 | team->t.t_parent = NULL; |
| 5407 | team->t.t_level = 0; |
| 5408 | team->t.t_active_level = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5409 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5410 | /* free the worker threads */ |
| 5411 | for (f = 1; f < team->t.t_nproc; ++f) { |
| 5412 | KMP_DEBUG_ASSERT(team->t.t_threads[f]); |
| 5413 | __kmp_free_thread(team->t.t_threads[f]); |
| 5414 | team->t.t_threads[f] = NULL; |
| 5415 | } |
| 5416 | |
| 5417 | /* put the team back in the team pool */ |
| 5418 | /* TODO limit size of team pool, call reap_team if pool too large */ |
Andrey Churbanov | c47afcd | 2017-07-03 11:24:08 +0000 | [diff] [blame] | 5419 | team->t.t_next_pool = CCAST(kmp_team_t *, __kmp_team_pool); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5420 | __kmp_team_pool = (volatile kmp_team_t *)team; |
| 5421 | } |
| 5422 | |
| 5423 | KMP_MB(); |
| 5424 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5425 | |
| 5426 | /* reap the team. destroy it, reclaim all its resources and free its memory */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5427 | kmp_team_t *__kmp_reap_team(kmp_team_t *team) { |
| 5428 | kmp_team_t *next_pool = team->t.t_next_pool; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5429 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5430 | KMP_DEBUG_ASSERT(team); |
| 5431 | KMP_DEBUG_ASSERT(team->t.t_dispatch); |
| 5432 | KMP_DEBUG_ASSERT(team->t.t_disp_buffer); |
| 5433 | KMP_DEBUG_ASSERT(team->t.t_threads); |
| 5434 | KMP_DEBUG_ASSERT(team->t.t_argv); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5435 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5436 | /* TODO clean the threads that are a part of this? */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5437 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5438 | /* free stuff */ |
| 5439 | __kmp_free_team_arrays(team); |
| 5440 | if (team->t.t_argv != &team->t.t_inline_argv[0]) |
| 5441 | __kmp_free((void *)team->t.t_argv); |
| 5442 | __kmp_free(team); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5443 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5444 | KMP_MB(); |
| 5445 | return next_pool; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5448 | // Free the thread. Don't reap it, just place it on the pool of available |
| 5449 | // threads. |
| 5450 | // |
| 5451 | // Changes for Quad issue 527845: We need a predictable OMP tid <-> gtid |
| 5452 | // binding for the affinity mechanism to be useful. |
| 5453 | // |
| 5454 | // Now, we always keep the free list (__kmp_thread_pool) sorted by gtid. |
| 5455 | // However, we want to avoid a potential performance problem by always |
| 5456 | // scanning through the list to find the correct point at which to insert |
| 5457 | // the thread (potential N**2 behavior). To do this we keep track of the |
| 5458 | // last place a thread struct was inserted (__kmp_thread_pool_insert_pt). |
| 5459 | // With single-level parallelism, threads will always be added to the tail |
| 5460 | // of the list, kept track of by __kmp_thread_pool_insert_pt. With nested |
| 5461 | // parallelism, all bets are off and we may need to scan through the entire |
| 5462 | // free list. |
| 5463 | // |
| 5464 | // This change also has a potentially large performance benefit, for some |
| 5465 | // applications. Previously, as threads were freed from the hot team, they |
| 5466 | // would be placed back on the free list in inverse order. If the hot team |
| 5467 | // grew back to it's original size, then the freed thread would be placed |
| 5468 | // back on the hot team in reverse order. This could cause bad cache |
| 5469 | // locality problems on programs where the size of the hot team regularly |
| 5470 | // grew and shrunk. |
| 5471 | // |
| 5472 | // Now, for single-level parallelism, the OMP tid is alway == gtid. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5473 | void __kmp_free_thread(kmp_info_t *this_th) { |
| 5474 | int gtid; |
| 5475 | kmp_info_t **scan; |
Jonathan Peyton | f439246 | 2017-07-27 20:58:41 +0000 | [diff] [blame] | 5476 | kmp_root_t *root = this_th->th.th_root; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5477 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5478 | KA_TRACE(20, ("__kmp_free_thread: T#%d putting T#%d back on free pool.\n", |
| 5479 | __kmp_get_gtid(), this_th->th.th_info.ds.ds_gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5480 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5481 | KMP_DEBUG_ASSERT(this_th); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5482 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5483 | // When moving thread to pool, switch thread to wait on own b_go flag, and |
| 5484 | // uninitialized (NULL team). |
| 5485 | int b; |
| 5486 | kmp_balign_t *balign = this_th->th.th_bar; |
| 5487 | for (b = 0; b < bs_last_barrier; ++b) { |
| 5488 | if (balign[b].bb.wait_flag == KMP_BARRIER_PARENT_FLAG) |
| 5489 | balign[b].bb.wait_flag = KMP_BARRIER_SWITCH_TO_OWN_FLAG; |
| 5490 | balign[b].bb.team = NULL; |
| 5491 | balign[b].bb.leaf_kids = 0; |
| 5492 | } |
| 5493 | this_th->th.th_task_state = 0; |
| 5494 | |
| 5495 | /* put thread back on the free pool */ |
| 5496 | TCW_PTR(this_th->th.th_team, NULL); |
| 5497 | TCW_PTR(this_th->th.th_root, NULL); |
| 5498 | TCW_PTR(this_th->th.th_dispatch, NULL); /* NOT NEEDED */ |
| 5499 | |
| 5500 | // If the __kmp_thread_pool_insert_pt is already past the new insert |
| 5501 | // point, then we need to re-scan the entire list. |
| 5502 | gtid = this_th->th.th_info.ds.ds_gtid; |
| 5503 | if (__kmp_thread_pool_insert_pt != NULL) { |
| 5504 | KMP_DEBUG_ASSERT(__kmp_thread_pool != NULL); |
| 5505 | if (__kmp_thread_pool_insert_pt->th.th_info.ds.ds_gtid > gtid) { |
| 5506 | __kmp_thread_pool_insert_pt = NULL; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 5507 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5508 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 5509 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5510 | // Scan down the list to find the place to insert the thread. |
| 5511 | // scan is the address of a link in the list, possibly the address of |
| 5512 | // __kmp_thread_pool itself. |
| 5513 | // |
| 5514 | // In the absence of nested parallism, the for loop will have 0 iterations. |
| 5515 | if (__kmp_thread_pool_insert_pt != NULL) { |
| 5516 | scan = &(__kmp_thread_pool_insert_pt->th.th_next_pool); |
| 5517 | } else { |
Andrey Churbanov | c47afcd | 2017-07-03 11:24:08 +0000 | [diff] [blame] | 5518 | scan = CCAST(kmp_info_t **, &__kmp_thread_pool); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5519 | } |
| 5520 | for (; (*scan != NULL) && ((*scan)->th.th_info.ds.ds_gtid < gtid); |
| 5521 | scan = &((*scan)->th.th_next_pool)) |
| 5522 | ; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5523 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5524 | // Insert the new element on the list, and set __kmp_thread_pool_insert_pt |
| 5525 | // to its address. |
| 5526 | TCW_PTR(this_th->th.th_next_pool, *scan); |
| 5527 | __kmp_thread_pool_insert_pt = *scan = this_th; |
| 5528 | KMP_DEBUG_ASSERT((this_th->th.th_next_pool == NULL) || |
| 5529 | (this_th->th.th_info.ds.ds_gtid < |
| 5530 | this_th->th.th_next_pool->th.th_info.ds.ds_gtid)); |
| 5531 | TCW_4(this_th->th.th_in_pool, TRUE); |
| 5532 | __kmp_thread_pool_nth++; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5533 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5534 | TCW_4(__kmp_nth, __kmp_nth - 1); |
Jonathan Peyton | f439246 | 2017-07-27 20:58:41 +0000 | [diff] [blame] | 5535 | root->r.r_cg_nthreads--; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5536 | |
| 5537 | #ifdef KMP_ADJUST_BLOCKTIME |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5538 | /* Adjust blocktime back to user setting or default if necessary */ |
| 5539 | /* Middle initialization might never have occurred */ |
| 5540 | if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) { |
| 5541 | KMP_DEBUG_ASSERT(__kmp_avail_proc > 0); |
| 5542 | if (__kmp_nth <= __kmp_avail_proc) { |
| 5543 | __kmp_zero_bt = FALSE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5544 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5545 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5546 | #endif /* KMP_ADJUST_BLOCKTIME */ |
| 5547 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5548 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5549 | } |
| 5550 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5551 | /* ------------------------------------------------------------------------ */ |
| 5552 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5553 | void *__kmp_launch_thread(kmp_info_t *this_thr) { |
| 5554 | int gtid = this_thr->th.th_info.ds.ds_gtid; |
| 5555 | /* void *stack_data;*/ |
| 5556 | kmp_team_t *(*volatile pteam); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5557 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5558 | KMP_MB(); |
| 5559 | KA_TRACE(10, ("__kmp_launch_thread: T#%d start\n", gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5560 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5561 | if (__kmp_env_consistency_check) { |
| 5562 | this_thr->th.th_cons = __kmp_allocate_cons_stack(gtid); // ATT: Memory leak? |
| 5563 | } |
| 5564 | |
| 5565 | #if OMPT_SUPPORT |
| 5566 | if (ompt_enabled) { |
| 5567 | this_thr->th.ompt_thread_info.state = ompt_state_overhead; |
| 5568 | this_thr->th.ompt_thread_info.wait_id = 0; |
| 5569 | this_thr->th.ompt_thread_info.idle_frame = __builtin_frame_address(0); |
| 5570 | if (ompt_callbacks.ompt_callback(ompt_event_thread_begin)) { |
| 5571 | __ompt_thread_begin(ompt_thread_worker, gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5572 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5573 | } |
| 5574 | #endif |
| 5575 | |
| 5576 | /* This is the place where threads wait for work */ |
| 5577 | while (!TCR_4(__kmp_global.g.g_done)) { |
| 5578 | KMP_DEBUG_ASSERT(this_thr == __kmp_threads[gtid]); |
| 5579 | KMP_MB(); |
| 5580 | |
| 5581 | /* wait for work to do */ |
| 5582 | KA_TRACE(20, ("__kmp_launch_thread: T#%d waiting for work\n", gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5583 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 5584 | #if OMPT_SUPPORT |
Jonathan Peyton | b68a85d | 2015-09-21 18:11:22 +0000 | [diff] [blame] | 5585 | if (ompt_enabled) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5586 | this_thr->th.ompt_thread_info.state = ompt_state_idle; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 5587 | } |
| 5588 | #endif |
| 5589 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5590 | /* No tid yet since not part of a team */ |
| 5591 | __kmp_fork_barrier(gtid, KMP_GTID_DNE); |
| 5592 | |
| 5593 | #if OMPT_SUPPORT |
| 5594 | if (ompt_enabled) { |
| 5595 | this_thr->th.ompt_thread_info.state = ompt_state_overhead; |
| 5596 | } |
| 5597 | #endif |
| 5598 | |
| 5599 | pteam = (kmp_team_t * (*))(&this_thr->th.th_team); |
| 5600 | |
| 5601 | /* have we been allocated? */ |
| 5602 | if (TCR_SYNC_PTR(*pteam) && !TCR_4(__kmp_global.g.g_done)) { |
| 5603 | #if OMPT_SUPPORT |
| 5604 | ompt_task_info_t *task_info; |
| 5605 | ompt_parallel_id_t my_parallel_id; |
| 5606 | if (ompt_enabled) { |
| 5607 | task_info = __ompt_get_taskinfo(0); |
| 5608 | my_parallel_id = (*pteam)->t.ompt_team_info.parallel_id; |
| 5609 | } |
| 5610 | #endif |
| 5611 | /* we were just woken up, so run our new task */ |
| 5612 | if (TCR_SYNC_PTR((*pteam)->t.t_pkfn) != NULL) { |
| 5613 | int rc; |
| 5614 | KA_TRACE(20, |
| 5615 | ("__kmp_launch_thread: T#%d(%d:%d) invoke microtask = %p\n", |
| 5616 | gtid, (*pteam)->t.t_id, __kmp_tid_from_gtid(gtid), |
| 5617 | (*pteam)->t.t_pkfn)); |
| 5618 | |
| 5619 | updateHWFPControl(*pteam); |
| 5620 | |
| 5621 | #if OMPT_SUPPORT |
| 5622 | if (ompt_enabled) { |
| 5623 | this_thr->th.ompt_thread_info.state = ompt_state_work_parallel; |
| 5624 | // Initialize OMPT task id for implicit task. |
| 5625 | int tid = __kmp_tid_from_gtid(gtid); |
| 5626 | task_info->task_id = __ompt_task_id_new(tid); |
| 5627 | } |
| 5628 | #endif |
| 5629 | |
| 5630 | { |
| 5631 | KMP_TIME_PARTITIONED_BLOCK(OMP_parallel); |
| 5632 | KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK); |
| 5633 | rc = (*pteam)->t.t_invoke(gtid); |
| 5634 | } |
| 5635 | KMP_ASSERT(rc); |
| 5636 | |
| 5637 | #if OMPT_SUPPORT |
| 5638 | if (ompt_enabled) { |
| 5639 | /* no frame set while outside task */ |
| 5640 | task_info->frame.exit_runtime_frame = NULL; |
| 5641 | |
| 5642 | this_thr->th.ompt_thread_info.state = ompt_state_overhead; |
| 5643 | } |
| 5644 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5645 | KMP_MB(); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5646 | KA_TRACE(20, ("__kmp_launch_thread: T#%d(%d:%d) done microtask = %p\n", |
| 5647 | gtid, (*pteam)->t.t_id, __kmp_tid_from_gtid(gtid), |
| 5648 | (*pteam)->t.t_pkfn)); |
| 5649 | } |
| 5650 | /* join barrier after parallel region */ |
| 5651 | __kmp_join_barrier(gtid); |
Jonathan Peyton | b4c73d8 | 2016-01-26 21:45:21 +0000 | [diff] [blame] | 5652 | #if OMPT_SUPPORT && OMPT_TRACE |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5653 | if (ompt_enabled) { |
| 5654 | if (ompt_callbacks.ompt_callback(ompt_event_implicit_task_end)) { |
| 5655 | // don't access *pteam here: it may have already been freed |
| 5656 | // by the master thread behind the barrier (possible race) |
| 5657 | ompt_callbacks.ompt_callback(ompt_event_implicit_task_end)( |
| 5658 | my_parallel_id, task_info->task_id); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5659 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5660 | task_info->frame.exit_runtime_frame = NULL; |
| 5661 | task_info->task_id = 0; |
| 5662 | } |
| 5663 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5664 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5665 | } |
| 5666 | TCR_SYNC_PTR((intptr_t)__kmp_global.g.g_done); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5667 | |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 5668 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5669 | if (ompt_enabled && ompt_callbacks.ompt_callback(ompt_event_thread_end)) { |
| 5670 | __ompt_thread_end(ompt_thread_worker, gtid); |
| 5671 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 5672 | #endif |
| 5673 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5674 | this_thr->th.th_task_team = NULL; |
| 5675 | /* run the destructors for the threadprivate data for this thread */ |
| 5676 | __kmp_common_destroy_gtid(gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5677 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5678 | KA_TRACE(10, ("__kmp_launch_thread: T#%d done\n", gtid)); |
| 5679 | KMP_MB(); |
| 5680 | return this_thr; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5681 | } |
| 5682 | |
| 5683 | /* ------------------------------------------------------------------------ */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5684 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5685 | void __kmp_internal_end_dest(void *specific_gtid) { |
| 5686 | #if KMP_COMPILER_ICC |
| 5687 | #pragma warning(push) |
| 5688 | #pragma warning(disable : 810) // conversion from "void *" to "int" may lose |
| 5689 | // significant bits |
| 5690 | #endif |
| 5691 | // Make sure no significant bits are lost |
| 5692 | int gtid = (kmp_intptr_t)specific_gtid - 1; |
| 5693 | #if KMP_COMPILER_ICC |
| 5694 | #pragma warning(pop) |
| 5695 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5696 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5697 | KA_TRACE(30, ("__kmp_internal_end_dest: T#%d\n", gtid)); |
| 5698 | /* NOTE: the gtid is stored as gitd+1 in the thread-local-storage |
| 5699 | * this is because 0 is reserved for the nothing-stored case */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5700 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5701 | /* josh: One reason for setting the gtid specific data even when it is being |
| 5702 | destroyed by pthread is to allow gtid lookup through thread specific data |
| 5703 | (__kmp_gtid_get_specific). Some of the code, especially stat code, |
| 5704 | that gets executed in the call to __kmp_internal_end_thread, actually |
| 5705 | gets the gtid through the thread specific data. Setting it here seems |
| 5706 | rather inelegant and perhaps wrong, but allows __kmp_internal_end_thread |
| 5707 | to run smoothly. |
| 5708 | todo: get rid of this after we remove the dependence on |
| 5709 | __kmp_gtid_get_specific */ |
| 5710 | if (gtid >= 0 && KMP_UBER_GTID(gtid)) |
| 5711 | __kmp_gtid_set_specific(gtid); |
| 5712 | #ifdef KMP_TDATA_GTID |
| 5713 | __kmp_gtid = gtid; |
| 5714 | #endif |
| 5715 | __kmp_internal_end_thread(gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5716 | } |
| 5717 | |
Jonathan Peyton | 9901699 | 2015-05-26 17:32:53 +0000 | [diff] [blame] | 5718 | #if KMP_OS_UNIX && KMP_DYNAMIC_LIB |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5719 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5720 | // 2009-09-08 (lev): It looks the destructor does not work. In simple test cases |
| 5721 | // destructors work perfectly, but in real libomp.so I have no evidence it is |
| 5722 | // ever called. However, -fini linker option in makefile.mk works fine. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5723 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5724 | __attribute__((destructor)) void __kmp_internal_end_dtor(void) { |
| 5725 | __kmp_internal_end_atexit(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5726 | } |
| 5727 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5728 | void __kmp_internal_end_fini(void) { __kmp_internal_end_atexit(); } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5729 | |
| 5730 | #endif |
| 5731 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5732 | /* [Windows] josh: when the atexit handler is called, there may still be more |
| 5733 | than one thread alive */ |
| 5734 | void __kmp_internal_end_atexit(void) { |
| 5735 | KA_TRACE(30, ("__kmp_internal_end_atexit\n")); |
| 5736 | /* [Windows] |
| 5737 | josh: ideally, we want to completely shutdown the library in this atexit |
| 5738 | handler, but stat code that depends on thread specific data for gtid fails |
| 5739 | because that data becomes unavailable at some point during the shutdown, so |
| 5740 | we call __kmp_internal_end_thread instead. We should eventually remove the |
| 5741 | dependency on __kmp_get_specific_gtid in the stat code and use |
| 5742 | __kmp_internal_end_library to cleanly shutdown the library. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5743 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5744 | // TODO: Can some of this comment about GVS be removed? |
| 5745 | I suspect that the offending stat code is executed when the calling thread |
| 5746 | tries to clean up a dead root thread's data structures, resulting in GVS |
| 5747 | code trying to close the GVS structures for that thread, but since the stat |
| 5748 | code uses __kmp_get_specific_gtid to get the gtid with the assumption that |
| 5749 | the calling thread is cleaning up itself instead of another thread, it get |
| 5750 | confused. This happens because allowing a thread to unregister and cleanup |
| 5751 | another thread is a recent modification for addressing an issue. |
| 5752 | Based on the current design (20050722), a thread may end up |
| 5753 | trying to unregister another thread only if thread death does not trigger |
| 5754 | the calling of __kmp_internal_end_thread. For Linux* OS, there is the |
| 5755 | thread specific data destructor function to detect thread death. For |
| 5756 | Windows dynamic, there is DllMain(THREAD_DETACH). For Windows static, there |
| 5757 | is nothing. Thus, the workaround is applicable only for Windows static |
| 5758 | stat library. */ |
| 5759 | __kmp_internal_end_library(-1); |
| 5760 | #if KMP_OS_WINDOWS |
| 5761 | __kmp_close_console(); |
| 5762 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5765 | static void __kmp_reap_thread(kmp_info_t *thread, int is_root) { |
| 5766 | // It is assumed __kmp_forkjoin_lock is acquired. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5767 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5768 | int gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5769 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5770 | KMP_DEBUG_ASSERT(thread != NULL); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5771 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5772 | gtid = thread->th.th_info.ds.ds_gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5773 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5774 | if (!is_root) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5775 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5776 | if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) { |
| 5777 | /* Assume the threads are at the fork barrier here */ |
| 5778 | KA_TRACE( |
| 5779 | 20, ("__kmp_reap_thread: releasing T#%d from fork barrier for reap\n", |
| 5780 | gtid)); |
| 5781 | /* Need release fence here to prevent seg faults for tree forkjoin barrier |
| 5782 | * (GEH) */ |
| 5783 | ANNOTATE_HAPPENS_BEFORE(thread); |
| 5784 | kmp_flag_64 flag(&thread->th.th_bar[bs_forkjoin_barrier].bb.b_go, thread); |
| 5785 | __kmp_release_64(&flag); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 5786 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5787 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5788 | // Terminate OS thread. |
| 5789 | __kmp_reap_worker(thread); |
Jonathan Peyton | 7ca7ef0 | 2016-11-21 16:18:57 +0000 | [diff] [blame] | 5790 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5791 | // The thread was killed asynchronously. If it was actively |
| 5792 | // spinning in the thread pool, decrement the global count. |
| 5793 | // |
| 5794 | // There is a small timing hole here - if the worker thread was just waking |
| 5795 | // up after sleeping in the pool, had reset it's th_active_in_pool flag but |
| 5796 | // not decremented the global counter __kmp_thread_pool_active_nth yet, then |
| 5797 | // the global counter might not get updated. |
| 5798 | // |
| 5799 | // Currently, this can only happen as the library is unloaded, |
| 5800 | // so there are no harmful side effects. |
| 5801 | if (thread->th.th_active_in_pool) { |
| 5802 | thread->th.th_active_in_pool = FALSE; |
Andrey Churbanov | 5ba90c7 | 2017-07-17 09:03:14 +0000 | [diff] [blame] | 5803 | KMP_TEST_THEN_DEC32(&__kmp_thread_pool_active_nth); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5804 | KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0); |
| 5805 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5806 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5807 | // Decrement # of [worker] threads in the pool. |
| 5808 | KMP_DEBUG_ASSERT(__kmp_thread_pool_nth > 0); |
| 5809 | --__kmp_thread_pool_nth; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 5810 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5811 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5812 | __kmp_free_implicit_task(thread); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5813 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5814 | // Free the fast memory for tasking |
| 5815 | #if USE_FAST_MEMORY |
| 5816 | __kmp_free_fast_memory(thread); |
| 5817 | #endif /* USE_FAST_MEMORY */ |
| 5818 | |
| 5819 | __kmp_suspend_uninitialize_thread(thread); |
| 5820 | |
| 5821 | KMP_DEBUG_ASSERT(__kmp_threads[gtid] == thread); |
| 5822 | TCW_SYNC_PTR(__kmp_threads[gtid], NULL); |
| 5823 | |
| 5824 | --__kmp_all_nth; |
| 5825 | // __kmp_nth was decremented when thread is added to the pool. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5826 | |
| 5827 | #ifdef KMP_ADJUST_BLOCKTIME |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5828 | /* Adjust blocktime back to user setting or default if necessary */ |
| 5829 | /* Middle initialization might never have occurred */ |
| 5830 | if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) { |
| 5831 | KMP_DEBUG_ASSERT(__kmp_avail_proc > 0); |
| 5832 | if (__kmp_nth <= __kmp_avail_proc) { |
| 5833 | __kmp_zero_bt = FALSE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5834 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5835 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5836 | #endif /* KMP_ADJUST_BLOCKTIME */ |
| 5837 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5838 | /* free the memory being used */ |
| 5839 | if (__kmp_env_consistency_check) { |
| 5840 | if (thread->th.th_cons) { |
| 5841 | __kmp_free_cons_stack(thread->th.th_cons); |
| 5842 | thread->th.th_cons = NULL; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 5843 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5844 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5845 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5846 | if (thread->th.th_pri_common != NULL) { |
| 5847 | __kmp_free(thread->th.th_pri_common); |
| 5848 | thread->th.th_pri_common = NULL; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 5849 | } |
Andrey Churbanov | 6d224db | 2015-02-10 18:37:43 +0000 | [diff] [blame] | 5850 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5851 | if (thread->th.th_task_state_memo_stack != NULL) { |
| 5852 | __kmp_free(thread->th.th_task_state_memo_stack); |
| 5853 | thread->th.th_task_state_memo_stack = NULL; |
| 5854 | } |
| 5855 | |
| 5856 | #if KMP_USE_BGET |
| 5857 | if (thread->th.th_local.bget_data != NULL) { |
| 5858 | __kmp_finalize_bget(thread); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 5859 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5860 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5861 | |
Alp Toker | 98758b0 | 2014-03-02 04:12:06 +0000 | [diff] [blame] | 5862 | #if KMP_AFFINITY_SUPPORTED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5863 | if (thread->th.th_affin_mask != NULL) { |
| 5864 | KMP_CPU_FREE(thread->th.th_affin_mask); |
| 5865 | thread->th.th_affin_mask = NULL; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 5866 | } |
Alp Toker | 98758b0 | 2014-03-02 04:12:06 +0000 | [diff] [blame] | 5867 | #endif /* KMP_AFFINITY_SUPPORTED */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5868 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5869 | __kmp_reap_team(thread->th.th_serial_team); |
| 5870 | thread->th.th_serial_team = NULL; |
| 5871 | __kmp_free(thread); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5872 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5873 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5874 | |
| 5875 | } // __kmp_reap_thread |
| 5876 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5877 | static void __kmp_internal_end(void) { |
| 5878 | int i; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5879 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5880 | /* First, unregister the library */ |
| 5881 | __kmp_unregister_library(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5882 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5883 | #if KMP_OS_WINDOWS |
| 5884 | /* In Win static library, we can't tell when a root actually dies, so we |
| 5885 | reclaim the data structures for any root threads that have died but not |
| 5886 | unregistered themselves, in order to shut down cleanly. |
| 5887 | In Win dynamic library we also can't tell when a thread dies. */ |
| 5888 | __kmp_reclaim_dead_roots(); // AC: moved here to always clean resources of |
| 5889 | // dead roots |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 5890 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5891 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5892 | for (i = 0; i < __kmp_threads_capacity; i++) |
| 5893 | if (__kmp_root[i]) |
| 5894 | if (__kmp_root[i]->r.r_active) |
| 5895 | break; |
| 5896 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 5897 | TCW_SYNC_4(__kmp_global.g.g_done, TRUE); |
| 5898 | |
| 5899 | if (i < __kmp_threads_capacity) { |
| 5900 | #if KMP_USE_MONITOR |
| 5901 | // 2009-09-08 (lev): Other alive roots found. Why do we kill the monitor?? |
| 5902 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 5903 | |
Jonathan Peyton | 94a114f | 2017-10-20 19:30:57 +0000 | [diff] [blame] | 5904 | // Need to check that monitor was initialized before reaping it. If we are |
| 5905 | // called form __kmp_atfork_child (which sets __kmp_init_parallel = 0), then |
| 5906 | // __kmp_monitor will appear to contain valid data, but it is only valid in |
| 5907 | // the parent process, not the child. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5908 | // New behavior (201008): instead of keying off of the flag |
| 5909 | // __kmp_init_parallel, the monitor thread creation is keyed off |
| 5910 | // of the new flag __kmp_init_monitor. |
| 5911 | __kmp_acquire_bootstrap_lock(&__kmp_monitor_lock); |
| 5912 | if (TCR_4(__kmp_init_monitor)) { |
| 5913 | __kmp_reap_monitor(&__kmp_monitor); |
| 5914 | TCW_4(__kmp_init_monitor, 0); |
| 5915 | } |
| 5916 | __kmp_release_bootstrap_lock(&__kmp_monitor_lock); |
| 5917 | KA_TRACE(10, ("__kmp_internal_end: monitor reaped\n")); |
| 5918 | #endif // KMP_USE_MONITOR |
| 5919 | } else { |
| 5920 | /* TODO move this to cleanup code */ |
| 5921 | #ifdef KMP_DEBUG |
| 5922 | /* make sure that everything has properly ended */ |
| 5923 | for (i = 0; i < __kmp_threads_capacity; i++) { |
| 5924 | if (__kmp_root[i]) { |
| 5925 | // KMP_ASSERT( ! KMP_UBER_GTID( i ) ); // AC: |
| 5926 | // there can be uber threads alive here |
| 5927 | KMP_ASSERT(!__kmp_root[i]->r.r_active); // TODO: can they be active? |
| 5928 | } |
| 5929 | } |
| 5930 | #endif |
| 5931 | |
| 5932 | KMP_MB(); |
| 5933 | |
| 5934 | // Reap the worker threads. |
| 5935 | // This is valid for now, but be careful if threads are reaped sooner. |
| 5936 | while (__kmp_thread_pool != NULL) { // Loop thru all the thread in the pool. |
| 5937 | // Get the next thread from the pool. |
Andrey Churbanov | c47afcd | 2017-07-03 11:24:08 +0000 | [diff] [blame] | 5938 | kmp_info_t *thread = CCAST(kmp_info_t *, __kmp_thread_pool); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5939 | __kmp_thread_pool = thread->th.th_next_pool; |
| 5940 | // Reap it. |
| 5941 | KMP_DEBUG_ASSERT(thread->th.th_reap_state == KMP_SAFE_TO_REAP); |
| 5942 | thread->th.th_next_pool = NULL; |
| 5943 | thread->th.th_in_pool = FALSE; |
| 5944 | __kmp_reap_thread(thread, 0); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 5945 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5946 | __kmp_thread_pool_insert_pt = NULL; |
| 5947 | |
| 5948 | // Reap teams. |
| 5949 | while (__kmp_team_pool != NULL) { // Loop thru all the teams in the pool. |
| 5950 | // Get the next team from the pool. |
Andrey Churbanov | c47afcd | 2017-07-03 11:24:08 +0000 | [diff] [blame] | 5951 | kmp_team_t *team = CCAST(kmp_team_t *, __kmp_team_pool); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5952 | __kmp_team_pool = team->t.t_next_pool; |
| 5953 | // Reap it. |
| 5954 | team->t.t_next_pool = NULL; |
| 5955 | __kmp_reap_team(team); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 5956 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5957 | |
| 5958 | __kmp_reap_task_teams(); |
| 5959 | |
| 5960 | for (i = 0; i < __kmp_threads_capacity; ++i) { |
| 5961 | // TBD: Add some checking... |
| 5962 | // Something like KMP_DEBUG_ASSERT( __kmp_thread[ i ] == NULL ); |
| 5963 | } |
| 5964 | |
| 5965 | /* Make sure all threadprivate destructors get run by joining with all |
| 5966 | worker threads before resetting this flag */ |
| 5967 | TCW_SYNC_4(__kmp_init_common, FALSE); |
| 5968 | |
| 5969 | KA_TRACE(10, ("__kmp_internal_end: all workers reaped\n")); |
| 5970 | KMP_MB(); |
| 5971 | |
| 5972 | #if KMP_USE_MONITOR |
| 5973 | // See note above: One of the possible fixes for CQ138434 / CQ140126 |
| 5974 | // |
| 5975 | // FIXME: push both code fragments down and CSE them? |
| 5976 | // push them into __kmp_cleanup() ? |
| 5977 | __kmp_acquire_bootstrap_lock(&__kmp_monitor_lock); |
| 5978 | if (TCR_4(__kmp_init_monitor)) { |
| 5979 | __kmp_reap_monitor(&__kmp_monitor); |
| 5980 | TCW_4(__kmp_init_monitor, 0); |
| 5981 | } |
| 5982 | __kmp_release_bootstrap_lock(&__kmp_monitor_lock); |
| 5983 | KA_TRACE(10, ("__kmp_internal_end: monitor reaped\n")); |
| 5984 | #endif |
| 5985 | } /* else !__kmp_global.t_active */ |
| 5986 | TCW_4(__kmp_init_gtid, FALSE); |
| 5987 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 5988 | |
| 5989 | __kmp_cleanup(); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 5990 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5991 | ompt_fini(); |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 5992 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5993 | } |
| 5994 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 5995 | void __kmp_internal_end_library(int gtid_req) { |
| 5996 | /* if we have already cleaned up, don't try again, it wouldn't be pretty */ |
| 5997 | /* this shouldn't be a race condition because __kmp_internal_end() is the |
| 5998 | only place to clear __kmp_serial_init */ |
| 5999 | /* we'll check this later too, after we get the lock */ |
| 6000 | // 2009-09-06: We do not set g_abort without setting g_done. This check looks |
| 6001 | // redundaant, because the next check will work in any case. |
| 6002 | if (__kmp_global.g.g_abort) { |
| 6003 | KA_TRACE(11, ("__kmp_internal_end_library: abort, exiting\n")); |
| 6004 | /* TODO abort? */ |
| 6005 | return; |
| 6006 | } |
| 6007 | if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) { |
| 6008 | KA_TRACE(10, ("__kmp_internal_end_library: already finished\n")); |
| 6009 | return; |
| 6010 | } |
| 6011 | |
| 6012 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 6013 | |
| 6014 | /* find out who we are and what we should do */ |
| 6015 | { |
| 6016 | int gtid = (gtid_req >= 0) ? gtid_req : __kmp_gtid_get_specific(); |
| 6017 | KA_TRACE( |
| 6018 | 10, ("__kmp_internal_end_library: enter T#%d (%d)\n", gtid, gtid_req)); |
| 6019 | if (gtid == KMP_GTID_SHUTDOWN) { |
| 6020 | KA_TRACE(10, ("__kmp_internal_end_library: !__kmp_init_runtime, system " |
| 6021 | "already shutdown\n")); |
| 6022 | return; |
| 6023 | } else if (gtid == KMP_GTID_MONITOR) { |
| 6024 | KA_TRACE(10, ("__kmp_internal_end_library: monitor thread, gtid not " |
| 6025 | "registered, or system shutdown\n")); |
| 6026 | return; |
| 6027 | } else if (gtid == KMP_GTID_DNE) { |
| 6028 | KA_TRACE(10, ("__kmp_internal_end_library: gtid not registered or system " |
| 6029 | "shutdown\n")); |
| 6030 | /* we don't know who we are, but we may still shutdown the library */ |
| 6031 | } else if (KMP_UBER_GTID(gtid)) { |
| 6032 | /* unregister ourselves as an uber thread. gtid is no longer valid */ |
| 6033 | if (__kmp_root[gtid]->r.r_active) { |
| 6034 | __kmp_global.g.g_abort = -1; |
| 6035 | TCW_SYNC_4(__kmp_global.g.g_done, TRUE); |
| 6036 | KA_TRACE(10, |
| 6037 | ("__kmp_internal_end_library: root still active, abort T#%d\n", |
| 6038 | gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6039 | return; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6040 | } else { |
| 6041 | KA_TRACE( |
| 6042 | 10, |
| 6043 | ("__kmp_internal_end_library: unregistering sibling T#%d\n", gtid)); |
| 6044 | __kmp_unregister_root_current_thread(gtid); |
| 6045 | } |
| 6046 | } else { |
| 6047 | /* worker threads may call this function through the atexit handler, if they |
| 6048 | * call exit() */ |
| 6049 | /* For now, skip the usual subsequent processing and just dump the debug buffer. |
| 6050 | TODO: do a thorough shutdown instead */ |
| 6051 | #ifdef DUMP_DEBUG_ON_EXIT |
| 6052 | if (__kmp_debug_buf) |
| 6053 | __kmp_dump_debug_buffer(); |
| 6054 | #endif |
| 6055 | return; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6056 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6057 | } |
| 6058 | /* synchronize the termination process */ |
| 6059 | __kmp_acquire_bootstrap_lock(&__kmp_initz_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6060 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6061 | /* have we already finished */ |
| 6062 | if (__kmp_global.g.g_abort) { |
| 6063 | KA_TRACE(10, ("__kmp_internal_end_library: abort, exiting\n")); |
| 6064 | /* TODO abort? */ |
| 6065 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
| 6066 | return; |
| 6067 | } |
| 6068 | if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) { |
| 6069 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
| 6070 | return; |
| 6071 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6072 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6073 | /* We need this lock to enforce mutex between this reading of |
| 6074 | __kmp_threads_capacity and the writing by __kmp_register_root. |
| 6075 | Alternatively, we can use a counter of roots that is atomically updated by |
| 6076 | __kmp_get_global_thread_id_reg, __kmp_do_serial_initialize and |
| 6077 | __kmp_internal_end_*. */ |
| 6078 | __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6079 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6080 | /* now we can safely conduct the actual termination */ |
| 6081 | __kmp_internal_end(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6082 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6083 | __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); |
| 6084 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6085 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6086 | KA_TRACE(10, ("__kmp_internal_end_library: exit\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6087 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6088 | #ifdef DUMP_DEBUG_ON_EXIT |
| 6089 | if (__kmp_debug_buf) |
| 6090 | __kmp_dump_debug_buffer(); |
| 6091 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6092 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6093 | #if KMP_OS_WINDOWS |
| 6094 | __kmp_close_console(); |
| 6095 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6096 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6097 | __kmp_fini_allocator(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6098 | |
| 6099 | } // __kmp_internal_end_library |
| 6100 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6101 | void __kmp_internal_end_thread(int gtid_req) { |
| 6102 | int i; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6103 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6104 | /* if we have already cleaned up, don't try again, it wouldn't be pretty */ |
| 6105 | /* this shouldn't be a race condition because __kmp_internal_end() is the |
| 6106 | * only place to clear __kmp_serial_init */ |
| 6107 | /* we'll check this later too, after we get the lock */ |
| 6108 | // 2009-09-06: We do not set g_abort without setting g_done. This check looks |
| 6109 | // redundant, because the next check will work in any case. |
| 6110 | if (__kmp_global.g.g_abort) { |
| 6111 | KA_TRACE(11, ("__kmp_internal_end_thread: abort, exiting\n")); |
| 6112 | /* TODO abort? */ |
| 6113 | return; |
| 6114 | } |
| 6115 | if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) { |
| 6116 | KA_TRACE(10, ("__kmp_internal_end_thread: already finished\n")); |
| 6117 | return; |
| 6118 | } |
| 6119 | |
| 6120 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 6121 | |
| 6122 | /* find out who we are and what we should do */ |
| 6123 | { |
| 6124 | int gtid = (gtid_req >= 0) ? gtid_req : __kmp_gtid_get_specific(); |
| 6125 | KA_TRACE(10, |
| 6126 | ("__kmp_internal_end_thread: enter T#%d (%d)\n", gtid, gtid_req)); |
| 6127 | if (gtid == KMP_GTID_SHUTDOWN) { |
| 6128 | KA_TRACE(10, ("__kmp_internal_end_thread: !__kmp_init_runtime, system " |
| 6129 | "already shutdown\n")); |
| 6130 | return; |
| 6131 | } else if (gtid == KMP_GTID_MONITOR) { |
| 6132 | KA_TRACE(10, ("__kmp_internal_end_thread: monitor thread, gtid not " |
| 6133 | "registered, or system shutdown\n")); |
| 6134 | return; |
| 6135 | } else if (gtid == KMP_GTID_DNE) { |
| 6136 | KA_TRACE(10, ("__kmp_internal_end_thread: gtid not registered or system " |
| 6137 | "shutdown\n")); |
| 6138 | return; |
| 6139 | /* we don't know who we are */ |
| 6140 | } else if (KMP_UBER_GTID(gtid)) { |
| 6141 | /* unregister ourselves as an uber thread. gtid is no longer valid */ |
| 6142 | if (__kmp_root[gtid]->r.r_active) { |
| 6143 | __kmp_global.g.g_abort = -1; |
| 6144 | TCW_SYNC_4(__kmp_global.g.g_done, TRUE); |
| 6145 | KA_TRACE(10, |
| 6146 | ("__kmp_internal_end_thread: root still active, abort T#%d\n", |
| 6147 | gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6148 | return; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6149 | } else { |
| 6150 | KA_TRACE(10, ("__kmp_internal_end_thread: unregistering sibling T#%d\n", |
| 6151 | gtid)); |
| 6152 | __kmp_unregister_root_current_thread(gtid); |
| 6153 | } |
| 6154 | } else { |
| 6155 | /* just a worker thread, let's leave */ |
| 6156 | KA_TRACE(10, ("__kmp_internal_end_thread: worker thread T#%d\n", gtid)); |
| 6157 | |
| 6158 | if (gtid >= 0) { |
| 6159 | __kmp_threads[gtid]->th.th_task_team = NULL; |
| 6160 | } |
| 6161 | |
| 6162 | KA_TRACE(10, |
| 6163 | ("__kmp_internal_end_thread: worker thread done, exiting T#%d\n", |
| 6164 | gtid)); |
| 6165 | return; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6166 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6167 | } |
| 6168 | #if defined KMP_DYNAMIC_LIB |
| 6169 | // AC: lets not shutdown the Linux* OS dynamic library at the exit of uber |
| 6170 | // thread, because we will better shutdown later in the library destructor. |
| 6171 | // The reason of this change is performance problem when non-openmp thread in |
| 6172 | // a loop forks and joins many openmp threads. We can save a lot of time |
| 6173 | // keeping worker threads alive until the program shutdown. |
| 6174 | // OM: Removed Linux* OS restriction to fix the crash on OS X* (DPD200239966) |
| 6175 | // and Windows(DPD200287443) that occurs when using critical sections from |
| 6176 | // foreign threads. |
| 6177 | KA_TRACE(10, ("__kmp_internal_end_thread: exiting T#%d\n", gtid_req)); |
| 6178 | return; |
| 6179 | #endif |
| 6180 | /* synchronize the termination process */ |
| 6181 | __kmp_acquire_bootstrap_lock(&__kmp_initz_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6182 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6183 | /* have we already finished */ |
| 6184 | if (__kmp_global.g.g_abort) { |
| 6185 | KA_TRACE(10, ("__kmp_internal_end_thread: abort, exiting\n")); |
| 6186 | /* TODO abort? */ |
| 6187 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
| 6188 | return; |
| 6189 | } |
| 6190 | if (TCR_4(__kmp_global.g.g_done) || !__kmp_init_serial) { |
| 6191 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
| 6192 | return; |
| 6193 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6194 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6195 | /* We need this lock to enforce mutex between this reading of |
| 6196 | __kmp_threads_capacity and the writing by __kmp_register_root. |
| 6197 | Alternatively, we can use a counter of roots that is atomically updated by |
| 6198 | __kmp_get_global_thread_id_reg, __kmp_do_serial_initialize and |
| 6199 | __kmp_internal_end_*. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6200 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6201 | /* should we finish the run-time? are all siblings done? */ |
| 6202 | __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6203 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6204 | for (i = 0; i < __kmp_threads_capacity; ++i) { |
| 6205 | if (KMP_UBER_GTID(i)) { |
| 6206 | KA_TRACE( |
| 6207 | 10, |
| 6208 | ("__kmp_internal_end_thread: remaining sibling task: gtid==%d\n", i)); |
| 6209 | __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); |
| 6210 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
| 6211 | return; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 6212 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6213 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6214 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6215 | /* now we can safely conduct the actual termination */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6216 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6217 | __kmp_internal_end(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6218 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6219 | __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); |
| 6220 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6221 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6222 | KA_TRACE(10, ("__kmp_internal_end_thread: exit T#%d\n", gtid_req)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6223 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6224 | #ifdef DUMP_DEBUG_ON_EXIT |
| 6225 | if (__kmp_debug_buf) |
| 6226 | __kmp_dump_debug_buffer(); |
| 6227 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6228 | } // __kmp_internal_end_thread |
| 6229 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6230 | // ----------------------------------------------------------------------------- |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6231 | // Library registration stuff. |
| 6232 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6233 | static long __kmp_registration_flag = 0; |
| 6234 | // Random value used to indicate library initialization. |
| 6235 | static char *__kmp_registration_str = NULL; |
| 6236 | // Value to be saved in env var __KMP_REGISTERED_LIB_<pid>. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6237 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6238 | static inline char *__kmp_reg_status_name() { |
| 6239 | /* On RHEL 3u5 if linked statically, getpid() returns different values in |
| 6240 | each thread. If registration and unregistration go in different threads |
| 6241 | (omp_misc_other_root_exit.cpp test case), the name of registered_lib_env |
| 6242 | env var can not be found, because the name will contain different pid. */ |
| 6243 | return __kmp_str_format("__KMP_REGISTERED_LIB_%d", (int)getpid()); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6244 | } // __kmp_reg_status_get |
| 6245 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6246 | void __kmp_register_library_startup(void) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6247 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6248 | char *name = __kmp_reg_status_name(); // Name of the environment variable. |
| 6249 | int done = 0; |
| 6250 | union { |
| 6251 | double dtime; |
| 6252 | long ltime; |
| 6253 | } time; |
| 6254 | #if KMP_ARCH_X86 || KMP_ARCH_X86_64 |
| 6255 | __kmp_initialize_system_tick(); |
| 6256 | #endif |
| 6257 | __kmp_read_system_time(&time.dtime); |
| 6258 | __kmp_registration_flag = 0xCAFE0000L | (time.ltime & 0x0000FFFFL); |
| 6259 | __kmp_registration_str = |
| 6260 | __kmp_str_format("%p-%lx-%s", &__kmp_registration_flag, |
| 6261 | __kmp_registration_flag, KMP_LIBRARY_FILE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6262 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6263 | KA_TRACE(50, ("__kmp_register_library_startup: %s=\"%s\"\n", name, |
| 6264 | __kmp_registration_str)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6265 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6266 | while (!done) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6267 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6268 | char *value = NULL; // Actual value of the environment variable. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6269 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6270 | // Set environment variable, but do not overwrite if it is exist. |
| 6271 | __kmp_env_set(name, __kmp_registration_str, 0); |
| 6272 | // Check the variable is written. |
| 6273 | value = __kmp_env_get(name); |
| 6274 | if (value != NULL && strcmp(value, __kmp_registration_str) == 0) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6275 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6276 | done = 1; // Ok, environment variable set successfully, exit the loop. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6277 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6278 | } else { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6279 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6280 | // Oops. Write failed. Another copy of OpenMP RTL is in memory. |
| 6281 | // Check whether it alive or dead. |
| 6282 | int neighbor = 0; // 0 -- unknown status, 1 -- alive, 2 -- dead. |
| 6283 | char *tail = value; |
| 6284 | char *flag_addr_str = NULL; |
| 6285 | char *flag_val_str = NULL; |
| 6286 | char const *file_name = NULL; |
| 6287 | __kmp_str_split(tail, '-', &flag_addr_str, &tail); |
| 6288 | __kmp_str_split(tail, '-', &flag_val_str, &tail); |
| 6289 | file_name = tail; |
| 6290 | if (tail != NULL) { |
| 6291 | long *flag_addr = 0; |
| 6292 | long flag_val = 0; |
| 6293 | KMP_SSCANF(flag_addr_str, "%p", &flag_addr); |
| 6294 | KMP_SSCANF(flag_val_str, "%lx", &flag_val); |
| 6295 | if (flag_addr != 0 && flag_val != 0 && strcmp(file_name, "") != 0) { |
| 6296 | // First, check whether environment-encoded address is mapped into |
| 6297 | // addr space. |
| 6298 | // If so, dereference it to see if it still has the right value. |
| 6299 | if (__kmp_is_address_mapped(flag_addr) && *flag_addr == flag_val) { |
| 6300 | neighbor = 1; |
| 6301 | } else { |
| 6302 | // If not, then we know the other copy of the library is no longer |
| 6303 | // running. |
| 6304 | neighbor = 2; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 6305 | } |
| 6306 | } |
| 6307 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6308 | switch (neighbor) { |
| 6309 | case 0: // Cannot parse environment variable -- neighbor status unknown. |
| 6310 | // Assume it is the incompatible format of future version of the |
| 6311 | // library. Assume the other library is alive. |
| 6312 | // WARN( ... ); // TODO: Issue a warning. |
| 6313 | file_name = "unknown library"; |
| 6314 | // Attention! Falling to the next case. That's intentional. |
| 6315 | case 1: { // Neighbor is alive. |
| 6316 | // Check it is allowed. |
| 6317 | char *duplicate_ok = __kmp_env_get("KMP_DUPLICATE_LIB_OK"); |
| 6318 | if (!__kmp_str_match_true(duplicate_ok)) { |
| 6319 | // That's not allowed. Issue fatal error. |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 6320 | __kmp_fatal(KMP_MSG(DuplicateLibrary, KMP_LIBRARY_FILE, file_name), |
| 6321 | KMP_HNT(DuplicateLibrary), __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 6322 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6323 | KMP_INTERNAL_FREE(duplicate_ok); |
| 6324 | __kmp_duplicate_library_ok = 1; |
| 6325 | done = 1; // Exit the loop. |
| 6326 | } break; |
| 6327 | case 2: { // Neighbor is dead. |
| 6328 | // Clear the variable and try to register library again. |
| 6329 | __kmp_env_unset(name); |
| 6330 | } break; |
| 6331 | default: { KMP_DEBUG_ASSERT(0); } break; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 6332 | } |
| 6333 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6334 | KMP_INTERNAL_FREE((void *)value); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 6335 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6336 | KMP_INTERNAL_FREE((void *)name); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6337 | |
| 6338 | } // func __kmp_register_library_startup |
| 6339 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6340 | void __kmp_unregister_library(void) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6341 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6342 | char *name = __kmp_reg_status_name(); |
| 6343 | char *value = __kmp_env_get(name); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6344 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6345 | KMP_DEBUG_ASSERT(__kmp_registration_flag != 0); |
| 6346 | KMP_DEBUG_ASSERT(__kmp_registration_str != NULL); |
| 6347 | if (value != NULL && strcmp(value, __kmp_registration_str) == 0) { |
| 6348 | // Ok, this is our variable. Delete it. |
| 6349 | __kmp_env_unset(name); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 6350 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6351 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6352 | KMP_INTERNAL_FREE(__kmp_registration_str); |
| 6353 | KMP_INTERNAL_FREE(value); |
| 6354 | KMP_INTERNAL_FREE(name); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6355 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6356 | __kmp_registration_flag = 0; |
| 6357 | __kmp_registration_str = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6358 | |
| 6359 | } // __kmp_unregister_library |
| 6360 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6361 | // End of Library registration stuff. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6362 | // ----------------------------------------------------------------------------- |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6363 | |
Jonathan Peyton | 492e0a3 | 2017-06-13 17:17:26 +0000 | [diff] [blame] | 6364 | #if KMP_MIC_SUPPORTED |
Andrey Churbanov | 613edeb | 2015-02-20 18:14:43 +0000 | [diff] [blame] | 6365 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6366 | static void __kmp_check_mic_type() { |
| 6367 | kmp_cpuid_t cpuid_state = {0}; |
| 6368 | kmp_cpuid_t *cs_p = &cpuid_state; |
| 6369 | __kmp_x86_cpuid(1, 0, cs_p); |
| 6370 | // We don't support mic1 at the moment |
| 6371 | if ((cs_p->eax & 0xff0) == 0xB10) { |
| 6372 | __kmp_mic_type = mic2; |
| 6373 | } else if ((cs_p->eax & 0xf0ff0) == 0x50670) { |
| 6374 | __kmp_mic_type = mic3; |
| 6375 | } else { |
| 6376 | __kmp_mic_type = non_mic; |
| 6377 | } |
Andrey Churbanov | 613edeb | 2015-02-20 18:14:43 +0000 | [diff] [blame] | 6378 | } |
| 6379 | |
Jonathan Peyton | 492e0a3 | 2017-06-13 17:17:26 +0000 | [diff] [blame] | 6380 | #endif /* KMP_MIC_SUPPORTED */ |
Andrey Churbanov | 613edeb | 2015-02-20 18:14:43 +0000 | [diff] [blame] | 6381 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6382 | static void __kmp_do_serial_initialize(void) { |
| 6383 | int i, gtid; |
| 6384 | int size; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6385 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6386 | KA_TRACE(10, ("__kmp_do_serial_initialize: enter\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6387 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6388 | KMP_DEBUG_ASSERT(sizeof(kmp_int32) == 4); |
| 6389 | KMP_DEBUG_ASSERT(sizeof(kmp_uint32) == 4); |
| 6390 | KMP_DEBUG_ASSERT(sizeof(kmp_int64) == 8); |
| 6391 | KMP_DEBUG_ASSERT(sizeof(kmp_uint64) == 8); |
| 6392 | KMP_DEBUG_ASSERT(sizeof(kmp_intptr_t) == sizeof(void *)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6393 | |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 6394 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6395 | ompt_pre_init(); |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 6396 | #endif |
| 6397 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6398 | __kmp_validate_locks(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6399 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6400 | /* Initialize internal memory allocator */ |
| 6401 | __kmp_init_allocator(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6402 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6403 | /* Register the library startup via an environment variable and check to see |
| 6404 | whether another copy of the library is already registered. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6405 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6406 | __kmp_register_library_startup(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6407 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6408 | /* TODO reinitialization of library */ |
| 6409 | if (TCR_4(__kmp_global.g.g_done)) { |
| 6410 | KA_TRACE(10, ("__kmp_do_serial_initialize: reinitialization of library\n")); |
| 6411 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6412 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6413 | __kmp_global.g.g_abort = 0; |
| 6414 | TCW_SYNC_4(__kmp_global.g.g_done, FALSE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6415 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6416 | /* initialize the locks */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6417 | #if KMP_USE_ADAPTIVE_LOCKS |
| 6418 | #if KMP_DEBUG_ADAPTIVE_LOCKS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6419 | __kmp_init_speculative_stats(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6420 | #endif |
| 6421 | #endif |
Jonathan Peyton | ad57992 | 2015-12-17 16:19:05 +0000 | [diff] [blame] | 6422 | #if KMP_STATS_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6423 | __kmp_stats_init(); |
Jonathan Peyton | ad57992 | 2015-12-17 16:19:05 +0000 | [diff] [blame] | 6424 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6425 | __kmp_init_lock(&__kmp_global_lock); |
| 6426 | __kmp_init_queuing_lock(&__kmp_dispatch_lock); |
| 6427 | __kmp_init_lock(&__kmp_debug_lock); |
| 6428 | __kmp_init_atomic_lock(&__kmp_atomic_lock); |
| 6429 | __kmp_init_atomic_lock(&__kmp_atomic_lock_1i); |
| 6430 | __kmp_init_atomic_lock(&__kmp_atomic_lock_2i); |
| 6431 | __kmp_init_atomic_lock(&__kmp_atomic_lock_4i); |
| 6432 | __kmp_init_atomic_lock(&__kmp_atomic_lock_4r); |
| 6433 | __kmp_init_atomic_lock(&__kmp_atomic_lock_8i); |
| 6434 | __kmp_init_atomic_lock(&__kmp_atomic_lock_8r); |
| 6435 | __kmp_init_atomic_lock(&__kmp_atomic_lock_8c); |
| 6436 | __kmp_init_atomic_lock(&__kmp_atomic_lock_10r); |
| 6437 | __kmp_init_atomic_lock(&__kmp_atomic_lock_16r); |
| 6438 | __kmp_init_atomic_lock(&__kmp_atomic_lock_16c); |
| 6439 | __kmp_init_atomic_lock(&__kmp_atomic_lock_20c); |
| 6440 | __kmp_init_atomic_lock(&__kmp_atomic_lock_32c); |
| 6441 | __kmp_init_bootstrap_lock(&__kmp_forkjoin_lock); |
| 6442 | __kmp_init_bootstrap_lock(&__kmp_exit_lock); |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 6443 | #if KMP_USE_MONITOR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6444 | __kmp_init_bootstrap_lock(&__kmp_monitor_lock); |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 6445 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6446 | __kmp_init_bootstrap_lock(&__kmp_tp_cached_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6447 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6448 | /* conduct initialization and initial setup of configuration */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6449 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6450 | __kmp_runtime_initialize(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6451 | |
Jonathan Peyton | 492e0a3 | 2017-06-13 17:17:26 +0000 | [diff] [blame] | 6452 | #if KMP_MIC_SUPPORTED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6453 | __kmp_check_mic_type(); |
Andrey Churbanov | 613edeb | 2015-02-20 18:14:43 +0000 | [diff] [blame] | 6454 | #endif |
| 6455 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6456 | // Some global variable initialization moved here from kmp_env_initialize() |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6457 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6458 | kmp_diag = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6459 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6460 | __kmp_abort_delay = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6461 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6462 | // From __kmp_init_dflt_team_nth() |
| 6463 | /* assume the entire machine will be used */ |
| 6464 | __kmp_dflt_team_nth_ub = __kmp_xproc; |
| 6465 | if (__kmp_dflt_team_nth_ub < KMP_MIN_NTH) { |
| 6466 | __kmp_dflt_team_nth_ub = KMP_MIN_NTH; |
| 6467 | } |
| 6468 | if (__kmp_dflt_team_nth_ub > __kmp_sys_max_nth) { |
| 6469 | __kmp_dflt_team_nth_ub = __kmp_sys_max_nth; |
| 6470 | } |
| 6471 | __kmp_max_nth = __kmp_sys_max_nth; |
Jonathan Peyton | f439246 | 2017-07-27 20:58:41 +0000 | [diff] [blame] | 6472 | __kmp_cg_max_nth = __kmp_sys_max_nth; |
Jonathan Peyton | 4f90c82 | 2017-08-02 20:04:45 +0000 | [diff] [blame] | 6473 | __kmp_teams_max_nth = __kmp_xproc; // set a "reasonable" default |
| 6474 | if (__kmp_teams_max_nth > __kmp_sys_max_nth) { |
| 6475 | __kmp_teams_max_nth = __kmp_sys_max_nth; |
| 6476 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6477 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6478 | // Three vars below moved here from __kmp_env_initialize() "KMP_BLOCKTIME" |
| 6479 | // part |
| 6480 | __kmp_dflt_blocktime = KMP_DEFAULT_BLOCKTIME; |
Jonathan Peyton | e1c7c13 | 2016-10-07 18:12:19 +0000 | [diff] [blame] | 6481 | #if KMP_USE_MONITOR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6482 | __kmp_monitor_wakeups = |
| 6483 | KMP_WAKEUPS_FROM_BLOCKTIME(__kmp_dflt_blocktime, __kmp_monitor_wakeups); |
| 6484 | __kmp_bt_intervals = |
| 6485 | KMP_INTERVALS_FROM_BLOCKTIME(__kmp_dflt_blocktime, __kmp_monitor_wakeups); |
Jonathan Peyton | e1c7c13 | 2016-10-07 18:12:19 +0000 | [diff] [blame] | 6486 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6487 | // From "KMP_LIBRARY" part of __kmp_env_initialize() |
| 6488 | __kmp_library = library_throughput; |
| 6489 | // From KMP_SCHEDULE initialization |
| 6490 | __kmp_static = kmp_sch_static_balanced; |
| 6491 | // AC: do not use analytical here, because it is non-monotonous |
| 6492 | //__kmp_guided = kmp_sch_guided_iterative_chunked; |
| 6493 | //__kmp_auto = kmp_sch_guided_analytical_chunked; // AC: it is the default, no |
| 6494 | // need to repeat assignment |
| 6495 | // Barrier initialization. Moved here from __kmp_env_initialize() Barrier branch |
| 6496 | // bit control and barrier method control parts |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 6497 | #if KMP_FAST_REDUCTION_BARRIER |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6498 | #define kmp_reduction_barrier_gather_bb ((int)1) |
| 6499 | #define kmp_reduction_barrier_release_bb ((int)1) |
| 6500 | #define kmp_reduction_barrier_gather_pat bp_hyper_bar |
| 6501 | #define kmp_reduction_barrier_release_pat bp_hyper_bar |
| 6502 | #endif // KMP_FAST_REDUCTION_BARRIER |
| 6503 | for (i = bs_plain_barrier; i < bs_last_barrier; i++) { |
| 6504 | __kmp_barrier_gather_branch_bits[i] = __kmp_barrier_gather_bb_dflt; |
| 6505 | __kmp_barrier_release_branch_bits[i] = __kmp_barrier_release_bb_dflt; |
| 6506 | __kmp_barrier_gather_pattern[i] = __kmp_barrier_gather_pat_dflt; |
| 6507 | __kmp_barrier_release_pattern[i] = __kmp_barrier_release_pat_dflt; |
| 6508 | #if KMP_FAST_REDUCTION_BARRIER |
| 6509 | if (i == bs_reduction_barrier) { // tested and confirmed on ALTIX only ( |
| 6510 | // lin_64 ): hyper,1 |
| 6511 | __kmp_barrier_gather_branch_bits[i] = kmp_reduction_barrier_gather_bb; |
| 6512 | __kmp_barrier_release_branch_bits[i] = kmp_reduction_barrier_release_bb; |
| 6513 | __kmp_barrier_gather_pattern[i] = kmp_reduction_barrier_gather_pat; |
| 6514 | __kmp_barrier_release_pattern[i] = kmp_reduction_barrier_release_pat; |
Andrey Churbanov | 613edeb | 2015-02-20 18:14:43 +0000 | [diff] [blame] | 6515 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6516 | #endif // KMP_FAST_REDUCTION_BARRIER |
| 6517 | } |
| 6518 | #if KMP_FAST_REDUCTION_BARRIER |
| 6519 | #undef kmp_reduction_barrier_release_pat |
| 6520 | #undef kmp_reduction_barrier_gather_pat |
| 6521 | #undef kmp_reduction_barrier_release_bb |
| 6522 | #undef kmp_reduction_barrier_gather_bb |
| 6523 | #endif // KMP_FAST_REDUCTION_BARRIER |
Jonathan Peyton | 492e0a3 | 2017-06-13 17:17:26 +0000 | [diff] [blame] | 6524 | #if KMP_MIC_SUPPORTED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6525 | if (__kmp_mic_type == mic2) { // KNC |
| 6526 | // AC: plane=3,2, forkjoin=2,1 are optimal for 240 threads on KNC |
| 6527 | __kmp_barrier_gather_branch_bits[bs_plain_barrier] = 3; // plain gather |
| 6528 | __kmp_barrier_release_branch_bits[bs_forkjoin_barrier] = |
| 6529 | 1; // forkjoin release |
| 6530 | __kmp_barrier_gather_pattern[bs_forkjoin_barrier] = bp_hierarchical_bar; |
| 6531 | __kmp_barrier_release_pattern[bs_forkjoin_barrier] = bp_hierarchical_bar; |
| 6532 | } |
| 6533 | #if KMP_FAST_REDUCTION_BARRIER |
| 6534 | if (__kmp_mic_type == mic2) { // KNC |
| 6535 | __kmp_barrier_gather_pattern[bs_reduction_barrier] = bp_hierarchical_bar; |
| 6536 | __kmp_barrier_release_pattern[bs_reduction_barrier] = bp_hierarchical_bar; |
| 6537 | } |
Jonathan Peyton | 492e0a3 | 2017-06-13 17:17:26 +0000 | [diff] [blame] | 6538 | #endif // KMP_FAST_REDUCTION_BARRIER |
| 6539 | #endif // KMP_MIC_SUPPORTED |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6540 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6541 | // From KMP_CHECKS initialization |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6542 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6543 | __kmp_env_checks = TRUE; /* development versions have the extra checks */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6544 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6545 | __kmp_env_checks = FALSE; /* port versions do not have the extra checks */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6546 | #endif |
| 6547 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6548 | // From "KMP_FOREIGN_THREADS_THREADPRIVATE" initialization |
| 6549 | __kmp_foreign_tp = TRUE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6550 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6551 | __kmp_global.g.g_dynamic = FALSE; |
| 6552 | __kmp_global.g.g_dynamic_mode = dynamic_default; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6553 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6554 | __kmp_env_initialize(NULL); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 6555 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6556 | // Print all messages in message catalog for testing purposes. |
| 6557 | #ifdef KMP_DEBUG |
| 6558 | char const *val = __kmp_env_get("KMP_DUMP_CATALOG"); |
| 6559 | if (__kmp_str_match_true(val)) { |
| 6560 | kmp_str_buf_t buffer; |
| 6561 | __kmp_str_buf_init(&buffer); |
| 6562 | __kmp_i18n_dump_catalog(&buffer); |
| 6563 | __kmp_printf("%s", buffer.str); |
| 6564 | __kmp_str_buf_free(&buffer); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 6565 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6566 | __kmp_env_free(&val); |
| 6567 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6568 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6569 | __kmp_threads_capacity = |
| 6570 | __kmp_initial_threads_capacity(__kmp_dflt_team_nth_ub); |
| 6571 | // Moved here from __kmp_env_initialize() "KMP_ALL_THREADPRIVATE" part |
| 6572 | __kmp_tp_capacity = __kmp_default_tp_capacity( |
| 6573 | __kmp_dflt_team_nth_ub, __kmp_max_nth, __kmp_allThreadsSpecified); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6574 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6575 | // If the library is shut down properly, both pools must be NULL. Just in |
| 6576 | // case, set them to NULL -- some memory may leak, but subsequent code will |
| 6577 | // work even if pools are not freed. |
| 6578 | KMP_DEBUG_ASSERT(__kmp_thread_pool == NULL); |
| 6579 | KMP_DEBUG_ASSERT(__kmp_thread_pool_insert_pt == NULL); |
| 6580 | KMP_DEBUG_ASSERT(__kmp_team_pool == NULL); |
| 6581 | __kmp_thread_pool = NULL; |
| 6582 | __kmp_thread_pool_insert_pt = NULL; |
| 6583 | __kmp_team_pool = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6584 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6585 | /* Allocate all of the variable sized records */ |
| 6586 | /* NOTE: __kmp_threads_capacity entries are allocated, but the arrays are |
| 6587 | * expandable */ |
| 6588 | /* Since allocation is cache-aligned, just add extra padding at the end */ |
| 6589 | size = |
| 6590 | (sizeof(kmp_info_t *) + sizeof(kmp_root_t *)) * __kmp_threads_capacity + |
| 6591 | CACHE_LINE; |
| 6592 | __kmp_threads = (kmp_info_t **)__kmp_allocate(size); |
| 6593 | __kmp_root = (kmp_root_t **)((char *)__kmp_threads + |
| 6594 | sizeof(kmp_info_t *) * __kmp_threads_capacity); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6595 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6596 | /* init thread counts */ |
| 6597 | KMP_DEBUG_ASSERT(__kmp_all_nth == |
| 6598 | 0); // Asserts fail if the library is reinitializing and |
| 6599 | KMP_DEBUG_ASSERT(__kmp_nth == 0); // something was wrong in termination. |
| 6600 | __kmp_all_nth = 0; |
| 6601 | __kmp_nth = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6602 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6603 | /* setup the uber master thread and hierarchy */ |
| 6604 | gtid = __kmp_register_root(TRUE); |
| 6605 | KA_TRACE(10, ("__kmp_do_serial_initialize T#%d\n", gtid)); |
| 6606 | KMP_ASSERT(KMP_UBER_GTID(gtid)); |
| 6607 | KMP_ASSERT(KMP_INITIAL_GTID(gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6608 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6609 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6610 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6611 | __kmp_common_initialize(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6612 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6613 | #if KMP_OS_UNIX |
| 6614 | /* invoke the child fork handler */ |
| 6615 | __kmp_register_atfork(); |
| 6616 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6617 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6618 | #if !defined KMP_DYNAMIC_LIB |
| 6619 | { |
| 6620 | /* Invoke the exit handler when the program finishes, only for static |
| 6621 | library. For dynamic library, we already have _fini and DllMain. */ |
| 6622 | int rc = atexit(__kmp_internal_end_atexit); |
| 6623 | if (rc != 0) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 6624 | __kmp_fatal(KMP_MSG(FunctionError, "atexit()"), KMP_ERR(rc), |
| 6625 | __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 6626 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6627 | } |
| 6628 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6629 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6630 | #if KMP_HANDLE_SIGNALS |
| 6631 | #if KMP_OS_UNIX |
| 6632 | /* NOTE: make sure that this is called before the user installs their own |
| 6633 | signal handlers so that the user handlers are called first. this way they |
| 6634 | can return false, not call our handler, avoid terminating the library, and |
| 6635 | continue execution where they left off. */ |
| 6636 | __kmp_install_signals(FALSE); |
| 6637 | #endif /* KMP_OS_UNIX */ |
| 6638 | #if KMP_OS_WINDOWS |
| 6639 | __kmp_install_signals(TRUE); |
| 6640 | #endif /* KMP_OS_WINDOWS */ |
| 6641 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6642 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6643 | /* we have finished the serial initialization */ |
| 6644 | __kmp_init_counter++; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6645 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6646 | __kmp_init_serial = TRUE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6647 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6648 | if (__kmp_settings) { |
| 6649 | __kmp_env_print(); |
| 6650 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6651 | |
| 6652 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6653 | if (__kmp_display_env || __kmp_display_env_verbose) { |
| 6654 | __kmp_env_print_2(); |
| 6655 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6656 | #endif // OMP_40_ENABLED |
| 6657 | |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 6658 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6659 | ompt_post_init(); |
Jonathan Peyton | 82a13bf | 2015-09-21 18:01:02 +0000 | [diff] [blame] | 6660 | #endif |
| 6661 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6662 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6663 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6664 | KA_TRACE(10, ("__kmp_do_serial_initialize: exit\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6665 | } |
| 6666 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6667 | void __kmp_serial_initialize(void) { |
| 6668 | if (__kmp_init_serial) { |
| 6669 | return; |
| 6670 | } |
| 6671 | __kmp_acquire_bootstrap_lock(&__kmp_initz_lock); |
| 6672 | if (__kmp_init_serial) { |
| 6673 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
| 6674 | return; |
| 6675 | } |
| 6676 | __kmp_do_serial_initialize(); |
| 6677 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
| 6678 | } |
| 6679 | |
| 6680 | static void __kmp_do_middle_initialize(void) { |
| 6681 | int i, j; |
| 6682 | int prev_dflt_team_nth; |
| 6683 | |
| 6684 | if (!__kmp_init_serial) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6685 | __kmp_do_serial_initialize(); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6686 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6687 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6688 | KA_TRACE(10, ("__kmp_middle_initialize: enter\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6689 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6690 | // Save the previous value for the __kmp_dflt_team_nth so that |
| 6691 | // we can avoid some reinitialization if it hasn't changed. |
| 6692 | prev_dflt_team_nth = __kmp_dflt_team_nth; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6693 | |
Alp Toker | 98758b0 | 2014-03-02 04:12:06 +0000 | [diff] [blame] | 6694 | #if KMP_AFFINITY_SUPPORTED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6695 | // __kmp_affinity_initialize() will try to set __kmp_ncores to the |
| 6696 | // number of cores on the machine. |
| 6697 | __kmp_affinity_initialize(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6698 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6699 | // Run through the __kmp_threads array and set the affinity mask |
| 6700 | // for each root thread that is currently registered with the RTL. |
| 6701 | for (i = 0; i < __kmp_threads_capacity; i++) { |
| 6702 | if (TCR_PTR(__kmp_threads[i]) != NULL) { |
| 6703 | __kmp_affinity_set_init_mask(i, TRUE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6704 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6705 | } |
Alp Toker | 98758b0 | 2014-03-02 04:12:06 +0000 | [diff] [blame] | 6706 | #endif /* KMP_AFFINITY_SUPPORTED */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6707 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6708 | KMP_ASSERT(__kmp_xproc > 0); |
| 6709 | if (__kmp_avail_proc == 0) { |
| 6710 | __kmp_avail_proc = __kmp_xproc; |
| 6711 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6712 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6713 | // If there were empty places in num_threads list (OMP_NUM_THREADS=,,2,3), |
| 6714 | // correct them now |
| 6715 | j = 0; |
| 6716 | while ((j < __kmp_nested_nth.used) && !__kmp_nested_nth.nth[j]) { |
| 6717 | __kmp_nested_nth.nth[j] = __kmp_dflt_team_nth = __kmp_dflt_team_nth_ub = |
| 6718 | __kmp_avail_proc; |
| 6719 | j++; |
| 6720 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6721 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6722 | if (__kmp_dflt_team_nth == 0) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6723 | #ifdef KMP_DFLT_NTH_CORES |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6724 | // Default #threads = #cores |
| 6725 | __kmp_dflt_team_nth = __kmp_ncores; |
| 6726 | KA_TRACE(20, ("__kmp_middle_initialize: setting __kmp_dflt_team_nth = " |
| 6727 | "__kmp_ncores (%d)\n", |
| 6728 | __kmp_dflt_team_nth)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6729 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6730 | // Default #threads = #available OS procs |
| 6731 | __kmp_dflt_team_nth = __kmp_avail_proc; |
| 6732 | KA_TRACE(20, ("__kmp_middle_initialize: setting __kmp_dflt_team_nth = " |
| 6733 | "__kmp_avail_proc(%d)\n", |
| 6734 | __kmp_dflt_team_nth)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6735 | #endif /* KMP_DFLT_NTH_CORES */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6736 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6737 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6738 | if (__kmp_dflt_team_nth < KMP_MIN_NTH) { |
| 6739 | __kmp_dflt_team_nth = KMP_MIN_NTH; |
| 6740 | } |
| 6741 | if (__kmp_dflt_team_nth > __kmp_sys_max_nth) { |
| 6742 | __kmp_dflt_team_nth = __kmp_sys_max_nth; |
| 6743 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6744 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6745 | // There's no harm in continuing if the following check fails, |
| 6746 | // but it indicates an error in the previous logic. |
| 6747 | KMP_DEBUG_ASSERT(__kmp_dflt_team_nth <= __kmp_dflt_team_nth_ub); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6748 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6749 | if (__kmp_dflt_team_nth != prev_dflt_team_nth) { |
| 6750 | // Run through the __kmp_threads array and set the num threads icv for each |
| 6751 | // root thread that is currently registered with the RTL (which has not |
| 6752 | // already explicitly set its nthreads-var with a call to |
| 6753 | // omp_set_num_threads()). |
| 6754 | for (i = 0; i < __kmp_threads_capacity; i++) { |
| 6755 | kmp_info_t *thread = __kmp_threads[i]; |
| 6756 | if (thread == NULL) |
| 6757 | continue; |
| 6758 | if (thread->th.th_current_task->td_icvs.nproc != 0) |
| 6759 | continue; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6760 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6761 | set__nproc(__kmp_threads[i], __kmp_dflt_team_nth); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6762 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6763 | } |
| 6764 | KA_TRACE( |
| 6765 | 20, |
| 6766 | ("__kmp_middle_initialize: final value for __kmp_dflt_team_nth = %d\n", |
| 6767 | __kmp_dflt_team_nth)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6768 | |
| 6769 | #ifdef KMP_ADJUST_BLOCKTIME |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6770 | /* Adjust blocktime to zero if necessary now that __kmp_avail_proc is set */ |
| 6771 | if (!__kmp_env_blocktime && (__kmp_avail_proc > 0)) { |
| 6772 | KMP_DEBUG_ASSERT(__kmp_avail_proc > 0); |
| 6773 | if (__kmp_nth > __kmp_avail_proc) { |
| 6774 | __kmp_zero_bt = TRUE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6775 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6776 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6777 | #endif /* KMP_ADJUST_BLOCKTIME */ |
| 6778 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6779 | /* we have finished middle initialization */ |
| 6780 | TCW_SYNC_4(__kmp_init_middle, TRUE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6781 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6782 | KA_TRACE(10, ("__kmp_do_middle_initialize: exit\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6783 | } |
| 6784 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6785 | void __kmp_middle_initialize(void) { |
| 6786 | if (__kmp_init_middle) { |
| 6787 | return; |
| 6788 | } |
| 6789 | __kmp_acquire_bootstrap_lock(&__kmp_initz_lock); |
| 6790 | if (__kmp_init_middle) { |
| 6791 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
| 6792 | return; |
| 6793 | } |
| 6794 | __kmp_do_middle_initialize(); |
| 6795 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
| 6796 | } |
| 6797 | |
| 6798 | void __kmp_parallel_initialize(void) { |
| 6799 | int gtid = __kmp_entry_gtid(); // this might be a new root |
| 6800 | |
| 6801 | /* synchronize parallel initialization (for sibling) */ |
| 6802 | if (TCR_4(__kmp_init_parallel)) |
| 6803 | return; |
| 6804 | __kmp_acquire_bootstrap_lock(&__kmp_initz_lock); |
| 6805 | if (TCR_4(__kmp_init_parallel)) { |
| 6806 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
| 6807 | return; |
| 6808 | } |
| 6809 | |
| 6810 | /* TODO reinitialization after we have already shut down */ |
| 6811 | if (TCR_4(__kmp_global.g.g_done)) { |
| 6812 | KA_TRACE( |
| 6813 | 10, |
| 6814 | ("__kmp_parallel_initialize: attempt to init while shutting down\n")); |
| 6815 | __kmp_infinite_loop(); |
| 6816 | } |
| 6817 | |
| 6818 | /* jc: The lock __kmp_initz_lock is already held, so calling |
| 6819 | __kmp_serial_initialize would cause a deadlock. So we call |
| 6820 | __kmp_do_serial_initialize directly. */ |
| 6821 | if (!__kmp_init_middle) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6822 | __kmp_do_middle_initialize(); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6823 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6824 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6825 | /* begin initialization */ |
| 6826 | KA_TRACE(10, ("__kmp_parallel_initialize: enter\n")); |
| 6827 | KMP_ASSERT(KMP_UBER_GTID(gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6828 | |
| 6829 | #if KMP_ARCH_X86 || KMP_ARCH_X86_64 |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6830 | // Save the FP control regs. |
| 6831 | // Worker threads will set theirs to these values at thread startup. |
| 6832 | __kmp_store_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word); |
| 6833 | __kmp_store_mxcsr(&__kmp_init_mxcsr); |
| 6834 | __kmp_init_mxcsr &= KMP_X86_MXCSR_MASK; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6835 | #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */ |
| 6836 | |
| 6837 | #if KMP_OS_UNIX |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6838 | #if KMP_HANDLE_SIGNALS |
| 6839 | /* must be after __kmp_serial_initialize */ |
| 6840 | __kmp_install_signals(TRUE); |
| 6841 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6842 | #endif |
| 6843 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6844 | __kmp_suspend_initialize(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6845 | |
Jonathan Peyton | 749b4d5 | 2016-01-27 21:02:04 +0000 | [diff] [blame] | 6846 | #if defined(USE_LOAD_BALANCE) |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6847 | if (__kmp_global.g.g_dynamic_mode == dynamic_default) { |
| 6848 | __kmp_global.g.g_dynamic_mode = dynamic_load_balance; |
| 6849 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6850 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6851 | if (__kmp_global.g.g_dynamic_mode == dynamic_default) { |
| 6852 | __kmp_global.g.g_dynamic_mode = dynamic_thread_limit; |
| 6853 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6854 | #endif |
| 6855 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6856 | if (__kmp_version) { |
| 6857 | __kmp_print_version_2(); |
| 6858 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6859 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6860 | /* we have finished parallel initialization */ |
| 6861 | TCW_SYNC_4(__kmp_init_parallel, TRUE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6862 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6863 | KMP_MB(); |
| 6864 | KA_TRACE(10, ("__kmp_parallel_initialize: exit\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6865 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6866 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6867 | } |
| 6868 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6869 | /* ------------------------------------------------------------------------ */ |
| 6870 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6871 | void __kmp_run_before_invoked_task(int gtid, int tid, kmp_info_t *this_thr, |
| 6872 | kmp_team_t *team) { |
| 6873 | kmp_disp_t *dispatch; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6874 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6875 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6876 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6877 | /* none of the threads have encountered any constructs, yet. */ |
| 6878 | this_thr->th.th_local.this_construct = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6879 | #if KMP_CACHE_MANAGE |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6880 | KMP_CACHE_PREFETCH(&this_thr->th.th_bar[bs_forkjoin_barrier].bb.b_arrived); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6881 | #endif /* KMP_CACHE_MANAGE */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6882 | dispatch = (kmp_disp_t *)TCR_PTR(this_thr->th.th_dispatch); |
| 6883 | KMP_DEBUG_ASSERT(dispatch); |
| 6884 | KMP_DEBUG_ASSERT(team->t.t_dispatch); |
| 6885 | // KMP_DEBUG_ASSERT( this_thr->th.th_dispatch == &team->t.t_dispatch[ |
| 6886 | // this_thr->th.th_info.ds.ds_tid ] ); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6887 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6888 | dispatch->th_disp_index = 0; /* reset the dispatch buffer counter */ |
Jonathan Peyton | df6818b | 2016-06-14 17:57:47 +0000 | [diff] [blame] | 6889 | #if OMP_45_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6890 | dispatch->th_doacross_buf_idx = |
| 6891 | 0; /* reset the doacross dispatch buffer counter */ |
Jonathan Peyton | 71909c5 | 2016-03-02 22:42:06 +0000 | [diff] [blame] | 6892 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6893 | if (__kmp_env_consistency_check) |
| 6894 | __kmp_push_parallel(gtid, team->t.t_ident); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6895 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6896 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6897 | } |
| 6898 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6899 | void __kmp_run_after_invoked_task(int gtid, int tid, kmp_info_t *this_thr, |
| 6900 | kmp_team_t *team) { |
| 6901 | if (__kmp_env_consistency_check) |
| 6902 | __kmp_pop_parallel(gtid, team->t.t_ident); |
Andrey Churbanov | df0d75e | 2016-10-27 11:43:07 +0000 | [diff] [blame] | 6903 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6904 | __kmp_finish_implicit_task(this_thr); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6905 | } |
| 6906 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6907 | int __kmp_invoke_task_func(int gtid) { |
| 6908 | int rc; |
| 6909 | int tid = __kmp_tid_from_gtid(gtid); |
| 6910 | kmp_info_t *this_thr = __kmp_threads[gtid]; |
| 6911 | kmp_team_t *team = this_thr->th.th_team; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6912 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6913 | __kmp_run_before_invoked_task(gtid, tid, this_thr, team); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6914 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6915 | if (__itt_stack_caller_create_ptr) { |
| 6916 | __kmp_itt_stack_callee_enter( |
| 6917 | (__itt_caller) |
| 6918 | team->t.t_stack_id); // inform ittnotify about entering user's code |
| 6919 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6920 | #endif /* USE_ITT_BUILD */ |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 6921 | #if INCLUDE_SSC_MARKS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6922 | SSC_MARK_INVOKING(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 6923 | #endif |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 6924 | |
| 6925 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6926 | void *dummy; |
| 6927 | void **exit_runtime_p; |
| 6928 | ompt_task_id_t my_task_id; |
| 6929 | ompt_parallel_id_t my_parallel_id; |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 6930 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6931 | if (ompt_enabled) { |
| 6932 | exit_runtime_p = &(team->t.t_implicit_task_taskdata[tid] |
| 6933 | .ompt_task_info.frame.exit_runtime_frame); |
| 6934 | } else { |
| 6935 | exit_runtime_p = &dummy; |
| 6936 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 6937 | |
| 6938 | #if OMPT_TRACE |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6939 | my_task_id = team->t.t_implicit_task_taskdata[tid].ompt_task_info.task_id; |
| 6940 | my_parallel_id = team->t.ompt_team_info.parallel_id; |
| 6941 | if (ompt_enabled && |
| 6942 | ompt_callbacks.ompt_callback(ompt_event_implicit_task_begin)) { |
| 6943 | ompt_callbacks.ompt_callback(ompt_event_implicit_task_begin)(my_parallel_id, |
| 6944 | my_task_id); |
| 6945 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 6946 | #endif |
| 6947 | #endif |
| 6948 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6949 | { |
| 6950 | KMP_TIME_PARTITIONED_BLOCK(OMP_parallel); |
| 6951 | KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK); |
| 6952 | rc = |
| 6953 | __kmp_invoke_microtask((microtask_t)TCR_SYNC_PTR(team->t.t_pkfn), gtid, |
| 6954 | tid, (int)team->t.t_argc, (void **)team->t.t_argv |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 6955 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6956 | , |
| 6957 | exit_runtime_p |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 6958 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6959 | ); |
Jonas Hahnfeld | 8a27064 | 2016-09-14 13:59:19 +0000 | [diff] [blame] | 6960 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6961 | *exit_runtime_p = NULL; |
Jonas Hahnfeld | 8a27064 | 2016-09-14 13:59:19 +0000 | [diff] [blame] | 6962 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6963 | } |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 6964 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6965 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6966 | if (__itt_stack_caller_create_ptr) { |
| 6967 | __kmp_itt_stack_callee_leave( |
| 6968 | (__itt_caller) |
| 6969 | team->t.t_stack_id); // inform ittnotify about leaving user's code |
| 6970 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6971 | #endif /* USE_ITT_BUILD */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6972 | __kmp_run_after_invoked_task(gtid, tid, this_thr, team); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6973 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6974 | return rc; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 6975 | } |
| 6976 | |
| 6977 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6978 | void __kmp_teams_master(int gtid) { |
| 6979 | // This routine is called by all master threads in teams construct |
| 6980 | kmp_info_t *thr = __kmp_threads[gtid]; |
| 6981 | kmp_team_t *team = thr->th.th_team; |
| 6982 | ident_t *loc = team->t.t_ident; |
| 6983 | thr->th.th_set_nproc = thr->th.th_teams_size.nth; |
| 6984 | KMP_DEBUG_ASSERT(thr->th.th_teams_microtask); |
| 6985 | KMP_DEBUG_ASSERT(thr->th.th_set_nproc); |
| 6986 | KA_TRACE(20, ("__kmp_teams_master: T#%d, Tid %d, microtask %p\n", gtid, |
| 6987 | __kmp_tid_from_gtid(gtid), thr->th.th_teams_microtask)); |
| 6988 | // Launch league of teams now, but not let workers execute |
| 6989 | // (they hang on fork barrier until next parallel) |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 6990 | #if INCLUDE_SSC_MARKS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6991 | SSC_MARK_FORKING(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 6992 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6993 | __kmp_fork_call(loc, gtid, fork_context_intel, team->t.t_argc, |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 6994 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6995 | (void *)thr->th.th_teams_microtask, // "unwrapped" task |
Andrey Churbanov | d7d088f | 2015-04-29 16:42:24 +0000 | [diff] [blame] | 6996 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 6997 | (microtask_t)thr->th.th_teams_microtask, // "wrapped" task |
| 6998 | VOLATILE_CAST(launch_t) __kmp_invoke_task_func, NULL); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 6999 | #if INCLUDE_SSC_MARKS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7000 | SSC_MARK_JOINING(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 7001 | #endif |
Jonathan Peyton | 6111849 | 2016-05-20 19:03:38 +0000 | [diff] [blame] | 7002 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7003 | // AC: last parameter "1" eliminates join barrier which won't work because |
| 7004 | // worker threads are in a fork barrier waiting for more parallel regions |
| 7005 | __kmp_join_call(loc, gtid |
Jonathan Peyton | f89fbbb | 2015-08-31 18:15:00 +0000 | [diff] [blame] | 7006 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7007 | , |
| 7008 | fork_context_intel |
Jonathan Peyton | f89fbbb | 2015-08-31 18:15:00 +0000 | [diff] [blame] | 7009 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7010 | , |
| 7011 | 1); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7012 | } |
| 7013 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7014 | int __kmp_invoke_teams_master(int gtid) { |
| 7015 | kmp_info_t *this_thr = __kmp_threads[gtid]; |
| 7016 | kmp_team_t *team = this_thr->th.th_team; |
| 7017 | #if KMP_DEBUG |
| 7018 | if (!__kmp_threads[gtid]->th.th_team->t.t_serialized) |
| 7019 | KMP_DEBUG_ASSERT((void *)__kmp_threads[gtid]->th.th_team->t.t_pkfn == |
| 7020 | (void *)__kmp_teams_master); |
| 7021 | #endif |
| 7022 | __kmp_run_before_invoked_task(gtid, 0, this_thr, team); |
| 7023 | __kmp_teams_master(gtid); |
| 7024 | __kmp_run_after_invoked_task(gtid, 0, this_thr, team); |
| 7025 | return 1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7026 | } |
| 7027 | #endif /* OMP_40_ENABLED */ |
| 7028 | |
| 7029 | /* this sets the requested number of threads for the next parallel region |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7030 | encountered by this team. since this should be enclosed in the forkjoin |
| 7031 | critical section it should avoid race conditions with assymmetrical nested |
| 7032 | parallelism */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7033 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7034 | void __kmp_push_num_threads(ident_t *id, int gtid, int num_threads) { |
| 7035 | kmp_info_t *thr = __kmp_threads[gtid]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7036 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7037 | if (num_threads > 0) |
| 7038 | thr->th.th_set_nproc = num_threads; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7039 | } |
| 7040 | |
| 7041 | #if OMP_40_ENABLED |
| 7042 | |
| 7043 | /* this sets the requested number of teams for the teams region and/or |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7044 | the number of threads for the next parallel region encountered */ |
| 7045 | void __kmp_push_num_teams(ident_t *id, int gtid, int num_teams, |
| 7046 | int num_threads) { |
| 7047 | kmp_info_t *thr = __kmp_threads[gtid]; |
| 7048 | KMP_DEBUG_ASSERT(num_teams >= 0); |
| 7049 | KMP_DEBUG_ASSERT(num_threads >= 0); |
Jonathan Peyton | 1be692e | 2015-11-30 20:14:05 +0000 | [diff] [blame] | 7050 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7051 | if (num_teams == 0) |
| 7052 | num_teams = 1; // default number of teams is 1. |
Jonathan Peyton | 4f90c82 | 2017-08-02 20:04:45 +0000 | [diff] [blame] | 7053 | if (num_teams > __kmp_teams_max_nth) { // if too many teams requested? |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7054 | if (!__kmp_reserve_warn) { |
| 7055 | __kmp_reserve_warn = 1; |
| 7056 | __kmp_msg(kmp_ms_warning, |
Jonathan Peyton | 4f90c82 | 2017-08-02 20:04:45 +0000 | [diff] [blame] | 7057 | KMP_MSG(CantFormThrTeam, num_teams, __kmp_teams_max_nth), |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7058 | KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7059 | } |
Jonathan Peyton | 4f90c82 | 2017-08-02 20:04:45 +0000 | [diff] [blame] | 7060 | num_teams = __kmp_teams_max_nth; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7061 | } |
| 7062 | // Set number of teams (number of threads in the outer "parallel" of the |
| 7063 | // teams) |
| 7064 | thr->th.th_set_nproc = thr->th.th_teams_size.nteams = num_teams; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 7065 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7066 | // Remember the number of threads for inner parallel regions |
| 7067 | if (num_threads == 0) { |
| 7068 | if (!TCR_4(__kmp_init_middle)) |
| 7069 | __kmp_middle_initialize(); // get __kmp_avail_proc calculated |
| 7070 | num_threads = __kmp_avail_proc / num_teams; |
Jonathan Peyton | 4f90c82 | 2017-08-02 20:04:45 +0000 | [diff] [blame] | 7071 | if (num_teams * num_threads > __kmp_teams_max_nth) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7072 | // adjust num_threads w/o warning as it is not user setting |
Jonathan Peyton | 4f90c82 | 2017-08-02 20:04:45 +0000 | [diff] [blame] | 7073 | num_threads = __kmp_teams_max_nth / num_teams; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7074 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7075 | } else { |
Jonathan Peyton | 4f90c82 | 2017-08-02 20:04:45 +0000 | [diff] [blame] | 7076 | if (num_teams * num_threads > __kmp_teams_max_nth) { |
| 7077 | int new_threads = __kmp_teams_max_nth / num_teams; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7078 | if (!__kmp_reserve_warn) { // user asked for too many threads |
Jonathan Peyton | 4f90c82 | 2017-08-02 20:04:45 +0000 | [diff] [blame] | 7079 | __kmp_reserve_warn = 1; // that conflicts with KMP_TEAMS_THREAD_LIMIT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7080 | __kmp_msg(kmp_ms_warning, |
| 7081 | KMP_MSG(CantFormThrTeam, num_threads, new_threads), |
| 7082 | KMP_HNT(Unset_ALL_THREADS), __kmp_msg_null); |
| 7083 | } |
| 7084 | num_threads = new_threads; |
| 7085 | } |
| 7086 | } |
| 7087 | thr->th.th_teams_size.nth = num_threads; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7088 | } |
| 7089 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7090 | // Set the proc_bind var to use in the following parallel region. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7091 | void __kmp_push_proc_bind(ident_t *id, int gtid, kmp_proc_bind_t proc_bind) { |
| 7092 | kmp_info_t *thr = __kmp_threads[gtid]; |
| 7093 | thr->th.th_set_proc_bind = proc_bind; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7094 | } |
| 7095 | |
| 7096 | #endif /* OMP_40_ENABLED */ |
| 7097 | |
| 7098 | /* Launch the worker threads into the microtask. */ |
| 7099 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7100 | void __kmp_internal_fork(ident_t *id, int gtid, kmp_team_t *team) { |
| 7101 | kmp_info_t *this_thr = __kmp_threads[gtid]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7102 | |
| 7103 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7104 | int f; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7105 | #endif /* KMP_DEBUG */ |
| 7106 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7107 | KMP_DEBUG_ASSERT(team); |
| 7108 | KMP_DEBUG_ASSERT(this_thr->th.th_team == team); |
| 7109 | KMP_ASSERT(KMP_MASTER_GTID(gtid)); |
| 7110 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7111 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7112 | team->t.t_construct = 0; /* no single directives seen yet */ |
| 7113 | team->t.t_ordered.dt.t_value = |
| 7114 | 0; /* thread 0 enters the ordered section first */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7115 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7116 | /* Reset the identifiers on the dispatch buffer */ |
| 7117 | KMP_DEBUG_ASSERT(team->t.t_disp_buffer); |
| 7118 | if (team->t.t_max_nproc > 1) { |
| 7119 | int i; |
| 7120 | for (i = 0; i < __kmp_dispatch_num_buffers; ++i) { |
| 7121 | team->t.t_disp_buffer[i].buffer_index = i; |
Jonathan Peyton | df6818b | 2016-06-14 17:57:47 +0000 | [diff] [blame] | 7122 | #if OMP_45_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7123 | team->t.t_disp_buffer[i].doacross_buf_idx = i; |
Jonathan Peyton | 71909c5 | 2016-03-02 22:42:06 +0000 | [diff] [blame] | 7124 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7125 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7126 | } else { |
| 7127 | team->t.t_disp_buffer[0].buffer_index = 0; |
| 7128 | #if OMP_45_ENABLED |
| 7129 | team->t.t_disp_buffer[0].doacross_buf_idx = 0; |
| 7130 | #endif |
| 7131 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7132 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7133 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 7134 | KMP_ASSERT(this_thr->th.th_team == team); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7135 | |
| 7136 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7137 | for (f = 0; f < team->t.t_nproc; f++) { |
| 7138 | KMP_DEBUG_ASSERT(team->t.t_threads[f] && |
| 7139 | team->t.t_threads[f]->th.th_team_nproc == team->t.t_nproc); |
| 7140 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7141 | #endif /* KMP_DEBUG */ |
| 7142 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7143 | /* release the worker threads so they may begin working */ |
| 7144 | __kmp_fork_barrier(gtid, 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7145 | } |
| 7146 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7147 | void __kmp_internal_join(ident_t *id, int gtid, kmp_team_t *team) { |
| 7148 | kmp_info_t *this_thr = __kmp_threads[gtid]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7149 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7150 | KMP_DEBUG_ASSERT(team); |
| 7151 | KMP_DEBUG_ASSERT(this_thr->th.th_team == team); |
| 7152 | KMP_ASSERT(KMP_MASTER_GTID(gtid)); |
| 7153 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7154 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7155 | /* Join barrier after fork */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7156 | |
| 7157 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7158 | if (__kmp_threads[gtid] && |
| 7159 | __kmp_threads[gtid]->th.th_team_nproc != team->t.t_nproc) { |
| 7160 | __kmp_printf("GTID: %d, __kmp_threads[%d]=%p\n", gtid, gtid, |
| 7161 | __kmp_threads[gtid]); |
| 7162 | __kmp_printf("__kmp_threads[%d]->th.th_team_nproc=%d, TEAM: %p, " |
| 7163 | "team->t.t_nproc=%d\n", |
| 7164 | gtid, __kmp_threads[gtid]->th.th_team_nproc, team, |
| 7165 | team->t.t_nproc); |
| 7166 | __kmp_print_structure(); |
| 7167 | } |
| 7168 | KMP_DEBUG_ASSERT(__kmp_threads[gtid] && |
| 7169 | __kmp_threads[gtid]->th.th_team_nproc == team->t.t_nproc); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7170 | #endif /* KMP_DEBUG */ |
| 7171 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7172 | __kmp_join_barrier(gtid); /* wait for everyone */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7173 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7174 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 7175 | KMP_ASSERT(this_thr->th.th_team == team); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7176 | } |
| 7177 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7178 | /* ------------------------------------------------------------------------ */ |
| 7179 | |
| 7180 | #ifdef USE_LOAD_BALANCE |
| 7181 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7182 | // Return the worker threads actively spinning in the hot team, if we |
| 7183 | // are at the outermost level of parallelism. Otherwise, return 0. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7184 | static int __kmp_active_hot_team_nproc(kmp_root_t *root) { |
| 7185 | int i; |
| 7186 | int retval; |
| 7187 | kmp_team_t *hot_team; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7188 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7189 | if (root->r.r_active) { |
| 7190 | return 0; |
| 7191 | } |
| 7192 | hot_team = root->r.r_hot_team; |
| 7193 | if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) { |
| 7194 | return hot_team->t.t_nproc - 1; // Don't count master thread |
| 7195 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7196 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7197 | // Skip the master thread - it is accounted for elsewhere. |
| 7198 | retval = 0; |
| 7199 | for (i = 1; i < hot_team->t.t_nproc; i++) { |
| 7200 | if (hot_team->t.t_threads[i]->th.th_active) { |
| 7201 | retval++; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7202 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7203 | } |
| 7204 | return retval; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7205 | } |
| 7206 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7207 | // Perform an automatic adjustment to the number of |
| 7208 | // threads used by the next parallel region. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7209 | static int __kmp_load_balance_nproc(kmp_root_t *root, int set_nproc) { |
| 7210 | int retval; |
| 7211 | int pool_active; |
| 7212 | int hot_team_active; |
| 7213 | int team_curr_active; |
| 7214 | int system_active; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7215 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7216 | KB_TRACE(20, ("__kmp_load_balance_nproc: called root:%p set_nproc:%d\n", root, |
| 7217 | set_nproc)); |
| 7218 | KMP_DEBUG_ASSERT(root); |
| 7219 | KMP_DEBUG_ASSERT(root->r.r_root_team->t.t_threads[0] |
| 7220 | ->th.th_current_task->td_icvs.dynamic == TRUE); |
| 7221 | KMP_DEBUG_ASSERT(set_nproc > 1); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7222 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7223 | if (set_nproc == 1) { |
| 7224 | KB_TRACE(20, ("__kmp_load_balance_nproc: serial execution.\n")); |
| 7225 | return 1; |
| 7226 | } |
| 7227 | |
| 7228 | // Threads that are active in the thread pool, active in the hot team for this |
| 7229 | // particular root (if we are at the outer par level), and the currently |
| 7230 | // executing thread (to become the master) are available to add to the new |
| 7231 | // team, but are currently contributing to the system load, and must be |
| 7232 | // accounted for. |
| 7233 | pool_active = TCR_4(__kmp_thread_pool_active_nth); |
| 7234 | hot_team_active = __kmp_active_hot_team_nproc(root); |
| 7235 | team_curr_active = pool_active + hot_team_active + 1; |
| 7236 | |
| 7237 | // Check the system load. |
| 7238 | system_active = __kmp_get_load_balance(__kmp_avail_proc + team_curr_active); |
| 7239 | KB_TRACE(30, ("__kmp_load_balance_nproc: system active = %d pool active = %d " |
| 7240 | "hot team active = %d\n", |
| 7241 | system_active, pool_active, hot_team_active)); |
| 7242 | |
| 7243 | if (system_active < 0) { |
| 7244 | // There was an error reading the necessary info from /proc, so use the |
| 7245 | // thread limit algorithm instead. Once we set __kmp_global.g.g_dynamic_mode |
| 7246 | // = dynamic_thread_limit, we shouldn't wind up getting back here. |
| 7247 | __kmp_global.g.g_dynamic_mode = dynamic_thread_limit; |
| 7248 | KMP_WARNING(CantLoadBalUsing, "KMP_DYNAMIC_MODE=thread limit"); |
| 7249 | |
| 7250 | // Make this call behave like the thread limit algorithm. |
| 7251 | retval = __kmp_avail_proc - __kmp_nth + |
| 7252 | (root->r.r_active ? 1 : root->r.r_hot_team->t.t_nproc); |
| 7253 | if (retval > set_nproc) { |
| 7254 | retval = set_nproc; |
| 7255 | } |
| 7256 | if (retval < KMP_MIN_NTH) { |
| 7257 | retval = KMP_MIN_NTH; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7258 | } |
| 7259 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7260 | KB_TRACE(20, ("__kmp_load_balance_nproc: thread limit exit. retval:%d\n", |
| 7261 | retval)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7262 | return retval; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7263 | } |
| 7264 | |
| 7265 | // There is a slight delay in the load balance algorithm in detecting new |
| 7266 | // running procs. The real system load at this instant should be at least as |
| 7267 | // large as the #active omp thread that are available to add to the team. |
| 7268 | if (system_active < team_curr_active) { |
| 7269 | system_active = team_curr_active; |
| 7270 | } |
| 7271 | retval = __kmp_avail_proc - system_active + team_curr_active; |
| 7272 | if (retval > set_nproc) { |
| 7273 | retval = set_nproc; |
| 7274 | } |
| 7275 | if (retval < KMP_MIN_NTH) { |
| 7276 | retval = KMP_MIN_NTH; |
| 7277 | } |
| 7278 | |
| 7279 | KB_TRACE(20, ("__kmp_load_balance_nproc: exit. retval:%d\n", retval)); |
| 7280 | return retval; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7281 | } // __kmp_load_balance_nproc() |
| 7282 | |
| 7283 | #endif /* USE_LOAD_BALANCE */ |
| 7284 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7285 | /* ------------------------------------------------------------------------ */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7286 | |
| 7287 | /* NOTE: this is called with the __kmp_init_lock held */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7288 | void __kmp_cleanup(void) { |
| 7289 | int f; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7290 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7291 | KA_TRACE(10, ("__kmp_cleanup: enter\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7292 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7293 | if (TCR_4(__kmp_init_parallel)) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7294 | #if KMP_HANDLE_SIGNALS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7295 | __kmp_remove_signals(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7296 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7297 | TCW_4(__kmp_init_parallel, FALSE); |
| 7298 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7299 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7300 | if (TCR_4(__kmp_init_middle)) { |
Alp Toker | 763b939 | 2014-02-28 09:42:41 +0000 | [diff] [blame] | 7301 | #if KMP_AFFINITY_SUPPORTED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7302 | __kmp_affinity_uninitialize(); |
Alp Toker | 763b939 | 2014-02-28 09:42:41 +0000 | [diff] [blame] | 7303 | #endif /* KMP_AFFINITY_SUPPORTED */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7304 | __kmp_cleanup_hierarchy(); |
| 7305 | TCW_4(__kmp_init_middle, FALSE); |
| 7306 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7307 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7308 | KA_TRACE(10, ("__kmp_cleanup: go serial cleanup\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7309 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7310 | if (__kmp_init_serial) { |
| 7311 | __kmp_runtime_destroy(); |
| 7312 | __kmp_init_serial = FALSE; |
| 7313 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7314 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7315 | for (f = 0; f < __kmp_threads_capacity; f++) { |
| 7316 | if (__kmp_root[f] != NULL) { |
| 7317 | __kmp_free(__kmp_root[f]); |
| 7318 | __kmp_root[f] = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7319 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7320 | } |
| 7321 | __kmp_free(__kmp_threads); |
| 7322 | // __kmp_threads and __kmp_root were allocated at once, as single block, so |
| 7323 | // there is no need in freeing __kmp_root. |
| 7324 | __kmp_threads = NULL; |
| 7325 | __kmp_root = NULL; |
| 7326 | __kmp_threads_capacity = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7327 | |
Andrey Churbanov | 5c56fb5 | 2015-02-20 18:05:17 +0000 | [diff] [blame] | 7328 | #if KMP_USE_DYNAMIC_LOCK |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7329 | __kmp_cleanup_indirect_user_locks(); |
Andrey Churbanov | 5c56fb5 | 2015-02-20 18:05:17 +0000 | [diff] [blame] | 7330 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7331 | __kmp_cleanup_user_locks(); |
Andrey Churbanov | 5c56fb5 | 2015-02-20 18:05:17 +0000 | [diff] [blame] | 7332 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7333 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7334 | #if KMP_AFFINITY_SUPPORTED |
Andrey Churbanov | c47afcd | 2017-07-03 11:24:08 +0000 | [diff] [blame] | 7335 | KMP_INTERNAL_FREE(CCAST(char *, __kmp_cpuinfo_file)); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7336 | __kmp_cpuinfo_file = NULL; |
| 7337 | #endif /* KMP_AFFINITY_SUPPORTED */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7338 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7339 | #if KMP_USE_ADAPTIVE_LOCKS |
| 7340 | #if KMP_DEBUG_ADAPTIVE_LOCKS |
| 7341 | __kmp_print_speculative_stats(); |
| 7342 | #endif |
| 7343 | #endif |
| 7344 | KMP_INTERNAL_FREE(__kmp_nested_nth.nth); |
| 7345 | __kmp_nested_nth.nth = NULL; |
| 7346 | __kmp_nested_nth.size = 0; |
| 7347 | __kmp_nested_nth.used = 0; |
| 7348 | KMP_INTERNAL_FREE(__kmp_nested_proc_bind.bind_types); |
| 7349 | __kmp_nested_proc_bind.bind_types = NULL; |
| 7350 | __kmp_nested_proc_bind.size = 0; |
| 7351 | __kmp_nested_proc_bind.used = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7352 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7353 | __kmp_i18n_catclose(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7354 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 7355 | #if KMP_STATS_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7356 | __kmp_stats_fini(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 7357 | #endif |
| 7358 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7359 | KA_TRACE(10, ("__kmp_cleanup: exit\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7360 | } |
| 7361 | |
| 7362 | /* ------------------------------------------------------------------------ */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7363 | |
| 7364 | int __kmp_ignore_mppbeg(void) { |
| 7365 | char *env; |
| 7366 | |
| 7367 | if ((env = getenv("KMP_IGNORE_MPPBEG")) != NULL) { |
| 7368 | if (__kmp_str_match_false(env)) |
| 7369 | return FALSE; |
| 7370 | } |
| 7371 | // By default __kmpc_begin() is no-op. |
| 7372 | return TRUE; |
| 7373 | } |
| 7374 | |
| 7375 | int __kmp_ignore_mppend(void) { |
| 7376 | char *env; |
| 7377 | |
| 7378 | if ((env = getenv("KMP_IGNORE_MPPEND")) != NULL) { |
| 7379 | if (__kmp_str_match_false(env)) |
| 7380 | return FALSE; |
| 7381 | } |
| 7382 | // By default __kmpc_end() is no-op. |
| 7383 | return TRUE; |
| 7384 | } |
| 7385 | |
| 7386 | void __kmp_internal_begin(void) { |
| 7387 | int gtid; |
| 7388 | kmp_root_t *root; |
| 7389 | |
| 7390 | /* this is a very important step as it will register new sibling threads |
| 7391 | and assign these new uber threads a new gtid */ |
| 7392 | gtid = __kmp_entry_gtid(); |
| 7393 | root = __kmp_threads[gtid]->th.th_root; |
| 7394 | KMP_ASSERT(KMP_UBER_GTID(gtid)); |
| 7395 | |
| 7396 | if (root->r.r_begin) |
| 7397 | return; |
| 7398 | __kmp_acquire_lock(&root->r.r_begin_lock, gtid); |
| 7399 | if (root->r.r_begin) { |
| 7400 | __kmp_release_lock(&root->r.r_begin_lock, gtid); |
| 7401 | return; |
| 7402 | } |
| 7403 | |
| 7404 | root->r.r_begin = TRUE; |
| 7405 | |
| 7406 | __kmp_release_lock(&root->r.r_begin_lock, gtid); |
| 7407 | } |
| 7408 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7409 | /* ------------------------------------------------------------------------ */ |
| 7410 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7411 | void __kmp_user_set_library(enum library_type arg) { |
| 7412 | int gtid; |
| 7413 | kmp_root_t *root; |
| 7414 | kmp_info_t *thread; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7415 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7416 | /* first, make sure we are initialized so we can get our gtid */ |
| 7417 | |
| 7418 | gtid = __kmp_entry_gtid(); |
| 7419 | thread = __kmp_threads[gtid]; |
| 7420 | |
| 7421 | root = thread->th.th_root; |
| 7422 | |
| 7423 | KA_TRACE(20, ("__kmp_user_set_library: enter T#%d, arg: %d, %d\n", gtid, arg, |
| 7424 | library_serial)); |
| 7425 | if (root->r.r_in_parallel) { /* Must be called in serial section of top-level |
| 7426 | thread */ |
| 7427 | KMP_WARNING(SetLibraryIncorrectCall); |
| 7428 | return; |
| 7429 | } |
| 7430 | |
| 7431 | switch (arg) { |
| 7432 | case library_serial: |
| 7433 | thread->th.th_set_nproc = 0; |
| 7434 | set__nproc(thread, 1); |
| 7435 | break; |
| 7436 | case library_turnaround: |
| 7437 | thread->th.th_set_nproc = 0; |
| 7438 | set__nproc(thread, __kmp_dflt_team_nth ? __kmp_dflt_team_nth |
| 7439 | : __kmp_dflt_team_nth_ub); |
| 7440 | break; |
| 7441 | case library_throughput: |
| 7442 | thread->th.th_set_nproc = 0; |
| 7443 | set__nproc(thread, __kmp_dflt_team_nth ? __kmp_dflt_team_nth |
| 7444 | : __kmp_dflt_team_nth_ub); |
| 7445 | break; |
| 7446 | default: |
| 7447 | KMP_FATAL(UnknownLibraryType, arg); |
| 7448 | } |
| 7449 | |
| 7450 | __kmp_aux_set_library(arg); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7451 | } |
| 7452 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7453 | void __kmp_aux_set_stacksize(size_t arg) { |
| 7454 | if (!__kmp_init_serial) |
| 7455 | __kmp_serial_initialize(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7456 | |
| 7457 | #if KMP_OS_DARWIN |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7458 | if (arg & (0x1000 - 1)) { |
| 7459 | arg &= ~(0x1000 - 1); |
| 7460 | if (arg + 0x1000) /* check for overflow if we round up */ |
| 7461 | arg += 0x1000; |
| 7462 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7463 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7464 | __kmp_acquire_bootstrap_lock(&__kmp_initz_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7465 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7466 | /* only change the default stacksize before the first parallel region */ |
| 7467 | if (!TCR_4(__kmp_init_parallel)) { |
| 7468 | size_t value = arg; /* argument is in bytes */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7469 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7470 | if (value < __kmp_sys_min_stksize) |
| 7471 | value = __kmp_sys_min_stksize; |
| 7472 | else if (value > KMP_MAX_STKSIZE) |
| 7473 | value = KMP_MAX_STKSIZE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7474 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7475 | __kmp_stksize = value; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7476 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7477 | __kmp_env_stksize = TRUE; /* was KMP_STACKSIZE specified? */ |
| 7478 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7479 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7480 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7481 | } |
| 7482 | |
| 7483 | /* set the behaviour of the runtime library */ |
| 7484 | /* TODO this can cause some odd behaviour with sibling parallelism... */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7485 | void __kmp_aux_set_library(enum library_type arg) { |
| 7486 | __kmp_library = arg; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7487 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7488 | switch (__kmp_library) { |
| 7489 | case library_serial: { |
| 7490 | KMP_INFORM(LibraryIsSerial); |
| 7491 | (void)__kmp_change_library(TRUE); |
| 7492 | } break; |
| 7493 | case library_turnaround: |
| 7494 | (void)__kmp_change_library(TRUE); |
| 7495 | break; |
| 7496 | case library_throughput: |
| 7497 | (void)__kmp_change_library(FALSE); |
| 7498 | break; |
| 7499 | default: |
| 7500 | KMP_FATAL(UnknownLibraryType, arg); |
| 7501 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7502 | } |
| 7503 | |
| 7504 | /* ------------------------------------------------------------------------ */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7505 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7506 | void __kmp_aux_set_blocktime(int arg, kmp_info_t *thread, int tid) { |
| 7507 | int blocktime = arg; /* argument is in milliseconds */ |
Jonathan Peyton | e1c7c13 | 2016-10-07 18:12:19 +0000 | [diff] [blame] | 7508 | #if KMP_USE_MONITOR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7509 | int bt_intervals; |
Jonathan Peyton | e1c7c13 | 2016-10-07 18:12:19 +0000 | [diff] [blame] | 7510 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7511 | int bt_set; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7512 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7513 | __kmp_save_internal_controls(thread); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7514 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7515 | /* Normalize and set blocktime for the teams */ |
| 7516 | if (blocktime < KMP_MIN_BLOCKTIME) |
| 7517 | blocktime = KMP_MIN_BLOCKTIME; |
| 7518 | else if (blocktime > KMP_MAX_BLOCKTIME) |
| 7519 | blocktime = KMP_MAX_BLOCKTIME; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7520 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7521 | set__blocktime_team(thread->th.th_team, tid, blocktime); |
| 7522 | set__blocktime_team(thread->th.th_serial_team, 0, blocktime); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7523 | |
Jonathan Peyton | e1c7c13 | 2016-10-07 18:12:19 +0000 | [diff] [blame] | 7524 | #if KMP_USE_MONITOR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7525 | /* Calculate and set blocktime intervals for the teams */ |
| 7526 | bt_intervals = KMP_INTERVALS_FROM_BLOCKTIME(blocktime, __kmp_monitor_wakeups); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7527 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7528 | set__bt_intervals_team(thread->th.th_team, tid, bt_intervals); |
| 7529 | set__bt_intervals_team(thread->th.th_serial_team, 0, bt_intervals); |
Jonathan Peyton | e1c7c13 | 2016-10-07 18:12:19 +0000 | [diff] [blame] | 7530 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7531 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7532 | /* Set whether blocktime has been set to "TRUE" */ |
| 7533 | bt_set = TRUE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7534 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7535 | set__bt_set_team(thread->th.th_team, tid, bt_set); |
| 7536 | set__bt_set_team(thread->th.th_serial_team, 0, bt_set); |
Jonathan Peyton | e1c7c13 | 2016-10-07 18:12:19 +0000 | [diff] [blame] | 7537 | #if KMP_USE_MONITOR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7538 | KF_TRACE(10, ("kmp_set_blocktime: T#%d(%d:%d), blocktime=%d, " |
| 7539 | "bt_intervals=%d, monitor_updates=%d\n", |
| 7540 | __kmp_gtid_from_tid(tid, thread->th.th_team), |
| 7541 | thread->th.th_team->t.t_id, tid, blocktime, bt_intervals, |
| 7542 | __kmp_monitor_wakeups)); |
Samuel Antao | 3351519 | 2016-10-20 13:20:17 +0000 | [diff] [blame] | 7543 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7544 | KF_TRACE(10, ("kmp_set_blocktime: T#%d(%d:%d), blocktime=%d\n", |
| 7545 | __kmp_gtid_from_tid(tid, thread->th.th_team), |
| 7546 | thread->th.th_team->t.t_id, tid, blocktime)); |
Jonathan Peyton | e1c7c13 | 2016-10-07 18:12:19 +0000 | [diff] [blame] | 7547 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7548 | } |
| 7549 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7550 | void __kmp_aux_set_defaults(char const *str, int len) { |
| 7551 | if (!__kmp_init_serial) { |
| 7552 | __kmp_serial_initialize(); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 7553 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7554 | __kmp_env_initialize(str); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7555 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7556 | if (__kmp_settings |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7557 | #if OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7558 | || __kmp_display_env || __kmp_display_env_verbose |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7559 | #endif // OMP_40_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7560 | ) { |
| 7561 | __kmp_env_print(); |
| 7562 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7563 | } // __kmp_aux_set_defaults |
| 7564 | |
| 7565 | /* ------------------------------------------------------------------------ */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7566 | /* internal fast reduction routines */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7567 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7568 | PACKED_REDUCTION_METHOD_T |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7569 | __kmp_determine_reduction_method( |
| 7570 | ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars, size_t reduce_size, |
| 7571 | void *reduce_data, void (*reduce_func)(void *lhs_data, void *rhs_data), |
| 7572 | kmp_critical_name *lck) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7573 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7574 | // Default reduction method: critical construct ( lck != NULL, like in current |
| 7575 | // PAROPT ) |
| 7576 | // If ( reduce_data!=NULL && reduce_func!=NULL ): the tree-reduction method |
| 7577 | // can be selected by RTL |
| 7578 | // If loc->flags contains KMP_IDENT_ATOMIC_REDUCE, the atomic reduce method |
| 7579 | // can be selected by RTL |
| 7580 | // Finally, it's up to OpenMP RTL to make a decision on which method to select |
| 7581 | // among generated by PAROPT. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7582 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7583 | PACKED_REDUCTION_METHOD_T retval; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7584 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7585 | int team_size; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7586 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7587 | KMP_DEBUG_ASSERT(loc); // it would be nice to test ( loc != 0 ) |
| 7588 | KMP_DEBUG_ASSERT(lck); // it would be nice to test ( lck != 0 ) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7589 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7590 | #define FAST_REDUCTION_ATOMIC_METHOD_GENERATED \ |
| 7591 | ((loc->flags & (KMP_IDENT_ATOMIC_REDUCE)) == (KMP_IDENT_ATOMIC_REDUCE)) |
| 7592 | #define FAST_REDUCTION_TREE_METHOD_GENERATED ((reduce_data) && (reduce_func)) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7593 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7594 | retval = critical_reduce_block; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7595 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7596 | // another choice of getting a team size (with 1 dynamic deference) is slower |
| 7597 | team_size = __kmp_get_team_num_threads(global_tid); |
| 7598 | if (team_size == 1) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7599 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7600 | retval = empty_reduce_block; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7601 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7602 | } else { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7603 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7604 | int atomic_available = FAST_REDUCTION_ATOMIC_METHOD_GENERATED; |
| 7605 | int tree_available = FAST_REDUCTION_TREE_METHOD_GENERATED; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7606 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7607 | #if KMP_ARCH_X86_64 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64 || KMP_ARCH_MIPS64 |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7608 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7609 | #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_WINDOWS || \ |
| 7610 | KMP_OS_DARWIN |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7611 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7612 | int teamsize_cutoff = 4; |
Jonathan Peyton | 91b7870 | 2015-06-08 19:39:07 +0000 | [diff] [blame] | 7613 | |
Jonathan Peyton | 492e0a3 | 2017-06-13 17:17:26 +0000 | [diff] [blame] | 7614 | #if KMP_MIC_SUPPORTED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7615 | if (__kmp_mic_type != non_mic) { |
| 7616 | teamsize_cutoff = 8; |
| 7617 | } |
Andrey Churbanov | 613edeb | 2015-02-20 18:14:43 +0000 | [diff] [blame] | 7618 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7619 | if (tree_available) { |
| 7620 | if (team_size <= teamsize_cutoff) { |
| 7621 | if (atomic_available) { |
| 7622 | retval = atomic_reduce_block; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7623 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7624 | } else { |
| 7625 | retval = TREE_REDUCE_BLOCK_WITH_REDUCTION_BARRIER; |
| 7626 | } |
| 7627 | } else if (atomic_available) { |
| 7628 | retval = atomic_reduce_block; |
| 7629 | } |
| 7630 | #else |
| 7631 | #error "Unknown or unsupported OS" |
| 7632 | #endif // KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_WINDOWS || |
| 7633 | // KMP_OS_DARWIN |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7634 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7635 | #elif KMP_ARCH_X86 || KMP_ARCH_ARM || KMP_ARCH_AARCH || KMP_ARCH_MIPS |
| 7636 | |
| 7637 | #if KMP_OS_LINUX || KMP_OS_WINDOWS |
| 7638 | |
| 7639 | // basic tuning |
| 7640 | |
| 7641 | if (atomic_available) { |
| 7642 | if (num_vars <= 2) { // && ( team_size <= 8 ) due to false-sharing ??? |
| 7643 | retval = atomic_reduce_block; |
| 7644 | } |
| 7645 | } // otherwise: use critical section |
| 7646 | |
| 7647 | #elif KMP_OS_DARWIN |
| 7648 | |
| 7649 | if (atomic_available && (num_vars <= 3)) { |
| 7650 | retval = atomic_reduce_block; |
| 7651 | } else if (tree_available) { |
| 7652 | if ((reduce_size > (9 * sizeof(kmp_real64))) && |
| 7653 | (reduce_size < (2000 * sizeof(kmp_real64)))) { |
| 7654 | retval = TREE_REDUCE_BLOCK_WITH_PLAIN_BARRIER; |
| 7655 | } |
| 7656 | } // otherwise: use critical section |
| 7657 | |
| 7658 | #else |
| 7659 | #error "Unknown or unsupported OS" |
| 7660 | #endif |
| 7661 | |
| 7662 | #else |
| 7663 | #error "Unknown or unsupported architecture" |
| 7664 | #endif |
| 7665 | } |
| 7666 | |
| 7667 | // KMP_FORCE_REDUCTION |
| 7668 | |
| 7669 | // If the team is serialized (team_size == 1), ignore the forced reduction |
| 7670 | // method and stay with the unsynchronized method (empty_reduce_block) |
| 7671 | if (__kmp_force_reduction_method != reduction_method_not_defined && |
| 7672 | team_size != 1) { |
| 7673 | |
| 7674 | PACKED_REDUCTION_METHOD_T forced_retval = critical_reduce_block; |
| 7675 | |
| 7676 | int atomic_available, tree_available; |
| 7677 | |
| 7678 | switch ((forced_retval = __kmp_force_reduction_method)) { |
| 7679 | case critical_reduce_block: |
| 7680 | KMP_ASSERT(lck); // lck should be != 0 |
| 7681 | break; |
| 7682 | |
| 7683 | case atomic_reduce_block: |
| 7684 | atomic_available = FAST_REDUCTION_ATOMIC_METHOD_GENERATED; |
| 7685 | if (!atomic_available) { |
| 7686 | KMP_WARNING(RedMethodNotSupported, "atomic"); |
| 7687 | forced_retval = critical_reduce_block; |
| 7688 | } |
| 7689 | break; |
| 7690 | |
| 7691 | case tree_reduce_block: |
| 7692 | tree_available = FAST_REDUCTION_TREE_METHOD_GENERATED; |
| 7693 | if (!tree_available) { |
| 7694 | KMP_WARNING(RedMethodNotSupported, "tree"); |
| 7695 | forced_retval = critical_reduce_block; |
| 7696 | } else { |
| 7697 | #if KMP_FAST_REDUCTION_BARRIER |
| 7698 | forced_retval = TREE_REDUCE_BLOCK_WITH_REDUCTION_BARRIER; |
| 7699 | #endif |
| 7700 | } |
| 7701 | break; |
| 7702 | |
| 7703 | default: |
| 7704 | KMP_ASSERT(0); // "unsupported method specified" |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7705 | } |
| 7706 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7707 | retval = forced_retval; |
| 7708 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7709 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7710 | KA_TRACE(10, ("reduction method selected=%08x\n", retval)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7711 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7712 | #undef FAST_REDUCTION_TREE_METHOD_GENERATED |
| 7713 | #undef FAST_REDUCTION_ATOMIC_METHOD_GENERATED |
| 7714 | |
| 7715 | return (retval); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7716 | } |
| 7717 | |
| 7718 | // this function is for testing set/get/determine reduce method |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 7719 | kmp_int32 __kmp_get_reduce_method(void) { |
| 7720 | return ((__kmp_entry_thread()->th.th_local.packed_reduction_method) >> 8); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 7721 | } |