Difference between revisions of "Guide:TAUChapel"
Line 3: | Line 3: | ||
== MonteCarlo example == | == 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 === | === 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 radius(p_x[i], p_y[i]) 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. | ||
=== Procedure promotion === | === Procedure promotion === |
Revision as of 03:26, 30 September 2013
Contents
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 radius(p_x[i], p_y[i]) 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.