Pthreads to compute Pi

Pages: 12
How about this one, i have returned the value and calculated at the end, but still there is some syntax error. What is the mistake i am doing in this snippet?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <semaphore.h>
#include <unistd.h>
#include <iomanip>
#include <cmath>
#include <pthread.h>
#include <iostream>
#include <stdlib.h>

const size_t N = 4;

struct Info
{
	Info() : pi(0) {}

	unsigned pi;
};

void* func(void* param)
{
	double pi, pointsIn = 0,
			   xValue,
			   yValue,
			   totalpoints=1000000,
			   distance;
			   srand(time(NULL));

			   if (Info* info = static_cast<Info*>(param))
			   //Monte-carlo method to calculate the value of Pi
		   for(int info=0; info<totalpoints; info++)
		   {
		      xValue = (double) rand()/RAND_MAX; //Random point: x
		      yValue = (double) rand()/RAND_MAX; //Random point: y
		      distance = (xValue * xValue) + (yValue * yValue);
		      if(distance <= 1.0)
		         ++pointsIn;
		   }



	return &pointsIn;
}

int main()
{
	double pi, Totalpoints=4000000,;
	srand(time(NULL));

	pthread_t handles[N];
	Info info[N];

	// start the threads
	for (size_t i = 0; i != 4; ++i)
		pthread_create(&handles[i], NULL, func, &info[i]);

	// wait for the threads to complete
	for (size_t i = 0; i != 4; ++i)
		pthread_join(handles[i], NULL);

	pi = 4.0 * info[i].pointsIn/(Totalpoints);
	// processing the reply
	for (size_t i = 0; i != 4; ++i)
		std::cout << "Thread[" << i << "] = " << info[i].pi << std::endl;

	return 0;
}
We're nearly there, but there are few things that aren't quite right. I'll do them one at a time.

I think you don't really understand how to pass data to and from the thread function. Let's do that first.

In the thread example I posted, I showed how the thread function can receive and pass information back to the caller thread. We create a struct and share it between both threads. But as we never access the data at the same time from the main thread and a worker thread, we don't need to synchronise access to it.
http://www.cplusplus.com/forum/unices/75447/#msg405258

We should use this mechanism to get the counts back from each thread.
Yes you are right, i am having an issue while returning, it says error in converting double to void, so thats the reason i am returning return &pointsIn;

Regarding the addition of the result from all the 4 threads is bit out of my range at the moment.
Hmm, interesting enough that I checked how it would be in Scala:

First the single-threaded version:

1
2
3
import util.Random._
def pi(n: Int) = 
  (1 to n count { _ => val (x, y) = (nextDouble, nextDouble); x * x + y * y <= 1.0 }) * 4.0 / n


Then the multithreaded version:
1
2
3
import util.Random._
def pi(n: Int) = 
  ((1 to n).par count { _ => val (x, y) = (nextDouble, nextDouble); x * x + y * y <= 1.0 }) * 4.0 / n


Result:
1
2
scala> pi(100000000)
res13: Double = 3.14170732

Topic archived. No new replies allowed.
Pages: 12