hg02_deadlock.stderr.exp-linuxthreads \
hg02_deadlock.vgtest \
hg03_inherit.stderr.exp \
- hg03_inherit.stderr.exp2 \
hg03_inherit.stderr.exp-linuxthreads \
+ hg03_inherit.stderr.exp2 \
hg03_inherit.vgtest \
hg04_race.stderr.exp \
hg04_race.stderr.exp-linuxthreads \
matinv.stdout.exp \
matinv.stdout.exp-linuxthreads \
matinv.vgtest \
+ matinv_openmp.stderr.exp \
+ matinv_openmp.stdout.exp \
+ matinv_openmp.vgtest \
pth_barrier.stderr.exp \
pth_barrier.stderr.exp-linuxthreads \
pth_barrier.vgtest \
tc24_nonzero_sem \
trylock
-check_PROGRAMS_OPENMP = matinv_openmp
+check_PROGRAMS_OPENMP = omp_prime matinv_openmp
if HAVE_GCC_FOPENMP
check_PROGRAMS = $(check_PROGRAMS_COMMON) $(check_PROGRAMS_OPENMP)
matinv_SOURCES = matinv.c
matinv_LDADD = -lpthread -lm
-if HAVE_GCC_FOPENMP
-matinv_openmp_SOURCES = matinv_openmp.c
-matinv_openmp_CFLAGS = -fopenmp
-matinv_openmp_LDADD = -lm
-endif
-
pth_barrier_SOURCES = pth_barrier.c
pth_barrier_LDADD = -lpthread
trylock_SOURCES = trylock.c
trylock_LDADD = -lpthread -lrt
+
+if HAVE_GCC_FOPENMP
+matinv_openmp_SOURCES = matinv_openmp.c
+matinv_openmp_CFLAGS = -fopenmp
+matinv_openmp_LDADD = -lm
+
+omp_prime_SOURCES = omp_prime.c
+omp_prime_CFLAGS = -fopenmp
+omp_prime_LDADD = -lm
+endif
--- /dev/null
+/** An OpenMP example.
+ * Based on the example listed on the following web page:
+ * http://developers.sun.com/sunstudio/downloads/ssx/tha/tha_using.html
+ */
+
+
+#include <assert.h>
+#include <math.h>
+#include <omp.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+
+static int is_prime(int* const pflag, int v)
+{
+ int i;
+ int bound = floor(sqrt ((double)v)) + 1;
+
+ for (i = 2; i < bound; i++)
+ {
+ /* No need to check against known composites */
+ if (!pflag[i])
+ continue;
+ if (v % i == 0)
+ {
+ pflag[v] = 0;
+ return 0;
+ }
+ }
+ return (v > 1);
+}
+
+int main(int argc, char **argv)
+{
+ int i;
+ int total = 0;
+ const int n = argc > 1 ? atoi(argv[1]) : 300;
+ const int num_threads = argc > 2 ? atoi(argv[2]) : 4;
+ int* primes;
+ int* pflag;
+
+ // Not the most user-friendly way to do error checking, but better than
+ // nothing.
+ assert(n > 2);
+ assert(num_threads >= 1);
+
+ primes = malloc(n * sizeof(primes[0]));
+ pflag = malloc(n * sizeof(pflag[0]));
+
+ omp_set_num_threads(num_threads);
+ omp_set_dynamic(0);
+
+ for (i = 0; i < n; i++) {
+ pflag[i] = 1;
+ }
+
+#pragma omp parallel for
+ for (i = 2; i < n; i++)
+ {
+ if (is_prime(pflag, i))
+ {
+ primes[total] = i;
+ total++;
+ }
+ }
+ printf("Number of prime numbers between 2 and %d: %d\n",
+ n, total);
+ for (i = 0; i < total; i++)
+ {
+ printf("%d\n", primes[i]);
+ }
+
+ free(pflag);
+ free(primes);
+
+ return 0;
+}