Thu Nov 21 23:12:14 2024
EVENTS
 FREE
SOFTWARE
INSTITUTE

POLITICS
JOBS
MEMBERS'
CORNER

MAILING
LIST

NYLXS Mailing Lists and Archives
NYLXS Members have a lot to say and share but we don't keep many secrets. Join the Hangout Mailing List and say your peice.

DATE 2015-04-01

LEARN

2024-11-21 | 2024-10-21 | 2024-09-21 | 2024-08-21 | 2024-07-21 | 2024-06-21 | 2024-05-21 | 2024-04-21 | 2024-03-21 | 2024-02-21 | 2024-01-21 | 2023-12-21 | 2023-11-21 | 2023-10-21 | 2023-09-21 | 2023-08-21 | 2023-07-21 | 2023-06-21 | 2023-05-21 | 2023-04-21 | 2023-03-21 | 2023-02-21 | 2023-01-21 | 2022-12-21 | 2022-11-21 | 2022-10-21 | 2022-09-21 | 2022-08-21 | 2022-07-21 | 2022-06-21 | 2022-05-21 | 2022-04-21 | 2022-03-21 | 2022-02-21 | 2022-01-21 | 2021-12-21 | 2021-11-21 | 2021-10-21 | 2021-09-21 | 2021-08-21 | 2021-07-21 | 2021-06-21 | 2021-05-21 | 2021-04-21 | 2021-03-21 | 2021-02-21 | 2021-01-21 | 2020-12-21 | 2020-11-21 | 2020-10-21 | 2020-09-21 | 2020-08-21 | 2020-07-21 | 2020-06-21 | 2020-05-21 | 2020-04-21 | 2020-03-21 | 2020-02-21 | 2020-01-21 | 2019-12-21 | 2019-11-21 | 2019-10-21 | 2019-09-21 | 2019-08-21 | 2019-07-21 | 2019-06-21 | 2019-05-21 | 2019-04-21 | 2019-03-21 | 2019-02-21 | 2019-01-21 | 2018-12-21 | 2018-11-21 | 2018-10-21 | 2018-09-21 | 2018-08-21 | 2018-07-21 | 2018-06-21 | 2018-05-21 | 2018-04-21 | 2018-03-21 | 2018-02-21 | 2018-01-21 | 2017-12-21 | 2017-11-21 | 2017-10-21 | 2017-09-21 | 2017-08-21 | 2017-07-21 | 2017-06-21 | 2017-05-21 | 2017-04-21 | 2017-03-21 | 2017-02-21 | 2017-01-21 | 2016-12-21 | 2016-11-21 | 2016-10-21 | 2016-09-21 | 2016-08-21 | 2016-07-21 | 2016-06-21 | 2016-05-21 | 2016-04-21 | 2016-03-21 | 2016-02-21 | 2016-01-21 | 2015-12-21 | 2015-11-21 | 2015-10-21 | 2015-09-21 | 2015-08-21 | 2015-07-21 | 2015-06-21 | 2015-05-21 | 2015-04-21 | 2015-03-21 | 2015-02-21 | 2015-01-21 | 2014-12-21 | 2014-11-21 | 2014-10-21

Key: Value:

Key: Value:

MESSAGE
DATE 2015-04-01
FROM Ruben Safir
SUBJECT Subject: [LIU Comp Sci] omp pthread madness
From owner-learn-outgoing-at-mrbrklyn.com Wed Apr 1 00:43:45 2015
Return-Path:
X-Original-To: archive-at-mrbrklyn.com
Delivered-To: archive-at-mrbrklyn.com
Received: by mrbrklyn.com (Postfix)
id 429AB16116B; Wed, 1 Apr 2015 00:43:45 -0400 (EDT)
Delivered-To: learn-outgoing-at-mrbrklyn.com
Received: by mrbrklyn.com (Postfix, from userid 28)
id 27AFB161168; Wed, 1 Apr 2015 00:43:45 -0400 (EDT)
Delivered-To: learn-at-nylxs.com
Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89])
by mrbrklyn.com (Postfix) with ESMTP id 7E2C3161167;
Wed, 1 Apr 2015 00:43:19 -0400 (EDT)
Received: from [10.0.0.19] (unknown [96.57.23.82])
by mailbackend.panix.com (Postfix) with ESMTPSA id A5085136BB;
Wed, 1 Apr 2015 00:43:18 -0400 (EDT)
Message-ID: <551B7766.1000401-at-panix.com>
Date: Wed, 01 Apr 2015 00:43:18 -0400
From: Ruben Safir
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0
MIME-Version: 1.0
To: Hangout , learn-at-nylxs.com
Subject: [LIU Comp Sci] omp pthread madness
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Sender: owner-learn-at-mrbrklyn.com
Precedence: bulk
Reply-To: learn-at-mrbrklyn.com

