GPU : A Giga@lobal Processing Unit ?

A way for computing Pi

Description of the plugin greekpi.dll

Greekpi is a plugin which computes Pi with the Monte Carlo method. Although not very useful in real life (206 billion of digits of Pi are known), it is a good academic example. The idea is to generate two random numbers between 0 and 1, which are considered as coordinates in the plane. Let’s call these two coordinates x and y. Now the plugin checks if the point (x,y) lies within a circle of radius 1. To do this square x and y, sum them together and check if the result is less than one.



If the point (x,y) lies in the circle, increment the counter Sum by one.
By repeating this operation many times, if the points are uniformly distributed, we can build the quotient between the counter Sum and the number of throws (in the plugin the number of throws is passed as parameter and stored in the variable) and we get the percentage of throws falling within the upper right quadrant of the unit circle. Recall that the points can only be thrown between the square with side 1 and area 1. The percentage becomes also the area of the quarter of the circle with radius 1. By multiplying the percentage by 4 we get the complete area of the circle, which is Pi.

Here the source code for the plugin Pi.



function pi(var stk : TStack):Boolean;stdcall;
 var
    Idx         : Integer;
    Count,Sum,i : Longint;
 
 begin
 Result := False;
 Idx := stk.StIdx; {this should speed up and also made the code more readable}
 {check if enough parameters
  first parameter is number of throws for computing Pi}
 if Idx < 1 then Exit;
 Count := Trunc(Stk.Stack[Idx]);
 if Count > 625000000 then Exit; {parameter is too big and we get an overflow}
 
 Sum   := 0;
 for i:=1 to Count do
           begin
            if (Sqr(Random)+Sqr(Random)) < 1 then
                Inc(Sum);
           end;
 
 {gives result back}
 Stk.Stack[Idx]:=4* Sum / Count;
 Result := True;
 end;


The logo was designed by Mark Grady. Graphics are by David A. Lucas.
© 2002-2003 the GPU Development team

Page generated in 2.4 milliseconds by PHP 4.4.3-dev on seeschloss.free.fr.