]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - liboffloadmic/plugin/libgomp-plugin-intelmic.cpp
libgomp-plugin-intelmic.cpp (struct TargetImageDesc): New.
[thirdparty/gcc.git] / liboffloadmic / plugin / libgomp-plugin-intelmic.cpp
index 772e1985dfbbaf75ac6ec296b0e68a98000cc851..f8c172562ae9f89d566aac1d32dbda8866694650 100644 (file)
@@ -65,6 +65,17 @@ typedef std::vector<AddrVect> DevAddrVect;
 /* Addresses for all images and all devices.  */
 typedef std::map<const void *, DevAddrVect> ImgDevAddrMap;
 
+/* Image descriptor needed by __offload_[un]register_image.  */
+struct TargetImageDesc {
+  int64_t size;
+  /* 10 characters is enough for max int value.  */
+  char name[sizeof ("lib0000000000.so")];
+  char data[];
+};
+
+/* Image descriptors, indexed by a pointer obtained from libgomp.  */
+typedef std::map<const void *, TargetImageDesc *> ImgDescMap;
+
 
 /* Total number of available devices.  */
 static int num_devices;
@@ -76,6 +87,9 @@ static int num_images;
    second key is number of device.  Contains a vector of pointer pairs.  */
 static ImgDevAddrMap *address_table;
 
+/* Descriptors of all images, registered in liboffloadmic.  */
+static ImgDescMap *image_descriptors;
+
 /* Thread-safe registration of the main image.  */
 static pthread_once_t main_image_is_registered = PTHREAD_ONCE_INIT;
 
@@ -156,6 +170,7 @@ init (void)
 
 out:
   address_table = new ImgDevAddrMap;
+  image_descriptors = new ImgDescMap;
   num_devices = _Offload_number_of_devices ();
 }
 
@@ -192,14 +207,13 @@ GOMP_OFFLOAD_get_num_devices (void)
 
 static void
 offload (const char *file, uint64_t line, int device, const char *name,
-        int num_vars, VarDesc *vars, VarDesc2 *vars2, const void **async_data)
+        int num_vars, VarDesc *vars, const void **async_data)
 {
   OFFLOAD ofld = __offload_target_acquire1 (&device, file, line);
   if (ofld)
     {
       if (async_data == NULL)
-       __offload_offload1 (ofld, name, 0, num_vars, vars, vars2, 0, NULL,
-                           NULL);
+       __offload_offload1 (ofld, name, 0, num_vars, vars, NULL, 0, NULL, NULL);
       else
        {
          OffloadFlags flags;
@@ -216,14 +230,28 @@ offload (const char *file, uint64_t line, int device, const char *name,
     }
 }
 
+static void
+unregister_main_image ()
+{
+  __offload_unregister_image (&main_target_image);
+}
+
 static void
 register_main_image ()
 {
+  /* Do not check the return value, because old versions of liboffloadmic did
+     not have return values.  */
   __offload_register_image (&main_target_image);
 
   /* liboffloadmic will call GOMP_PLUGIN_target_task_completion when
      asynchronous task on target is completed.  */
   __offload_register_task_callback (GOMP_PLUGIN_target_task_completion);
+
+  if (atexit (unregister_main_image) != 0)
+    {
+      fprintf (stderr, "%s: atexit failed\n", __FILE__);
+      exit (1);
+    }
 }
 
 /* liboffloadmic loads and runs offload_target_main on all available devices
@@ -231,16 +259,16 @@ register_main_image ()
 extern "C" void
 GOMP_OFFLOAD_init_device (int device)
 {
-  TRACE ("");
+  TRACE ("(device = %d)", device);
   pthread_once (&main_image_is_registered, register_main_image);
-  offload (__FILE__, __LINE__, device, "__offload_target_init_proc", 0,
-          NULL, NULL, NULL);
+  offload (__FILE__, __LINE__, device, "__offload_target_init_proc", 0, NULL,
+          NULL);
 }
 
 extern "C" void
 GOMP_OFFLOAD_fini_device (int device)
 {
-  TRACE ("");
+  TRACE ("(device = %d)", device);
   /* Unreachable for GOMP_OFFLOAD_CAP_OPENMP_400.  */
   abort ();
 }
@@ -253,10 +281,9 @@ get_target_table (int device, int &num_funcs, int &num_vars, void **&table)
   vd1[0].size = sizeof (num_funcs);
   vd1[1].ptr = &num_vars;
   vd1[1].size = sizeof (num_vars);
-  VarDesc2 vd1g[2] = { { "num_funcs", 0 }, { "num_vars", 0 } };
 
-  offload (__FILE__, __LINE__, device, "__offload_target_table_p1", 2,
-          vd1, vd1g, NULL);
+  offload (__FILE__, __LINE__, device, "__offload_target_table_p1", 2, vd1,
+          NULL);
 
   int table_size = num_funcs + 2 * num_vars;
   if (table_size > 0)
@@ -267,10 +294,9 @@ get_target_table (int device, int &num_funcs, int &num_vars, void **&table)
       vd2 = vd_tgt2host;
       vd2.ptr = table;
       vd2.size = table_size * sizeof (void *);
-      VarDesc2 vd2g = { "table", 0 };
 
-      offload (__FILE__, __LINE__, device, "__offload_target_table_p2", 1,
-              &vd2, &vd2g, NULL);
+      offload (__FILE__, __LINE__, device, "__offload_target_table_p2", 1, &vd2,
+              NULL);
     }
 }
 
