This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Use DO_PRAGMA in libgomp.oacc-c-c++-common/reduction-1.c


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
/* { 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]