#set numprocs 0
#set proc 0
#set intervals 0
tkwait variable intervals
These three set commands are commented out, because it's up to
the master processor to initialize these variables (we need to read
them but dare not write them). So we idle, waiting for our master to
send the three real set
commands. When the set
intervals command arrives, it changes the value of
intervals, the tkwait
completes, and we continue.
set h [expr 1.0 / $intervals]
set helper_sum 0
for {set i [expr $proc + 1]} {$i <= $intervals} {incr i $numprocs} {
set x [expr $h * ($i - 0.5)]
set helper_sum [expr $helper_sum + 4.0 / (1.0 + $x*$x)]
set status "Processor $proc, interval = $i, partial sum = $helper_sum"
update idletasks
}
set helper_sum [expr $h * $helper_sum]
set status "Processor $proc, final sum = $helper_sum"
This is a straightforward Tcl translation of the Perl-
-computing
code we saw earlier. It only sums "one numprocs-th" of
the interval, but that's okay, because the other
"numprocs - 1" processors sum the rest. Notice how the
status label is updated in real time, via update
idletasks, just as in Perl/Tk.
while {1} {
catch {send pi.tcl "set sums($proc) $helper_sum"}
after 1000
}
Finally, send the partial sum back to the master, who is expecting to
see it appear in its sums associative array,
indexed by processor number. A Tcl associative array works the same
way as a Perl hash; in fact, Perl used to call its hashes associative
arrays too, but we all got lazy and shortened the phrase to simply
hash (which is more descriptive anyway).