@@ -280,13 +306,6 @@ get_target_table (int device, int &num_funcs, int &num_vars, void **&table)
 static void
 offload_image (const void *target_image)
 {
-  struct TargetImage {
-    int64_t size;
-    /* 10 characters is enough for max int value.  */
-    char name[sizeof ("lib0000000000.so")];
-    char data[];
-  } __attribute__ ((packed));
-
   void *image_start = ((void **) target_image)[0];
   void *image_end   = ((void **) target_image)[1];
 
@@ -294,9 +313,8 @@ offload_image (const void *target_image)
         target_image, image_start, image_end);
 
   int64_t image_size = (uintptr_t) image_end - (uintptr_t) image_start;
-  TargetImage *image
-    = (TargetImage *) malloc (sizeof (int64_t) + sizeof ("lib0000000000.so")
-                             + image_size);
+  TargetImageDesc *image = (TargetImageDesc *) malloc (offsetof (TargetImageDesc, data)
+                                                      + image_size);
   if (!image)
     {
       fprintf (stderr, "%s: Can't allocate memory\n", __FILE__);
@@ -309,6 +327,8 @@ offload_image (const void *target_image)
 
   TRACE ("() __offload_register_image %s { %p, %d }",
         image->name, image_start, image->size);
+  /* Do not check the return value, because old versions of liboffloadmic did
+     not have return values.  */
   __offload_register_image (image);
 
   /* Receive tables for target_image from all devices.  */
@@ -343,11 +363,11 @@ offload_image (const void *target_image)
        }
 
       dev_table.push_back (curr_dev_table);
+      delete [] table;
     }
 
   address_table->insert (std::make_pair (target_image, dev_table));
