Need an algorithm to calculate pi in parallel with pthreads in c

Before any rapid answer i would like to explain my needs. I'm working on a C-Posix project focused on parallel computing with Pthreads. I did my research, and i found dozens of algorithms to calculate Pi: Bailey–Borwein–Plouffe formula, Machin-like formula, Leibniz formula for π, Chudnovsky algorithm, monte-carlo, Ramanujan, etc. The problem is that I don't need the "Best" algorithm in terms of speed convergence or digits per iteration, because obtaining pi is not the objective of the project per se. The real objective of the project is to compare the performance of the same programming problem solved both serial, and parallel with pthreads. For instance at the end of the project I'm expecting to get a comparative table like this:

在这里输入图像描述

After saying all of that i need an algorithm that is simple as it can be, simple to code in pthreads(not MPI, OpenMP, or similars)(Being embarrassingly parallel would be a perfect fit) and last but not least the algorithm must be suitable to show a great impact on parallelization

PD: i would also be greatful with anyone that can sugest any other parallel calculation that can fit to this project.

Thanks!


A Monte Carlo method to approximate pi (to arbitrary precision) is easily parallelizable. In pseudocode:

total=0;
inside=0;
while(total<whatever) {
    double1=random(0,1); //Random number from zero to 1
    double2=random(0,1);
    total++;
    if(double1**2 + double2**2 < 1) {inside++;}
}
pi=4*inside/total;

Everything inside the loop can be parallelized on as many threads as you like, and you just have to add up the results at the end.

Edit: As caf mentions in the comments, it's important that the RNGs are independent for each thread or you will get no improvement from running multiple threads.

链接地址: http://www.djcxy.com/p/70982.html

上一篇: 使用代码来计算余数的阶乘和

下一篇: 需要一个算法来与c中的pthreads并行计算pi