Skip to content

Commit

Permalink
[OpenMP][Stats] Fix stats gathering for distribute and team clause
Browse files Browse the repository at this point in the history
The distribute clause needs an explicit push of a timer. The teams
clause needs a timer added and also, similarly to parallel, exchanged
with the serial timer when encountered so that serial regions are
counted properly.

Differential Revision: https://reviews.llvm.org/D59801

llvm-svn: 357621
  • Loading branch information
jpeyton52 committed Apr 3, 2019
1 parent 3d90e7e commit d2b53ca
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 32 deletions.
15 changes: 15 additions & 0 deletions openmp/runtime/src/kmp_csupport.cpp
Expand Up @@ -382,7 +382,15 @@ void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro microtask,
va_list ap;
va_start(ap, microtask);

#if KMP_STATS_ENABLED
KMP_COUNT_BLOCK(OMP_TEAMS);
stats_state_e previous_state = KMP_GET_THREAD_STATE();
if (previous_state == stats_state_e::SERIAL_REGION) {
KMP_EXCHANGE_PARTITIONED_TIMER(OMP_teams_overhead);
} else {
KMP_PUSH_PARTITIONED_TIMER(OMP_teams_overhead);
}
#endif

// remember teams entry point and nesting level
this_thr->th.th_teams_microtask = microtask;
Expand Down Expand Up @@ -442,6 +450,13 @@ void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro microtask,
this_thr->th.th_teams_level = 0;
*(kmp_int64 *)(&this_thr->th.th_teams_size) = 0L;
va_end(ap);
#if KMP_STATS_ENABLED
if (previous_state == stats_state_e::SERIAL_REGION) {
KMP_EXCHANGE_PARTITIONED_TIMER(OMP_serial);
} else {
KMP_POP_PARTITIONED_TIMER();
}
#endif // KMP_STATS_ENABLED
}
#endif /* OMP_40_ENABLED */

Expand Down
6 changes: 6 additions & 0 deletions openmp/runtime/src/kmp_dispatch.cpp
Expand Up @@ -283,6 +283,12 @@ void __kmp_dispatch_init_algorithm(ident_t *loc, int gtid,
}
}

#if KMP_STATS_ENABLED
if (KMP_MASTER_GTID(gtid)) {
KMP_COUNT_VALUE(OMP_loop_dynamic_total_iterations, tc);
}
#endif

pr->u.p.lb = lb;
pr->u.p.ub = ub;
pr->u.p.st = st;
Expand Down
48 changes: 38 additions & 10 deletions openmp/runtime/src/kmp_runtime.cpp
Expand Up @@ -2285,9 +2285,25 @@ int __kmp_fork_call(ident_t *loc, int gtid,
team->t.t_id, team->t.t_pkfn));
} // END of timer KMP_fork_call block

#if KMP_STATS_ENABLED && OMP_40_ENABLED
// If beginning a teams construct, then change thread state
stats_state_e previous_state = KMP_GET_THREAD_STATE();
if (!ap) {
KMP_SET_THREAD_STATE(stats_state_e::TEAMS_REGION);
}
#endif

if (!team->t.t_invoke(gtid)) {
KMP_ASSERT2(0, "cannot invoke microtask for MASTER thread");
}

#if KMP_STATS_ENABLED && OMP_40_ENABLED
// If was beginning of a teams construct, then reset thread state
if (!ap) {
KMP_SET_THREAD_STATE(previous_state);
}
#endif

KA_TRACE(20, ("__kmp_fork_call: T#%d(%d:0) done microtask = %p\n", gtid,
team->t.t_id, team->t.t_pkfn));
KMP_MB(); /* Flush all pending memory write invalidates. */
Expand Down Expand Up @@ -7106,21 +7122,33 @@ int __kmp_invoke_task_func(int gtid) {
}
#endif

