Author: bart Date: 2008-03-10 19:26:42 +0000 (Mon, 10 Mar 2008) New Revision: 7632
Log: Added omp_prime.c Added: trunk/exp-drd/tests/omp_prime.c Modified: trunk/exp-drd/tests/Makefile.am Modified: trunk/exp-drd/tests/Makefile.am =================================================================== --- trunk/exp-drd/tests/Makefile.am 2008-03-10 19:18:20 UTC (rev 7631) +++ trunk/exp-drd/tests/Makefile.am 2008-03-10 19:26:42 UTC (rev 7632) @@ -30,8 +30,8 @@ hg02_deadlock.stderr.exp-linuxthreads \ hg02_deadlock.vgtest \ hg03_inherit.stderr.exp \ + hg03_inherit.stderr.exp-linuxthreads \ hg03_inherit.stderr.exp2 \ - hg03_inherit.stderr.exp-linuxthreads \ hg03_inherit.vgtest \ hg04_race.stderr.exp \ hg04_race.stderr.exp-linuxthreads \ @@ -51,6 +51,9 @@ 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 \ @@ -234,7 +237,7 @@ 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) @@ -272,12 +275,6 @@ 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 @@ -376,3 +373,13 @@ 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 Added: trunk/exp-drd/tests/omp_prime.c =================================================================== --- trunk/exp-drd/tests/omp_prime.c (rev 0) +++ trunk/exp-drd/tests/omp_prime.c 2008-03-10 19:26:42 UTC (rev 7632) @@ -0,0 +1,77 @@ +/** 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; +} ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ Valgrind-developers mailing list Valgrind-developers@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/valgrind-developers