[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-

  1. 2015-04-01 Ruben <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] external paths
  2. 2015-04-01 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] omp pthread madness
  3. 2015-04-02 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: Fw: Perl Developer / Linux SysAdmin Opportunities in South Florida
  4. 2015-04-02 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: omp pthread madness
  5. 2015-04-02 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: omp pthread madness
  6. 2015-04-02 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: [New discussion] which technologies/skills are good to have
  7. 2015-04-02 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Re: omp pthread madness
  8. 2015-04-05 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Re: Depths and heights
  9. 2015-04-09 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Re: Kernel thread scheduling
  10. 2015-04-09 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Re: Kernel thread scheduling
  11. 2015-04-13 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] BASh Shell Scripting
  12. 2015-04-13 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Chapter 5 Monitor
  13. 2015-04-13 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: [Perlweekly] #194 - The CPAN PR Challenge Now Has a Mini-Me!
  14. 2015-04-13 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Kernel Hacking Resources
  15. 2015-04-14 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: Invitation: Vintage Computer Festival
  16. 2015-04-14 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] hashing slots sizes
  17. 2015-04-15 Ruben <mrbrklyn-at-panix.com> Re: [LIU Comp Sci] BASh Shell Scripting
  18. 2015-04-15 Maneesh Kongara <maneeshkongara-at-gmail.com> Re: [LIU Comp Sci] BASh Shell Scripting
  19. 2015-04-15 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: Monday: Join 121 HackerNestologists at "HackerNest NYC April
  20. 2015-04-15 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: You're confirmed for: NYLUG Open hacker hours
  21. 2015-04-15 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Hash Tables Examples
  22. 2015-04-15 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Re: hashing quadradic probes
  23. 2015-04-15 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] [ruben-at-mrbrklyn.com: Re: Heap Management Problem]
  24. 2015-04-16 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: Re: virtualbox
  25. 2015-04-16 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] linsched
  26. 2015-04-16 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] scheduler project thus far
  27. 2015-04-17 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Come Sunday Morning
  28. 2015-04-17 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Kernel Scheduling Project
  29. 2015-04-17 Ruben <ruben.safir-at-my.liu.edu> Subject: [LIU Comp Sci] more advanced coding musings ...
  30. 2015-04-19 Tony Genao <tony.genao-at-my.liu.edu> Re: [LIU Comp Sci] Fwd: [NYLXS - HANGOUT] Summers Here
  31. 2015-04-19 Ruben Safir <mrbrklyn-at-panix.com> Re: [LIU Comp Sci] Fwd: [NYLXS - HANGOUT] Summers Here
  32. 2015-04-19 Tony Genao <tony.genao-at-my.liu.edu> Re: [LIU Comp Sci] Fwd: [NYLXS - HANGOUT] Summers Here
  33. 2015-04-19 Ruben <mrbrklyn-at-panix.com> Re: [LIU Comp Sci] Fwd: [NYLXS - HANGOUT] Summers Here
  34. 2015-04-19 Tony Genao <tony.genao-at-my.liu.edu> Re: [LIU Comp Sci] Fwd: [NYLXS - HANGOUT] Summers Here
  35. 2015-04-19 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: [NYLXS - HANGOUT] Summers Here
  36. 2015-04-19 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Re: The more I learn the less I know
  37. 2015-04-22 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: Re: wait queues semiphores kernel implementations
  38. 2015-04-22 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] linux locks and mutex
  39. 2015-04-23 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: Re: Kernel Scheduler and wiat queues
  40. 2015-04-25 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: Kernel Scheduling and wait queues
  41. 2015-04-27 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: OS Cpt 9 27 question
  42. 2015-04-27 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Fwd: Tomorrow: You and 30 other Flatirons are going to "Flatiron
  43. 2015-04-28 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Re: DMCA exemption commenting process broken beyond repair
  44. 2015-04-29 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Do you have notes for this question
  45. 2015-04-29 Ruben Safir <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Professional Troll job available
  46. 2015-04-29 Ruben <mrbrklyn-at-panix.com> Subject: [LIU Comp Sci] Re: DMCA exemption commenting process broken beyond repair

NYLXS are Do'ers and the first step of Doing is Joining! Join NYLXS and make a difference in your community today!