{
KMP_TIME_PARTITIONED_BLOCK(OMP_parallel);
KMP_SET_THREAD_STATE_BLOCK(IMPLICIT_TASK);
rc =
__kmp_invoke_microtask((microtask_t)TCR_SYNC_PTR(team->t.t_pkfn), gtid,
tid, (int)team->t.t_argc, (void **)team->t.t_argv
#if KMP_STATS_ENABLED
stats_state_e previous_state = KMP_GET_THREAD_STATE();
if (previous_state == stats_state_e::TEAMS_REGION) {
KMP_PUSH_PARTITIONED_TIMER(OMP_teams);
} else {
KMP_PUSH_PARTITIONED_TIMER(OMP_parallel);
}
KMP_SET_THREAD_STATE(IMPLICIT_TASK);
#endif

rc = __kmp_invoke_microtask((microtask_t)TCR_SYNC_PTR(team->t.t_pkfn), gtid,
tid, (int)team->t.t_argc, (void **)team->t.t_argv
#if OMPT_SUPPORT
,
exit_runtime_p
,
exit_runtime_p
#endif
);
);
#if OMPT_SUPPORT
*exit_runtime_p = NULL;
*exit_runtime_p = NULL;
#endif

#if KMP_STATS_ENABLED
if (previous_state == stats_state_e::TEAMS_REGION) {
KMP_SET_THREAD_STATE(previous_state);
}
KMP_POP_PARTITIONED_TIMER();
#endif

#if USE_ITT_BUILD
if (__itt_stack_caller_create_ptr) {
Expand Down
56 changes: 36 additions & 20 deletions openmp/runtime/src/kmp_sched.cpp
Expand Up @@ -38,6 +38,29 @@ char const *traits_t<long>::spec = "ld";
//-------------------------------------------------------------------------
#endif

#if KMP_STATS_ENABLED
#define KMP_STATS_LOOP_END(stat) \
{ \
kmp_int64 t; \
kmp_int64 u = (kmp_int64)(*pupper); \
kmp_int64 l = (kmp_int64)(*plower); \
kmp_int64 i = (kmp_int64)incr; \
if (i == 1) { \
t = u - l + 1; \
} else if (i == -1) { \
t = l - u + 1; \
} else if (i > 0) { \
t = (u - l) / i + 1; \
} else { \
t = (l - u) / (-i) + 1; \
} \
KMP_COUNT_VALUE(stat, t); \
KMP_POP_PARTITIONED_TIMER(); \
}
#else
#define KMP_STATS_LOOP_END(stat) /* Nothing */
#endif

template <typename T>
static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
kmp_int32 schedtype, kmp_int32 *plastiter,
Expand Down Expand Up @@ -151,6 +174,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
&(task_info->task_data), 0, codeptr);
}
#endif
KMP_STATS_LOOP_END(OMP_loop_static_iterations);
return;
}

Expand Down Expand Up @@ -202,6 +226,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
&(task_info->task_data), *pstride, codeptr);
}
#endif
KMP_STATS_LOOP_END(OMP_loop_static_iterations);
return;
}
nth = team->t.t_nproc;
Expand Down Expand Up @@ -231,6 +256,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
&(task_info->task_data), *pstride, codeptr);
}
#endif
KMP_STATS_LOOP_END(OMP_loop_static_iterations);
return;
}

Expand All @@ -246,6 +272,12 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
trip_count = (UT)(*plower - *pupper) / (-incr) + 1;
}

#if KMP_STATS_ENABLED
if (KMP_MASTER_GTID(gtid)) {
KMP_COUNT_VALUE(OMP_loop_static_total_iterations, trip_count);
}
#endif

