- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi, every one.
I want to calculate PI using multi-core parallel algorithms. I have following code:
It seems to work well, but I can't write this with lambda expression.I tried to use example from this topic, but still something is wrong.. Please, show me how it could be look like..
[cpp]
#include <stdio.h>
#include <tbb/blocked_range.h>
#include <tbb/parallel_reduce.h>
#include <tbb/task_scheduler_init.h>
class Pi_MC
{
private:
long num_steps;
double step;
public:
double pi;
void operator () (const tbb::blocked_range<long> &r)
{
double x, sum = 0.0;
long end = r.end();
for (int i = r.begin(); i != end; i++)
{
x = (i - 0.5) * step;
sum = sum + 4.0 / (1.0 + x*x);
}
pi += sum * step;
}
void join(Pi_MC &p)
{
pi += p.pi;
}
Pi_MC(Pi_MC &p, tbb::split)
{
pi = 0.0;
num_steps = p.num_steps;
step = p.step;
}
Pi_MC(long n)
{
pi = 0.0;
num_steps = n;
step = 1.0 / (double) num_steps;
}
};
int main()
{
tbb::task_scheduler_init init;
const long n = 100000000;
Pi_MC pi(n);
tbb::parallel_reduce(tbb::blocked_range<long>(0, n, 1000000), pi);
printf ("Pi = %.16f steps: %d \n", pi.pi, n);
system("pause");
}
[/cpp]
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page