LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2015, 03:55 PM   #1
san_man
LQ Newbie
 
Registered: Sep 2014
Posts: 5

Rep: Reputation: Disabled
compute pi value using monte carlo method multithreading


I am trying to find value of PI using montecarlo method, and using parallel C code. i have write serail code and works fine. But the parallel code gives me wrong values of pi some times 0 or minus values.
please i need help

my code

Code:
#include <pthread.h>
 #include <stdio.h>
#include <stdlib.h>
#include <math.h>
 #define NUM_THREADS 4 //number of threads
#define TOT_COUNT 10000055 //total number of iterations



void *doCalcs(void *threadid)
{
long longTid;
longTid = (long)threadid;
int tid = (int)longTid; //obtain the integer value of thread id
//using malloc for the return variable in order make
//sure that it is not destroyed once the thread call is finished
float *in_count = (float *)malloc(sizeof(float));
*in_count=0;
unsigned int rand_state = rand();
//get the total number of iterations for a thread
float tot_iterations= TOT_COUNT/NUM_THREADS;
int counter=0;
//calculation
for(counter=0;counter<tot_iterations;counter++){
    //float x = (double)random()/RAND_MAX;
    //float y = (double)random()/RAND_MAX;
    //float result = sqrt((x*x) + (y*y));
    double x = rand_r(&rand_state) / ((double)RAND_MAX + 1) * 2.0 - 1.0;
    double y = rand_r(&rand_state) / ((double)RAND_MAX + 1) * 2.0 - 1.0;
    float result = sqrt((x*x) + (y*y));
    if(result<1){
        *in_count+=1; //check if the generated value is inside a unit circle
    }
}
//get the remaining iterations calculated by thread 0
 if(tid==0){
      float remainder = TOT_COUNT%NUM_THREADS;
      for(counter=0;counter<remainder;counter++){
            float x = (double)random()/RAND_MAX;
         float y = (double)random()/RAND_MAX;
          float result = sqrt((x*x) + (y*y));
        if(result<1){
            *in_count+=1; //check if the generated value is inside a unit circle
        }
    }
}
 }


 int main(int argc, char *argv[])
  {
    pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   void *status;
    float tot_in=0;
     for(t=0;t<NUM_THREADS;t++){
      rc = pthread_create(&threads[t], NULL, doCalcs, (void *)t);
      if (rc){
    printf("ERROR; return code from pthread_create() is %d\n", rc);
    exit(-1);
     }
}
//join the threads
for(t=0;t<NUM_THREADS;t++){
pthread_join(threads[t], &status);
//printf("Return from thread %ld is : %f\n",t, *(float*)status);
tot_in+=*(float*)status; //keep track of the total in count
}
printf("Value for PI is %f \n",1, 4*(tot_in/TOT_COUNT));
/* Last thing that main() should do */
pthread_exit(NULL);
  }
 
Old 11-14-2015, 06:51 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Just eyeballing your code, I don't see a return statement in doCalcs().
 
Old 11-15-2015, 12:31 PM   #3
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Can you post the single-threaded code ? It's usually much easier to multi-thread using openmp than pthreads, so if you have the single-threaded code, you can just insert a few pragmas, change a few variables and you're good to go. Lots more features as well, which can lead to better performance.
 
  


Reply

Tags
c++, gcc, multithreading



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: RDO Kilo DVR Deployment (Controller/Network)+Compute+Compute on CentOS 7.1 LXer Syndicated Linux News 0 09-30-2015 01:53 PM
LXer: RDO Liberty DVR Deployment (Controller/Network)+Compute+Compute (ML2&OVS&VXLAN) on CentOS 7.1 LXer Syndicated Linux News 0 09-25-2015 05:20 AM
LXer: RDO Juno DVR Deployment (Controller/Network)&Compute&Compute on CentOS 7.1 LXer Syndicated Linux News 0 09-02-2015 08:10 AM
GSL on Cygwin can't find the Monte Carlo library. thethinker Linux - Software 0 02-01-2012 11:36 PM
LXer: Interview with Carlo Piana LXer Syndicated Linux News 0 03-10-2008 05:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration