aboutsummaryrefslogtreecommitdiff
path: root/libgomp/teams.c
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2021-11-11 13:57:31 +0100
committerJakub Jelinek <jakub@redhat.com>2021-11-11 13:57:31 +0100
commitfa4fcb111ad4ef015c1caf7c4b5a60d35b3aa997 (patch)
tree47cf272646cfeb4696cf717425f5ddfb2de4c70d /libgomp/teams.c
parent3e5a19053310ad090b930fbecebceb28bd1b91a4 (diff)
libgomp: Use TLS storage for omp_get_num_teams()/omp_get_team_num() values
When thinking about GOMP_teams3, I've realized that using global variables for the values returned by omp_get_num_teams()/omp_get_team_num() calls is incorrect even with our right now dumb way of implementing host teams. The problems are two, one is if host teams is used from multiple pthread_create created threads - the spec says that host teams can't be nested inside of explicit parallel or other teams constructs, but with pthread_create the standard says obviously nothing about it. Another more important thing is host fallback, right now we don't do anything for omp_get_num_teams() or omp_get_team_num() which was fine before host teams was introduced and the 5.1 requirement that num_teams clause specifies minimum of teams, but with the global vars it means inside of target teams num_teams (2) we happily return omp_get_num_teams() == 4 if the target teams is inside of host teams with num_teams(4). With target fallback being invoked from parallel regions global vars simply can't work right on the host. So, this patch moves them to struct gomp_thread and propagates those for parallel to child threads. For host fallback, the implicit zeroing of *thr results in us returning omp_get_num_teams () == 1 and omp_get_team_num () == 0 which is fine for target teams without num_teams clause, for target teams with num_teams clause something to work on and for target without teams nested in it I've asked on omp-lang what should be done. 2021-11-11 Jakub Jelinek <jakub@redhat.com> * libgomp.h (struct gomp_thread): Add num_teams and team_num members. * team.c (struct gomp_thread_start_data): Likewise. (gomp_thread_start): Initialize thr->num_teams and thr->team_num. (gomp_team_start): Initialize start_data->num_teams and start_data->team_num. Update nthr->num_teams and nthr->team_num. * teams.c (gomp_num_teams, gomp_team_num): Remove. (GOMP_teams_reg): Set and restore thr->num_teams and thr->team_num instead of gomp_num_teams and gomp_team_num. (omp_get_num_teams): Use thr->num_teams + 1 instead of gomp_num_teams. (omp_get_team_num): Use thr->team_num instead of gomp_team_num. * testsuite/libgomp.c/teams-4.c: New test.
Diffstat (limited to 'libgomp/teams.c')
-rw-r--r--libgomp/teams.c18
1 files changed, 9 insertions, 9 deletions
diff --git a/libgomp/teams.c b/libgomp/teams.c
index 9409f8ee6aa..a93fb7cc6e1 100644
--- a/libgomp/teams.c
+++ b/libgomp/teams.c
@@ -28,14 +28,12 @@
#include "libgomp.h"
#include <limits.h>
-static unsigned gomp_num_teams = 1, gomp_team_num = 0;
-
void
GOMP_teams_reg (void (*fn) (void *), void *data, unsigned int num_teams,
unsigned int thread_limit, unsigned int flags)
{
+ struct gomp_thread *thr = gomp_thread ();
(void) flags;
- (void) num_teams;
unsigned old_thread_limit_var = 0;
if (thread_limit == 0)
thread_limit = gomp_teams_thread_limit_var;
@@ -48,11 +46,11 @@ GOMP_teams_reg (void (*fn) (void *), void *data, unsigned int num_teams,
}
if (num_teams == 0)
num_teams = gomp_nteams_var ? gomp_nteams_var : 3;
- gomp_num_teams = num_teams;
- for (gomp_team_num = 0; gomp_team_num < num_teams; gomp_team_num++)
+ thr->num_teams = num_teams - 1;
+ for (thr->team_num = 0; thr->team_num < num_teams; thr->team_num++)
fn (data);
- gomp_num_teams = 1;
- gomp_team_num = 0;
+ thr->num_teams = 0;
+ thr->team_num = 0;
if (thread_limit)
{
struct gomp_task_icv *icv = gomp_icv (true);
@@ -63,13 +61,15 @@ GOMP_teams_reg (void (*fn) (void *), void *data, unsigned int num_teams,
int
omp_get_num_teams (void)
{
- return gomp_num_teams;
+ struct gomp_thread *thr = gomp_thread ();
+ return thr->num_teams + 1;
}
int
omp_get_team_num (void)
{
- return gomp_team_num;
+ struct gomp_thread *thr = gomp_thread ();
+ return thr->team_num;
}
ialias (omp_get_num_teams)