Home | History | Annotate | Line # | Download | only in nvptx
target.c revision 1.1.1.9
      1 /* Copyright (C) 2013-2024 Free Software Foundation, Inc.
      2    Contributed by Jakub Jelinek <jakub (at) redhat.com>.
      3 
      4    This file is part of the GNU Offloading and Multi Processing Library
      5    (libgomp).
      6 
      7    Libgomp is free software; you can redistribute it and/or modify it
      8    under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3, or (at your option)
     10    any later version.
     11 
     12    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
     13    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
     14    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
     15    more details.
     16 
     17    Under Section 7 of GPL version 3, you are granted additional
     18    permissions described in the GCC Runtime Library Exception, version
     19    3.1, as published by the Free Software Foundation.
     20 
     21    You should have received a copy of the GNU General Public License and
     22    a copy of the GCC Runtime Library Exception along with this program;
     23    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     24    <http://www.gnu.org/licenses/>.  */
     25 
     26 #include "libgomp.h"
     27 #include "libgomp-nvptx.h"  /* For struct rev_offload + GOMP_REV_OFFLOAD_VAR. */
     28 #include <limits.h>
     29 
     30 extern int __gomp_team_num __attribute__((shared));
     31 extern volatile struct gomp_offload_icvs GOMP_ADDITIONAL_ICVS;
     32 volatile struct rev_offload *GOMP_REV_OFFLOAD_VAR;
     33 
     34 bool
     35 GOMP_teams4 (unsigned int num_teams_lower, unsigned int num_teams_upper,
     36 	     unsigned int thread_limit, bool first)
     37 {
     38   unsigned int num_blocks, block_id;
     39   asm ("mov.u32 %0, %%nctaid.x;" : "=r" (num_blocks));
     40   if (!first)
     41     {
     42       unsigned int team_num;
     43       if (num_blocks > gomp_num_teams_var)
     44 	return false;
     45       team_num = __gomp_team_num;
     46       if (team_num > gomp_num_teams_var - num_blocks)
     47 	return false;
     48       __gomp_team_num = team_num + num_blocks;
     49       return true;
     50     }
     51   if (thread_limit)
     52     {
     53       struct gomp_task_icv *icv = gomp_icv (true);
     54       icv->thread_limit_var
     55 	= thread_limit > INT_MAX ? UINT_MAX : thread_limit;
     56     }
     57   if (!num_teams_upper)
     58     num_teams_upper = ((GOMP_ADDITIONAL_ICVS.nteams > 0
     59 			&& num_blocks > GOMP_ADDITIONAL_ICVS.nteams)
     60 		       ? GOMP_ADDITIONAL_ICVS.nteams : num_blocks);
     61   else if (num_blocks < num_teams_lower)
     62     num_teams_upper = num_teams_lower;
     63   else if (num_blocks < num_teams_upper)
     64     num_teams_upper = num_blocks;
     65   asm ("mov.u32 %0, %%ctaid.x;" : "=r" (block_id));
     66   if (block_id >= num_teams_upper)
     67     return false;
     68   __gomp_team_num = block_id;
     69   gomp_num_teams_var = num_teams_upper - 1;
     70   return true;
     71 }
     72 
     73 int
     74 omp_pause_resource (omp_pause_resource_t kind, int device_num)
     75 {
     76   (void) kind;
     77   (void) device_num;
     78   return -1;
     79 }
     80 
     81 int
     82 omp_pause_resource_all (omp_pause_resource_t kind)
     83 {
     84   (void) kind;
     85   return -1;
     86 }
     87 
     88 ialias (omp_pause_resource)
     89 ialias (omp_pause_resource_all)
     90 
     91 void
     92 GOMP_target_ext (int device, void (*fn) (void *), size_t mapnum,
     93 		 void **hostaddrs, size_t *sizes, unsigned short *kinds,
     94 		 unsigned int flags, void **depend, void **args)
     95 {
     96   static int lock = 0;  /* == gomp_mutex_t lock; gomp_mutex_init (&lock); */
     97   (void) flags;
     98   (void) depend;
     99   (void) args;
    100 
    101   if (device != GOMP_DEVICE_HOST_FALLBACK
    102       || fn == NULL
    103       || GOMP_REV_OFFLOAD_VAR == NULL)
    104     return;
    105 
    106   gomp_mutex_lock (&lock);
    107 
    108   GOMP_REV_OFFLOAD_VAR->mapnum = mapnum;
    109   GOMP_REV_OFFLOAD_VAR->addrs = (uint64_t) hostaddrs;
    110   GOMP_REV_OFFLOAD_VAR->sizes = (uint64_t) sizes;
    111   GOMP_REV_OFFLOAD_VAR->kinds = (uint64_t) kinds;
    112   GOMP_REV_OFFLOAD_VAR->dev_num = GOMP_ADDITIONAL_ICVS.device_num;
    113 
    114   /* Set 'fn' to trigger processing on the host; wait for completion,
    115      which is flagged by setting 'fn' back to 0 on the host.  */
    116   uint64_t addr_struct_fn = (uint64_t) &GOMP_REV_OFFLOAD_VAR->fn;
    117 #if __PTX_SM__ >= 700
    118   asm volatile ("st.global.release.sys.u64 [%0], %1;"
    119 		: : "r"(addr_struct_fn), "r" (fn) : "memory");
    120 #else
    121   __sync_synchronize ();  /* membar.sys */
    122   asm volatile ("st.volatile.global.u64 [%0], %1;"
    123 		: : "r"(addr_struct_fn), "r" (fn) : "memory");
    124 #endif
    125 
    126 #if __PTX_SM__ >= 700
    127   uint64_t fn2;
    128   do
    129     {
    130       asm volatile ("ld.acquire.sys.global.u64 %0, [%1];"
    131 		    : "=r" (fn2) : "r" (addr_struct_fn) : "memory");
    132     }
    133   while (fn2 != 0);
    134 #else
    135   /* ld.global.u64 %r64,[__gomp_rev_offload_var];
    136      ld.u64 %r36,[%r64];
    137      membar.sys;  */
    138   while (__atomic_load_n (&GOMP_REV_OFFLOAD_VAR->fn, __ATOMIC_ACQUIRE) != 0)
    139     ;  /* spin  */
    140 #endif
    141 
    142   gomp_mutex_unlock (&lock);
    143 }
    144 
    145 void
    146 GOMP_target_data_ext (int device, size_t mapnum, void **hostaddrs,
    147 		      size_t *sizes, unsigned short *kinds)
    148 {
    149   (void) device;
    150   (void) mapnum;
    151   (void) hostaddrs;
    152   (void) sizes;
    153   (void) kinds;
    154   __builtin_unreachable ();
    155 }
    156 
    157 void
    158 GOMP_target_end_data (void)
    159 {
    160   __builtin_unreachable ();
    161 }
    162 
    163 void
    164 GOMP_target_update_ext (int device, size_t mapnum, void **hostaddrs,
    165 			size_t *sizes, unsigned short *kinds,
    166 			unsigned int flags, void **depend)
    167 {
    168   (void) device;
    169   (void) mapnum;
    170   (void) hostaddrs;
    171   (void) sizes;
    172   (void) kinds;
    173   (void) flags;
    174   (void) depend;
    175   __builtin_unreachable ();
    176 }
    177 
    178 void
    179 GOMP_target_enter_exit_data (int device, size_t mapnum, void **hostaddrs,
    180 			     size_t *sizes, unsigned short *kinds,
    181 			     unsigned int flags, void **depend)
    182 {
    183   (void) device;
    184   (void) mapnum;
    185   (void) hostaddrs;
    186   (void) sizes;
    187   (void) kinds;
    188   (void) flags;
    189   (void) depend;
    190   __builtin_unreachable ();
    191 }
    192