-
-  free (image);
+  image_descriptors->insert (std::make_pair (target_image, image));
 }
 
 /* Return the libgomp version number we're compatible with.  There is
@@ -398,49 +418,53 @@ GOMP_OFFLOAD_unload_image (int device, unsigned version,
 
   TRACE ("(device = %d, target_image = %p)", device, target_image);
 
-  /* TODO: Currently liboffloadmic doesn't support __offload_unregister_image
-     for libraries.  */
+  /* liboffloadmic unloads the image from all available devices.  */
+  if (image_descriptors->count (target_image) > 0)
+    {
+      TargetImageDesc *image_desc = (*image_descriptors)[target_image];
+      __offload_unregister_image (image_desc);
+      free (image_desc);
 
-  address_table->erase (target_image);
+      address_table->erase (target_image);
+      image_descriptors->erase (target_image);
+    }
 }
 
 extern "C" void *
 GOMP_OFFLOAD_alloc (int device, size_t size)
 {
-  TRACE ("(size = %d)", size);
+  TRACE ("(device = %d, size = %d)", device, size);
 
   void *tgt_ptr;
-  VarDesc vd1[2] = { vd_host2tgt, vd_tgt2host };
-  vd1[0].ptr = &size;
-  vd1[0].size = sizeof (size);
-  vd1[1].ptr = &tgt_ptr;
-  vd1[1].size = sizeof (void *);
-  VarDesc2 vd1g[2] = { { "size", 0 }, { "tgt_ptr", 0 } };
-
-  offload (__FILE__, __LINE__, device, "__offload_target_alloc", 2, vd1, vd1g,
-          NULL);
+  VarDesc vd[2] = { vd_host2tgt, vd_tgt2host };
+  vd[0].ptr = &size;
+  vd[0].size = sizeof (size);
+  vd[1].ptr = &tgt_ptr;
+  vd[1].size = sizeof (void *);
+
+  offload (__FILE__, __LINE__, device, "__offload_target_alloc", 2, vd, NULL);
+
   return tgt_ptr;
 }
 
 extern "C" void
 GOMP_OFFLOAD_free (int device, void *tgt_ptr)
 {
-  TRACE ("(tgt_ptr = %p)", tgt_ptr);
+  TRACE ("(device = %d, tgt_ptr = %p)", device, tgt_ptr);
 
-  VarDesc vd1 = vd_host2tgt;
-  vd1.ptr = &tgt_ptr;
-  vd1.size = sizeof (void *);
-  VarDesc2 vd1g = { "tgt_ptr", 0 };
+  VarDesc vd = vd_host2tgt;
+  vd.ptr = &tgt_ptr;
+  vd.size = sizeof (void *);
 
-  offload (__FILE__, __LINE__, device, "__offload_target_free", 1, &vd1, &vd1g,
-          NULL);
+  offload (__FILE__, __LINE__, device, "__offload_target_free", 1, &vd, NULL);
 }
 
 extern "C" void *
 GOMP_OFFLOAD_host2dev (int device, void *tgt_ptr, const void *host_ptr,
                       size_t size)
 {
-  TRACE ("(tgt_ptr = %p, host_ptr = %p, size = %d)", tgt_ptr, host_ptr, size);
+  TRACE ("(device = %d, tgt_ptr = %p, host_ptr = %p, size = %d)",
+        device, tgt_ptr, host_ptr, size);
   if (!size)
     return tgt_ptr;
 
@@ -449,18 +473,16 @@ GOMP_OFFLOAD_host2dev (int device, void *tgt_ptr, const void *host_ptr,
   vd1[0].size = sizeof (void *);
   vd1[1].ptr = &size;
   vd1[1].size = sizeof (size);
-  VarDesc2 vd1g[2] = { { "tgt_ptr", 0 }, { "size", 0 } };
 
-  offload (__FILE__, __LINE__, device, "__offload_target_host2tgt_p1", 2,
-          vd1, vd1g, NULL);
+  offload (__FILE__, __LINE__, device, "__offload_target_host2tgt_p1", 2, vd1,
+          NULL);
 
   VarDesc vd2 = vd_host2tgt;
   vd2.ptr = (void *) host_ptr;
   vd2.size = size;
-  VarDesc2 vd2g = { "var", 0 };
 
-  offload (__FILE__, __LINE__, device, "__offload_target_host2tgt_p2", 1,
-          &vd2, &vd2g, NULL);
+  offload (__FILE__, __LINE__, device, "__offload_target_host2tgt_p2", 1, &vd2,
+          NULL);
 
   return tgt_ptr;
 }
@@ -469,7 +491,8 @@ extern "C" void *
 GOMP_OFFLOAD_dev2host (int device, void *host_ptr, const void *tgt_ptr,
                       size_t size)
 {
-  TRACE ("(host_ptr = %p, tgt_ptr = %p, size = %d)", host_ptr, tgt_ptr, size);
+  TRACE ("(device = %d, host_ptr = %p, tgt_ptr = %p, size = %d)",
+        device, host_ptr, tgt_ptr, size);
   if (!size)
     return host_ptr;
 
@@ -478,18 +501,16 @@ GOMP_OFFLOAD_dev2host (int device, void *host_ptr, const void *tgt_ptr,
   vd1[0].size = sizeof (void *);
   vd1[1].ptr = &size;
   vd1[1].size = sizeof (size);
-  VarDesc2 vd1g[2] = { { "tgt_ptr", 0 }, { "size", 0 } };
 
-  offload (__FILE__, __LINE__, device, "__offload_target_tgt2host_p1", 2,
-          vd1, vd1g, NULL);
+  offload (__FILE__, __LINE__, device, "__offload_target_tgt2host_p1", 2, vd1,
+          NULL);
 
   VarDesc vd2 = vd_tgt2host;
   vd2.ptr = (void *) host_ptr;
   vd2.size = size;
-  VarDesc2 vd2g = { "var", 0 };
 
-  offload (__FILE__, __LINE__, device, "__offload_target_tgt2host_p2", 1,
-          &vd2, &vd2g, NULL);
+  offload (__FILE__, __LINE__, device, "__offload_target_tgt2host_p2", 1, &vd2,
+          NULL);
 
   return host_ptr;
 }
@@ -498,21 +519,20 @@ extern "C" void *
 GOMP_OFFLOAD_dev2dev (int device, void *dst_ptr, const void *src_ptr,
                      size_t size)
 {
-  TRACE ("(dst_ptr = %p, src_ptr = %p, size = %d)", dst_ptr, src_ptr, size);
+  TRACE ("(device = %d, dst_ptr = %p, src_ptr = %p, size = %d)",
+        device, dst_ptr, src_ptr, size);
   if (!size)
     return dst_ptr;
 
-  VarDesc vd1[3] = { vd_host2tgt, vd_host2tgt, vd_host2tgt };
-  vd1[0].ptr = &dst_ptr;
-  vd1[0].size = sizeof (void *);
-  vd1[1].ptr = &src_ptr;
-  vd1[1].size = sizeof (void *);
-  vd1[2].ptr = &size;
-  vd1[2].size = sizeof (size);
-  VarDesc2 vd1g[3] = { { "dst_ptr", 0 }, { "src_ptr", 0 }, { "size", 0 } };
+  VarDesc vd[3] = { vd_host2tgt, vd_host2tgt, vd_host2tgt };
+  vd[0].ptr = &dst_ptr;
+  vd[0].size = sizeof (void *);
+  vd[1].ptr = &src_ptr;
+  vd[1].size = sizeof (void *);
+  vd[2].ptr = &size;
+  vd[2].size = sizeof (size);
 
-  offload (__FILE__, __LINE__, device, "__offload_target_tgt2tgt", 3, vd1,
-          vd1g, NULL);
+  offload (__FILE__, __LINE__, device, "__offload_target_tgt2tgt", 3, vd, NULL);
 
   return dst_ptr;
 }
@@ -530,7 +550,7 @@ GOMP_OFFLOAD_async_run (int device, void *tgt_fn, void *tgt_vars,
   vd[1].ptr = &tgt_vars;
   vd[1].size = sizeof (void *);
 
-  offload (__FILE__, __LINE__, device, "__offload_target_run", 2, vd, NULL,
+  offload (__FILE__, __LINE__, device, "__offload_target_run", 2, vd,
           (const void **) async_data);
 }