public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tobias@codesourcery.com>
To: Thomas Schwinge <thomas@codesourcery.com>,
	<gcc-patches@gcc.gnu.org>, Jakub Jelinek <jakub@redhat.com>
Subject: Re: [Patch] libgomp: Handle OpenMP's reverse offloads
Date: Thu, 15 Dec 2022 21:13:20 +0100	[thread overview]
Message-ID: <1f985418-b6ae-150b-ba11-52a32438d2b5@codesourcery.com> (raw)
In-Reply-To: <fd3e5c7d-e4c3-cb6d-9043-6a8f9e15c41f@codesourcery.com>

[-- Attachment #1: Type: text/plain, Size: 2049 bytes --]

Hi,

On 15.12.22 20:42, Tobias Burnus wrote:
>> If the libgomp plugin doesn't request special
>> 'host_to_dev_cpy'/'dev_to_host_cpy' for 'gomp_target_rev', then standard
>> 'gomp_copy_host2dev'/'gomp_copy_dev2host' are used, which use
>> 'gomp_device_copy', which expects the device to be locked.  (As can be
>> told by the unconditional 'gomp_mutex_unlock (&devicep->lock);' before
>> 'gomp_fatal'.)  However, in a number of the
>> 'gomp_copy_host2dev'/'gomp_copy_dev2host' calls from 'gomp_target_rev',
>> the device definitely is not locked; see

Actually, reading it + the source code again, I think it makes sense to
return a boolean – similar to devicep->host2dev_func and
devicep->dev2host_func — and possibly wrap it into some convenience
function, similar to gomp_device_copy – at least a bare exit() without
further diagnostic does not seem to userfriendly.

BTW: In line with the other code, you could use CUDA_CALL instead of
CUDA_CALL_ERET; the fomer already calls the latter with 'false' as first
argument + is used elsewhere.

Regarding the lock: It seems the problem is the copying of
devaddrs/sizes/kinds; this does not need any lock as the stack variables
are on the device and only used for this reverse offload. Thus, there is
no need for a lock as there are no races.

However, as the existing gomp_copy_dev2host removes the lock, we could
simply keep this lock – and probably should move it down to just before
the user-function call – removing all (non-error) locks and unlocks on
the way. — I mean something like the attached patch.

Finally, I think we need to find a solution for the issue Andrew tried
to address. — The current code invokes CUDA_CALL_ASSERT – which calls
GOMP_PLUGIN_fatal.

Tobias
-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955

[-- Attachment #2: patch.diff --]
[-- Type: text/x-patch, Size: 1286 bytes --]

diff --git a/libgomp/target.c b/libgomp/target.c
index e38cc3b6f1c..4b7233307cd 100644
--- a/libgomp/target.c
+++ b/libgomp/target.c
@@ -3319,5 +3319,6 @@ gomp_target_rev (uint64_t fn_ptr, uint64_t mapnum, uint64_t devaddrs_ptr,
   gomp_mutex_lock (&devicep->lock);
   n = gomp_map_lookup_rev (&devicep->mem_map_rev, &k);
-  gomp_mutex_unlock (&devicep->lock);
+  if (devicep->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
+    gomp_mutex_unlock (&devicep->lock);
 
   if (n == NULL)
@@ -3409,5 +3410,4 @@ gomp_target_rev (uint64_t fn_ptr, uint64_t mapnum, uint64_t devaddrs_ptr,
       cdata = gomp_alloca (sizeof (*cdata) * mapnum);
       memset (cdata, '\0', sizeof (*cdata) * mapnum);
-      gomp_mutex_lock (&devicep->lock);
       for (uint64_t i = 0; i < mapnum; i++)
 	{
@@ -3643,4 +3643,5 @@ gomp_target_rev (uint64_t fn_ptr, uint64_t mapnum, uint64_t devaddrs_ptr,
       uint64_t struct_cpy = 0;
       bool clean_struct = false;
+      gomp_mutex_lock (&devicep->lock);
       for (uint64_t i = 0; i < mapnum; i++)
 	{
@@ -3695,5 +3696,5 @@ gomp_target_rev (uint64_t fn_ptr, uint64_t mapnum, uint64_t devaddrs_ptr,
 	      gomp_aligned_free ((void *) (uintptr_t) devaddrs[i]);
 	    }
-
+      gomp_mutex_unlock (&devicep->lock);
       free (devaddrs);
       free (sizes);

      reply	other threads:[~2022-12-15 20:13 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-06  7:45 Tobias Burnus
2022-12-07  8:08 ` [Patch] libgomp.texi: Reverse-offload updates (was: [Patch] libgomp: Handle OpenMP's reverse offloads) Tobias Burnus
2022-12-10  8:18   ` Tobias Burnus
2023-01-31 12:21     ` Jakub Jelinek
2022-12-09 14:44 ` [Patch] libgomp: Handle OpenMP's reverse offloads Jakub Jelinek
2022-12-10  8:11   ` Tobias Burnus
2022-12-10  8:28     ` Jakub Jelinek
2022-12-15 17:34 ` Thomas Schwinge
2022-12-15 17:49   ` Jakub Jelinek
2022-12-15 19:42   ` Tobias Burnus
2022-12-15 20:13     ` Tobias Burnus [this message]

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=1f985418-b6ae-150b-ba11-52a32438d2b5@codesourcery.com \
    --to=tobias@codesourcery.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=thomas@codesourcery.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).