public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Julian Brown <julian@codesourcery.com>
Cc: gcc-patches@gcc.gnu.org, fortran@gcc.gnu.org,
	Tobias Burnus <tobias@codesourcery.com>
Subject: Re: [PATCH v4 4/4] OpenMP/OpenACC: Unordered/non-constant component offset struct mapping
Date: Mon, 10 Oct 2022 12:38:47 +0200	[thread overview]
Message-ID: <Y0P2N9M7Ud+xLZNv@tucnak> (raw)
In-Reply-To: <3ff03cb463d35ffe96b1271a146f24899b2cb573.1665351785.git.julian@codesourcery.com>

On Sun, Oct 09, 2022 at 02:51:37PM -0700, Julian Brown wrote:
> This patch adds support for non-constant component offsets in "map"
> clauses for OpenMP (and the equivalants for OpenACC), which are not able
> to be sorted into order at compile time.  Normally struct accesses in
> such clauses are gathered together and sorted into increasing address
> order after a "GOMP_MAP_STRUCT" node: if we have variable indices,
> that is no longer possible.
> 
> This patch adds support for such mappings by adding a new variant of
> GOMP_MAP_STRUCT that does not require the list of nodes following to
> be in sorted order.  This passes right down to the runtime: the list is
> sorted in libgomp according to the dynamic values of the offsets after
> the newly-introduced GOMP_MAP_STRUCT_UNORD node.
> 
> This mostly affects arrays of structs indexed by variables in C and C++,
> but can also affect derived-type arrays with constant indexes when they
> have an array descriptor.

I don't understand why this is needed.
We have a restriction that ought to make all such cases invalid:
"If multiple list items are explicitly mapped on the same construct and have the same containing
array or have base pointers that share original storage, and if any of the list items do not have
corresponding list items that are present in the device data environment prior to a task
encountering the construct, then the list items must refer to the same array elements of either the
containing array or the implicit array of the base pointers."

So, all those
#pragma omp target map(t->a[i].p, t->a[j].p) etc. cases are invalid unless
i == j, so IMNSHO one doesn't need to worry about unordered cases.

> +#if defined(_GNU_SOURCE) || defined(__GNUC__)
> +static int
> +compare_addr_r (const void *a, const void *b, void *data)
> +{
> +  void **hostaddrs = (void **) data;
> +  int ai = *(int *) a, bi = *(int *) b;
> +  if (hostaddrs[ai] < hostaddrs[bi])
> +    return -1;
> +  else if (hostaddrs[ai] > hostaddrs[bi])
> +    return 1;
> +  return 0;
> +}
> +#endif

Note, not all glibcs have qsort_r and _GNU_SOURCE macro being defined
doesn't imply glibc.  You'd need something like _GLIBC_PREREQ macro
and require 2.8 or later.

> +
>  static inline __attribute__((always_inline)) struct target_mem_desc *
>  gomp_map_vars_internal (struct gomp_device_descr *devicep,
>  			struct goacc_asyncqueue *aq, size_t mapnum,
> @@ -968,6 +982,17 @@ gomp_map_vars_internal (struct gomp_device_descr *devicep,
>    tgt->device_descr = devicep;
>    tgt->prev = NULL;
>    struct gomp_coalesce_buf cbuf, *cbufp = NULL;
> +  size_t hostaddr_idx;
> +
> +#if !defined(_GNU_SOURCE) && defined(__GNUC__)
> +  /* If we don't have _GNU_SOURCE (thus no qsort_r), but we are compiling with
> +     GCC (and why wouldn't we be?), we can use this nested function for
> +     regular qsort.  */
> +  int compare_addr (const void *a, const void *b)
> +    {
> +      return compare_addr_r (a, b, (void *) &hostaddrs[hostaddr_idx]);
> +    }
> +#endif

Please don't use nested functions in libgomp.

> +	  int *order = NULL;
> +	  if ((kind & typemask) == GOMP_MAP_STRUCT_UNORD)
> +	    {
> +	      order = (int *) gomp_alloca (sizeof (int) * sizes[i]);
> +	      for (int j = 0; j < sizes[i]; j++)
> +		order[j] = j;
> +#ifdef _GNU_SOURCE
> +	      qsort_r (order, sizes[i], sizeof (int), &compare_addr_r,
> +		       &hostaddrs[i + 1]);
> +#elif defined(__GNUC__)
> +	      hostaddr_idx = i + 1;
> +	      qsort (order, sizes[i], sizeof (int), &compare_addr);
> +#else
> +#error no threadsafe qsort
> +#endif

This is too ugly.  If this is really needed (see above) and
you need fallback for missing qsort_t, better sort array of tuples
containing the order number and some data pointer needed for the comparison
routine.

	Jakub


      parent reply	other threads:[~2022-10-10 10:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-09 21:51 [PATCH v4 0/4] Fortran pointers and unordered structs Julian Brown
2022-10-09 21:51 ` [PATCH v4 1/4] OpenMP: Pointers and member mappings Julian Brown
2022-10-09 21:51 ` [PATCH v4 2/4] OpenMP/OpenACC: Reindent TO/FROM/_CACHE_ stanza in {c_}finish_omp_clause Julian Brown
2022-10-09 21:51 ` [PATCH v4 3/4] OpenMP/OpenACC: Rework clause expansion and nested struct handling Julian Brown
2022-10-09 21:51 ` [PATCH v4 4/4] OpenMP/OpenACC: Unordered/non-constant component offset struct mapping Julian Brown
2022-10-10  6:24   ` Julian Brown
2022-10-10  6:24     ` Julian Brown
2022-10-10 10:38   ` Jakub Jelinek [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=Y0P2N9M7Ud+xLZNv@tucnak \
    --to=jakub@redhat.com \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=julian@codesourcery.com \
    --cc=tobias@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).