public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tburnus@baylibre.com>
To: Kwok Cheung Yeung <kcyeung@baylibre.com>,
	gcc-patches <gcc-patches@gcc.gnu.org>
Cc: Jakub Jelinek <jakub@redhat.com>,
	Thomas Schwinge <tschwinge@baylibre.com>
Subject: Re: [PATCH v2] openmp: Change to using a hashtab to lookup offload target addresses for indirect function calls
Date: Thu, 14 Mar 2024 12:38:23 +0100	[thread overview]
Message-ID: <1b7d9cfd-8586-49a0-8bc4-fa65f2c59bf3@baylibre.com> (raw)
In-Reply-To: <679889de-bf47-4a01-887e-db96f7fad427@baylibre.com>

Hi Kwok,

On January 22, 2024, Kwok Cheung Yeung wrote:
> There was a bug in the declare-target-indirect-2.c libgomp testcase 
> (testing indirect calls in offloaded target regions, spread over 
> multiple teams/threads) that due to an errant fallthrough in a switch 
> statement resulted in only one indirect function ever getting called:

(When applying, also the 'dg-xfail-run-if' needs to be removed from
libgomp.fortran/declare-target-indirect-2.f90) ...

> However, when the missing break statements are added, the testcase 
> fails with an invalid memory access. Upon investigation, this is due 
> to the use of a splay-tree as the lookup structure for indirect 
> addresses, as the splay-tree moves frequently accessed elements closer 
> to the root node and so needs locking when used from multiple threads. 
> However, this would end up partially serialising all the threads and 
> kill performance. I have switched the lookup structure from a splay 
> tree to a hashtab instead to avoid locking during lookup.
>
> I have also tidied up the initialisation of the lookup table by 
> calling it only from the first thread of the first team, instead of 
> redundantly calling it from every thread and only having the first one 
> reached do the initialisation. This removes the need for locking 
> during initialisation.

LGTM - except of the following, which we need to solve
(as suggested or differently (locking, or ...) or
by declaring it a nonissue (e.g. because of thinko of mine).

Thoughts about the following?

* * *

Namely, I wonder whether there will be an issue for

#pragma target nowait
    ...
#pragma target
    ...

Once the kernel is started, thegcn_expand_prologue creates some setup code and then a call to 
gomp_gcn_enter_kernel. Likewise for gcc/config/nvptx/nvptx.cc, where 
nvptx_declare_function_name adds via write_omp_entry a call to 
gomp_nvptx_main. And one of the first tasks there is 'build_indirect_map'. Assume a very simple kernel for the second item (i.e. it is quickly started)
and a very large number of reverse kernels.

Now, I wonder whether it is possible to have a race between the two kernels;
it seems as if that might happen but is extremely unlikely accounting for all
the overhead of launching and the rather small list of reverse offload items.

As it is unlikely, I wonder whether doing the following lock free, opportunistic
approach will be the best solution. Namely, assuming that no other kernel updates
the hash, but if that happens by chance, use the one that was created first.
(If we are lucky, the atomic overhead is fully cancelled by using a local
variable in the function but neither should matter much.)

if (!indirect_htab) // or: __atomic_load_n (&indirect_htab, __ATOMIC_RELAXED) ?
{
   htab_t local_indirect_htab = htab_create (num_ind_funcs);
   ...
   htab_t expected = NULL;
   __atomic_compare_exchange_n (&indirect_htab, &expected,
			       local_indirect_htab, false, ...);
   if (expected) // Other kernel was faster, drop our version
     htab_free (local_indirect_htab);
}

On January 29, 2024, Kwok Cheung Yeung wrote:
>> Can you please akso update the comments to talk about hashtab instead 
>> of splay?
> This version has the comments updated and removes a stray 'volatile' 
> in the #ifdefed out code.
Thanks,

Tobias


  parent reply	other threads:[~2024-03-14 11:38 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-08 13:13 [PATCH] openmp: Add support for the 'indirect' clause in C/C++ Kwok Cheung Yeung
2023-10-17 13:12 ` Tobias Burnus
2023-10-17 13:34   ` Jakub Jelinek
2023-10-17 14:41     ` Tobias Burnus
2023-11-03 19:53   ` Kwok Cheung Yeung
2023-11-06  8:48     ` Tobias Burnus
2023-11-07 21:37       ` Joseph Myers
2023-11-07 21:51         ` Jakub Jelinek
2023-11-07 21:59           ` Kwok Cheung Yeung
2023-11-09 12:24     ` Thomas Schwinge
2023-11-09 16:00       ` Tobias Burnus
2023-11-13 10:59         ` Thomas Schwinge
2023-11-13 11:47           ` Tobias Burnus
2024-04-11 10:10             ` Thomas Schwinge
2024-01-03 14:47       ` [committed] " Kwok Cheung Yeung
2024-01-03 15:54       ` Kwok Cheung Yeung
2024-01-22 20:33     ` [PATCH] openmp: Change to using a hashtab to lookup offload target addresses for indirect function calls Kwok Cheung Yeung
2024-01-24  7:06       ` rep.dot.nop
2024-01-29 17:48         ` [PATCH v2] " Kwok Cheung Yeung
2024-03-08 13:40           ` Thomas Schwinge
2024-03-14 11:38           ` Tobias Burnus [this message]
2024-01-22 20:41     ` [PATCH] openmp, fortran: Add Fortran support for indirect clause on the declare target directive Kwok Cheung Yeung
2024-01-23 19:14       ` Tobias Burnus
2024-02-05 21:37         ` [PATCH v2] " Kwok Cheung Yeung
2024-02-06  9:03           ` Tobias Burnus
2024-02-06  9:50             ` Kwok Cheung Yeung
2024-02-12  8:51               ` Tobias Burnus
2024-02-15 21:37                 ` [COMMITTED] libgomp: Update documentation for indirect calls in target regions Kwok Cheung Yeung

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=1b7d9cfd-8586-49a0-8bc4-fa65f2c59bf3@baylibre.com \
    --to=tburnus@baylibre.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=kcyeung@baylibre.com \
    --cc=tschwinge@baylibre.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).