(root)/
gcc-13.2.0/
libgomp/
oacc-host.c
       1  /* OpenACC Runtime Library: acc_device_host.
       2  
       3     Copyright (C) 2013-2023 Free Software Foundation, Inc.
       4  
       5     Contributed by Mentor Embedded.
       6  
       7     This file is part of the GNU Offloading and Multi Processing Library
       8     (libgomp).
       9  
      10     Libgomp is free software; you can redistribute it and/or modify it
      11     under the terms of the GNU General Public License as published by
      12     the Free Software Foundation; either version 3, or (at your option)
      13     any later version.
      14  
      15     Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
      16     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      17     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
      18     more details.
      19  
      20     Under Section 7 of GPL version 3, you are granted additional
      21     permissions described in the GCC Runtime Library Exception, version
      22     3.1, as published by the Free Software Foundation.
      23  
      24     You should have received a copy of the GNU General Public License and
      25     a copy of the GCC Runtime Library Exception along with this program;
      26     see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      27     <http://www.gnu.org/licenses/>.  */
      28  
      29  #include "libgomp.h"
      30  #include "oacc-int.h"
      31  #include "gomp-constants.h"
      32  
      33  #include <stdbool.h>
      34  #include <stddef.h>
      35  
      36  static struct gomp_device_descr host_dispatch;
      37  
      38  static const char *
      39  host_get_name (void)
      40  {
      41    return host_dispatch.name;
      42  }
      43  
      44  static unsigned int
      45  host_get_caps (void)
      46  {
      47    return host_dispatch.capabilities;
      48  }
      49  
      50  static int
      51  host_get_type (void)
      52  {
      53    return host_dispatch.type;
      54  }
      55  
      56  static int
      57  host_get_num_devices (unsigned int omp_requires_mask __attribute__((unused)))
      58  {
      59    return 1;
      60  }
      61  
      62  static bool
      63  host_init_device (int n __attribute__ ((unused)))
      64  {
      65    return true;
      66  }
      67  
      68  static bool
      69  host_fini_device (int n __attribute__ ((unused)))
      70  {
      71    return true;
      72  }
      73  
      74  static unsigned
      75  host_version (void)
      76  {
      77    return GOMP_VERSION;
      78  }
      79  
      80  static int
      81  host_load_image (int n __attribute__ ((unused)),
      82  		 unsigned v __attribute__ ((unused)),
      83  		 const void *t __attribute__ ((unused)),
      84  		 struct addr_pair **r __attribute__ ((unused)),
      85  		 uint64_t **f __attribute__ ((unused)))
      86  {
      87    return 0;
      88  }
      89  
      90  static bool
      91  host_unload_image (int n __attribute__ ((unused)),
      92  		   unsigned v __attribute__ ((unused)),
      93  		   const void *t __attribute__ ((unused)))
      94  {
      95    return true;
      96  }
      97  
      98  static void *
      99  host_alloc (int n __attribute__ ((unused)), size_t s)
     100  {
     101    return gomp_malloc (s);
     102  }
     103  
     104  static bool
     105  host_free (int n __attribute__ ((unused)), void *p)
     106  {
     107    free (p);
     108    return true;
     109  }
     110  
     111  static bool
     112  host_dev2host (int n __attribute__ ((unused)),
     113  	       void *h __attribute__ ((unused)),
     114  	       const void *d __attribute__ ((unused)),
     115  	       size_t s __attribute__ ((unused)))
     116  {
     117    return true;
     118  }
     119  
     120  static bool
     121  host_host2dev (int n __attribute__ ((unused)),
     122  	       void *d __attribute__ ((unused)),
     123  	       const void *h __attribute__ ((unused)),
     124  	       size_t s __attribute__ ((unused)))
     125  {
     126    return true;
     127  }
     128  
     129  static void
     130  host_run (int n __attribute__ ((unused)), void *fn_ptr, void *vars,
     131  	  void **args __attribute__((unused)))
     132  {
     133    void (*fn)(void *) = (void (*)(void *)) fn_ptr;
     134  
     135    fn (vars);
     136  }
     137  
     138  static void
     139  host_openacc_exec (void (*fn) (void *),
     140  		   size_t mapnum __attribute__ ((unused)),
     141  		   void **hostaddrs,
     142  		   void **devaddrs __attribute__ ((unused)),
     143  		   unsigned *dims __attribute__ ((unused)),
     144  		   void *targ_mem_desc __attribute__ ((unused)))
     145  {
     146    fn (hostaddrs);
     147  }
     148  
     149  static void
     150  host_openacc_async_exec (void (*fn) (void *),
     151  			 size_t mapnum __attribute__ ((unused)),
     152  			 void **hostaddrs,
     153  			 void **devaddrs __attribute__ ((unused)),
     154  			 unsigned *dims __attribute__ ((unused)),
     155  			 void *targ_mem_desc __attribute__ ((unused)),
     156  			 struct goacc_asyncqueue *aq __attribute__ ((unused)))
     157  {
     158    fn (hostaddrs);
     159  }
     160  
     161  static int
     162  host_openacc_async_test (struct goacc_asyncqueue *aq __attribute__ ((unused)))
     163  {
     164    return 1;
     165  }
     166  
     167  static bool
     168  host_openacc_async_synchronize (struct goacc_asyncqueue *aq
     169  				__attribute__ ((unused)))
     170  {
     171    return true;
     172  }
     173  
     174  static bool
     175  host_openacc_async_serialize (struct goacc_asyncqueue *aq1
     176  			      __attribute__ ((unused)),
     177  			      struct goacc_asyncqueue *aq2
     178  			      __attribute__ ((unused)))
     179  {
     180    return true;
     181  }
     182  
     183  static bool
     184  host_openacc_async_host2dev (int ord __attribute__ ((unused)),
     185  			     void *dst __attribute__ ((unused)),
     186  			     const void *src __attribute__ ((unused)),
     187  			     size_t n __attribute__ ((unused)),
     188  			     struct goacc_asyncqueue *aq
     189  			     __attribute__ ((unused)))
     190  {
     191    return true;
     192  }
     193  
     194  static bool
     195  host_openacc_async_dev2host (int ord __attribute__ ((unused)),
     196  			     void *dst __attribute__ ((unused)),
     197  			     const void *src __attribute__ ((unused)),
     198  			     size_t n __attribute__ ((unused)),
     199  			     struct goacc_asyncqueue *aq
     200  			     __attribute__ ((unused)))
     201  {
     202    return true;
     203  }
     204  
     205  static void
     206  host_openacc_async_queue_callback (struct goacc_asyncqueue *aq
     207  				   __attribute__ ((unused)),
     208  				   void (*callback_fn)(void *)
     209  				   __attribute__ ((unused)),
     210  				   void *userptr __attribute__ ((unused)))
     211  {
     212  }
     213  
     214  static struct goacc_asyncqueue *
     215  host_openacc_async_construct (int device __attribute__((unused)))
     216  {
     217    /* Non-NULL 0xffff... value as opaque dummy.  */
     218    return (struct goacc_asyncqueue *) -1;
     219  }
     220  
     221  static bool
     222  host_openacc_async_destruct (struct goacc_asyncqueue *aq
     223  			     __attribute__ ((unused)))
     224  {
     225    return true;
     226  }
     227  
     228  static union goacc_property_value
     229  host_openacc_get_property (int n, enum goacc_property prop)
     230  {
     231    union goacc_property_value nullval = { .val = 0 };
     232  
     233    if (n >= host_get_num_devices (0))
     234      return nullval;
     235  
     236    switch (prop)
     237      {
     238      case GOACC_PROPERTY_NAME:
     239        return (union goacc_property_value) { .ptr = "GOMP" };
     240      case GOACC_PROPERTY_VENDOR:
     241        return (union goacc_property_value) { .ptr = "GNU" };
     242      case GOACC_PROPERTY_DRIVER:
     243        return (union goacc_property_value) { .ptr = VERSION };
     244      case GOACC_PROPERTY_MEMORY:
     245      case GOACC_PROPERTY_FREE_MEMORY:
     246      default:
     247        return nullval;
     248      }
     249  }
     250  
     251  static void *
     252  host_openacc_create_thread_data (int ord __attribute__ ((unused)))
     253  {
     254    return NULL;
     255  }
     256  
     257  static void
     258  host_openacc_destroy_thread_data (void *tls_data __attribute__ ((unused)))
     259  {
     260  }
     261  
     262  static struct gomp_device_descr host_dispatch =
     263    {
     264      .name = "host",
     265      .capabilities = (GOMP_OFFLOAD_CAP_SHARED_MEM
     266  		     | GOMP_OFFLOAD_CAP_NATIVE_EXEC
     267  		     | GOMP_OFFLOAD_CAP_OPENACC_200),
     268      .target_id = 0,
     269      .type = OFFLOAD_TARGET_TYPE_HOST,
     270  
     271      .get_name_func = host_get_name,
     272      .get_caps_func = host_get_caps,
     273      .get_type_func = host_get_type,
     274      .get_num_devices_func = host_get_num_devices,
     275      .init_device_func = host_init_device,
     276      .fini_device_func = host_fini_device,
     277      .version_func = host_version,
     278      .load_image_func = host_load_image,
     279      .unload_image_func = host_unload_image,
     280      .alloc_func = host_alloc,
     281      .free_func = host_free,
     282      .dev2host_func = host_dev2host,
     283      .host2dev_func = host_host2dev,
     284      .run_func = host_run,
     285  
     286      .mem_map = { NULL },
     287      .mem_map_rev = { NULL },
     288      /* .lock initialized in goacc_host_init.  */
     289      .state = GOMP_DEVICE_UNINITIALIZED,
     290  
     291      .openacc = {
     292        .exec_func = host_openacc_exec,
     293  
     294        .create_thread_data_func = host_openacc_create_thread_data,
     295        .destroy_thread_data_func = host_openacc_destroy_thread_data,
     296  
     297        .async = {
     298  	.construct_func = host_openacc_async_construct,
     299  	.destruct_func = host_openacc_async_destruct,
     300  	.test_func = host_openacc_async_test,
     301  	.synchronize_func = host_openacc_async_synchronize,
     302  	.serialize_func = host_openacc_async_serialize,
     303  	.queue_callback_func = host_openacc_async_queue_callback,
     304  	.exec_func = host_openacc_async_exec,
     305  	.dev2host_func = host_openacc_async_dev2host,
     306  	.host2dev_func = host_openacc_async_host2dev,
     307        },
     308  
     309        .get_property_func = host_openacc_get_property,
     310  
     311        .cuda = {
     312  	.get_current_device_func = NULL,
     313  	.get_current_context_func = NULL,
     314  	.get_stream_func = NULL,
     315  	.set_stream_func = NULL,
     316        }
     317      }
     318    };
     319  
     320  /* Initialize and register this device type.  */
     321  void
     322  goacc_host_init (void)
     323  {
     324    gomp_mutex_init (&host_dispatch.lock);
     325    goacc_register (&host_dispatch);
     326  }