1  /* Copyright (C) 2013-2023 Free Software Foundation, Inc.
       2     Contributed by Jakub Jelinek <jakub@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 = num_blocks;
      59    else if (num_blocks < num_teams_lower)
      60      num_teams_upper = num_teams_lower;
      61    else if (num_blocks < num_teams_upper)
      62      num_teams_upper = num_blocks;
      63    asm ("mov.u32 %0, %%ctaid.x;" : "=r" (block_id));
      64    if (block_id >= num_teams_upper)
      65      return false;
      66    __gomp_team_num = block_id;
      67    gomp_num_teams_var = num_teams_upper - 1;
      68    return true;
      69  }
      70  
      71  int
      72  omp_pause_resource (omp_pause_resource_t kind, int device_num)
      73  {
      74    (void) kind;
      75    (void) device_num;
      76    return -1;
      77  }
      78  
      79  int
      80  omp_pause_resource_all (omp_pause_resource_t kind)
      81  {
      82    (void) kind;
      83    return -1;
      84  }
      85  
      86  ialias (omp_pause_resource)
      87  ialias (omp_pause_resource_all)
      88  
      89  void
      90  GOMP_target_ext (int device, void (*fn) (void *), size_t mapnum,
      91  		 void **hostaddrs, size_t *sizes, unsigned short *kinds,
      92  		 unsigned int flags, void **depend, void **args)
      93  {
      94    static int lock = 0;  /* == gomp_mutex_t lock; gomp_mutex_init (&lock); */
      95    (void) flags;
      96    (void) depend;
      97    (void) args;
      98  
      99    if (device != GOMP_DEVICE_HOST_FALLBACK
     100        || fn == NULL
     101        || GOMP_REV_OFFLOAD_VAR == NULL)
     102      return;
     103  
     104    gomp_mutex_lock (&lock);
     105  
     106    GOMP_REV_OFFLOAD_VAR->mapnum = mapnum;
     107    GOMP_REV_OFFLOAD_VAR->addrs = (uint64_t) hostaddrs;
     108    GOMP_REV_OFFLOAD_VAR->sizes = (uint64_t) sizes;
     109    GOMP_REV_OFFLOAD_VAR->kinds = (uint64_t) kinds;
     110    GOMP_REV_OFFLOAD_VAR->dev_num = GOMP_ADDITIONAL_ICVS.device_num;
     111  
     112    /* Set 'fn' to trigger processing on the host; wait for completion,
     113       which is flagged by setting 'fn' back to 0 on the host.  */
     114    uint64_t addr_struct_fn = (uint64_t) &GOMP_REV_OFFLOAD_VAR->fn;
     115  #if __PTX_SM__ >= 700
     116    asm volatile ("st.global.release.sys.u64 [%0], %1;"
     117  		: : "r"(addr_struct_fn), "r" (fn) : "memory");
     118  #else
     119    __sync_synchronize ();  /* membar.sys */
     120    asm volatile ("st.volatile.global.u64 [%0], %1;"
     121  		: : "r"(addr_struct_fn), "r" (fn) : "memory");
     122  #endif
     123  
     124  #if __PTX_SM__ >= 700
     125    uint64_t fn2;
     126    do
     127      {
     128        asm volatile ("ld.acquire.sys.global.u64 %0, [%1];"
     129  		    : "=r" (fn2) : "r" (addr_struct_fn) : "memory");
     130      }
     131    while (fn2 != 0);
     132  #else
     133    /* ld.global.u64 %r64,[__gomp_rev_offload_var];
     134       ld.u64 %r36,[%r64];
     135       membar.sys;  */
     136    while (__atomic_load_n (&GOMP_REV_OFFLOAD_VAR->fn, __ATOMIC_ACQUIRE) != 0)
     137      ;  /* spin  */
     138  #endif
     139  
     140    gomp_mutex_unlock (&lock);
     141  }
     142  
     143  void
     144  GOMP_target_data_ext (int device, size_t mapnum, void **hostaddrs,
     145  		      size_t *sizes, unsigned short *kinds)
     146  {
     147    (void) device;
     148    (void) mapnum;
     149    (void) hostaddrs;
     150    (void) sizes;
     151    (void) kinds;
     152    __builtin_unreachable ();
     153  }
     154  
     155  void
     156  GOMP_target_end_data (void)
     157  {
     158    __builtin_unreachable ();
     159  }
     160  
     161  void
     162  GOMP_target_update_ext (int device, size_t mapnum, void **hostaddrs,
     163  			size_t *sizes, unsigned short *kinds,
     164  			unsigned int flags, void **depend)
     165  {
     166    (void) device;
     167    (void) mapnum;
     168    (void) hostaddrs;
     169    (void) sizes;
     170    (void) kinds;
     171    (void) flags;
     172    (void) depend;
     173    __builtin_unreachable ();
     174  }
     175  
     176  void
     177  GOMP_target_enter_exit_data (int device, size_t mapnum, void **hostaddrs,
     178  			     size_t *sizes, unsigned short *kinds,
     179  			     unsigned int flags, void **depend)
     180  {
     181    (void) device;
     182    (void) mapnum;
     183    (void) hostaddrs;
     184    (void) sizes;
     185    (void) kinds;
     186    (void) flags;
     187    (void) depend;
     188    __builtin_unreachable ();
     189  }