public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] openmp: Differentiate between order(concurrent) and order(reproducible:concurrent)
@ 2021-10-01  9:03 Jakub Jelinek
  2021-10-01  9:34 ` [Patch] Add libgomp.fortran/order-reproducible-*.f90 [Re: [committed] openmp: Differentiate between order(concurrent) and order(reproducible:concurrent)] Tobias Burnus
  0 siblings, 1 reply; 5+ messages in thread
From: Jakub Jelinek @ 2021-10-01  9:03 UTC (permalink / raw)
  To: gcc-patches; +Cc: Tobias Burnus

Hi!

While OpenMP 5.1 implies order(concurrent) is the same thing as
order(reproducible:concurrent), this is going to change in OpenMP 5.2, where
essentially order(concurrent) means nothing is stated on whether it is
reproducible or unconstrained (and is determined by other means, e.g. for/do
with schedule static or runtime with static being selected is implicitly
reproducible, distribute with dist_schedule static is implicitly reproducible,
loop is implicitly reproducible) and when the modifier is specified explicitly,
it overrides the implicit behavior either way.
And, when order(reproducible:concurrent) is used with e.g. schedule(dynamic)
or some other schedule that is by definition not reproducible, it is
implementation's duty to ensure it is reproducible, either by remembering how
it scheduled some loop and then replaying the same schedule when seeing loops
with the same directive/schedule/number of iterations, or by overriding the
schedule to some reproducible one.

This patch doesn't implement the 5.2 wording just yet, but in the FEs
differentiates between the 3 states - no explicit modifier, explicit reproducible
or explicit unconstrainted, so that the middle-end can easily switch any time.
Instead it follows the 5.1 wording where both order(concurrent) (implicit or
explicit) or order(reproducible:concurrent) imply reproducibility.
And, it implements the easier method, when for/do should be reproducible, it
just chooses static schedule.  order(concurrent) implies no OpenMP APIs in the
loop body nor threadprivate vars, so the exact scheduling isn't (easily at least)
observable.

Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk.

2021-10-01  Jakub Jelinek  <jakub@redhat.com>

gcc/
	* tree.h (OMP_CLAUSE_ORDER_REPRODUCIBLE): Define.
	* tree-pretty-print.c (dump_omp_clause) <case OMP_CLAUSE_ORDER>: Print
	reproducible: for OMP_CLAUSE_ORDER_REPRODUCIBLE.
	* omp-general.c (omp_extract_for_data): If OMP_CLAUSE_ORDER is seen
	without OMP_CLAUSE_ORDER_UNCONSTRAINED, overwrite sched_kind to
	OMP_CLAUSE_SCHEDULE_STATIC.
gcc/c-family/
	* c-omp.c (c_omp_split_clauses): Also copy
	OMP_CLAUSE_ORDER_REPRODUCIBLE.
gcc/c/
	* c-parser.c (c_parser_omp_clause_order): Set
	OMP_CLAUSE_ORDER_REPRODUCIBLE for explicit reproducible: modifier.
gcc/cp/
	* parser.c (cp_parser_omp_clause_order): Set
	OMP_CLAUSE_ORDER_REPRODUCIBLE for explicit reproducible: modifier.
gcc/fortran/
	* gfortran.h (gfc_omp_clauses): Add order_reproducible bitfield.
	* dump-parse-tree.c (show_omp_clauses): Print REPRODUCIBLE: for it.
	* openmp.c (gfc_match_omp_clauses): Set order_reproducible for
	explicit reproducible: modifier.
	* trans-openmp.c (gfc_trans_omp_clauses): Set
	OMP_CLAUSE_ORDER_REPRODUCIBLE for order_reproducible.
	(gfc_split_omp_clauses): Also copy order_reproducible.
gcc/testsuite/
	* gfortran.dg/gomp/order-5.f90: Adjust scan-tree-dump-times regexps.
libgomp/
	* testsuite/libgomp.c-c++-common/order-reproducible-1.c: New test.
	* testsuite/libgomp.c-c++-common/order-reproducible-2.c: New test.

--- gcc/tree.h.jj	2021-09-22 09:29:01.049814034 +0200
+++ gcc/tree.h	2021-09-30 16:17:53.051099703 +0200
@@ -1718,6 +1718,9 @@ class auto_suppress_location_wrappers
 /* True for unconstrained modifier on order(concurrent) clause.  */
 #define OMP_CLAUSE_ORDER_UNCONSTRAINED(NODE) \
   (OMP_CLAUSE_SUBCODE_CHECK (NODE, OMP_CLAUSE_ORDER)->base.public_flag)
+/* True for reproducible modifier on order(concurrent) clause.  */
+#define OMP_CLAUSE_ORDER_REPRODUCIBLE(NODE) \
+  TREE_PROTECTED (OMP_CLAUSE_SUBCODE_CHECK (NODE, OMP_CLAUSE_ORDER))
 
 #define OMP_CLAUSE_REDUCTION_CODE(NODE)	\
   (OMP_CLAUSE_RANGE_CHECK (NODE, OMP_CLAUSE_REDUCTION, \
--- gcc/tree-pretty-print.c.jj	2021-09-22 09:29:01.051814006 +0200
+++ gcc/tree-pretty-print.c	2021-09-30 16:18:42.713406739 +0200
@@ -1165,6 +1165,8 @@ dump_omp_clause (pretty_printer *pp, tre
       pp_string (pp, "order(");
       if (OMP_CLAUSE_ORDER_UNCONSTRAINED (clause))
 	pp_string (pp, "unconstrained:");
+      else if (OMP_CLAUSE_ORDER_REPRODUCIBLE (clause))
+	pp_string (pp, "reproducible:");
       pp_string (pp, "concurrent)");
       break;
 
--- gcc/omp-general.c.jj	2021-09-01 11:37:41.966556334 +0200
+++ gcc/omp-general.c	2021-09-30 16:36:56.599142089 +0200
@@ -193,6 +193,7 @@ omp_extract_for_data (gomp_for *for_stmt
 		    == GF_OMP_FOR_KIND_DISTRIBUTE;
   bool taskloop = gimple_omp_for_kind (for_stmt)
 		  == GF_OMP_FOR_KIND_TASKLOOP;
+  bool order_reproducible = false;
   tree iterv, countv;
 
   fd->for_stmt = for_stmt;
@@ -277,10 +278,25 @@ omp_extract_for_data (gomp_for *for_stmt
 	    && !OMP_CLAUSE__SCANTEMP__CONTROL (t))
 	  fd->have_nonctrl_scantemp = true;
 	break;
+      case OMP_CLAUSE_ORDER:
+	/* FIXME: For OpenMP 5.2 this should change to
+	   if (OMP_CLAUSE_ORDER_REPRODUCIBLE (t))
+	   (with the exception of loop construct but that lowers to
+	   no schedule/dist_schedule clauses currently).  */
+	if (!OMP_CLAUSE_ORDER_UNCONSTRAINED (t))
+	  order_reproducible = true;
       default:
 	break;
       }
 
+  /* For order(reproducible:concurrent) schedule ({dynamic,guided,runtime})
+     we have either the option to expensively remember at runtime how we've
+     distributed work from first loop and reuse that in following loops with
+     the same number of iterations and schedule, or just force static schedule.
+     OpenMP API calls etc. aren't allowed in order(concurrent) bodies so
+     users can't observe it easily anyway.  */
+  if (order_reproducible)
+    fd->sched_kind = OMP_CLAUSE_SCHEDULE_STATIC;
   if (fd->collapse > 1 || fd->tiling)
     fd->loops = loops;
   else
--- gcc/c-family/c-omp.c.jj	2021-09-22 09:29:01.053813977 +0200
+++ gcc/c-family/c-omp.c	2021-09-30 16:21:16.671258496 +0200
@@ -2128,6 +2128,8 @@ c_omp_split_clauses (location_t loc, enu
 				    OMP_CLAUSE_ORDER);
 	      OMP_CLAUSE_ORDER_UNCONSTRAINED (c)
 		= OMP_CLAUSE_ORDER_UNCONSTRAINED (clauses);
+	      OMP_CLAUSE_ORDER_REPRODUCIBLE (c)
+		= OMP_CLAUSE_ORDER_REPRODUCIBLE (clauses);
 	      OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
 	      cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE] = c;
 	    }
@@ -2139,6 +2141,8 @@ c_omp_split_clauses (location_t loc, enu
 					OMP_CLAUSE_ORDER);
 		  OMP_CLAUSE_ORDER_UNCONSTRAINED (c)
 		    = OMP_CLAUSE_ORDER_UNCONSTRAINED (clauses);
+		  OMP_CLAUSE_ORDER_REPRODUCIBLE (c)
+		    = OMP_CLAUSE_ORDER_REPRODUCIBLE (clauses);
 		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
 		  cclauses[C_OMP_CLAUSE_SPLIT_FOR] = c;
 		  s = C_OMP_CLAUSE_SPLIT_SIMD;
--- gcc/c/c-parser.c.jj	2021-09-29 10:07:41.757881850 +0200
+++ gcc/c/c-parser.c	2021-09-30 16:20:18.429071176 +0200
@@ -14626,6 +14626,7 @@ c_parser_omp_clause_order (c_parser *par
   tree c;
   const char *p;
   bool unconstrained = false;
+  bool reproducible = false;
 
   matching_parens parens;
   if (!parens.require_open (parser))
@@ -14636,7 +14637,9 @@ c_parser_omp_clause_order (c_parser *par
       p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
       if (strcmp (p, "unconstrained") == 0)
 	unconstrained = true;
-      else if (strcmp (p, "reproducible") != 0)
+      else if (strcmp (p, "reproducible") == 0)
+	reproducible = true;
+      else
 	{
 	  c_parser_error (parser, "expected %<reproducible%> or "
 				  "%<unconstrained%>");
@@ -14661,6 +14664,7 @@ c_parser_omp_clause_order (c_parser *par
   check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order");
   c = build_omp_clause (loc, OMP_CLAUSE_ORDER);
   OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
+  OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
   OMP_CLAUSE_CHAIN (c) = list;
   return c;
 
--- gcc/cp/parser.c.jj	2021-09-27 10:47:15.723086479 +0200
+++ gcc/cp/parser.c	2021-09-30 16:22:23.635324116 +0200
@@ -37734,6 +37734,7 @@ cp_parser_omp_clause_order (cp_parser *p
   tree c, id;
   const char *p;
   bool unconstrained = false;
+  bool reproducible = false;
 
   matching_parens parens;
   if (!parens.require_open (parser))
@@ -37746,7 +37747,9 @@ cp_parser_omp_clause_order (cp_parser *p
       p = IDENTIFIER_POINTER (id);
       if (strcmp (p, "unconstrained") == 0)
 	unconstrained = true;
-      else if (strcmp (p, "reproducible") != 0)
+      else if (strcmp (p, "reproducible") == 0)
+	reproducible = true;
+      else
 	{
 	  cp_parser_error (parser, "expected %<reproducible%> or "
 				   "%<unconstrained%>");
@@ -37777,6 +37780,7 @@ cp_parser_omp_clause_order (cp_parser *p
   check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
   c = build_omp_clause (location, OMP_CLAUSE_ORDER);
   OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
+  OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
   OMP_CLAUSE_CHAIN (c) = list;
   return c;
 
--- gcc/fortran/gfortran.h.jj	2021-09-23 10:07:15.887183299 +0200
+++ gcc/fortran/gfortran.h	2021-09-30 16:23:22.676500285 +0200
@@ -1491,8 +1491,8 @@ typedef struct gfc_omp_clauses
   unsigned inbranch:1, notinbranch:1, nogroup:1;
   unsigned sched_simd:1, sched_monotonic:1, sched_nonmonotonic:1;
   unsigned simd:1, threads:1, depend_source:1, destroy:1, order_concurrent:1;
-  unsigned order_unconstrained:1, capture:1, grainsize_strict:1;
-  unsigned num_tasks_strict:1;
+  unsigned order_unconstrained:1, order_reproducible:1, capture:1;
+  unsigned grainsize_strict:1, num_tasks_strict:1;
   ENUM_BITFIELD (gfc_omp_sched_kind) sched_kind:3;
   ENUM_BITFIELD (gfc_omp_device_type) device_type:2;
   ENUM_BITFIELD (gfc_omp_memorder) memorder:3;
--- gcc/fortran/dump-parse-tree.c.jj	2021-09-20 12:20:53.025759807 +0200
+++ gcc/fortran/dump-parse-tree.c	2021-09-30 16:24:04.818912255 +0200
@@ -1634,6 +1634,8 @@ show_omp_clauses (gfc_omp_clauses *omp_c
       fputs (" ORDER(", dumpfile);
       if (omp_clauses->order_unconstrained)
 	fputs ("UNCONSTRAINED:", dumpfile);
+      else if (omp_clauses->order_reproducible)
+	fputs ("REPRODUCIBLE:", dumpfile);
       fputs ("CONCURRENT)", dumpfile);
     }
   if (omp_clauses->ordered)
--- gcc/fortran/openmp.c.jj	2021-09-20 12:20:53.026759792 +0200
+++ gcc/fortran/openmp.c	2021-09-30 16:24:48.992295885 +0200
@@ -2374,8 +2374,9 @@ gfc_match_omp_clauses (gfc_omp_clauses *
 	    {
 	      if (m == MATCH_ERROR)
 		goto error;
-	      if (gfc_match (" reproducible : concurrent )") == MATCH_YES
-		  || gfc_match (" concurrent )") == MATCH_YES)
+	      if (gfc_match (" reproducible : concurrent )") == MATCH_YES)
+		c->order_reproducible = true;
+	      else if (gfc_match (" concurrent )") == MATCH_YES)
 		;
 	      else if (gfc_match (" unconstrained : concurrent )") == MATCH_YES)
 		c->order_unconstrained = true;
--- gcc/fortran/trans-openmp.c.jj	2021-09-20 12:20:53.027759778 +0200
+++ gcc/fortran/trans-openmp.c	2021-09-30 16:26:03.523255918 +0200
@@ -3804,6 +3804,7 @@ gfc_trans_omp_clauses (stmtblock_t *bloc
     {
       c = build_omp_clause (gfc_get_location (&where), OMP_CLAUSE_ORDER);
       OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = clauses->order_unconstrained;
+      OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = clauses->order_reproducible;
       omp_clauses = gfc_trans_add_clause (c, omp_clauses);
     }
 
@@ -5895,6 +5896,8 @@ gfc_split_omp_clauses (gfc_code *code,
 	    = code->ext.omp_clauses->order_concurrent;
 	  clausesa[GFC_OMP_SPLIT_DISTRIBUTE].order_unconstrained
 	    = code->ext.omp_clauses->order_unconstrained;
+	  clausesa[GFC_OMP_SPLIT_DISTRIBUTE].order_reproducible
+	    = code->ext.omp_clauses->order_reproducible;
 	}
       if (mask & GFC_OMP_MASK_PARALLEL)
 	{
@@ -5951,6 +5954,8 @@ gfc_split_omp_clauses (gfc_code *code,
 	    = code->ext.omp_clauses->order_concurrent;
 	  clausesa[GFC_OMP_SPLIT_DO].order_unconstrained
 	    = code->ext.omp_clauses->order_unconstrained;
+	  clausesa[GFC_OMP_SPLIT_DO].order_reproducible
+	    = code->ext.omp_clauses->order_reproducible;
 	}
       if (mask & GFC_OMP_MASK_SIMD)
 	{
@@ -5969,6 +5974,8 @@ gfc_split_omp_clauses (gfc_code *code,
 	    = code->ext.omp_clauses->order_concurrent;
 	  clausesa[GFC_OMP_SPLIT_SIMD].order_unconstrained
 	    = code->ext.omp_clauses->order_unconstrained;
+	  clausesa[GFC_OMP_SPLIT_SIMD].order_reproducible
+	    = code->ext.omp_clauses->order_reproducible;
 	  /* And this is copied to all.  */
 	  clausesa[GFC_OMP_SPLIT_SIMD].if_expr
 	    = code->ext.omp_clauses->if_expr;
--- gcc/testsuite/gfortran.dg/gomp/order-5.f90.jj	2021-09-21 23:31:01.070248170 +0200
+++ gcc/testsuite/gfortran.dg/gomp/order-5.f90	2021-10-01 10:20:44.592567063 +0200
@@ -116,14 +116,14 @@ subroutine f4 (a)
   end do
 end
 
-! { dg-final { scan-tree-dump-times "#pragma omp distribute order\\(concurrent\\)" 6 "original"} }
+! { dg-final { scan-tree-dump-times "#pragma omp distribute order\\(reproducible:concurrent\\)" 6 "original"} }
 ! { dg-final { scan-tree-dump-times "#pragma omp distribute order\\(unconstrained:concurrent\\)" 6 "original"} }
-! { dg-final { scan-tree-dump-times "#pragma omp for nowait order\\(concurrent\\)" 6 "original"} }
+! { dg-final { scan-tree-dump-times "#pragma omp for nowait order\\(reproducible:concurrent\\)" 6 "original"} }
 ! { dg-final { scan-tree-dump-times "#pragma omp for nowait order\\(unconstrained:concurrent\\)" 6 "original"} }
-! { dg-final { scan-tree-dump-times "#pragma omp for order\\(concurrent\\)" 2 "original"} }
+! { dg-final { scan-tree-dump-times "#pragma omp for order\\(reproducible:concurrent\\)" 2 "original"} }
 ! { dg-final { scan-tree-dump-times "#pragma omp for order\\(unconstrained:concurrent\\)" 2 "original"} }
 ! { dg-final { scan-tree-dump-times "#pragma omp parallel" 12 "original"} }
-! { dg-final { scan-tree-dump-times "#pragma omp simd linear\\(i:1\\) order\\(concurrent\\)" 6 "original"} }
+! { dg-final { scan-tree-dump-times "#pragma omp simd linear\\(i:1\\) order\\(reproducible:concurrent\\)" 6 "original"} }
 ! { dg-final { scan-tree-dump-times "#pragma omp simd linear\\(i:1\\) order\\(unconstrained:concurrent\\)" 6 "original"} }
 ! { dg-final { scan-tree-dump-times "#pragma omp taskloop" 2 "original"} }
 ! { dg-final { scan-tree-dump-times "#pragma omp teams" 8 "original"} }
--- libgomp/testsuite/libgomp.c-c++-common/order-reproducible-1.c.jj	2021-09-30 16:43:25.998707836 +0200
+++ libgomp/testsuite/libgomp.c-c++-common/order-reproducible-1.c	2021-09-30 16:52:22.418220987 +0200
@@ -0,0 +1,63 @@
+#include <unistd.h>
+#include <stdlib.h>
+
+int
+main ()
+{
+  int a[128];
+  #pragma omp teams num_teams(5)
+  {
+    #pragma omp loop bind(teams)
+    for (int i = 0; i < 128; i++)
+      {
+	a[i] = i;
+	if (i == 0)
+	  usleep (20);
+	else if (i == 17)
+	  usleep (40);
+      }
+    #pragma omp loop bind(teams)
+    for (int i = 0; i < 128; i++)
+      a[i] += i;
+  }
+  for (int i = 0; i < 128; i++)
+    if (a[i] != 2 * i)
+      abort ();
+  #pragma omp teams num_teams(5)
+  {
+    #pragma omp loop bind(teams) order(concurrent)
+    for (int i = 0; i < 128; i++)
+      {
+	a[i] *= 2;
+	if (i == 1)
+	  usleep (20);
+	else if (i == 13)
+	  usleep (40);
+      }
+    #pragma omp loop bind(teams) order(concurrent)
+    for (int i = 0; i < 128; i++)
+      a[i] += i;
+  }
+  for (int i = 0; i < 128; i++)
+    if (a[i] != 5 * i)
+      abort ();
+  #pragma omp teams num_teams(5)
+  {
+    #pragma omp loop bind(teams) order(reproducible:concurrent)
+    for (int i = 0; i < 128; i++)
+      {
+	a[i] *= 2;
+	if (i == 2)
+	  usleep (20);
+	else if (i == 105)
+	  usleep (40);
+      }
+    #pragma omp loop bind(teams) order(reproducible:concurrent)
+    for (int i = 0; i < 128; i++)
+      a[i] += i;
+  }
+  for (int i = 0; i < 128; i++)
+    if (a[i] != 11 * i)
+      abort ();
+  return 0;
+}
--- libgomp/testsuite/libgomp.c-c++-common/order-reproducible-2.c.jj	2021-09-30 16:43:32.045623448 +0200
+++ libgomp/testsuite/libgomp.c-c++-common/order-reproducible-2.c	2021-09-30 16:50:54.612446521 +0200
@@ -0,0 +1,28 @@
+#include <unistd.h>
+#include <stdlib.h>
+
+int
+main ()
+{
+  int a[128];
+  #pragma omp parallel num_threads(8)
+  {
+    #pragma omp barrier
+    #pragma omp for nowait schedule (dynamic, 2) order(reproducible:concurrent)
+    for (int i = 0; i < 128; i++)
+      {
+	a[i] = i;
+	if (i == 0)
+	  usleep (20);
+	else if (i == 17)
+	  usleep (40);
+      }
+    #pragma omp for nowait schedule (dynamic, 2) order(reproducible:concurrent)
+    for (int i = 0; i < 128; i++)
+      a[i] += i;
+  }
+  for (int i = 0; i < 128; i++)
+    if (a[i] != 2 * i)
+      abort ();
+  return 0;
+}
	Jakub


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-10-02  8:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-01  9:03 [committed] openmp: Differentiate between order(concurrent) and order(reproducible:concurrent) Jakub Jelinek
2021-10-01  9:34 ` [Patch] Add libgomp.fortran/order-reproducible-*.f90 [Re: [committed] openmp: Differentiate between order(concurrent) and order(reproducible:concurrent)] Tobias Burnus
2021-10-01  9:45   ` Jakub Jelinek
2021-10-02  7:52     ` Tobias Burnus
2021-10-02  8:16       ` Jakub Jelinek

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).