MESSAGE
DATE | 2015-04-01 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] omp pthread madness
|
[This mail was also posted to comp.unix.programmer.]
I'm doing this test program with OMP threads and I'm getting a very odd result which I have no idea where this is happening.
i set up a shared workspace for the threads called wordspace[MAX_SPACE] and what should happen is that it gets filled with coordinate points. PI can be estimated by a formula based on how many at random as they are picked, end up within a circle.
When I construct it without threads, it does a decent job of predicting about 3.14 etc.
With OMP, it is giving numbers in the 6 zone. I don't see anything that would make my random selection change, nor do I see anything else, so I'm at a loss, and admittedly can use some mentoring here, not only to understand my mistake, but to understand the theory so I don't repeat it.
pi_lib.h
#ifndef MONTI_PI #define MONTI_PI
#define MEMORY_SPACE 10000
struct point{ double x; double y; };
struct point * random_point(void); double calc_pi(void); int count_inside(void);
#endif
pid_lib.c
/* Question 4.18 from the operating systems HW from Text * OS Ed 9 * Class Operating SYstems at LIU * Prog M Ghriger * * Ruben Safir - Student * ********************************************************/ #include #include #include "pi_lib.h" #include #include
struct point workspace[MEMORY_SPACE]; long seed = 0; long inside_the_circle = 0; struct timespec ts; struct point sample;
struct point * random_point(void) { int i; double x,y;
if(seed == 0){ clock_gettime(CLOCK_REALTIME, &ts); seed = ts.tv_nsec; srand(seed); } for (i = 0; i < MEMORY_SPACE; i++){ //fprintf( stderr, "Error %d\n", __LINE__); //x = ((double)rand()) % (double)1000000 ; x = rand() % 1000000 ; x = x / (double)1000000;
// printf ("x==> %F", x); //y = ((double)rand())% (double)1000000 ; y = rand()% 10000000 ; y = y / (double)10000000; // printf(" y==> %F\n", y); workspace[i].x = x; workspace[i].y = y; } return &workspace[0]; }
int count_inside(void) { int i = 0; double d; for(i = 0; i < MEMORY_SPACE; i++){ d = sqrt(pow(workspace[i].x, 2.0) + pow(workspace[i].y, 2.0)); // printf ("distance => %f\n", d); if (d <= 1.0) inside_the_circle++; } return inside_the_circle; }
double calc_pi(){ return 4.0 * (( double)inside_the_circle/ (double)MEMORY_SPACE) ; }
monty_pi_omp.c
#include #include #include "pi_lib.h" #include #include
void * wrapper(void*); double pi;
int main(int argc, char * argv[]){ double pi; #pragma omp parallel { random_point(); count_inside(); }
pi = calc_pi(); printf("Pi is estimated at ==> %f\n\n", pi);
return 1; }
and just for comparison
this is monty_pi.c
#include #include #include "pi_lib.h" #include
void * wrapper(void*);
int main(int argc, char * argv[]){ double pi; pthread_attr_t attr; pthread_attr_init(&attr); pthread_t tid; void * arg = NULL; pthread_create(&tid, &attr, wrapper, arg); pthread_join(tid, NULL);
pi = calc_pi(); printf("Pi is estimated at ==> %f\n\n", pi);
return 1; }
void * wrapper(void * arg){ random_point(); count_inside(); pthread_exit(arg); }
and this was the makefile
XX:=gcc CXXFLAGS:=-Wall -g -pg LDFLAGS:= -pg -pthread -fopenmp
monty_pi_omp : monty_pi_omp.o pi_lib.o $(CXX) $(CXXFLAGS) $(LDFLAGS) -o monty_pi_omp.exe monty_pi_omp.o pi_lib.o
monty_pi : monty_pi.o pi_lib.o $(CXX) $(CXXFLAGS) $(LDFLAGS) -o monty_pi.exe monty_pi.o pi_lib.o
monty_pi_omp.o : monty_pi_omp.c pi_lib.h $(CXX) $(CXXFLAGS) $(LDFLAGS) -c monty_pi_omp.c
monty_pi.o : monty_pi.c pi_lib.h $(CXX) $(CXXFLAGS) $(LDFLAGS) -c monty_pi.c
pi_lib.o : pi_lib.h pi_lib.c $(CXX) $(CXXFLAGS) $(LDFLAGS) -c pi_lib.c
include make.deps make.deps: *.c; ${CXX} ${CXXFLAGS} -M *.c >$-at-
|
|