Manycore Parallel Algorithm - WordPress.com

4 downloads 166 Views 873KB Size Report
TE091585 – Komputasi Grid. Outline. ❑ How to compute Phi ? ❑ Implementation. ❑ Result and Experiment. TE091585 – Komputasi Grid ...
Manycore Parallel Algorithm Compute Phi (𝜋)

Outline How to compute Phi ?  Implementation  Result and Experiment 

TE091585 – Komputasi Grid

Outline How to compute Phi ?  Implementation  Result and Experiment 

TE091585 – Komputasi Grid

Phi ?

TE091585 – Komputasi Grid

Compute Value Of Phi • Simulation Approach: – Consider a dartboard with circle of radius R inscribed in square • Area of circle = 𝜋𝑅2 • Area of square = (2𝑅)2 = 4𝑅2 •

𝐴𝑟𝑒𝑎 𝑜𝑓 𝐶𝑖𝑟𝑐𝑙𝑒 𝐴𝑟𝑒𝑎 𝑜𝑓 𝑆𝑞𝑢𝑎𝑟𝑒

TE091585 – Komputasi Grid

=

𝜋𝑅2 4𝑅2

=

𝜋 4

Compute Value Of Phi • From those equation we have – So, ratio of areas proportional to 𝜋 – How to find areas ? • Suppose we throw dart randomly at dartboard • Count total dart that landing inside the circle and total that landing on the square (total all darts) • Ratio of those numbers give approximation to ratio of area • Quality of approximation increases with number of darts

–𝜋 =4×

#𝑑𝑎𝑟𝑡𝑠 𝑖𝑛𝑠𝑖𝑑𝑒 𝑐𝑖𝑟𝑐𝑙𝑒 #𝑑𝑎𝑟𝑡𝑠 𝑡ℎ𝑟𝑜𝑤𝑛

– This method call Monte Carlo Simulation TE091585 – Komputasi Grid

Outline How to compute Phi ?  Implementation  Result and Experiment 

TE091585 – Komputasi Grid

Compute Value Of Phi • How do we simulate this experiment on computer ? – Decide on length R – Generate pairs of random numbers (𝑥, 𝑦) s.t. − 𝑅 ≤ 𝑥, 𝑦 ≤ 𝑅 – If (𝑥, 𝑦) within circle (i.e if (𝑥, 𝑦) ≤ 𝑅2 ), add one to tally for inside circle – Last, find ratio

TE091585 – Komputasi Grid

Source Code (Serial) #include #include #include static long num_trials = 10000; int main() { long i; long Ncirc = 0; double pi, x, y; double r = 1.0; // radius of circle double r2 = r*r; for (i = 0; i < num_trials; i++) { x = (double)rand() / (double)RAND_MAX; y = (double)rand() / (double)RAND_MAX; if ((x*x + y*y)