https://gcc.gnu.org/g:00513d11fb2b5a6b9a036ff3cb170e06e78ca18b

commit r17-5916-g00513d11fb2b5a6b9a036ff3cb170e06e78ca18b
Author: Tobias Burnus <[email protected]>
Date:   Wed Nov 4 09:30:19 2026 +0100

    libgomp: Add omp_{s,g}et_device_{num_teams,teams_thread_limit}
    
    Add the four OpenMP 6.0 API routines.
    
    Avoid pointless copy back of device ICV at the end of GOMP_target_ext
    as the host-used device ICV variables are not changeable from the
    device side.
    Doing so, revealed that three tests wrongly used the 5.1 API
    routines omp_set_{num_teams,teams_thread_limit} inside a target
    region. However, instead of scratching those parts of the patches,
    those call could be replaced by host-side called to the here
    added new 6.0 API routine!
    
    Also fix documentation for omp_set_num_teams (zero is permitted).
    
    gcc/ChangeLog:
    
            * omp-general.cc (omp_runtime_api_procname): Add
            omp_{s,g}et_device_{num_teams,teams_thread_limit}.
    
    libgomp/ChangeLog:
    
            * icv-device.c (omp_set_teams_thread_limit): Add comment.
            * libgomp.map (OMP_6.0): Add
            omp_{s,g}et_device_{num_teams,teams_thread_limit}.
            * libgomp.texi: Add some more @refs related to new new routines.
            (Device Information Routines): Document new routines.
            (6.0 Impl. Status): Mark them as implemented.
            * omp.h.in: Add prototypes for them.
            * omp_lib.f90.in: Add iterface for them.
            * omp_lib.h.in: Likewise.
            * target.c (gomp_get_offload_icv_item): Add usage comment.
            (gomp_copy_back_icvs): Return gomp_offload_icvs instead of void.
            (gomp_copy_icvs_to_device): New.
            (GOMP_target_ext): Remove gomp_copy_back_icvs.
            (omp_set_device_num_teams, omp_get_device_num_teams,
            omp_set_device_teams_thread_limit,
            omp_get_device_teams_thread_limit): New.
            * testsuite/libgomp.c-c++-common/icv-5.c: Change from invalid ICV
            setting on the device to setting it on the host.
            * testsuite/libgomp.c-c++-common/icv-6.c: Likewise.
            * testsuite/libgomp.fortran/icv-6.f90: Likewise.
            * 
testsuite/libgomp.c-c++-common/omp-device-num-teams-thread-limit-1.c:
            New test.
            * testsuite/libgomp.fortran/omp-device-num-teams-thread-limit-1.f90:
            New test.

Diff:
---
 gcc/omp-general.cc                                 |   4 +
 libgomp/icv-device.c                               |   3 +
 libgomp/libgomp.map                                |   4 +
 libgomp/libgomp.texi                               | 189 ++++++++++++++++++--
 libgomp/omp.h.in                                   |   4 +
 libgomp/omp_lib.f90.in                             |  35 ++++
 libgomp/omp_lib.h.in                               |  36 ++++
 libgomp/target.c                                   | 148 +++++++++++++++-
 libgomp/testsuite/libgomp.c-c++-common/icv-5.c     |  18 +-
 libgomp/testsuite/libgomp.c-c++-common/icv-6.c     |  18 +-
 .../omp-device-num-teams-thread-limit-1.c          | 193 ++++++++++++++++++++
 libgomp/testsuite/libgomp.fortran/icv-6.f90        |  26 ++-
 .../omp-device-num-teams-thread-limit-1.f90        | 194 +++++++++++++++++++++
 13 files changed, 843 insertions(+), 29 deletions(-)

diff --git a/gcc/omp-general.cc b/gcc/omp-general.cc
index 9162972dc88d..0a65108b6165 100644
--- a/gcc/omp-general.cc
+++ b/gcc/omp-general.cc
@@ -3982,6 +3982,8 @@ omp_runtime_api_procname (const char *name)
       "calloc",
       "free",
       "get_device_distances",
+      "get_device_num_teams",
+      "get_device_teams_thread_limit",
       "get_interop_int",
       "get_interop_ptr",
       "get_mapped_ptr",
@@ -3994,6 +3996,8 @@ omp_runtime_api_procname (const char *name)
       "get_thread_limit_dim",
       "get_thread_num_dim",
       "realloc",
+      "set_device_num_teams",
+      "set_device_teams_thread_limit",
       "target_alloc",
       "target_associate_ptr",
       "target_disassociate_ptr",
diff --git a/libgomp/icv-device.c b/libgomp/icv-device.c
index 4cf464be8f53..07ab0c239232 100644
--- a/libgomp/icv-device.c
+++ b/libgomp/icv-device.c
@@ -113,6 +113,9 @@ omp_get_teams_thread_limit (void)
 
 ialias (omp_get_teams_thread_limit)
 
