public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Use DO_PRAGMA in libgomp.oacc-c-c++-common/reduction-1.c
@ 2015-02-23 17:14 Tom de Vries
  2015-02-23 17:17 ` Jakub Jelinek
  0 siblings, 1 reply; 5+ messages in thread
From: Tom de Vries @ 2015-02-23 17:14 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: GCC Patches

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

Hi,

this patch is a rewrite of libgomp.oacc-c-c++-common/reduction-1.c (included as 
a whole in the patch prefix).

The patch attempts to get rid of the errorprone copy-paste style. An example of 
such an error in this test-case is:
...
   result = 5;
   vresult = 5;

   lresult = false;
   lvresult = false;

   /* '||' reductions.  */
#pragma acc parallel vector_length (vl)
#pragma acc loop reduction (||:lresult)
   for (i = 0; i < n; i++)
     lresult = lresult || (result > array[i]);

   /* Verify the reduction.  */
   for (i = 0; i < n; i++)
     lvresult = lresult || (result > array[i]);

   if (lresult != lvresult)
     abort ();
...
In the verification loop lvresult should have been used instead of lresult (and 
probably the intention was to use vresult instead of result there. Now vresult 
is assigned to but not used).

The patch factors out the common parts and uses parameters for the non-common 
parts. Since the common parts involve the acc pragma, I'm using DO_PRAGMA ( 
https://gcc.gnu.org/onlinedocs/cpp/Pragmas.html ). [ Unfortunately, I haven't 
found something similar to use for fortran. ]

Furthermore, I've updated the initial values in order to prevent cases where the 
initial value of the reduction variable is the same as the final one (f.i. we 
were doing a multiply reduction with initial value 0).

The only thing I'm not sure about is the two-level pragma expansion using the 
apply pragmas. It maximizes factoring out common parts, but it makes things less 
readable.

Tested on x86_64.

OK for stage4?

Thanks,
- Tom

[-- Attachment #2: 0001-Use-DO_PRAGMA-in-libgomp.oacc-c-c-common-reduction-1.patch --]
[-- Type: text/x-patch, Size: 8343 bytes --]

/* { dg-do run } */

/* Integer reductions.  */

#include <stdlib.h>
#include <stdbool.h>

#define vl 32

#define DO_PRAGMA(x) _Pragma (#x)

#define check_reduction(pragmaop,op,res,vres,init,apply)	\
  res = (init);							\
DO_PRAGMA (acc parallel vector_length (vl))\
DO_PRAGMA (acc loop reduction (pragmaop:res))\
  for (i = 0; i < n; i++)					\
    res = apply (res, op, array[i]);				\
								\
  vres = (init);						\
  for (i = 0; i < n; i++)					\
    vres = apply (vres, op, array[i]);				\
								\
  if (res != vres)						\
    abort ();

#define apply_bin_int_op(a, op, b) ((a) op (b))
#define check_reduction_int(op, init)					\
  check_reduction (op, op, result, vresult, init, apply_bin_int_op)

static void
test_reductions_int (void)
{
  const int n = 1000;
  int i;
  int vresult, result, array[n];

  for (i = 0; i < n; i++)
    array[i] = i;

  check_reduction_int (+, 0);
  check_reduction_int (*, 1);
  check_reduction_int (&, -1);
  check_reduction_int (|, 0);
  check_reduction_int (^, 0);
}

#define apply_max_op(a, op, b) (((a) > (b)) ? (a) : (b))
#define apply_min_op(a, op, b) (((a) < (b)) ? (a) : (b))
#define check_reduction_minmax(pragmaop,apply,init)		\
  check_reduction (pragmaop, , result, vresult, init, apply)

static void
test_reductions_minmax (void)
{
  const int n = 1000;
  int i;
  int vresult, result, array[n];

  for (i = 0; i < n; i++)
    array[i] = i;

  check_reduction_minmax (min, apply_min_op, n + 1);
  check_reduction_minmax (max, apply_max_op, -1);
}

#define apply_bin_bool_op(a, op, b) (a op (cmp_val > b))
#define check_reduction_bool(op, init)					\
  check_reduction (op, op, lresult, lvresult, init, apply_bin_bool_op)

static void
test_reductions_bool (void)
{
  const int n = 1000;
  int i;
  int array[n];
  bool lvresult, lresult;
  int cmp_val;

  for (i = 0; i < n; i++)
    array[i] = i;

  cmp_val = 5;
  check_reduction_bool (&&, true);
  check_reduction_bool (||, false);
}

int
main (void)
{
  test_reductions_int ();
  test_reductions_minmax ();
  test_reductions_bool ();
  return 0;
}

2015-02-23  Tom de Vries  <tom@codesourcery.com>

	* testsuite/libgomp.oacc-c-c++-common/reduction-1.c
	(DO_PRAGMA, check_reduction, apply_bin_int_op, check_reduction_int)
	(apply_max_op, apply_min_op, check_reduction_minmax, apply_bin_bool_op)
	(check_reduction_bool): Declare.
	(test_reductions_int, test_reductions_minmax, test_reductions_bool): New
	function.
	(main): Use new functions.
---
 .../libgomp.oacc-c-c++-common/reduction-1.c        | 208 +++++++--------------
 1 file changed, 64 insertions(+), 144 deletions(-)

diff --git a/libgomp/testsuite/libgomp.oacc-c-c++-common/reduction-1.c b/libgomp/testsuite/libgomp.oacc-c-c++-common/reduction-1.c
index acf9540..87afaf4 100644
--- a/libgomp/testsuite/libgomp.oacc-c-c++-common/reduction-1.c
+++ b/libgomp/testsuite/libgomp.oacc-c-c++-common/reduction-1.c
@@ -7,168 +7,88 @@
 
 #define vl 32
 
-int
-main(void)
+#define DO_PRAGMA(x) _Pragma (#x)
+
+#define check_reduction(pragmaop,op,res,vres,init,apply)	\
+  res = (init);							\
+DO_PRAGMA (acc parallel vector_length (vl))\
+DO_PRAGMA (acc loop reduction (pragmaop:res))\
+  for (i = 0; i < n; i++)					\
+    res = apply (res, op, array[i]);				\
+								\
+  vres = (init);						\
+  for (i = 0; i < n; i++)					\
+    vres = apply (vres, op, array[i]);				\
+								\
+  if (res != vres)						\
+    abort ();
+
+#define apply_bin_int_op(a, op, b) ((a) op (b))
+#define check_reduction_int(op, init)					\
+  check_reduction (op, op, result, vresult, init, apply_bin_int_op)
+
+static void
+test_reductions_int (void)
 {
   const int n = 1000;
   int i;
   int vresult, result, array[n];
-  bool lvresult, lresult;
 
   for (i = 0; i < n; i++)
     array[i] = i;
 
-  result = 0;
-  vresult = 0;
-
-  /* '+' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (+:result)
-  for (i = 0; i < n; i++)
-    result += array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult += array[i];
-
-  if (result != vresult)
-    abort ();
-
-  result = 0;
-  vresult = 0;
-
-  /* '*' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (*:result)
-  for (i = 0; i < n; i++)
-    result *= array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult *= array[i];
-
-  if (result != vresult)
-    abort ();
-
-//   result = 0;
-//   vresult = 0;
-// 
-//   /* 'max' reductions.  */
-// #pragma acc parallel vector_length (vl)
-// #pragma acc loop reduction (+:result)
-//   for (i = 0; i < n; i++)
-//       result = result > array[i] ? result : array[i];
-// 
-//   /* Verify the reduction.  */
-//   for (i = 0; i < n; i++)
-//       vresult = vresult > array[i] ? vresult : array[i];
-// 
-//   printf("%d != %d\n", result, vresult);
-//   if (result != vresult)
-//     abort ();
-// 
-//   result = 0;
-//   vresult = 0;
-// 
-//   /* 'min' reductions.  */
-// #pragma acc parallel vector_length (vl)
-// #pragma acc loop reduction (+:result)
-//   for (i = 0; i < n; i++)
-//       result = result < array[i] ? result : array[i];
-// 
-//   /* Verify the reduction.  */
-//   for (i = 0; i < n; i++)
-//       vresult = vresult < array[i] ? vresult : array[i];
-// 
-//   printf("%d != %d\n", result, vresult);
-//   if (result != vresult)
-//     abort ();
-
-  result = 0;
-  vresult = 0;
-
-  /* '&' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (&:result)
-  for (i = 0; i < n; i++)
-    result &= array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult &= array[i];
-
-  if (result != vresult)
-    abort ();
-
-  result = 0;
-  vresult = 0;
-
-  /* '|' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (|:result)
-  for (i = 0; i < n; i++)
-    result |= array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult |= array[i];
-
-  if (result != vresult)
-    abort ();
-
-  result = 0;
-  vresult = 0;
-
-  /* '^' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (^:result)
-  for (i = 0; i < n; i++)
-    result ^= array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult ^= array[i];
-
-  if (result != vresult)
-    abort ();
-
-  result = 5;
-  vresult = 5;
+  check_reduction_int (+, 0);
+  check_reduction_int (*, 1);
+  check_reduction_int (&, -1);
+  check_reduction_int (|, 0);
+  check_reduction_int (^, 0);
+}
 
-  lresult = false;
-  lvresult = false;
+#define apply_max_op(a, op, b) (((a) > (b)) ? (a) : (b))
+#define apply_min_op(a, op, b) (((a) < (b)) ? (a) : (b))
+#define check_reduction_minmax(pragmaop,apply,init)		\
+  check_reduction (pragmaop, , result, vresult, init, apply)
 
-  /* '&&' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (&&:lresult)
-  for (i = 0; i < n; i++)
-    lresult = lresult && (result > array[i]);
+static void
+test_reductions_minmax (void)
+{
+  const int n = 1000;
+  int i;
+  int vresult, result, array[n];
 
-  /* Verify the reduction.  */
   for (i = 0; i < n; i++)
-    lvresult = lresult && (result > array[i]);
-
-  if (lresult != lvresult)
-    abort ();
+    array[i] = i;
 
-  result = 5;
-  vresult = 5;
+  check_reduction_minmax (min, apply_min_op, n + 1);
+  check_reduction_minmax (max, apply_max_op, -1);
+}
 
-  lresult = false;
-  lvresult = false;
+#define apply_bin_bool_op(a, op, b) (a op (cmp_val > b))
+#define check_reduction_bool(op, init)					\
+  check_reduction (op, op, lresult, lvresult, init, apply_bin_bool_op)
 
-  /* '||' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (||:lresult)
-  for (i = 0; i < n; i++)
-    lresult = lresult || (result > array[i]);
+static void
+test_reductions_bool (void)
+{
+  const int n = 1000;
+  int i;
+  int array[n];
+  bool lvresult, lresult;
+  int cmp_val;
 
-  /* Verify the reduction.  */
   for (i = 0; i < n; i++)
-    lvresult = lresult || (result > array[i]);
+    array[i] = i;
 
-  if (lresult != lvresult)
-    abort ();
+  cmp_val = 5;
+  check_reduction_bool (&&, true);
+  check_reduction_bool (||, false);
+}
 
+int
+main (void)
+{
+  test_reductions_int ();
+  test_reductions_minmax ();
+  test_reductions_bool ();
   return 0;
 }
-- 
1.9.1


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

* Re: [PATCH] Use DO_PRAGMA in libgomp.oacc-c-c++-common/reduction-1.c
  2015-02-23 17:14 [PATCH] Use DO_PRAGMA in libgomp.oacc-c-c++-common/reduction-1.c Tom de Vries
@ 2015-02-23 17:17 ` Jakub Jelinek
  2015-02-23 17:40   ` Tom de Vries
  0 siblings, 1 reply; 5+ messages in thread
From: Jakub Jelinek @ 2015-02-23 17:17 UTC (permalink / raw)
  To: Tom de Vries; +Cc: GCC Patches

On Mon, Feb 23, 2015 at 04:52:56PM +0100, Tom de Vries wrote:
> The only thing I'm not sure about is the two-level pragma expansion using
> the apply pragmas. It maximizes factoring out common parts, but it makes
> things less readable.
> 
> Tested on x86_64.
> 
> OK for stage4?

If Thomas is ok with that, it is ok with me too.

	Jakub

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

* Re: [PATCH] Use DO_PRAGMA in libgomp.oacc-c-c++-common/reduction-1.c
  2015-02-23 17:17 ` Jakub Jelinek
