Control flow
Chapel, as most high-level programming languages, has different staments to control the flow of the program or code. The conditional statements are the if
statement and the while
statement.
The general syntax of a while
statement is one of the two:
while condition do
instruction;
while condition {
instruction1;
...
instructionN;
}
With multiple instructions inside the curly brackets, all of them are executed one by one if the condition evaluates to True
. This block will be repeated over and over again until the condition does not hold anymore.
In our Julia set code we don’t use the while
construct, however, we need to check if our iteration goes beyond the \(|z|=4\) circle – this is the type of control that an if statement gives us. The general syntax is:
if condition then
instruction1;
else
instruction2;
and you can group multiple instructions into a block by using the curly brackets {}
.
Let’s package our calculation into a function: for each complex number we want to iterate until either we reach the maximum number of iterations, or we cross the \(|z|=4\) circle:
proc pixel(z0) {
const c: complex = 0.355 + 0.355i; // Julia set constant
var z = z0*1.2; // zoom out
for i in 1..255 {
z = z*z + c;
if abs(z) >= 4 then
return i;
}
return 255;
}
Let’s compile and run our code using the job script serial.sh
:
#!/bin/bash
#SBATCH --time=00:05:00 # walltime in d-hh:mm or hh:mm:ss format
#SBATCH --mem-per-cpu=3600 # in MB
#SBATCH --output=solution.out
./juliaSetSerial
$ chpl juliaSetSerial.chpl
$ sbatch serial.sh
$ tail -f solution.out