From 2f7c077b5adc9598dd9b70a28602ae678a8f8f7b Mon Sep 17 00:00:00 2001 From: Jonathan Peyton Date: Thu, 25 Feb 2016 18:49:52 +0000 Subject: [PATCH] Add new OpenMP 4.5 affinity API This change introduces the new OpenMP 4.5 affinity api surrounding OpenMP Places. There are six new entry points: Typically called in serial region: * omp_get_num_places - returns the number of places available to the execution environment in the place list. * omp_get_place_num_procs - returns the number of processors available to the execution environment in the specified place. * omp_get_place_proc_ids - returns the numerical identifiers of the processors available to the execution environment in the specified place. Typically called inside parallel region: * omp_get_place_num - returns the place number of the place to which the encountering thread is bound. * omp_get_partition_num_places - returns the number of places in the place partition of the innermost implicit task. * omp_get_partition_place_nums - returns the list of place numbers corresponding to the places in the place-var ICV of the innermost implicit task. Differential Revision: http://reviews.llvm.org/D17417 llvm-svn: 261915 --- openmp/runtime/src/dllexports | 6 + openmp/runtime/src/include/41/omp.h.var | 8 ++ openmp/runtime/src/include/41/omp_lib.f.var | 32 +++++ openmp/runtime/src/include/41/omp_lib.f90.var | 32 +++++ openmp/runtime/src/include/41/omp_lib.h.var | 32 +++++ openmp/runtime/src/kmp_ftn_entry.h | 133 ++++++++++++++++++ openmp/runtime/src/kmp_ftn_os.h | 24 ++++ openmp/runtime/src/kmp_runtime.c | 7 + 8 files changed, 274 insertions(+) diff --git a/openmp/runtime/src/dllexports b/openmp/runtime/src/dllexports index f14da7079e11d..a8a70c0a364b7 100644 --- a/openmp/runtime/src/dllexports +++ b/openmp/runtime/src/dllexports @@ -498,6 +498,12 @@ kmp_set_warnings_off 780 omp_init_lock_with_hint 870 omp_init_nest_lock_with_hint 871 omp_get_max_task_priority 872 + omp_get_num_places 873 + omp_get_place_num_procs 874 + omp_get_place_proc_ids 875 + omp_get_place_num 876 + omp_get_partition_num_places 877 + omp_get_partition_place_nums 878 %endif # OMP_41 %ifndef stub diff --git a/openmp/runtime/src/include/41/omp.h.var b/openmp/runtime/src/include/41/omp.h.var index 4597d0eaa81fa..9b7ac71b1a986 100644 --- a/openmp/runtime/src/include/41/omp.h.var +++ b/openmp/runtime/src/include/41/omp.h.var @@ -153,6 +153,14 @@ extern omp_proc_bind_t __KAI_KMPC_CONVENTION omp_get_proc_bind (void); + /* OpenMP 4.5 affinity API */ + extern int __KAI_KMPC_CONVENTION omp_get_num_places (void); + extern int __KAI_KMPC_CONVENTION omp_get_place_num_procs (int); + extern void __KAI_KMPC_CONVENTION omp_get_place_proc_ids (int, int *); + extern int __KAI_KMPC_CONVENTION omp_get_place_num (void); + extern int __KAI_KMPC_CONVENTION omp_get_partition_num_places (void); + extern void __KAI_KMPC_CONVENTION omp_get_partition_place_nums (int *); + extern void * __KAI_KMPC_CONVENTION kmp_malloc (size_t); extern void * __KAI_KMPC_CONVENTION kmp_calloc (size_t, size_t); extern void * __KAI_KMPC_CONVENTION kmp_realloc (void *, size_t); diff --git a/openmp/runtime/src/include/41/omp_lib.f.var b/openmp/runtime/src/include/41/omp_lib.f.var index d3406e0060131..fe705ad65af21 100644 --- a/openmp/runtime/src/include/41/omp_lib.f.var +++ b/openmp/runtime/src/include/41/omp_lib.f.var @@ -180,6 +180,38 @@ integer (kind=omp_proc_bind_kind) omp_get_proc_bind end function omp_get_proc_bind + function omp_get_num_places() + use omp_lib_kinds + integer (kind=omp_integer_kind) omp_get_num_places + end function omp_get_num_places + + function omp_get_place_num_procs(place_num) + use omp_lib_kinds + integer (kind=omp_integer_kind) place_num + integer (kind=omp_integer_kind) omp_get_place_num_procs + end function omp_get_place_num_procs + + subroutine omp_get_place_proc_ids(place_num, ids) + use omp_lib_kinds + integer (kind=omp_integer_kind) place_num + integer (kind=kmp_pointer_kind) ids + end subroutine omp_get_place_proc_ids + + function omp_get_place_num() + use omp_lib_kinds + integer (kind=omp_integer_kind) omp_get_place_num + end function omp_get_place_num + + function omp_get_partition_num_places() + use omp_lib_kinds + integer (kind=omp_integer_kind) omp_get_partition_num_places + end function omp_get_partition_num_places + + subroutine omp_get_partition_place_nums(place_nums) + use omp_lib_kinds + integer (kind=kmp_pointer_kind) place_nums + end subroutine omp_get_partition_place_nums + function omp_get_wtime() double precision omp_get_wtime end function omp_get_wtime diff --git a/openmp/runtime/src/include/41/omp_lib.f90.var b/openmp/runtime/src/include/41/omp_lib.f90.var index 18770e4c979da..737a68e504b05 100644 --- a/openmp/runtime/src/include/41/omp_lib.f90.var +++ b/openmp/runtime/src/include/41/omp_lib.f90.var @@ -183,6 +183,38 @@ integer (kind=omp_proc_bind_kind) omp_get_proc_bind end function omp_get_proc_bind + function omp_get_num_places() bind(c) + use omp_lib_kinds + integer (kind=omp_integer_kind) omp_get_num_places + end function omp_get_num_places + + function omp_get_place_num_procs(place_num) bind(c) + use omp_lib_kinds + integer (kind=omp_integer_kind), value :: place_num + integer (kind=omp_integer_kind) omp_get_place_num_procs + end function omp_get_place_num_procs + + subroutine omp_get_place_proc_ids(place_num, ids) bind(c) + use omp_lib_kinds + integer (kind=omp_integer_kind), value :: place_num + integer (kind=kmp_pointer_kind) ids + end subroutine omp_get_place_proc_ids + + function omp_get_place_num() bind(c) + use omp_lib_kinds + integer (kind=omp_integer_kind) omp_get_place_num + end function omp_get_place_num + + function omp_get_partition_num_places() bind(c) + use omp_lib_kinds + integer (kind=omp_integer_kind) omp_get_partition_num_places + end function omp_get_partition_num_places + + subroutine omp_get_partition_place_nums(place_nums) bind(c) + use omp_lib_kinds + integer (kind=kmp_pointer_kind) place_nums + end subroutine omp_get_partition_place_nums + function omp_get_wtime() bind(c) use omp_lib_kinds real (kind=kmp_double_kind) omp_get_wtime diff --git a/openmp/runtime/src/include/41/omp_lib.h.var b/openmp/runtime/src/include/41/omp_lib.h.var index fbdd8484f2690..a91569dfa46c1 100644 --- a/openmp/runtime/src/include/41/omp_lib.h.var +++ b/openmp/runtime/src/include/41/omp_lib.h.var @@ -172,6 +172,38 @@ integer (kind=omp_proc_bind_kind) omp_get_proc_bind end function omp_get_proc_bind + function omp_get_num_places() bind(c) + import + integer (kind=omp_integer_kind) omp_get_num_places + end function omp_get_num_places + + function omp_get_place_num_procs(place_num) bind(c) + import + integer (kind=omp_integer_kind), value :: place_num + integer (kind=omp_integer_kind) omp_get_place_num_procs + end function omp_get_place_num_procs + + subroutine omp_get_place_proc_ids(place_num, ids) bind(c) + import + integer (kind=omp_integer_kind), value :: place_num + integer (kind=kmp_pointer_kind) ids + end subroutine omp_get_place_proc_ids + + function omp_get_place_num() bind(c) + import + integer (kind=omp_integer_kind) omp_get_place_num + end function omp_get_place_num + + function omp_get_partition_num_places() bind(c) + import + integer (kind=omp_integer_kind) omp_get_partition_num_places + end function omp_get_partition_num_places + + subroutine omp_get_partition_place_nums(place_nums) bind(c) + import + integer (kind=kmp_pointer_kind) place_nums + end subroutine omp_get_partition_place_nums + function omp_get_wtime() bind(c) double precision omp_get_wtime end function omp_get_wtime diff --git a/openmp/runtime/src/kmp_ftn_entry.h b/openmp/runtime/src/kmp_ftn_entry.h index 2711c5d12fad7..e654e7f2e9b16 100644 --- a/openmp/runtime/src/kmp_ftn_entry.h +++ b/openmp/runtime/src/kmp_ftn_entry.h @@ -676,6 +676,139 @@ xexpand(FTN_GET_PROC_BIND)( void ) #endif } +#if OMP_41_ENABLED +int FTN_STDCALL +FTN_GET_NUM_PLACES( void ) +{ + #if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED + return 0; + #else + if ( ! TCR_4(__kmp_init_middle) ) { + __kmp_middle_initialize(); + } + return __kmp_affinity_num_masks; + #endif +} + +int FTN_STDCALL +FTN_GET_PLACE_NUM_PROCS( int place_num ) +{ + #if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED + return 0; + #else + int i; + int retval = 0; + if ( ! TCR_4(__kmp_init_middle) ) { + __kmp_middle_initialize(); + } + if ( place_num < 0 || place_num >= (int)__kmp_affinity_num_masks ) + return 0; + kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity_masks, place_num); + KMP_CPU_SET_ITERATE(i, mask) { + if ( !KMP_CPU_ISSET(i, mask) ) + continue; + ++retval; + } + return retval; + #endif +} + +void FTN_STDCALL +FTN_GET_PLACE_PROC_IDS( int place_num, int *ids ) +{ + #if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED + // Nothing. + #else + int i,j; + if ( ! TCR_4(__kmp_init_middle) ) { + __kmp_middle_initialize(); + } + if ( place_num < 0 || place_num >= (int)__kmp_affinity_num_masks ) + return; + kmp_affin_mask_t *mask = KMP_CPU_INDEX(__kmp_affinity_masks, place_num); + j = 0; + KMP_CPU_SET_ITERATE(i, mask) { + if ( !KMP_CPU_ISSET(i, mask) ) + continue; + ids[j++] = i; + } + #endif +} + +int FTN_STDCALL +FTN_GET_PLACE_NUM( void ) +{ + #if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED + return -1; + #else + int gtid; + kmp_info_t *thread; + if ( ! TCR_4(__kmp_init_middle) ) { + __kmp_middle_initialize(); + } + gtid = __kmp_entry_gtid(); + thread = __kmp_thread_from_gtid(gtid); + if ( thread->th.th_current_place < 0 ) + return -1; + return thread->th.th_current_place; + #endif +} + +int FTN_STDCALL +FTN_GET_PARTITION_NUM_PLACES( void ) +{ + #if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED + return 0; + #else + int gtid, num_places, first_place, last_place; + kmp_info_t *thread; + if ( ! TCR_4(__kmp_init_middle) ) { + __kmp_middle_initialize(); + } + gtid = __kmp_entry_gtid(); + thread = __kmp_thread_from_gtid(gtid); + first_place = thread->th.th_first_place; + last_place = thread->th.th_last_place; + if ( first_place < 0 || last_place < 0 ) + return 0; + if ( first_place <= last_place ) + num_places = last_place - first_place + 1; + else + num_places = __kmp_affinity_num_masks - first_place + last_place + 1; + return num_places; + #endif +} + +void FTN_STDCALL +FTN_GET_PARTITION_PLACE_NUMS( int *place_nums ) { + #if defined(KMP_STUB) || !KMP_AFFINITY_SUPPORTED + // Nothing. + #else + int i, gtid, place_num, first_place, last_place, start, end; + kmp_info_t *thread; + if ( ! TCR_4(__kmp_init_middle) ) { + __kmp_middle_initialize(); + } + gtid = __kmp_entry_gtid(); + thread = __kmp_thread_from_gtid(gtid); + first_place = thread->th.th_first_place; + last_place = thread->th.th_last_place; + if ( first_place < 0 || last_place < 0 ) + return; + if ( first_place <= last_place ) { + start = first_place; + end = last_place; + } else { + start = last_place; + end = first_place; + } + for (i = 0, place_num = start; place_num <= end; ++place_num, ++i) { + place_nums[i] = place_num; + } + #endif +} +#endif + int FTN_STDCALL xexpand(FTN_GET_NUM_TEAMS)( void ) { diff --git a/openmp/runtime/src/kmp_ftn_os.h b/openmp/runtime/src/kmp_ftn_os.h index d9157504cbc40..04a64a7bd9087 100644 --- a/openmp/runtime/src/kmp_ftn_os.h +++ b/openmp/runtime/src/kmp_ftn_os.h @@ -115,6 +115,12 @@ #if OMP_41_ENABLED #define FTN_GET_MAX_TASK_PRIORITY omp_get_max_task_priority + #define FTN_GET_NUM_PLACES omp_get_num_places + #define FTN_GET_PLACE_NUM_PROCS omp_get_place_num_procs + #define FTN_GET_PLACE_PROC_IDS omp_get_place_proc_ids + #define FTN_GET_PLACE_NUM omp_get_place_num + #define FTN_GET_PARTITION_NUM_PLACES omp_get_partition_num_places + #define FTN_GET_PARTITION_PLACE_NUMS omp_get_partition_place_nums #endif #endif /* KMP_FTN_PLAIN */ @@ -216,6 +222,12 @@ #if OMP_41_ENABLED #define FTN_GET_MAX_TASK_PRIORITY omp_get_max_task_priority_ + #define FTN_GET_NUM_PLACES omp_get_num_places_ + #define FTN_GET_PLACE_NUM_PROCS omp_get_place_num_procs_ + #define FTN_GET_PLACE_PROC_IDS omp_get_place_proc_ids_ + #define FTN_GET_PLACE_NUM omp_get_place_num_ + #define FTN_GET_PARTITION_NUM_PLACES omp_get_partition_num_places_ + #define FTN_GET_PARTITION_PLACE_NUMS omp_get_partition_place_nums_ #endif #endif /* KMP_FTN_APPEND */ @@ -317,6 +329,12 @@ #if OMP_41_ENABLED #define FTN_GET_MAX_TASK_PRIORITY OMP_GET_MAX_TASK_PRIORITY + #define FTN_GET_NUM_PLACES OMP_GET_NUM_PLACES + #define FTN_GET_PLACE_NUM_PROCS OMP_GET_PLACE_NUM_PROCS + #define FTN_GET_PLACE_PROC_IDS OMP_GET_PLACE_PROC_IDS + #define FTN_GET_PLACE_NUM OMP_GET_PLACE_NUM + #define FTN_GET_PARTITION_NUM_PLACES OMP_GET_PARTITION_NUM_PLACES + #define FTN_GET_PARTITION_PLACE_NUMS OMP_GET_PARTITION_PLACE_NUMS #endif #endif /* KMP_FTN_UPPER */ @@ -418,6 +436,12 @@ #if OMP_41_ENABLED #define FTN_GET_MAX_TASK_PRIORITY OMP_GET_MAX_TASK_PRIORITY_ + #define FTN_GET_NUM_PLACES OMP_GET_NUM_PLACES_ + #define FTN_GET_PLACE_NUM_PROCS OMP_GET_PLACE_NUM_PROCS_ + #define FTN_GET_PLACE_PROC_IDS OMP_GET_PLACE_PROC_IDS_ + #define FTN_GET_PLACE_NUM OMP_GET_PLACE_NUM_ + #define FTN_GET_PARTITION_NUM_PLACES OMP_GET_PARTITION_NUM_PLACES_ + #define FTN_GET_PARTITION_PLACE_NUMS OMP_GET_PARTITION_PLACE_NUMS_ #endif #endif /* KMP_FTN_UAPPEND */ diff --git a/openmp/runtime/src/kmp_runtime.c b/openmp/runtime/src/kmp_runtime.c index 8801ed86b120a..7a2fa7bac55eb 100644 --- a/openmp/runtime/src/kmp_runtime.c +++ b/openmp/runtime/src/kmp_runtime.c @@ -3834,6 +3834,13 @@ __kmp_register_root( int initial_thread ) KMP_DEBUG_ASSERT( root->r.r_hot_team->t.t_bar[ bs_forkjoin_barrier ].b_arrived == KMP_INIT_BARRIER_STATE ); #if KMP_AFFINITY_SUPPORTED +# if OMP_40_ENABLED + root_thread->th.th_current_place = KMP_PLACE_UNDEFINED; + root_thread->th.th_new_place = KMP_PLACE_UNDEFINED; + root_thread->th.th_first_place = KMP_PLACE_UNDEFINED; + root_thread->th.th_last_place = KMP_PLACE_UNDEFINED; +# endif + if ( TCR_4(__kmp_init_middle) ) { __kmp_affinity_set_init_mask( gtid, TRUE ); }