This is a simple OpenMP program which does a barrier sync at the end of
each parallel for loop section.

Signed-off-by: Rohit Jain <rohit.k.j...@oracle.com>
---
 tools/testing/selftests/openmp_barrier/Makefile  |  6 +++++
 tools/testing/selftests/openmp_barrier/barrier.c | 29 ++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 tools/testing/selftests/openmp_barrier/Makefile
 create mode 100644 tools/testing/selftests/openmp_barrier/barrier.c

diff --git a/tools/testing/selftests/openmp_barrier/Makefile 
b/tools/testing/selftests/openmp_barrier/Makefile
new file mode 100644
index 0000000..a6b4455
--- /dev/null
+++ b/tools/testing/selftests/openmp_barrier/Makefile
@@ -0,0 +1,6 @@
+CFLAGS += -m64 $(BUILD_FLAGS)
+LDFLAGS += -O -fopenmp
+
+TEST_GEN_PROGS = barrier
+
+include ../lib.mk
diff --git a/tools/testing/selftests/openmp_barrier/barrier.c 
b/tools/testing/selftests/openmp_barrier/barrier.c
new file mode 100644
index 0000000..6dccd24
--- /dev/null
+++ b/tools/testing/selftests/openmp_barrier/barrier.c
@@ -0,0 +1,29 @@
+#include <sys/time.h>
+#include <stdio.h>
+#include <stdlib.h>
+#define TV2NS(tv)      ((tv).tv_sec * 1000000000L + (tv).tv_usec * 1000L)
+
+void work(void)
+{
+       int i;
+       volatile int *p = &i;
+       for (*p = 0; *p < 1000000; (*p)++)
+               ;
+}
+
+int main(int argc, char *argv[])
+{
+       struct timeval t1, t2;
+       long i, j, elapsed;
+       int n = (argc > 1 ? atoi(argv[1]) : 1000);
+       int nt = atoi(getenv("OMP_NUM_THREADS"));
+       gettimeofday(&t1, 0);
+
+       for (i = 0; i < n; i++)
+               #pragma omp parallel for
+               for (j = 0; j < nt; j++)
+                       work();
+       gettimeofday(&t2, 0);
+       elapsed = TV2NS(t2) - TV2NS(t1);
+       printf("%.2f iters/sec\n", n / (elapsed / 1e9));
+}
-- 
2.7.4

Reply via email to