if (__kmp_env_consistency_check) {
/* tripcount overflow? */
if (trip_count == 0 && *pupper != *plower) {
Expand Down Expand Up @@ -388,26 +420,7 @@ static void __kmp_for_static_init(ident_t *loc, kmp_int32 global_tid,
}
#endif

#if KMP_STATS_ENABLED
{
kmp_int64 t;
kmp_int64 u = (kmp_int64)(*pupper);
kmp_int64 l = (kmp_int64)(*plower);
kmp_int64 i = (kmp_int64)incr;
/* compute trip count */
if (i == 1) {
t = u - l + 1;
} else if (i == -1) {
t = l - u + 1;
} else if (i > 0) {
t = (u - l) / i + 1;
} else {
t = (l - u) / (-i) + 1;
}
KMP_COUNT_VALUE(OMP_loop_static_iterations, t);
KMP_POP_PARTITIONED_TIMER();
}
#endif
KMP_STATS_LOOP_END(OMP_loop_static_iterations);
return;
}

Expand All @@ -419,6 +432,8 @@ static void __kmp_dist_for_static_init(ident_t *loc, kmp_int32 gtid,
typename traits_t<T>::signed_t incr,
typename traits_t<T>::signed_t chunk) {
KMP_COUNT_BLOCK(OMP_DISTRIBUTE);
KMP_PUSH_PARTITIONED_TIMER(OMP_distribute);
KMP_PUSH_PARTITIONED_TIMER(OMP_distribute_scheduling);
typedef typename traits_t<T>::unsigned_t UT;
typedef typename traits_t<T>::signed_t ST;
kmp_uint32 tid;
Expand Down Expand Up @@ -648,6 +663,7 @@ end:;
}
#endif
KE_TRACE(10, ("__kmpc_dist_for_static_init: T#%d return\n", gtid));
KMP_STATS_LOOP_END(OMP_distribute_iterations);
return;
}

Expand Down
1 change: 0 additions & 1 deletion openmp/runtime/src/kmp_stats.cpp
Expand Up @@ -546,7 +546,6 @@ static std::string generateFilename(char const *prototype,
// of __kmp_stats_global_output
void kmp_stats_output_module::init() {

fprintf(stderr, "*** Stats enabled OpenMP* runtime ***\n");
char *statsFileName = getenv("KMP_STATS_FILE");
eventsFileName = getenv("KMP_STATS_EVENTS_FILE");
plotFileName = getenv("KMP_STATS_PLOT_FILE");
Expand Down
13 changes: 12 additions & 1 deletion openmp/runtime/src/kmp_stats.h
Expand Up @@ -69,7 +69,8 @@ enum stats_state_e {
TASKYIELD,
TASKGROUP,
IMPLICIT_TASK,
EXPLICIT_TASK
EXPLICIT_TASK,
TEAMS_REGION
};

/*!
Expand Down Expand Up @@ -137,10 +138,14 @@ enum stats_state_e {
macro (OMP_worker_thread_life, stats_flags_e::logEvent, arg) \
macro (OMP_parallel, stats_flags_e::logEvent, arg) \
macro (OMP_parallel_overhead, stats_flags_e::logEvent, arg) \
macro (OMP_teams, stats_flags_e::logEvent, arg) \
macro (OMP_teams_overhead, stats_flags_e::logEvent, arg) \
macro (OMP_loop_static, 0, arg) \
macro (OMP_loop_static_scheduling, 0, arg) \
macro (OMP_loop_dynamic, 0, arg) \
macro (OMP_loop_dynamic_scheduling, 0, arg) \
macro (OMP_distribute, 0, arg) \
macro (OMP_distribute_scheduling, 0, arg) \
macro (OMP_critical, 0, arg) \
macro (OMP_critical_wait, 0, arg) \
macro (OMP_single, 0, arg) \
Expand All @@ -163,8 +168,14 @@ enum stats_state_e {
arg) \
macro (OMP_loop_static_iterations, \
stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \
macro (OMP_loop_static_total_iterations, \
stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \
macro (OMP_loop_dynamic_iterations, \
stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \
macro (OMP_loop_dynamic_total_iterations, \
stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \
macro (OMP_distribute_iterations, \
stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \
KMP_FOREACH_DEVELOPER_TIMER(macro, arg)
// clang-format on

Expand Down

0 comments on commit d2b53ca

Please sign in to comment.