@ 2015-02-23 17:40   ` Tom de Vries
  2015-02-25 11:59     ` Thomas Schwinge
  0 siblings, 1 reply; 5+ messages in thread
From: Tom de Vries @ 2015-02-23 17:40 UTC (permalink / raw)
  To: Thomas Schwinge; +Cc: Jakub Jelinek, GCC Patches

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

On 23-02-15 17:08, Jakub Jelinek wrote:
> On Mon, Feb 23, 2015 at 04:52:56PM +0100, Tom de Vries wrote:
>> The only thing I'm not sure about is the two-level pragma expansion using
>> the apply pragmas. It maximizes factoring out common parts, but it makes
>> things less readable.
>>
>> Tested on x86_64.
>>
>> OK for stage4?
>
> If Thomas is ok with that, it is ok with me too.

For comparison, this is a less convoluted, but longer version.

Thanks,
- Tom

[-- Attachment #2: 0001-Use-DO_PRAGMA-in-libgomp.oacc-c-c-common-reduction-1-v2.patch --]
[-- Type: text/x-patch, Size: 9116 bytes --]

/* { dg-do run } */

/* Integer reductions.  */

#include <stdlib.h>
#include <stdbool.h>

#define vl 32

#define DO_PRAGMA(x) _Pragma (#x)

#define check_reduction_int(op, init, res, vres)	\
  res = (init);						\
DO_PRAGMA (acc parallel vector_length (vl))\
DO_PRAGMA (acc loop reduction (op:res))\
  for (i = 0; i < n; i++)				\
    res = res op array[i];				\
							\
  vres = (init);					\
  for (i = 0; i < n; i++)				\
    vres = vres op array[i];				\
							\
  if (res != vres)					\
    abort ();

static void
test_reductions_int (void)
{
  const int n = 1000;
  int i;
  int vresult, result, array[n];

  for (i = 0; i < n; i++)
    array[i] = i;

  check_reduction_int (+, 0, result, vresult);
  check_reduction_int (*, 1, result, vresult);
  check_reduction_int (&, -1, result, vresult);
  check_reduction_int (|, 0, result, vresult);
  check_reduction_int (^, 0, result, vresult);
}

#define check_reduction_minmax(pragmaop, op, init, res, vres)	\
  res = (init);							\
DO_PRAGMA (acc parallel vector_length (vl))\
DO_PRAGMA (acc loop reduction (pragmaop:res))\
  for (i = 0; i < n; i++)					\
    res = op (res, array[i]);					\
								\
  vres = (init);						\
  for (i = 0; i < n; i++)					\
    vres = op (vres, array[i]);					\
								\
  if (res != vres)						\
    abort ();

#define max_op(a, b) (((a) > (b)) ? (a) : (b))
#define min_op(a, b) (((a) < (b)) ? (a) : (b))

static void
test_reductions_minmax (void)
{
  const int n = 1000;
  int i;
  int vresult, result, array[n];

  for (i = 0; i < n; i++)
    array[i] = i;

  check_reduction_minmax (min, min_op, n + 1, vresult, result);
  check_reduction_minmax (max, max_op, -1, vresult, result);
}

#define check_reduction_bool(op, init, res, vres)	\
  res = (init);						\
DO_PRAGMA (acc parallel vector_length (vl))\
DO_PRAGMA (acc loop reduction (op:res))\
  for (i = 0; i < n; i++)				\
    res = res op (cmp_val > array[i]);			\
							\
  vres = (init);					\
  for (i = 0; i < n; i++)				\
    vres = vres op (cmp_val > array[i]);		\
							\
  if (res != vres)					\
    abort ();

static void
test_reductions_bool (void)
{
  const int n = 1000;
  int i;
  int array[n];
  bool lvresult, lresult;
  int cmp_val;

  for (i = 0; i < n; i++)
    array[i] = i;

  cmp_val = 5;
  check_reduction_bool (&&, true, lresult, lvresult);
  check_reduction_bool (||, false, lresult, lvresult);
}

int
main (void)
{
  test_reductions_int ();
  test_reductions_minmax ();
  test_reductions_bool ();
  return 0;
}

2015-02-23  Tom de Vries  <tom@codesourcery.com>

	* testsuite/libgomp.oacc-c-c++-common/reduction-1.c
	(DO_PRAGMA, check_reduction_int, max_op, min_op, check_reduction_minmax)
	(check_reduction_bool): Declare.
	(test_reductions_int, test_reductions_minmax, test_reductions_bool): New
	function.
	(main): Use new functions.
---
 .../libgomp.oacc-c-c++-common/reduction-1.c        | 220 ++++++++-------------
 1 file changed, 79 insertions(+), 141 deletions(-)

diff --git a/libgomp/testsuite/libgomp.oacc-c-c++-common/reduction-1.c b/libgomp/testsuite/libgomp.oacc-c-c++-common/reduction-1.c
index acf9540..c30bb65 100644
--- a/libgomp/testsuite/libgomp.oacc-c-c++-common/reduction-1.c
+++ b/libgomp/testsuite/libgomp.oacc-c-c++-common/reduction-1.c
@@ -7,168 +7,106 @@
 
 #define vl 32
 
-int
-main(void)
+#define DO_PRAGMA(x) _Pragma (#x)
+
+#define check_reduction_int(op, init, res, vres)	\
+  res = (init);						\
+DO_PRAGMA (acc parallel vector_length (vl))\
+DO_PRAGMA (acc loop reduction (op:res))\
+  for (i = 0; i < n; i++)				\
+    res = res op array[i];				\
+							\
+  vres = (init);					\
+  for (i = 0; i < n; i++)				\
+    vres = vres op array[i];				\
+							\
+  if (res != vres)					\
+    abort ();
+
+static void
+test_reductions_int (void)
 {
   const int n = 1000;
   int i;
   int vresult, result, array[n];
-  bool lvresult, lresult;
 
   for (i = 0; i < n; i++)
     array[i] = i;
 
-  result = 0;
-  vresult = 0;
-
-  /* '+' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (+:result)
-  for (i = 0; i < n; i++)
-    result += array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult += array[i];
-
-  if (result != vresult)
-    abort ();
-
-  result = 0;
-  vresult = 0;
-
-  /* '*' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (*:result)
-  for (i = 0; i < n; i++)
-    result *= array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult *= array[i];
-
-  if (result != vresult)
-    abort ();
-
-//   result = 0;
-//   vresult = 0;
-// 
-//   /* 'max' reductions.  */
-// #pragma acc parallel vector_length (vl)
-// #pragma acc loop reduction (+:result)
-//   for (i = 0; i < n; i++)
-//       result = result > array[i] ? result : array[i];
-// 
-//   /* Verify the reduction.  */
-//   for (i = 0; i < n; i++)
-//       vresult = vresult > array[i] ? vresult : array[i];
-// 
-//   printf("%d != %d\n", result, vresult);
-//   if (result != vresult)
-//     abort ();
-// 
-//   result = 0;
-//   vresult = 0;
-// 
-//   /* 'min' reductions.  */
-// #pragma acc parallel vector_length (vl)
-// #pragma acc loop reduction (+:result)
-//   for (i = 0; i < n; i++)
-//       result = result < array[i] ? result : array[i];
-// 
-//   /* Verify the reduction.  */
-//   for (i = 0; i < n; i++)
-//       vresult = vresult < array[i] ? vresult : array[i];
-// 
-//   printf("%d != %d\n", result, vresult);
-//   if (result != vresult)
-//     abort ();
-
-  result = 0;
-  vresult = 0;
-
-  /* '&' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (&:result)
-  for (i = 0; i < n; i++)
-    result &= array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult &= array[i];
-
-  if (result != vresult)
-    abort ();
-
-  result = 0;
-  vresult = 0;
-
-  /* '|' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (|:result)
-  for (i = 0; i < n; i++)
-    result |= array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult |= array[i];
-
-  if (result != vresult)
-    abort ();
-
-  result = 0;
-  vresult = 0;
-
-  /* '^' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (^:result)
-  for (i = 0; i < n; i++)
-    result ^= array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult ^= array[i];
+  check_reduction_int (+, 0, result, vresult);
+  check_reduction_int (*, 1, result, vresult);
+  check_reduction_int (&, -1, result, vresult);
+  check_reduction_int (|, 0, result, vresult);
+  check_reduction_int (^, 0, result, vresult);
+}
 
-  if (result != vresult)
+#define check_reduction_minmax(pragmaop, op, init, res, vres)	\
+  res = (init);							\
+DO_PRAGMA (acc parallel vector_length (vl))\
+DO_PRAGMA (acc loop reduction (pragmaop:res))\
+  for (i = 0; i < n; i++)					\
+    res = op (res, array[i]);					\
+								\
+  vres = (init);						\
+  for (i = 0; i < n; i++)					\
+    vres = op (vres, array[i]);					\
+								\
+  if (res != vres)						\
     abort ();
 
-  result = 5;
-  vresult = 5;
+#define max_op(a, b) (((a) > (b)) ? (a) : (b))
+#define min_op(a, b) (((a) < (b)) ? (a) : (b))
 
-  lresult = false;
-  lvresult = false;
+static void
+test_reductions_minmax (void)
+{
+  const int n = 1000;
+  int i;
+  int vresult, result, array[n];
 
-  /* '&&' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (&&:lresult)
   for (i = 0; i < n; i++)
-    lresult = lresult && (result > array[i]);
+    array[i] = i;
 
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    lvresult = lresult && (result > array[i]);
+  check_reduction_minmax (min, min_op, n + 1, vresult, result);
+  check_reduction_minmax (max, max_op, -1, vresult, result);
+}
 
-  if (lresult != lvresult)
+#define check_reduction_bool(op, init, res, vres)	\
+  res = (init);						\
+DO_PRAGMA (acc parallel vector_length (vl))\
+DO_PRAGMA (acc loop reduction (op:res))\
+  for (i = 0; i < n; i++)				\
+    res = res op (cmp_val > array[i]);			\
+							\
+  vres = (init);					\
+  for (i = 0; i < n; i++)				\
+    vres = vres op (cmp_val > array[i]);		\
+							\
+  if (res != vres)					\
     abort ();
 
-  result = 5;
-  vresult = 5;
-
-  lresult = false;
-  lvresult = false;
-
-  /* '||' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (||:lresult)
-  for (i = 0; i < n; i++)
-    lresult = lresult || (result > array[i]);
+static void
+test_reductions_bool (void)
+{
+  const int n = 1000;
+  int i;
+  int array[n];
+  bool lvresult, lresult;
+  int cmp_val;
 
-  /* Verify the reduction.  */
   for (i = 0; i < n; i++)
-    lvresult = lresult || (result > array[i]);
+    array[i] = i;
 
-  if (lresult != lvresult)
-    abort ();
+  cmp_val = 5;
+  check_reduction_bool (&&, true, lresult, lvresult);
+  check_reduction_bool (||, false, lresult, lvresult);
+}
 
+int
+main (void)
+{
+  test_reductions_int ();
+  test_reductions_minmax ();
+  test_reductions_bool ();
   return 0;
 }
-- 
1.9.1


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

* Re: [PATCH] Use DO_PRAGMA in libgomp.oacc-c-c++-common/reduction-1.c
  2015-02-23 17:40   ` Tom de Vries
@ 2015-02-25 11:59     ` Thomas Schwinge
  2015-02-25 16:19       ` Tom de Vries
  0 siblings, 1 reply; 5+ messages in thread
From: Thomas Schwinge @ 2015-02-25 11:59 UTC (permalink / raw)
  To: Tom de Vries; +Cc: Jakub Jelinek, GCC Patches

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

Hi!

On Mon, 23 Feb 2015 18:14:35 +0100, Tom de Vries <Tom_deVries@mentor.com> wrote:
> On 23-02-15 17:08, Jakub Jelinek wrote:
> > On Mon, Feb 23, 2015 at 04:52:56PM +0100, Tom de Vries wrote:
> >> The only thing I'm not sure about is the two-level pragma expansion using
> >> the apply pragmas. It maximizes factoring out common parts, but it makes
> >> things less readable.
> >>
> >> Tested on x86_64.
> >>
> >> OK for stage4?
> >
> > If Thomas is ok with that, it is ok with me too.

OK, thanks!

> For comparison, this is a less convoluted, but longer version.

Hmm, I can't quite decide which of the two to prefer.  Your call.  ;-P
(Maybe, toss a coin if it takes more than a minute to decide.)


Grüße,
 Thomas

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

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

* Re: [PATCH] Use DO_PRAGMA in libgomp.oacc-c-c++-common/reduction-1.c
  2015-02-25 11:59     ` Thomas Schwinge
@ 2015-02-25 16:19       ` Tom de Vries
  0 siblings, 0 replies; 5+ messages in thread
From: Tom de Vries @ 2015-02-25 16:19 UTC (permalink / raw)
  To: Thomas Schwinge; +Cc: Jakub Jelinek, GCC Patches

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

On 25-02-15 12:40, Thomas Schwinge wrote:
> Hi!
>
> On Mon, 23 Feb 2015 18:14:35 +0100, Tom de Vries <Tom_deVries@mentor.com> wrote:
>> On 23-02-15 17:08, Jakub Jelinek wrote:
>>> On Mon, Feb 23, 2015 at 04:52:56PM +0100, Tom de Vries wrote:
>>>> The only thing I'm not sure about is the two-level pragma expansion using
>>>> the apply pragmas. It maximizes factoring out common parts, but it makes
>>>> things less readable.
>>>>
>>>> Tested on x86_64.
>>>>
>>>> OK for stage4?
>>>
>>> If Thomas is ok with that, it is ok with me too.
>
> OK, thanks!
>
>> For comparison, this is a less convoluted, but longer version.
>
> Hmm, I can't quite decide which of the two to prefer.  Your call.  ;-P
> (Maybe, toss a coin if it takes more than a minute to decide.)
>
>

Committed this version, I'm happy with this one. There are now just two 
reduction macros (check_reduction_op and check_reduction_macro), which are 
reasonably readable.

Thanks,
- Tom


[-- Attachment #2: 0001-Use-DO_PRAGMA-in-libgomp.oacc-c-c-common-reduction-1-v3.patch --]
[-- Type: text/x-patch, Size: 8318 bytes --]

/* { dg-do run } */

/* Integer reductions.  */

#include <stdlib.h>
#include <stdbool.h>

#define vl 32

#define DO_PRAGMA(x) _Pragma (#x)

#define check_reduction_op(type, op, init, b)	\
  {						\
    type res, vres;				\
    res = (init);				\
DO_PRAGMA (acc parallel vector_length (vl))\
DO_PRAGMA (acc loop reduction (op:res))\
    for (i = 0; i < n; i++)			\
      res = res op (b);				\
						\
    vres = (init);				\
    for (i = 0; i < n; i++)			\
      vres = vres op (b);			\
						\
    if (res != vres)				\
      abort ();					\
  }

static void
test_reductions_int (void)
{
  const int n = 1000;
  int i;
  int array[n];

  for (i = 0; i < n; i++)
    array[i] = i;

  check_reduction_op (int, +, 0, array[i]);
  check_reduction_op (int, *, 1, array[i]);
  check_reduction_op (int, &, -1, array[i]);
  check_reduction_op (int, |, 0, array[i]);
  check_reduction_op (int, ^, 0, array[i]);
}

static void
test_reductions_bool (void)
{
  const int n = 1000;
  int i;
  int array[n];
  int cmp_val;

  for (i = 0; i < n; i++)
    array[i] = i;

  cmp_val = 5;
  check_reduction_op (bool, &&, true, (cmp_val > array[i]));
  check_reduction_op (bool, ||, false, (cmp_val > array[i]));
}

#define check_reduction_macro(type, op, init, b)	\
  {							\
    type res, vres;					\
    res = (init);					\
DO_PRAGMA (acc parallel vector_length (vl))\
DO_PRAGMA (acc loop reduction (op:res))\
    for (i = 0; i < n; i++)				\
      res = op (res, (b));				\
							\
    vres = (init);					\
    for (i = 0; i < n; i++)				\
      vres = op (vres, (b));				\
							\
    if (res != vres)					\
      abort ();						\
  }

#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))

static void
test_reductions_minmax (void)
{
  const int n = 1000;
  int i;
  int array[n];

  for (i = 0; i < n; i++)
    array[i] = i;

  check_reduction_macro (int, min, n + 1, array[i]);
  check_reduction_macro (int, max, -1, array[i]);
}

int
main (void)
{
  test_reductions_int ();
  test_reductions_bool ();
  test_reductions_minmax ();
  return 0;
}

2015-02-25  Tom de Vries  <tom@codesourcery.com>

	* testsuite/libgomp.oacc-c-c++-common/reduction-1.c (DO_PRAGMA)
	(check_reduction_op, check_reduction_macro, max, min):
	Declare.
	(test_reductions_int, test_reductions_minmax, test_reductions_bool): New
	function.
	(main): Use new functions.
---
 .../libgomp.oacc-c-c++-common/reduction-1.c        | 223 +++++++--------------
 1 file changed, 76 insertions(+), 147 deletions(-)

diff --git a/libgomp/testsuite/libgomp.oacc-c-c++-common/reduction-1.c b/libgomp/testsuite/libgomp.oacc-c-c++-common/reduction-1.c
index acf9540..4501f8e 100644
--- a/libgomp/testsuite/libgomp.oacc-c-c++-common/reduction-1.c
+++ b/libgomp/testsuite/libgomp.oacc-c-c++-common/reduction-1.c
@@ -7,168 +7,97 @@
 
 #define vl 32
 
-int
-main(void)
+#define DO_PRAGMA(x) _Pragma (#x)
+
+#define check_reduction_op(type, op, init, b)	\
+  {						\
+    type res, vres;				\
+    res = (init);				\
+DO_PRAGMA (acc parallel vector_length (vl))\
+DO_PRAGMA (acc loop reduction (op:res))\
+    for (i = 0; i < n; i++)			\
+      res = res op (b);				\
+						\
+    vres = (init);				\
+    for (i = 0; i < n; i++)			\
+      vres = vres op (b);			\
+						\
+    if (res != vres)				\
+      abort ();					\
+  }
+
+static void
+test_reductions_int (void)
 {
   const int n = 1000;
   int i;
-  int vresult, result, array[n];
-  bool lvresult, lresult;
+  int array[n];
 
   for (i = 0; i < n; i++)
     array[i] = i;
 
-  result = 0;
-  vresult = 0;
-
-  /* '+' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (+:result)
-  for (i = 0; i < n; i++)
-    result += array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult += array[i];
-
-  if (result != vresult)
-    abort ();
-
-  result = 0;
-  vresult = 0;
-
-  /* '*' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (*:result)
-  for (i = 0; i < n; i++)
-    result *= array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult *= array[i];
-
-  if (result != vresult)
-    abort ();
-
-//   result = 0;
-//   vresult = 0;
-// 
-//   /* 'max' reductions.  */
-// #pragma acc parallel vector_length (vl)
-// #pragma acc loop reduction (+:result)
-//   for (i = 0; i < n; i++)
-//       result = result > array[i] ? result : array[i];
-// 
-//   /* Verify the reduction.  */
-//   for (i = 0; i < n; i++)
-//       vresult = vresult > array[i] ? vresult : array[i];
-// 
-//   printf("%d != %d\n", result, vresult);
-//   if (result != vresult)
-//     abort ();
-// 
-//   result = 0;
-//   vresult = 0;
-// 
-//   /* 'min' reductions.  */
-// #pragma acc parallel vector_length (vl)
-// #pragma acc loop reduction (+:result)
-//   for (i = 0; i < n; i++)
-//       result = result < array[i] ? result : array[i];
-// 
-//   /* Verify the reduction.  */
-//   for (i = 0; i < n; i++)
-//       vresult = vresult < array[i] ? vresult : array[i];
-// 
-//   printf("%d != %d\n", result, vresult);
-//   if (result != vresult)
-//     abort ();
-
-  result = 0;
-  vresult = 0;
-
-  /* '&' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (&:result)
-  for (i = 0; i < n; i++)
-    result &= array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult &= array[i];
-
-  if (result != vresult)
-    abort ();
-
-  result = 0;
-  vresult = 0;
-
-  /* '|' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (|:result)
-  for (i = 0; i < n; i++)
-    result |= array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult |= array[i];
-
-  if (result != vresult)
-    abort ();
-
-  result = 0;
-  vresult = 0;
-
-  /* '^' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (^:result)
-  for (i = 0; i < n; i++)
-    result ^= array[i];
-
-  /* Verify the reduction.  */
-  for (i = 0; i < n; i++)
-    vresult ^= array[i];
-
-  if (result != vresult)
-    abort ();
-
-  result = 5;
-  vresult = 5;
-
-  lresult = false;
-  lvresult = false;
+  check_reduction_op (int, +, 0, array[i]);
+  check_reduction_op (int, *, 1, array[i]);
+  check_reduction_op (int, &, -1, array[i]);
+  check_reduction_op (int, |, 0, array[i]);
+  check_reduction_op (int, ^, 0, array[i]);
+}
 
-  /* '&&' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (&&:lresult)
-  for (i = 0; i < n; i++)
-    lresult = lresult && (result > array[i]);
+static void
+test_reductions_bool (void)
+{
+  const int n = 1000;
+  int i;
+  int array[n];
+  int cmp_val;
 
-  /* Verify the reduction.  */
   for (i = 0; i < n; i++)
-    lvresult = lresult && (result > array[i]);
-
-  if (lresult != lvresult)
-    abort ();
-
-  result = 5;
-  vresult = 5;
+    array[i] = i;
 
-  lresult = false;
-  lvresult = false;
+  cmp_val = 5;
+  check_reduction_op (bool, &&, true, (cmp_val > array[i]));
+  check_reduction_op (bool, ||, false, (cmp_val > array[i]));
+}
 
-  /* '||' reductions.  */
-#pragma acc parallel vector_length (vl)
-#pragma acc loop reduction (||:lresult)
-  for (i = 0; i < n; i++)
-    lresult = lresult || (result > array[i]);
+#define check_reduction_macro(type, op, init, b)	\
+  {							\
+    type res, vres;					\
+    res = (init);					\
+DO_PRAGMA (acc parallel vector_length (vl))\
+DO_PRAGMA (acc loop reduction (op:res))\
+    for (i = 0; i < n; i++)				\
+      res = op (res, (b));				\
+							\
+    vres = (init);					\
+    for (i = 0; i < n; i++)				\
+      vres = op (vres, (b));				\
+							\
+    if (res != vres)					\
+      abort ();						\
+  }
+
+#define max(a, b) (((a) > (b)) ? (a) : (b))
+#define min(a, b) (((a) < (b)) ? (a) : (b))
+
+static void
+test_reductions_minmax (void)
+{
+  const int n = 1000;
+  int i;
+  int array[n];
 
-  /* Verify the reduction.  */
   for (i = 0; i < n; i++)
-    lvresult = lresult || (result > array[i]);
+    array[i] = i;
 
-  if (lresult != lvresult)
-    abort ();
+  check_reduction_macro (int, min, n + 1, array[i]);
+  check_reduction_macro (int, max, -1, array[i]);
+}
 
+int
+main (void)
+{
+  test_reductions_int ();
+  test_reductions_bool ();
+  test_reductions_minmax ();
   return 0;
 }
-- 
1.9.1


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

end of thread, other threads:[~2015-02-25 16:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-23 17:14 [PATCH] Use DO_PRAGMA in libgomp.oacc-c-c++-common/reduction-1.c Tom de Vries
2015-02-23 17:17 ` Jakub Jelinek
2015-02-23 17:40   ` Tom de Vries
2015-02-25 11:59     ` Thomas Schwinge
2015-02-25 16:19       ` Tom de Vries

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