Difference between revisions of "Guide:TAUChapel"

From Tau Wiki
Jump to: navigation, search
Line 15: Line 15:
 
   c = 0;
 
   c = 0;
 
   forall i in 1..n {
 
   forall i in 1..n {
     if radius(p_x[i], p_y[i]) then
+
     if (x ** 2 + y ** 2 <= 1) then
 
         c += 1;
 
         c += 1;
 
   }
 
   }
Line 22: Line 22:
 
  }
 
  }
  
Notice that the '''forall''' here will compute each iteration in parallel, hence the need to define variable '''c''' as a '''sync''' variable.
+
Notice that the '''forall''' here will compute each iteration in parallel, hence the need to define variable '''c''' as a '''sync''' variable. Performance here is limited by the need to synchronize access to '''c'''. Take a look of this profile:
 +
 
 +
 
 +
X% percent of the time is spent in synchronization. Let's see if we can do better.
  
 
=== Procedure promotion ===
 
=== Procedure promotion ===
 +
 +
Only feature of Chapel is procedure promotion where calling a procedure that takes scalar arguments with an array, the procedure is called for each element of the array in parallel:
 +
 +
proc compute_pi(p_x: [] real(64), p_y: [] real(64)) : real {
 +
 +
  var c : int;
 +
  for i in in_circle(p_x, p_y) {
 +
    c += i;
 +
  }
 +
  return c * 4.0 / n;
 +
 +
}
 +
proc in_circle(x: real(64), y: real(64)): bool
 +
{
 +
  return (x ** 2 + y ** 2) <= 1;
 +
}
 +
  
 
=== Reduction ===
 
=== Reduction ===

Revision as of 03:58, 30 September 2013

Chapel

MonteCarlo example

To test out some Chapel's language features let program a MonteCarlo simulation to calculate PI. We can calculate PI by assess how many points with coordinates x,y fit in the unit circle, ie x^2+y^2<=1.

Basic

Here is the basic routine that computes PI:

proc compute_pi(p_x: [] real(64), p_y: [] real(64)) : real {

 var c : sync int;
 c = 0;
 forall i in 1..n {
   if (x ** 2 + y ** 2 <= 1) then
       c += 1;
 }
 return c * 4.0 / n;

}

Notice that the forall here will compute each iteration in parallel, hence the need to define variable c as a sync variable. Performance here is limited by the need to synchronize access to c. Take a look of this profile:


X% percent of the time is spent in synchronization. Let's see if we can do better.

Procedure promotion

Only feature of Chapel is procedure promotion where calling a procedure that takes scalar arguments with an array, the procedure is called for each element of the array in parallel:

proc compute_pi(p_x: [] real(64), p_y: [] real(64)) : real {

 var c : int;
 for i in in_circle(p_x, p_y) {
   c += i;
 }
 return c * 4.0 / n;

}
proc in_circle(x: real(64), y: real(64)): bool
{
  return (x ** 2 + y ** 2) <= 1;
}

Reduction

Multiple Locals

Performance Results