+/* Accepts thread_limits = 0 [which is well defined to mean implementation
+   defined], even though only positive values are permitted.  */
+
 void
 omp_set_teams_thread_limit (int thread_limit)
 {
diff --git a/libgomp/libgomp.map b/libgomp/libgomp.map
index 19890c05c76c..68e10d5bb7dd 100644
--- a/libgomp/libgomp.map
+++ b/libgomp/libgomp.map
@@ -245,6 +245,10 @@ OMP_6.0 {
        omp_control_tool;
        omp_control_tool_;
        omp_control_tool_8_;
+       omp_get_device_num_teams;
+       omp_get_device_teams_thread_limit;
+       omp_set_device_num_teams;
+       omp_set_device_teams_thread_limit;
 } OMP_5.2;
 
 OMP_6.1 {
diff --git a/libgomp/libgomp.texi b/libgomp/libgomp.texi
index c276b4a0a2d1..192e8493d0c0 100644
--- a/libgomp/libgomp.texi
+++ b/libgomp/libgomp.texi
@@ -605,7 +605,7 @@ to address of matching mapped list item per 5.1, Sect. 
2.21.7.2 @tab N @tab
       @tab Y @tab
 @item @code{omp_get_device_num_teams}, @code{omp_set_device_num_teams},
       @code{omp_get_device_teams_thread_limit}, and
-      @code{omp_set_device_teams_thread_limit} routines @tab N @tab
+      @code{omp_set_device_teams_thread_limit} routines @tab Y @tab
 @item @code{omp_target_memset} and @code{omp_target_memset_async} routines
       @tab Y @tab
 @item Fortran version of the interop runtime routines @tab Y @tab
@@ -869,7 +869,8 @@ that does not use the clause @code{num_threads}.
 @end multitable
 
 @item @emph{See also}:
-@ref{omp_set_num_threads}, @ref{omp_set_dynamic}, @ref{omp_get_thread_limit}
+@ref{omp_set_num_threads}, @ref{omp_set_dynamic}, @ref{omp_get_thread_limit},
+@ref{omp_get_teams_thread_limit}
 
 @item @emph{Reference}:
 @uref{https://www.openmp.org, OpenMP specification v4.5}, Section 3.2.3.
@@ -1182,7 +1183,8 @@ each team created by a teams construct.
 @end multitable
 
 @item @emph{See also}:
-@ref{omp_set_teams_thread_limit}, @ref{OMP_TEAMS_THREAD_LIMIT}
+@ref{omp_set_teams_thread_limit}, @ref{OMP_TEAMS_THREAD_LIMIT},
+@ref{omp_get_device_teams_thread_limit}
 
 @item @emph{Reference}:
 @uref{https://www.openmp.org, OpenMP specification v5.1}, Section 3.4.6.
@@ -1453,7 +1455,8 @@ region.  They have C linkage and do not throw exceptions.
 @subsection @code{omp_get_num_teams} -- Number of teams
 @table @asis
 @item @emph{Description}:
-Returns the number of teams in the current team region.
+Returns the number of teams in the current team region and one if not inside
+a teams region.
 
 @item @emph{C/C++}:
 @multitable @columnfractions .20 .80
@@ -1466,7 +1469,8 @@ Returns the number of teams in the current team region.
 @end multitable
 
 @item @emph{See also}:
-@ref{omp_get_num_teams_dim}, @ref{OMP_NUM_TEAMS}, @ref{omp_get_team_num}
+@ref{omp_get_num_teams_dim}, @ref{OMP_NUM_TEAMS}, @ref{omp_get_team_num},
+@ref{omp_get_device_num_teams}
 
 @item @emph{Reference}:
 @uref{https://www.openmp.org, OpenMP specification v4.5}, Section 3.2.32.
@@ -1505,7 +1509,10 @@ Returns the team number of the calling thread.
 @item @emph{Description}:
 Specifies the upper bound for number of teams created by the teams construct
 which does not specify a @code{num_teams} clause.  The
-argument of @code{omp_set_num_teams} shall be a positive integer.
+argument of @code{omp_set_num_teams} shall be a non-negative integer; the value
+zero implies an implementation defined number of teams.  This routine may only
+be called on the host, strictly nested in the implicit parallel region of the
+OpenMP program.
 
 @item @emph{C/C++}:
 @multitable @columnfractions .20 .80
@@ -1519,7 +1526,8 @@ argument of @code{omp_set_num_teams} shall be a positive 
integer.
 @end multitable
 
 @item @emph{See also}:
-@ref{OMP_NUM_TEAMS}, @ref{omp_get_num_teams}, @ref{omp_get_max_teams}
+@ref{OMP_NUM_TEAMS}, @ref{omp_get_num_teams}, @ref{omp_get_max_teams},
+@ref{omp_set_device_num_teams}
 
 @item @emph{Reference}:
 @uref{https://www.openmp.org, OpenMP specification v5.1}, Section 3.4.3.
@@ -1560,7 +1568,9 @@ that does not use the clause @code{num_teams}.
 Specifies the upper bound for number of threads that are available
 for each team created by the teams construct which does not specify a
 @code{thread_limit} clause.  The argument of
-@code{omp_set_teams_thread_limit} shall be a positive integer.
+@code{omp_set_teams_thread_limit} shall be a positive integer.  This routine 
may
+only be called on the host, strictly nested in the implicit parallel region of
+the OpenMP program.
 
 @item @emph{C/C++}:
 @multitable @columnfractions .20 .80
@@ -1574,7 +1584,8 @@ for each team created by the teams construct which does 
not specify a
 @end multitable
 
 @item @emph{See also}:
-@ref{OMP_TEAMS_THREAD_LIMIT}, @ref{omp_get_teams_thread_limit}, 
@ref{omp_get_thread_limit}
+@ref{OMP_TEAMS_THREAD_LIMIT}, @ref{omp_get_teams_thread_limit}, 
@ref{omp_get_thread_limit},
+@ref{omp_set_device_teams_thread_limit}
 
 @item @emph{Reference}:
 @uref{https://www.openmp.org, OpenMP specification v5.1}, Section 3.4.5.
@@ -2158,10 +2169,10 @@ They have C linkage and do not throw exceptions.
 * omp_get_uid_from_device::     Obtain the unique id of a device
 * omp_is_initial_device::       Whether executing on the host device
 * omp_get_initial_device::      Device number of host device
-@c * omp_get_device_num_teams::  <fixme>/TR13
-@c * omp_set_device_num_teams::  <fixme>/TR13
-@c * omp_get_device_teams_thread_limit::  <fixme>/TR13
-@c * omp_set_device_teams_thread_limit::  <fixme>/TR13
+* omp_get_device_num_teams::    Number of teams of a device
+* omp_set_device_num_teams::    Set upper teams limit on a device
+* omp_get_device_teams_thread_limit:: Get upper thread limit of a device
+* omp_set_device_teams_thread_limit:: Set upper thread limit of a device
 * omp_get_device_distances:: Obtain the NUMA distance of a device
 @end menu
 
@@ -2441,6 +2452,153 @@ to @code{omp_initial_device}; and otherwise it is 
replaced with a call to
 @end table
 
 
+
+@node omp_get_device_num_teams
+@subsection @code{omp_get_device_num_teams} -- Number of teams of a device
+@table @asis
+@item @emph{Description}:
+Returns the value of the @emph{nteams-var} ICV for the specified device number,
+which is used to determine the number of teams for a @code{teams} region on
+the device specified by the @var{device_num} argument, unless overridden by
+a @code{num_teams} clause on the construct.  If the @var{device_num} argument
+denotes the host device, the routine returns the same value as
+@code{omp_get_num_teams}.  If the @var{device_num} argument specifies a
+non-conforming device number or @code{omp_invalid_device}, the value zero is
+returned.  Invoking this routine on a non-host device has unspecified behavior.
+
+@item @emph{C/C++}:
+@multitable @columnfractions .20 .80
+@item @emph{Prototype}: @tab @code{int omp_get_device_num_teams(int 
device_num)}
+@end multitable
+
+@item @emph{Fortran}:
+@multitable @columnfractions .20 .80
+@item @emph{Interface}: @tab @code{integer function 
omp_get_device_num_teams(device_num)}
+@item                   @tab @code{integer, intent(in) :: device_num}
+@end multitable
+
+@item @emph{See also}:
+@ref{omp_set_device_num_teams}. @ref{omp_get_num_teams},
+@ref{omp_get_num_teams_dim}, @ref{OMP_NUM_TEAMS},
+
+@item @emph{Reference}:
+@uref{https://www.openmp.org, OpenMP specification v6.0}, Section 24.11
+@end table
+
+
+
+@node omp_set_device_num_teams
+@subsection @code{omp_set_device_num_teams} -- Set upper teams limit on a 
device
+@table @asis
+@item @emph{Description}:
+Sets the value of the @emph{nteams-var} ICV for the specified device number, 
which
+specifies the upper bound for number of teams created by a @code{teams} 
construct
+on the device specified by the @var{device_num} argument; this value is used if
+no @code{num_teams} clause is specified on the @code{teams} directive.  The
+@var{num_teams} argument shall be a non-negative integer; the value zero 
implies
+an implementation defined number of teams.  If the @var{device_num} argument
+denotes the host device, the routine is identical to the 
@code{omp_set_num_teams}
+routine.  The @var{device_num} argument must be a conforming device number 
other
+than @code{omp_invalid_device}, else error termination is performed.  Invoking
+this routine on a non-host device has unspecified behavior.  When the
+@code{device_num} arguments denotes the host, the 
@code{omp_set_device_num_teams}
+region must be strictly nested in the implicit parallel region of the OpenMP
+program.
+
+@item @emph{C/C++}:
+@multitable @columnfractions .20 .80
+@item @emph{Prototype}: @tab @code{void omp_set_device_num_teams(int 
num_teams, int device_num)}
+@end multitable
+
+@item @emph{Fortran}:
+@multitable @columnfractions .20 .80
+@item @emph{Interface}: @tab @code{subroutine 
omp_set_device_num_teams(num_teams, device_num)}
+@item                   @tab @code{integer, intent(in) :: num_teams, 
device_num}
+@end multitable
+
+@item @emph{See also}:
+@ref{omp_get_device_num_teams}, @ref{omp_set_num_teams}, 
@ref{omp_get_num_teams},
+@ref{omp_get_max_teams}, @ref{omp_get_supported_teams_dim}, @ref{OMP_NUM_TEAMS}
+
+@item @emph{Reference}:
+@uref{https://www.openmp.org, OpenMP specification v6.0}, Section 24.12
+@end table
+
+
+
+@node omp_get_device_teams_thread_limit
+@subsection @code{omp_get_device_teams_thread_limit} -- Get upper thread limit 
of a device
+@table @asis
+@item @emph{Description}:
+Returns the value of the @emph{teams-thread-limit-var} ICV for the specified
+device number, which is used to limits the number of threads that participate
+in each team created by a @code{teams} construct.  If the @var{device_num} 
argument
+denotes the host device, the routine returns the same value as
+@code{omp_get_teams_thread_limit}.  If the @var{device_num} argument specifies 
a
+non-conforming device number or @code{omp_invalid_device}, the value zero is
+returned.  Invoking this routine on a non-host device has unspecified behavior.
+When the @code{device_num} arguments denotes the host, the
+@code{omp_get_device_teams_thread_limit} region must be strictly nested in the
+implicit parallel region of the OpenMP program.
+
+@item @emph{C/C++}:
+@multitable @columnfractions .20 .80
+@item @emph{Prototype}: @tab @code{int omp_get_device_teams_thread_limit(int 
device_num)}
+@end multitable
+
+@item @emph{Fortran}:
+@multitable @columnfractions .20 .80
+@item @emph{Interface}: @tab @code{integer function 
omp_get_device_teams_thread_limit(device_num)}
+@item                   @tab @code{integer, intent(in) :: device_num}
+@end multitable
+
+@item @emph{See also}:
+@ref{omp_get_device_teams_thread_limit}, @ref{omp_get_teams_thread_limit},
+@ref{OMP_TEAMS_THREAD_LIMIT}, @ref{OMP_THREAD_LIMIT}
+
+@item @emph{Reference}:
+@uref{https://www.openmp.org, OpenMP specification v6.0}, Section 24.13
+@end table
+
+
+
+@node omp_set_device_teams_thread_limit
+@subsection @code{omp_set_device_teams_thread_limit} -- Set upper thread limit 
of a device
+@table @asis
+@item @emph{Description}:
+Sets the value of the @emph{teams-thread-limit-var} ICV for the specified 
device
+number, which specifies the upper bound for number of teams created by a
+@code{teams} construct on the device specified by the @var{device_num} 
argument.
+The @var{thread_limit} argument shall be positive integer.  If the 
@var{device_num}
+argument denotes the host device, the routine is identical to the
+@code{omp_set_teams_thread_limit} routine.  The @var{device_num} argument must 
be a
+conforming device number other than @code{omp_invalid_device}, else error
+termination is performed.  Invoking this routine on a non-host device has
+unspecified behavior.
+
+@item @emph{C/C++}:
+@multitable @columnfractions .20 .80
+@item @emph{Prototype}: @tab @code{void omp_set_device_teams_thread_limit(int 
thread_limit, device_num)}
+@end multitable
+
+@item @emph{Fortran}:
+@multitable @columnfractions .20 .80
+@item @emph{Interface}: @tab @code{subroutine 
omp_set_device_teams_thread_limit(thread_limit, device_num)}
+@item                   @tab @code{integer, intent(in) :: thread_limit, 
device_num}
+@end multitable
+
+@item @emph{See also}:
+@ref{omp_get_device_teams_thread_limit}, @ref{omp_set_teams_thread_limit},
+@ref{omp_get_teams_thread_limit},
+@ref{omp_get_thread_limit}, @ref{omp_get_supported_threads_dim},
+@ref{OMP_TEAMS_THREAD_LIMIT}, @ref{OMP_THREAD_LIMIT}
+
+@item @emph{Reference}:
+@uref{https://www.openmp.org, OpenMP specification v6.0}, Section 24.14
+@end table
+
+
+
 @node omp_get_device_distances
 @subsection @code{omp_get_device_distances} -- Obtain the NUMA distance of a 
device
 @table @asis
@@ -5007,7 +5165,7 @@ be a positive integer.  If undefined it defaults to 0 
which means
 implementation defined upper bound.
 
 @item @emph{See also}:
-@ref{omp_set_num_teams}
+@ref{omp_set_num_teams}, @ref{omp_set_device_num_teams}
 
 @item @emph{Reference}: 
 @uref{https://www.openmp.org, OpenMP specification v5.1}, Section 6.23
@@ -5226,7 +5384,8 @@ the value of 0 is used which stands for an implementation 
defined upper
 limit.
 
 @item @emph{See also}:
-@ref{OMP_THREAD_LIMIT}, @ref{omp_set_teams_thread_limit}
+@ref{OMP_THREAD_LIMIT}, @ref{omp_set_teams_thread_limit},
+@ref{omp_set_device_teams_thread_limit}
 
 @item @emph{Reference}: 
 @uref{https://www.openmp.org, OpenMP specification v5.1}, Section 6.24
diff --git a/libgomp/omp.h.in b/libgomp/omp.h.in
index 6ffec871fd0e..1f7b12ea7103 100644
--- a/libgomp/omp.h.in
+++ b/libgomp/omp.h.in
@@ -342,6 +342,7 @@ extern int omp_get_num_devices (void) __GOMP_NOTHROW;
 extern int omp_get_device_num (void) __GOMP_NOTHROW;
 extern int omp_get_num_teams (void) __GOMP_NOTHROW;
 extern int omp_get_num_teams_dim (int) __GOMP_NOTHROW;
+extern int omp_get_device_num_teams (int) __GOMP_NOTHROW;
 extern int omp_get_team_num (void) __GOMP_NOTHROW;
 extern int omp_get_team_num_dim (int) __GOMP_NOTHROW;
 
@@ -355,9 +356,12 @@ extern __SIZE_TYPE__ omp_get_gprivate_limit (int,
 extern void omp_fulfill_event (omp_event_handle_t) __GOMP_NOTHROW;
 
 extern void omp_set_num_teams (int) __GOMP_NOTHROW;
+extern void omp_set_device_num_teams (int, int) __GOMP_NOTHROW;
 extern int omp_get_max_teams (void) __GOMP_NOTHROW;
 extern void omp_set_teams_thread_limit (int) __GOMP_NOTHROW;
+extern void omp_set_device_teams_thread_limit (int, int) __GOMP_NOTHROW;
 extern int omp_get_teams_thread_limit (void) __GOMP_NOTHROW;
+extern int omp_get_device_teams_thread_limit (int) __GOMP_NOTHROW;
 
 extern void *omp_target_alloc (__SIZE_TYPE__, int) __GOMP_NOTHROW;
 extern void omp_target_free (void *, int) __GOMP_NOTHROW;
diff --git a/libgomp/omp_lib.f90.in b/libgomp/omp_lib.f90.in
index 598ae10fbdf2..8641607bfd92 100644
--- a/libgomp/omp_lib.f90.in
+++ b/libgomp/omp_lib.f90.in
@@ -476,6 +476,15 @@
           end function omp_get_thread_limit_dim
         end interface
 
+        interface
+          ! Use bind(C) for simplicity; will fail with default-kind-8 integer
+          function omp_get_device_teams_thread_limit (device_num) bind(C)
+            use iso_c_binding, only: c_int
+            integer (c_int), value :: device_num
+            integer (c_int) :: omp_get_device_teams_thread_limit
+          end function omp_get_device_teams_thread_limit
+        end interface
+
         interface omp_set_max_active_levels
           subroutine omp_set_max_active_levels (max_levels)
             integer (4), intent (in) :: max_levels
@@ -644,6 +653,15 @@
           end function omp_get_num_teams_dim
         end interface
 
+        interface
+          ! Use bind(C) for simplicity; will fail with default-kind-8 integer
+          function omp_get_device_num_teams (device_num) bind(C)
+            use iso_c_binding, only: c_int
+            integer (c_int), value :: device_num
+            integer (c_int) :: omp_get_device_num_teams
+          end function omp_get_device_num_teams
+        end interface
+
         interface
           function omp_get_team_num ()
             integer (4) :: omp_get_team_num
@@ -692,6 +710,14 @@
           end subroutine omp_set_num_teams_8
         end interface
 
+        interface
+          ! Use bind(C) for simplicity; will fail with default-kind-8 integer
+          subroutine omp_set_device_num_teams (num_teams, device_num) bind(C)
+            use iso_c_binding, only: c_int
+            integer (c_int), value, intent (in) :: num_teams, device_num
+          end subroutine omp_set_device_num_teams
+        end interface
+
         interface
           function omp_get_max_teams ()
             integer (4) :: omp_get_max_teams
@@ -707,6 +733,15 @@
           end subroutine omp_set_teams_thread_limit_8
         end interface
 
+        interface
+          ! Use bind(C) for simplicity; will fail with default-kind-8 integer
+          subroutine omp_set_device_teams_thread_limit (thread_limit, 
device_num) &
+              bind(C)
+            use iso_c_binding, only: c_int
+            integer (c_int), value, intent (in) :: thread_limit, device_num
+          end subroutine omp_set_device_teams_thread_limit
+        end interface
+
         interface
           function omp_get_teams_thread_limit ()
             integer (4) :: omp_get_teams_thread_limit
diff --git a/libgomp/omp_lib.h.in b/libgomp/omp_lib.h.in
index 70ebca71a9b7..28a409a855f1 100644
--- a/libgomp/omp_lib.h.in
+++ b/libgomp/omp_lib.h.in
@@ -371,6 +371,15 @@
         end function omp_get_thread_limit_dim
       end interface
 
+      interface
+!       Use bind(C) for simplicity; will fail with default-kind-8 integer
+        function omp_get_device_teams_thread_limit (device_num) bind(C)
+          use iso_c_binding, only: c_int
+          integer (c_int), value :: device_num
+          integer (c_int) :: omp_get_device_teams_thread_limit
+        end function omp_get_device_teams_thread_limit
+      end interface
+
       interface
 !       Implementation choice: bind(C) and no default-kind-8 integer
         function omp_get_num_teams_dim (dim) bind(C)
@@ -380,6 +389,15 @@
         end function omp_get_num_teams_dim
       end interface
 
+      interface
+!       Use bind(C) for simplicity; will fail with default-kind-8 integer
+        function omp_get_device_num_teams (device_num) bind(C)
+          use iso_c_binding, only: c_int
+          integer (c_int), value :: device_num
+          integer (c_int) :: omp_get_device_num_teams
+        end function omp_get_device_num_teams
+      end interface
+
       interface
 !       Implementation choice: bind(C) and no default-kind-8 integer
         function omp_get_team_num_dim (dim) bind(C)
@@ -404,6 +422,24 @@
       external omp_get_max_teams, omp_get_teams_thread_limit
       integer(4) omp_get_max_teams, omp_get_teams_thread_limit
 
+      interface
+!       Use bind(C) for simplicity; will fail with default-kind-8 integer
+        subroutine omp_set_device_num_teams (num_teams, device_num)            
 &
+     &      bind(C)
+          use iso_c_binding, only: c_int
+          integer (c_int), value, intent (in) :: num_teams, device_num
+        end subroutine omp_set_device_num_teams
+      end interface
+
+      interface
+!       Use bind(C) for simplicity; will fail with default-kind-8 integer
+        subroutine omp_set_device_teams_thread_limit (thread_limit,            
 &
+     &      device_num) bind(C)
+          use iso_c_binding, only: c_int
+          integer (c_int), value, intent (in) :: thread_limit,device_num
+        end subroutine omp_set_device_teams_thread_limit
+      end interface
+
       external omp_fulfill_event
 
       external omp_set_affinity_format, omp_get_affinity_format
diff --git a/libgomp/target.c b/libgomp/target.c
index f09e4ef38550..2bd1d573de45 100644
--- a/libgomp/target.c
+++ b/libgomp/target.c
@@ -2622,6 +2622,20 @@ gomp_update (struct gomp_device_descr *devicep, size_t 
mapnum, void **hostaddrs,
     }
 }
 
+/* WARNING: This gets the original on-host values of the device ICV values.
+   That is fine for the following ICVs:
+   - nteams-var ICV:
+     'omp_set_num_teams' sets it only for the host and
+     'omp_set_device_num_teams' also only runs on the host (but needs then
+     to update also the device value)
+   - thread-limit-var and thread-limit-teams-var ICVs:
+     The former is only temporarily modifiable using a clause, the latter
+     is settable via 'omp_set_teams_thread_limit' and
+     'omp_set_device_teams_thread_limit' but those run also only on the host.
+
+   If you call this function for any other ICV than the listed ones, you
+   may need to call gomp_copy_back_icvs first.  */
+
 static struct gomp_offload_icv_list *
 gomp_get_offload_icv_item (int dev_num)
 {
@@ -3404,12 +3418,12 @@ clear_unsupported_flags (struct gomp_device_descr 
*devicep, unsigned int flags)
   return flags;
 }
 
-static void
+static struct gomp_offload_icvs *
 gomp_copy_back_icvs (struct gomp_device_descr *devicep, int device)
 {
   struct gomp_offload_icv_list *item = gomp_get_offload_icv_item (device);
   if (item == NULL)
-    return;
+    return NULL;
 
   gomp_mutex_lock (&devicep->lock);
 
@@ -3433,6 +3447,35 @@ gomp_copy_back_icvs (struct gomp_device_descr *devicep, 
int device)
   if (dev_ptr != NULL)
     gomp_copy_dev2host (devicep, NULL, host_ptr, dev_ptr,
                        sizeof (struct gomp_offload_icvs));
+  return &item->icvs;
+}
+
+void
+gomp_copy_icvs_to_device (struct gomp_device_descr *devicep,
+                         struct gomp_offload_icvs *icvs)
+{
+  gomp_mutex_lock (&devicep->lock);
+
+  struct splay_tree_s *mem_map = &devicep->mem_map;
+  struct splay_tree_key_s cur_node;
+  void *dev_ptr = NULL;
+
+  void *host_ptr = (void*) icvs;
+  cur_node.host_start = (uintptr_t) host_ptr;
+  cur_node.host_end = cur_node.host_start;
+  splay_tree_key n = gomp_map_0len_lookup (mem_map, &cur_node);
+
+  if (n)
+    {
+      uintptr_t offset = cur_node.host_start - n->host_start;
+      dev_ptr = (void *) (n->tgt->tgt_start + n->tgt_offset + offset);
+    }
+
+  gomp_mutex_unlock (&devicep->lock);
+
+  if (dev_ptr != NULL)
+    gomp_copy_host2dev (devicep, NULL, dev_ptr, host_ptr,
+                       sizeof (struct gomp_offload_icvs), false, NULL);
 }
 
 /* Like GOMP_target, but KINDS is 16-bit, UNUSED is no longer present,
@@ -3769,12 +3812,6 @@ GOMP_target_ext (int device, void (*fn) (void *), size_t 
mapnum,
     }
   if (refcount_set)
     htab_free (refcount_set);
-
-  /* Copy back ICVs from device to host.
-     HOST_PTR is expected to exist since it was added in
-     gomp_load_image_to_device if not already available.  */
-  gomp_copy_back_icvs (devicep, device);
-
 }
 
 
@@ -6286,6 +6323,97 @@ GOMP_interop (int device_num, int n_init, struct 
interop_obj_t ***init,
     }
 }
 
+void
+omp_set_device_num_teams (int num_teams, int device_num)
+{
+  if (device_num == omp_default_device)
+    device_num = gomp_get_default_device ();
+
+  if (device_num == omp_initial_device || device_num == gomp_get_num_devices 
())
+    {
+      if (num_teams >= 0)
+       gomp_nteams_var = num_teams;
+      return;
+    }
+
+  struct gomp_device_descr *devicep = resolve_device (device_num, false);
+  if (devicep == NULL || num_teams < 0)
+    return;
+  struct gomp_offload_icvs *icvs = gomp_copy_back_icvs (devicep, device_num);
+  icvs->nteams = num_teams;
+  gomp_copy_icvs_to_device (devicep, icvs);
+}
+
+int
+omp_get_device_num_teams (int device_num)
+{
+  if (device_num == omp_default_device)
+    device_num = gomp_get_default_device ();
+
+  int num_devices = gomp_get_num_devices ();
+
+  if (device_num == omp_initial_device || device_num == num_devices)
+    {
+      struct gomp_thread *thr = gomp_thread ();
+      return thr->num_teams + 1;
+    }
+
+  if (device_num < 0 || device_num > num_devices)
+    return 0;
+
+  struct gomp_device_descr *devicep = resolve_device (device_num, false);
+  if (devicep == NULL)
+    return 0;
+  struct gomp_offload_icv_list *item = gomp_get_offload_icv_item (device_num);
+  return item ? item->icvs.nteams : 0;
+}
+
+/* Accepts thread_limits = 0 [which is well defined to mean implementation
+   defined], even though only positive values are permitted.  */
+
+void
+omp_set_device_teams_thread_limit (int thread_limit, int device_num)
+{
+  if (device_num == omp_default_device)
+    device_num = gomp_get_default_device ();
+
+  if (device_num == omp_initial_device || device_num == gomp_get_num_devices 
())
+    {
+      if (thread_limit >= 0)
+       gomp_teams_thread_limit_var = thread_limit;
+      return;
+    }
+
+  struct gomp_device_descr *devicep = resolve_device (device_num, false);
+  if (devicep == NULL || thread_limit < 0)
+    return;
+  struct gomp_offload_icvs *icvs = gomp_copy_back_icvs (devicep, device_num);
+  icvs->teams_thread_limit = thread_limit;
+  gomp_copy_icvs_to_device (devicep, icvs);
+}
+
+int
+omp_get_device_teams_thread_limit (int device_num)
+{
+  if (device_num == omp_default_device)
+    device_num = gomp_get_default_device ();
+
+  int num_devices = gomp_get_num_devices ();
+
+  if (device_num == omp_initial_device || device_num == num_devices)
+    return gomp_teams_thread_limit_var;
+
+  if (device_num < 0 || device_num > num_devices)
+    return 0;
+
+  struct gomp_device_descr *devicep = resolve_device (device_num, false);
+  if (devicep == NULL)
+    return 0;
+
+  struct gomp_offload_icv_list *item = gomp_get_offload_icv_item (device_num);
+  return item ? item->icvs.teams_thread_limit : 0;
+}
+
 /* Query device-related data.  */
 
 int
@@ -6464,6 +6592,10 @@ omp_get_device_distances (int ndevs, const int *devs, 
int *distances)
     }
 }
 
+ialias (omp_set_device_num_teams)
+ialias (omp_get_device_num_teams)
+ialias (omp_set_device_teams_thread_limit)
+ialias (omp_get_device_teams_thread_limit)
 ialias (omp_get_supported_active_team_dims)
 ialias (omp_get_supported_active_league_dims)
 ialias (omp_get_supported_threads_dim)
diff --git a/libgomp/testsuite/libgomp.c-c++-common/icv-5.c 
b/libgomp/testsuite/libgomp.c-c++-common/icv-5.c
index 72d7af6dfd41..36462115786c 100644
--- a/libgomp/testsuite/libgomp.c-c++-common/icv-5.c
+++ b/libgomp/testsuite/libgomp.c-c++-common/icv-5.c
@@ -89,10 +89,24 @@ main ()
          || omp_get_thread_num () >= 5 + i)
        abort ();
 
+      /* The following is required per OpenMP semantic:
+
+        An omp_set_num_teams region must be a strictly nested region of the
+        implicit parallel region that surrounds the whole OpenMP program.
+
+        And likewise for omp_set_teams_thread_limit.
+
+        That's not the case inside 'target' and GCC fails unless the device 
ICVs
+        are copied back to the host at the beginning or end of GOMP_target_ext.
+
+        Hence set it on the host:  */
+
+      omp_set_device_num_teams (5 + i, i);
+      omp_set_device_teams_thread_limit (4 + i, i);
       #pragma omp target device (i)
       {
-       omp_set_num_teams (5 + i);
-       omp_set_teams_thread_limit (4 + i);
+       // invalid: omp_set_num_teams (5 + i);
+       // invalid: omp_set_teams_thread_limit (4 + i);
        if (omp_get_max_teams () != 5 + i
            || omp_get_teams_thread_limit () != 4 + i)
          abort ();
diff --git a/libgomp/testsuite/libgomp.c-c++-common/icv-6.c 
b/libgomp/testsuite/libgomp.c-c++-common/icv-6.c
index 1b17f2d977ad..b5aafbca5dc7 100644
--- a/libgomp/testsuite/libgomp.c-c++-common/icv-6.c
+++ b/libgomp/testsuite/libgomp.c-c++-common/icv-6.c
@@ -62,10 +62,24 @@ main ()
          abort ();
       }
 
+      /* The following is required per OpenMP semantic:
+
+        An omp_set_num_teams region must be a strictly nested region of the
+        implicit parallel region that surrounds the whole OpenMP program.
+
+        And likewise for omp_set_teams_thread_limit.
+
+        That's not the case inside 'target' and GCC fails unless the device 
ICVs
+        are copied back to the host at the beginning or end of GOMP_target_ext.
+
+        Hence set it on the host:  */
+
+      omp_set_device_num_teams (3 + i, i);
+      omp_set_device_teams_thread_limit (2 + i, i);
       #pragma omp target device (i)
       {
-       omp_set_num_teams (3 + i);
-       omp_set_teams_thread_limit (2 + i);
+       // invalid: omp_set_num_teams (3 + i);
+       // invalid: omp_set_teams_thread_limit (2 + i);
        if (omp_get_max_teams () != 3 + i
            || omp_get_teams_thread_limit () != 2 + i)
          abort ();
diff --git 
a/libgomp/testsuite/libgomp.c-c++-common/omp-device-num-teams-thread-limit-1.c 
b/libgomp/testsuite/libgomp.c-c++-common/omp-device-num-teams-thread-limit-1.c
new file mode 100644
index 000000000000..bd52df32c02f
--- /dev/null
+++ 
b/libgomp/testsuite/libgomp.c-c++-common/omp-device-num-teams-thread-limit-1.c
@@ -0,0 +1,193 @@
+// This test assumes that no environment variable changes the default!
+// i.e. neither NUM_TEAMS (> 0) is set nor OMP_TEAMS_THREAD_LIMIT.
+
+#include <omp.h>
+
+int
+main ()
+{
+  int nteams, tlimit;
+  int ndevs = omp_get_num_devices ();
+
+  if (omp_default_device < -10 || omp_invalid_device < -10)
+    __builtin_abort ();
+
+  for (int dev = -10; dev < 10; dev++)
+    {
+      bool is_host = (dev == omp_initial_device
+                     || dev == ndevs
+                     || (dev == omp_default_device
+                         && (omp_get_default_device() == omp_initial_device
+                             || omp_get_default_device() == ndevs)));
+      bool is_invalid = (dev != omp_default_device
+                        && dev != omp_initial_device
+                        && (dev < 0 || dev > ndevs));
+
+      nteams = omp_get_device_num_teams (dev);
+      tlimit = omp_get_device_teams_thread_limit (dev);
+
+      if (is_host
+         && (tlimit != omp_get_teams_thread_limit ()
+             || tlimit != omp_get_device_teams_thread_limit (dev)))
+       __builtin_abort ();
+
+      if (is_invalid)
+       {
+         if (nteams != 0 || tlimit != 0)
+           __builtin_abort ();
+       }
+      else if (is_host && nteams != 1) // outside 'teams'
+       __builtin_abort ();
+      else if (!is_host && nteams != 0) // default init = 0 (according to the 
spec)
+       __builtin_abort ();
+
+      if (!is_invalid)
+       {
+         #pragma omp target firstprivate(nteams, tlimit) device(dev)
+         {
+           if (tlimit != omp_get_teams_thread_limit ())
+             __builtin_abort ();
+         }
+       }
+    }
+
+  omp_set_default_device (omp_initial_device);
+
+  for (int i = 0; i < 4; i++)
+    {
+      int val1 = 4 + i;
+      int val2 = 3 + i;
+
+      switch(i)
+       {
+       case 0: omp_set_num_teams(val1); break;
+       case 1: omp_set_device_num_teams (val1, omp_initial_device); break;
+       case 2: omp_set_device_num_teams (val1, omp_default_device); break;
+       case 3: omp_set_device_num_teams (val1, ndevs); break;
+       }
+
+       #pragma omp teams
+       {
+         #pragma omp parallel if(0)
+         {
+         if (val1 != omp_get_num_teams ())
+           __builtin_abort ();
+         if (val1 != omp_get_device_num_teams (omp_initial_device))
+           __builtin_abort ();
+         if (val1 != omp_get_device_num_teams (omp_default_device))
+           __builtin_abort ();
+         if (val1 != omp_get_device_num_teams (ndevs))
+           __builtin_abort ();
+         }
+       }
+
+      switch(i)
+       {
+       case 0: omp_set_teams_thread_limit (val2); break;
+       case 1: omp_set_device_teams_thread_limit (val2, omp_initial_device); 
break;
+       case 2: omp_set_device_teams_thread_limit (val2, omp_default_device); 
break;
+       case 3: omp_set_device_teams_thread_limit (val2, ndevs); break;
+       }
+
+      if (val2 != omp_get_teams_thread_limit ())
+       __builtin_abort ();
+      if (val2 != omp_get_device_teams_thread_limit (omp_initial_device))
+       __builtin_abort ();
+      if (val2 != omp_get_device_teams_thread_limit (omp_default_device))
+       __builtin_abort ();
+      if (val2 != omp_get_device_teams_thread_limit (ndevs))
+       __builtin_abort ();
+    }
+
+  for (int dev = 0; dev < ndevs; dev++)
+    {
+        omp_set_device_num_teams (5, dev);
+        if (5 != omp_get_device_num_teams (dev))
+          __builtin_abort ();
+
+        #pragma omp target teams device(dev)
+         if (5 != omp_get_num_teams ())
+           __builtin_abort ();
+
+        omp_set_device_teams_thread_limit (7, dev);
+
+        if (7 != omp_get_device_teams_thread_limit (dev))
+          __builtin_abort ();
+
+      #pragma omp target teams device(dev) num_teams(1)
+       {
+         #pragma omp parallel if(0)
+         {
+         if (7 != omp_get_teams_thread_limit ())
+           __builtin_abort ();
+         }
+        }
+    }
+
+  for (int dev = omp_initial_device; dev <= ndevs; dev++)
+    {
+      bool is_host = (dev == omp_initial_device || dev == ndevs);
+
+      omp_set_device_teams_thread_limit (8, dev);
+      if (omp_get_device_teams_thread_limit (dev) != 8)
+       __builtin_abort ();
+      if (is_host && omp_get_teams_thread_limit () != 8)
+       __builtin_abort ();
+
+      // Setting to value -1 is not permitted, hence, expect old value:
+      omp_set_device_teams_thread_limit (-1, dev);
+      if (omp_get_device_teams_thread_limit (dev) != 8)
+       __builtin_abort ();
+      if (is_host && omp_get_teams_thread_limit () != 8)
+       __builtin_abort ();
+
+      // Likewise.
+      if (is_host)
+       {
+         omp_set_teams_thread_limit (-1);
+         if (omp_get_device_teams_thread_limit (dev) != 8)
+           __builtin_abort ();
+         if (is_host && omp_get_teams_thread_limit () != 8)
+           __builtin_abort ();
+       }
+
+      // Let's try value = 0. This value is well defined (to mean 
implementation
+      // defined) but not permitted. Hence, permit both results:
+      omp_set_device_teams_thread_limit (0, dev);
+      if (omp_get_device_teams_thread_limit (dev) != 8
+         && omp_get_device_teams_thread_limit (dev) != 0)
+       __builtin_abort ();
+      if (is_host
+         && omp_get_teams_thread_limit () != 8
+         && omp_get_teams_thread_limit () != 0)
+       __builtin_abort ();
+
+      // Likewise.
+      if (is_host)
+       {
+         omp_set_teams_thread_limit (0);
+         if (omp_get_device_teams_thread_limit (dev) != 8
+             &&  omp_get_device_teams_thread_limit (dev) != 0)
+           __builtin_abort ();
+         if (is_host
+             && omp_get_teams_thread_limit () != 8
+             && omp_get_teams_thread_limit () != 0)
+           __builtin_abort ();
+       }
+
+      // However, the nteams-ICV can be set to zero,
+      // but we cannot check its value on the host as
+      // omp_get_num_teams gives the effective team size not the ICV value.
+      if (is_host)
+       continue;
+      
+      omp_set_device_num_teams (8, dev);
+      if (omp_get_device_num_teams (dev) != 8)
+       __builtin_abort ();
+
+      // Setting to value 0 is permitted, hence, expect that value:
+      omp_set_device_num_teams (0, dev);
+      if (omp_get_device_num_teams (dev) != 0)
+       __builtin_abort ();
+    }
+}
diff --git a/libgomp/testsuite/libgomp.fortran/icv-6.f90 
b/libgomp/testsuite/libgomp.fortran/icv-6.f90
index c8e6a0d0f128..3984a6943c6e 100644
--- a/libgomp/testsuite/libgomp.fortran/icv-6.f90
+++ b/libgomp/testsuite/libgomp.fortran/icv-6.f90
@@ -52,9 +52,20 @@ implicit none (type, external)
       !$omp end target
       if (err) stop 2
 
+      ! The following is required per OpenMP semantic:
+      !
+      ! An omp_set_num_teams region must be a strictly nested region of the
+      ! implicit parallel region that surrounds the whole OpenMP program.
+      !
+      ! That's not the case inside 'target' and GCC fails unless the device 
ICVs
+      ! are copied back to the host at the beginning or end of GOMP_target_ext.
+      !
+      ! Hence set it on the host:
+
+      call omp_set_device_num_teams (3 + i, i)
       err = .false.
       !$omp target device(i) map(tofrom: err)
-      call omp_set_num_teams (3 + i)
+       ! invalid: call omp_set_num_teams (3 + i)
       if (omp_get_max_teams () /= 3 + i) err = .true.
       !$omp end target
       if (err) stop 3
@@ -94,9 +105,20 @@ implicit none (type, external)
       !$omp end target
       if (err) stop 7
 
+      ! The following is required per OpenMP 6.x semantic:
+      !
+      ! An omp_set_device_teams_thread_limit region must be a strictly nested 
region
+      ! of the implicit parallel region that surrounds the whole OpenMP 
program.
+      !
+      ! That's not the case inside 'target' and GCC fails unless the device 
ICVs
+      ! are copied back to the host at the beginning or end of GOMP_target_ext.
+      !
+      ! Hence set it on the host:
+
+      call omp_set_device_teams_thread_limit (2 + i, i)
       err = .false.
       !$omp target device(i) map(tofrom: err)
-      call omp_set_teams_thread_limit (2 + i)
+      ! invalid: call omp_set_teams_thread_limit (2 + i)
       if (omp_get_teams_thread_limit () /= 2 + i) err = .true.
       !$omp end target
       if (err) stop 8
diff --git 
a/libgomp/testsuite/libgomp.fortran/omp-device-num-teams-thread-limit-1.f90 
b/libgomp/testsuite/libgomp.fortran/omp-device-num-teams-thread-limit-1.f90
new file mode 100644
index 000000000000..c61e7b8d1941
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/omp-device-num-teams-thread-limit-1.f90
@@ -0,0 +1,194 @@
+! This test assumes that no environment variable changes the default!
+! i.e. neither NUM_TEAMS (> 0) is set nor OMP_TEAMS_THREAD_LIMIT.
+
+program main
+  use omp_lib
+  implicit none (type, external)
+
+  integer :: nteams, tlimit
+  integer :: ndevs, dev, i
+
+  ndevs = omp_get_num_devices ()
+
+  if (omp_default_device < -10 .or. omp_invalid_device < -10) &
+    stop 1
+
+  do dev = -10, 10
+    block
+      logical :: is_host, is_invalid
+
+      is_host = (dev == omp_initial_device  &
+                 .or. dev == ndevs  &
+                 .or. (dev == omp_default_device  &
+                       .and. (omp_get_default_device() == omp_initial_device  &
+                              .or. omp_get_default_device() == ndevs)))
+      is_invalid = (dev /= omp_default_device  &
+                    .and. dev /= omp_initial_device  &
+                    .and. (dev < 0 .or. dev > ndevs))
+
+      nteams = omp_get_device_num_teams (dev)
+      tlimit = omp_get_device_teams_thread_limit (dev)
+
+      if (is_host  &
+          .and. (tlimit /= omp_get_teams_thread_limit ()  &
+                 .or. tlimit /= omp_get_device_teams_thread_limit (dev))) &
+        stop 2
+
+      if (is_invalid) then
+        if (nteams /= 0 .or. tlimit /= 0) &
+          stop 3
+      else if (is_host .and. nteams /= 1) then  ! outside 'teams'
+        stop 4
+      else if (.not. is_host .and. nteams /= 0) then  ! default init = 0 
(according to the spec)
+        stop 5
+      end if
+
+      if (.not. is_invalid) then
+        !$omp target firstprivate(nteams, tlimit) device(dev)
+          if (tlimit /= omp_get_teams_thread_limit ()) &
+            stop 6
+        !$omp end target
+      end if
+    end block
+  end do
+
+  call omp_set_default_device (omp_initial_device)
+
+  do i = 0, 3
+  block
+    integer :: val1, val2
+
+    val1 = 4 + i
+    val2 = 3 + i
+
+    select case (i)
+      case(0); call omp_set_num_teams(val1)
+      case(1); call omp_set_device_num_teams (val1, omp_initial_device)
+      case(2); call omp_set_device_num_teams (val1, omp_default_device)
+      case(3); call omp_set_device_num_teams (val1, ndevs)
+    end select
+
+    !$omp teams
+      !$omp parallel if(.false.)
+        if (val1 /= omp_get_num_teams ()) &
+            stop 7
+        if (val1 /= omp_get_device_num_teams (omp_initial_device)) &
+            stop 8
+        if (val1 /= omp_get_device_num_teams (omp_default_device)) &
+            stop 9
+        if (val1 /= omp_get_device_num_teams (ndevs)) &
+            stop 10
+      !$omp end parallel
+    !$omp end teams
+
+    select case(i)
+      case(0); call omp_set_teams_thread_limit (val2)
+      case(1); call omp_set_device_teams_thread_limit (val2, 
omp_initial_device)
+      case(2); call omp_set_device_teams_thread_limit (val2, 
omp_default_device)
+      case(3); call omp_set_device_teams_thread_limit (val2, ndevs)
+    end select
+
+      if (val2 /= omp_get_teams_thread_limit ()) &
+        stop 11
+      if (val2 /= omp_get_device_teams_thread_limit (omp_initial_device)) &
+        stop 12
+      if (val2 /= omp_get_device_teams_thread_limit (omp_default_device)) &
+        stop 13
+      if (val2 /= omp_get_device_teams_thread_limit (ndevs)) &
+        stop 14
+  end block
+  end do
+
+  do dev = 0, ndevs - 1
+    call omp_set_device_num_teams (5, dev)
+    if (5 /= omp_get_device_num_teams (dev)) &
+      stop 15
+
+    !$omp target teams device(dev)
+      if (5 /= omp_get_num_teams ()) &
+        stop 16
+    !$omp end target teams
+
+    call omp_set_device_teams_thread_limit (7, dev)
+
+    if (7 /= omp_get_device_teams_thread_limit (dev)) &
+      stop 17
+
+    !$omp target teams device(dev) num_teams(1)
+      !$omp parallel if(.false.)
+        if (7 /= omp_get_teams_thread_limit ()) &
+          stop 18
+      !$omp end parallel
+    !$omp end target teams
+  end do
+
+  do dev = omp_initial_device, ndevs
+    block
+      logical :: is_host
+
+      is_host = (dev == omp_initial_device .or. dev == ndevs)
+
+      call omp_set_device_teams_thread_limit (8, dev)
+
+      if (omp_get_device_teams_thread_limit (dev) /= 8) &
+        stop 19
+      if (is_host .and. omp_get_teams_thread_limit () /= 8) &
+        stop 19
+
+      ! Setting to value -1 is not permitted, hence, expect old value:
+      call omp_set_device_teams_thread_limit (-1, dev)
+
+      if (omp_get_device_teams_thread_limit (dev) /= 8) &
+        stop 20
+      if (is_host .and. omp_get_teams_thread_limit () /= 8) &
+        stop 21
+
+      ! Likewise.
+      if (is_host) then
+        call omp_set_teams_thread_limit (-1)
+        if (omp_get_device_teams_thread_limit (dev) /= 8) &
+          stop 22
+        if (is_host .and. omp_get_teams_thread_limit () /= 8) &
+          stop 23
+      end if
+
+      ! Let's try value = 0. This value is well defined (to mean implementation
+      ! defined) but not permitted. Hence, permit both results:
+      call omp_set_device_teams_thread_limit (0, dev)
+      if (omp_get_device_teams_thread_limit (dev) /= 8 &
+          .and. omp_get_device_teams_thread_limit (dev) /= 0) &
+        stop 24
+      if (is_host &
+          .and. omp_get_teams_thread_limit () /= 8 &
+          .and. omp_get_teams_thread_limit () /= 0) &
+        stop 25
+
+      ! Likewise.
+      if (is_host) then
+        call omp_set_teams_thread_limit (0)
+        if (omp_get_device_teams_thread_limit (dev) /= 8 &
+            .and.  omp_get_device_teams_thread_limit (dev) /= 0) &
+          stop 26
+        if (is_host &
+            .and. omp_get_teams_thread_limit () /= 8 &
+            .and. omp_get_teams_thread_limit () /= 0) &
+          stop 27
+      end if
+
+      ! However, the nteams-ICV can be set to zero,
+      ! but we cannot check its value on the host as
+      ! omp_get_num_teams gives the effective team size not the ICV value.
+      if (is_host) &
+        cycle
+      
+      call omp_set_device_num_teams (8, dev)
+      if (omp_get_device_num_teams (dev) /= 8) &
+        stop 28
+
+      ! Setting to value 0 is permitted, hence, expect that value:
+      call omp_set_device_num_teams (0, dev)
+      if (omp_get_device_num_teams (dev) /= 0) &
+        stop 29
+    end block
+  end do
+end program

Reply via email to