Changes

Jump to: navigation, search

Midnight Tiger

1,698 bytes added, 23:22, 4 April 2016
Demonstration of Sample Code
== Demonstration of Sample Code ==
 
While learning Chapel, I found a pi program for serial & parallel. I tweaked a little bit to remove errors in the code and added few more lines to check the performance. This is tested on dual-core computer. You can find original code here: http://chapel.cray.com/tutorials/SC10/
 
=== Serial Pi Program ===
 
<pre>
use Random;
use Time;
 
config const n = 100000, // number of random points to try
seed = 589494289; // seed for random number generator
const ts = getCurrentTime();
 
writeln("Number of points = ", n);
writeln("Random number seed = ", seed);
 
var rs = new RandomStream(seed, parSafe=false);
var count = 0;
 
for i in 1..n do
if (rs.getNext()**2 + rs.getNext()**2) <= 1.0 then
count += 1;
const te = getCurrentTime();
writeln("Approximation of pi = ", count * 4.0 / n);
writeln("Integration :", te-ts);
delete rs;
</pre>
 
 
=== Task Parallelized Pi Program ===
 
<pre>
use Time;
use Random;
 
config const n = 100000,
tasks = here.numCores,
seed = 589494289;
const ts = getCurrentTime();
 
writeln("Number of points = ", n);
writeln("Random number seed = ", seed);
writeln("Number of tasks = ", tasks);
 
var counts: [0..#tasks] int;
'''coforall''' tid in 0..#tasks {
var rs = new RandomStream(seed, parSafe=false);
const nPerTask = n/tasks,
extras = n%tasks;
rs.skipToNth(2*(tid*nPerTask + (if tid < extras then tid else extras)) + 1);
 
var count = 0;
for i in 1..nPerTask + (tid < extras) do
count += (rs.getNext()**2 + rs.getNext()**2) <= 1.0;
 
counts[tid] = count;
 
delete rs;
}
 
var count = + '''reduce''' counts;
const te = getCurrentTime();
writeln("Approximation of pi = ", count * 4.0 / n);
writeln("Integration: ", te - ts);
</pre>
 
== Useful Links ==

Navigation menu