public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Ilya Verbin <iverbin@gmail.com>
To: Thomas Schwinge <thomas@codesourcery.com>
Cc: Bernd Schmidt <bernds@codesourcery.com>,
	Jakub Jelinek <jakub@redhat.com>,
	Richard Biener <rguenther@suse.de>,
	Kirill Yukhin <kirill.yukhin@gmail.com>,
	"Michael V. Zolotukhin" <michael.v.zolotukhin@gmail.com>,
	Andrey Turetskiy <andrey.turetskiy@gmail.com>,
	Ilya Tocar <tocarip.intel@gmail.com>,
	gcc-patches@gcc.gnu.org
Subject: Re: [RFC][gomp4] Offloading: Add device initialization and host->target function mapping
Date: Wed, 12 Mar 2014 18:21:00 -0000	[thread overview]
Message-ID: <20140312175644.GA40186@msticlxl57.ims.intel.com> (raw)
In-Reply-To: <20140115150917.GA6442@msticlxl57.ims.intel.com>

Hi Thomas,

Here is a new version of this patch (it was discussed in other thread: http://gcc.gnu.org/ml/gcc-patches/2014-03/msg00573.html ) with ChangeLog.
Bootstrap and make check passed.
Ok to commit?


 libgomp/ChangeLog.gomp |   29 ++++++++
 libgomp/libgomp.map    |    1 +
 libgomp/plugin-host.c  |   58 ++++++++++++++++-
 libgomp/target.c       |  170 ++++++++++++++++++++++++++++++++++++++++++++----
 4 files changed, 242 insertions(+), 16 deletions(-)

diff --git a/libgomp/ChangeLog.gomp b/libgomp/ChangeLog.gomp
index 7f9ce11..57a600e 100644
--- a/libgomp/ChangeLog.gomp
+++ b/libgomp/ChangeLog.gomp
@@ -1,3 +1,32 @@
+2014-03-12  Ilya Verbin  <ilya.verbin@intel.com>
+
+	* libgomp.map (GOMP_4.0): Add GOMP_offload_register.
+	* plugin-host.c (device_available): Replace with:
+	(get_num_devices): This.
+	(get_type): New.
+	(offload_register): Ditto.
+	(device_init): Ditto.
+	(device_get_table): Ditto.
+	(device_run): Ditto.
+	* target.c (target_type): New enum.
+	(offload_image_descr): New struct.
+	(offload_images, num_offload_images): New globals.
+	(struct gomp_device_descr): Remove device_available_func.
+	Add type, is_initialized, get_type_func, get_num_devices_func,
+	offload_register_func, device_init_func, device_get_table_func,
+	device_run_func.
+	(mapping_table): New struct.
+	(GOMP_offload_register): New function.
+	(gomp_init_device): Ditto.
+	(GOMP_target): Add device initialization and lookup for target fn.
+	(GOMP_target_data): Add device initialization.
+	(GOMP_target_update): Ditto.
+	(gomp_load_plugin_for_device): Take handles for get_type,
+	get_num_devices, offload_register, device_init, device_get_table,
+	device_run functions.
+	(gomp_register_images_for_device): New function.
+	(gomp_find_available_plugins): Add registration of offload images.
+
 2014-02-28  Thomas Schwinge  <thomas@codesourcery.com>
 
 	* testsuite/libgomp.oacc-c/goacc_kernels.c: New file.
diff --git a/libgomp/libgomp.map b/libgomp/libgomp.map
index e9f8b55..9328767 100644
--- a/libgomp/libgomp.map
+++ b/libgomp/libgomp.map
@@ -208,6 +208,7 @@ GOMP_3.0 {
 
 GOMP_4.0 {
   global:
+	GOMP_offload_register;
 	GOMP_barrier_cancel;
 	GOMP_cancel;
 	GOMP_cancellation_point;
diff --git a/libgomp/plugin-host.c b/libgomp/plugin-host.c
index 5354ebe..ec0c78c 100644
--- a/libgomp/plugin-host.c
+++ b/libgomp/plugin-host.c
@@ -33,14 +33,53 @@
 #include <stdlib.h>
 #include <string.h>
 
-bool
-device_available (void)
+const int TARGET_TYPE_HOST = 0;
+
+int
+get_type (void)
 {
 #ifdef DEBUG
   printf ("libgomp plugin: %s:%s\n", __FILE__, __FUNCTION__);
 #endif
 
-  return true;
+  return TARGET_TYPE_HOST;
+}
+
+int
+get_num_devices (void)
+{
+#ifdef DEBUG
+  printf ("libgomp plugin: %s:%s\n", __FILE__, __FUNCTION__);
+#endif
+
+  return 1;
+}
+
+void
+offload_register (void *host_table, void *target_data)
+{
+#ifdef DEBUG
+  printf ("libgomp plugin: %s:%s (%p, %p)\n", __FILE__, __FUNCTION__,
+	  host_table, target_data);
+#endif
+}
+
+void
+device_init (void)
+{
+#ifdef DEBUG
+  printf ("libgomp plugin: %s:%s\n", __FILE__, __FUNCTION__);
+#endif
+}
+
+int
+device_get_table (void *table)
+{
+#ifdef DEBUG
+  printf ("libgomp plugin: %s:%s (%p)\n", __FILE__, __FUNCTION__, table);
+#endif
+
+  return 0;
 }
 
 void *
@@ -82,3 +121,16 @@ void *device_host2dev (void *dest, const void *src, size_t n)
 
   return memcpy (dest, src, n);
 }
+
+void
+device_run (void *fn_ptr, void *vars)
+{
+#ifdef DEBUG
+  printf ("libgomp plugin: %s:%s (%p, %p)\n", __FILE__, __FUNCTION__, fn_ptr,
+	  vars);
+#endif
+
+  void (*fn)(void *) = (void (*)(void *)) fn_ptr;
+
+  fn (vars);
+}
diff --git a/libgomp/target.c b/libgomp/target.c
index a6a5505..0715b31 100644
--- a/libgomp/target.c
+++ b/libgomp/target.c
@@ -84,6 +84,26 @@ struct splay_tree_key_s {
   bool copy_from;
 };
 
+enum target_type {
+  TARGET_TYPE_HOST,
+  TARGET_TYPE_INTEL_MIC
+};
+
+/* This structure describes an offload image.
+   It contains type of the target, pointer to host table descriptor, and pointer
+   to target data.  */
+struct offload_image_descr {
+  int type;
+  void *host_table;
+  void *target_data;
+};
+
+/* Array of descriptors of offload images.  */
+static struct offload_image_descr *offload_images;
+
+/* Total number of offload images.  */
+static int num_offload_images;
+
 /* Array of descriptors of all available devices.  */
 static struct gomp_device_descr *devices;
 
@@ -117,15 +137,26 @@ struct gomp_device_descr
      TARGET construct.  */
   int id;
 
+  /* This is the TYPE of device.  */
+  int type;
+
+  /* Set to true when device is initialized.  */
+  bool is_initialized;
+
   /* Plugin file handler.  */
   void *plugin_handle;
 
   /* Function handlers.  */
-  bool (*device_available_func) (void);
+  int (*get_type_func) (void);
+  int (*get_num_devices_func) (void);
+  void (*offload_register_func) (void *, void *);
+  void (*device_init_func) (void);
+  int (*device_get_table_func) (void *);
   void *(*device_alloc_func) (size_t);
   void (*device_free_func) (void *);
-  void *(*device_dev2host_func)(void *, const void *, size_t);
-  void *(*device_host2dev_func)(void *, const void *, size_t);
+  void *(*device_dev2host_func) (void *, const void *, size_t);
+  void *(*device_host2dev_func) (void *, const void *, size_t);
+  void (*device_run_func) (void *, void *);
 
   /* Splay tree containing information about mapped memory regions.  */
   struct splay_tree_s dev_splay_tree;
@@ -134,6 +165,13 @@ struct gomp_device_descr
   gomp_mutex_t dev_env_lock;
 };
 
+struct mapping_table {
+  uintptr_t host_start;
+  uintptr_t host_end;
+  uintptr_t tgt_start;
+  uintptr_t tgt_end;
+};
+
 attribute_hidden int
 gomp_get_num_devices (void)
 {
@@ -471,6 +509,63 @@ gomp_update (struct gomp_device_descr *devicep, size_t mapnum,
   gomp_mutex_unlock (&devicep->dev_env_lock);
 }
 
+/* This function should be called from every offload image.  It gets the
+   descriptor of the host func and var tables HOST_TABLE, TYPE of the target,
+   and TARGET_DATA needed by target plugin (target tables, etc.)  */
+void
+GOMP_offload_register (void *host_table, int type, void *target_data)
+{
+  offload_images = realloc (offload_images,
+			    (num_offload_images + 1)
+			    * sizeof (struct offload_image_descr));
+
+  if (offload_images == NULL)
+    return;
+
+  offload_images[num_offload_images].type = type;
+  offload_images[num_offload_images].host_table = host_table;
+  offload_images[num_offload_images].target_data = target_data;
+
+  num_offload_images++;
+}
+
+static void
+gomp_init_device (struct gomp_device_descr *devicep)
+{
+  /* Initialize the target device.  */
+  devicep->device_init_func ();
+
+  /* Get address mapping table for device.  */
+  struct mapping_table *table = NULL;
+  int num_entries = devicep->device_get_table_func (&table);
+
+  /* Insert host-target address mapping into dev_splay_tree.  */
+  int i;
+  for (i = 0; i < num_entries; i++)
+    {
+      struct target_mem_desc *tgt = gomp_malloc (sizeof (*tgt));
+      tgt->refcount = 1;
+      tgt->array = gomp_malloc (sizeof (*tgt->array));
+      tgt->tgt_start = table[i].tgt_start;
+      tgt->tgt_end = table[i].tgt_end;
+      tgt->to_free = NULL;
+      tgt->list_count = 0;
+      tgt->device_descr = devicep;
+      splay_tree_node node = tgt->array;
+      splay_tree_key k = &node->key;
+      k->host_start = table[i].host_start;
+      k->host_end = table[i].host_end;
+      k->tgt_offset = 0;
+      k->tgt = tgt;
+      node->left = NULL;
+      node->right = NULL;
+      splay_tree_insert (&devicep->dev_splay_tree, node);
+    }
+
+  free (table);
+  devicep->is_initialized = true;
+}
+
 /* Called when encountering a target directive.  If DEVICE
    is -1, it means use device-var ICV.  If it is -2 (or any other value
    larger than last available hw device, use host fallback.
@@ -504,7 +599,17 @@ GOMP_target (int device, void (*fn) (void *), const void *openmp_target,
       return;
     }
 
-  struct target_mem_desc *tgt
+  if (!devicep->is_initialized)
+    gomp_init_device (devicep);
+
+  struct splay_tree_key_s k;
+  k.host_start = (uintptr_t) fn;
+  k.host_end = k.host_start + 1;
+  splay_tree_key tgt_fn = splay_tree_lookup (&devicep->dev_splay_tree, &k);
+  if (tgt_fn == NULL && devicep->type != TARGET_TYPE_HOST)
+    gomp_fatal ("Target function wasn't mapped");
+
+  struct target_mem_desc *tgt_vars
     = gomp_map_vars (devicep, mapnum, hostaddrs, sizes, kinds, true);
   struct gomp_thread old_thr, *thr = gomp_thread ();
   old_thr = *thr;
@@ -514,10 +619,14 @@ GOMP_target (int device, void (*fn) (void *), const void *openmp_target,
       thr->place = old_thr.place;
       thr->ts.place_partition_len = gomp_places_list_len;
     }
-  fn ((void *) tgt->tgt_start);
+  if (devicep->type == TARGET_TYPE_HOST)
+    devicep->device_run_func (fn, (void *) tgt_vars->tgt_start);
+  else
+    devicep->device_run_func ((void *) tgt_fn->tgt->tgt_start,
+			      (void *) tgt_vars->tgt_start);
   gomp_free_thread (thr);
   *thr = old_thr;
-  gomp_unmap_vars (tgt);
+  gomp_unmap_vars (tgt_vars);
 }
 
 void
@@ -543,6 +652,9 @@ GOMP_target_data (int device, const void *openmp_target, size_t mapnum,
       return;
     }
 
+  if (!devicep->is_initialized)
+    gomp_init_device (devicep);
+
   struct target_mem_desc *tgt
     = gomp_map_vars (devicep, mapnum, hostaddrs, sizes, kinds, false);
   struct gomp_task_icv *icv = gomp_icv (true);
@@ -570,6 +682,9 @@ GOMP_target_update (int device, const void *openmp_target, size_t mapnum,
   if (devicep == NULL)
     return;
 
+  if (!devicep->is_initialized)
+    gomp_init_device (devicep);
+
   gomp_update (devicep, mapnum, hostaddrs, sizes, kinds);
 }
 
@@ -636,11 +751,16 @@ gomp_load_plugin_for_device (struct gomp_device_descr *device,
 	goto out;							\
     }									\
   while (0)
-  DLSYM (device_available);
+  DLSYM (get_type);
+  DLSYM (get_num_devices);
+  DLSYM (offload_register);
+  DLSYM (device_init);
+  DLSYM (device_get_table);
   DLSYM (device_alloc);
   DLSYM (device_free);
   DLSYM (device_dev2host);
   DLSYM (device_host2dev);
+  DLSYM (device_run);
 #undef DLSYM
 
  out:
@@ -653,6 +773,21 @@ gomp_load_plugin_for_device (struct gomp_device_descr *device,
   return err == NULL;
 }
 
+/* This function finds OFFLOAD_IMAGES corresponding to DEVICE type, and
+   registers them in the plugin.  */
+static void
+gomp_register_images_for_device (struct gomp_device_descr *device)
+{
+  int i;
+  for (i = 0; i < num_offload_images; i++)
+    {
+      struct offload_image_descr *image = &offload_images[i];
+
+      if (device->type == image->type || device->type == TARGET_TYPE_HOST)
+	device->offload_register_func (image->host_table, image->target_data);
+    }
+}
+
 /* This functions scans folder, specified in environment variable
    LIBGOMP_PLUGIN_PATH, and loads all suitable libgomp plugins from this folder.
    For a plugin to be suitable, its name should be "libgomp-plugin-*.so.1" and
@@ -698,16 +833,25 @@ gomp_find_available_plugins (void)
 	  goto out;
 	}
 
-      devices[num_devices] = current_device;
-      devices[num_devices].id = num_devices + 1;
-      devices[num_devices].dev_splay_tree.root = NULL;
-      gomp_mutex_init (&devices[num_devices].dev_env_lock);
-      num_devices++;
+      /* FIXME: Properly handle multiple devices of the same type.  */
+      if (current_device.get_num_devices_func () >= 1)
+	{
+	  current_device.id = num_devices + 1;
+	  current_device.type = current_device.get_type_func ();
+	  current_device.is_initialized = false;
+	  current_device.dev_splay_tree.root = NULL;
+	  gomp_register_images_for_device (&current_device);
+	  devices[num_devices] = current_device;
+	  gomp_mutex_init (&devices[num_devices].dev_env_lock);
+	  num_devices++;
+	}
     }
 
- out:
+out:
   if (dir)
     closedir (dir);
+  free (offload_images);
+  num_offload_images = 0;
 }
 
 /* This function initializes runtime needed for offloading.
-- 
1.7.1


  -- Ilya

  reply	other threads:[~2014-03-12 17:57 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-20 17:18 Ilya Verbin
2013-12-26 13:37 ` Ilya Verbin
2014-01-15 15:09   ` Ilya Verbin
2014-03-12 18:21     ` Ilya Verbin [this message]
2014-03-17 13:10       ` Ilya Verbin
2014-03-17 15:12       ` Fwd: [RFC][gomp4] Offloading patches (2/3): Add tables generation Thomas Schwinge
2014-03-17 16:49         ` Jakub Jelinek
2014-03-18 17:36         ` Ilya Verbin
  -- strict thread matches above, loose matches on Subject: below --
2013-12-17 11:40 Michael V. Zolotukhin
2014-01-16 11:37 ` Michael Zolotukhin
2014-01-28 11:20 ` Bernd Schmidt
     [not found]   ` <CADG=Z0GQ8ORLe1XRUU7VMYeLhwuWisMyCcGLQj-nY_bhkbD_1Q@mail.gmail.com>
2014-01-28 12:53     ` Fwd: " Ilya Verbin
2014-01-29 14:43       ` Bernd Schmidt
2014-01-29 16:06         ` Ilya Verbin
2014-02-14 14:49           ` Ilya Verbin
2014-02-14 15:02             ` Bernd Schmidt
2014-02-14 15:12               ` Jakub Jelinek
2014-02-14 15:50                 ` Bernd Schmidt
2014-02-14 16:06                   ` Jakub Jelinek
2014-02-20 18:27             ` Bernd Schmidt
2014-02-21 15:17               ` Ilya Verbin
2014-02-21 15:41                 ` Bernd Schmidt
2014-02-21 18:00                   ` Ilya Verbin
2014-02-28 16:09               ` Ilya Verbin
2014-02-28 16:23                 ` Bernd Schmidt
2014-02-28 21:41                   ` Bernd Schmidt
2014-05-27 10:18                     ` Ilya Verbin
2014-05-27 10:59                       ` Bernd Schmidt
2014-05-27 11:11                         ` Ilya Verbin
2014-05-27 11:16                           ` Bernd Schmidt
2014-05-27 15:33                             ` Ilya Verbin
2014-05-27 19:03                               ` Bernd Schmidt
2014-02-28 22:10                   ` Ilya Verbin
2014-03-05 17:15                   ` Ilya Verbin
2014-03-06  8:48                     ` Bernd Schmidt
2014-03-06 11:11                       ` Ilya Verbin
2014-03-06 11:54                         ` Bernd Schmidt
2014-03-06 12:52                           ` Ilya Verbin
2014-03-08 15:39                             ` Ilya Verbin
2014-03-12 14:14                               ` Bernd Schmidt
2014-03-12 14:52                                 ` Ilya Verbin
2014-03-20 17:41                                   ` Bernd Schmidt
2014-06-17 18:20                   ` Ilya Verbin
2014-06-17 19:23                     ` Bernd Schmidt
2014-06-18 14:14                       ` Ilya Verbin
2014-06-18 14:23                         ` Bernd Schmidt
2014-06-19 10:19                           ` Ilya Verbin
2014-06-27  7:33                             ` Bernd Schmidt
2014-07-07 14:51                               ` Ilya Verbin
2014-07-07 15:04                                 ` Bernd Schmidt
2014-07-07 15:14                                   ` Jakub Jelinek
2014-07-07 15:22                                     ` Jakub Jelinek
2014-07-10 18:24                                   ` Ilya Verbin
2014-07-10 18:28                                     ` Bernd Schmidt
2014-07-23 14:18                                 ` Bernd Schmidt
2014-07-23 14:40                                   ` Ilya Verbin
2014-08-04 21:00                                     ` Bernd Schmidt
2014-08-07 17:35                                       ` Thomas Schwinge
2014-09-05 16:36                                         ` [gomp4] Revert more old Makefile/configure bits Bernd Schmidt
2014-09-08  9:42                                           ` Thomas Schwinge
2014-07-24 15:58                                   ` Fwd: [RFC][gomp4] Offloading patches (2/3): Add tables generation Ilya Verbin
2014-05-08  9:44 ` [gomp4] Mark __OPENMP_TARGET__ as addressable (was: Offloading patches (2/3): Add tables generation) Thomas Schwinge

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140312175644.GA40186@msticlxl57.ims.intel.com \
    --to=iverbin@gmail.com \
    --cc=andrey.turetskiy@gmail.com \
    --cc=bernds@codesourcery.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=kirill.yukhin@gmail.com \
    --cc=michael.v.zolotukhin@gmail.com \
    --cc=rguenther@suse.de \
    --cc=thomas@codesourcery.com \
    --cc=tocarip.intel@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).