Reputation: 301
I am trying to improve my run time on my matrix multiplication by rearranging the loop order by improving spatial locality. I have an example of doing this from my class in c++ but trying to implement it in Chapel has caused me this error.
var grid : [1..size, 1..size] uint(8);
var grid2 : [1..size, 1..size] uint(8);
var grid3 : [1..size, 1..size] int;
var grid4 : [1..size, 1..size] uint(8);
forall i in 1..size do
forall k in 1..size do
grid4 = grid[i,k];
forall j in 1..size do
grid3[i,j] += grid4 * grid2[k,j];
hello.chpl:20: error: 'i' undeclared (first use this function)
hello.chpl:20: error: 'k' undeclared (first use this function)
Upvotes: 2
Views: 176
Reputation: 4008
There are two things that, in combination, are contributing to these errors:
1) Chapel is not a whitespace-sensitive language. So where in Python, say, indentation of statements indicates their relationship to one another, this is not the case in Chapel. In that respect, it's more like C, C++, or Java, say.
2) The do
keyword in a loop statement (or the then
keyword in a conditional statement) is a shorthand form that is specific to control structures whose bodies only have a single statement. To create a loop or conditional with multiple statements in its body, you should use curly brackets to specify a compound statement. A defensive way to write Chapel programs is to always use curly brackets. Note that I often use the shorthands in slides or StackOverflow posts when legal simply because it's more concise.
As a result, your loop structure as written, properly indented, is as follows:
forall i in 1..size do
forall k in 1..size do
grid4 = grid[i,k];
forall j in 1..size do
grid3[i,j] += grid4 * grid2[k,j];
which shows why i
and k
were not accessible in that final loop—they're specific to the previous scope. And one fix would be to do:
forall i in 1..size do
forall k in 1..size {
grid4 = grid[i,k];
forall j in 1..size do
grid3[i,j] += grid4 * grid2[k,j];
}
or, in a fully-bracketed version:
forall i in 1..size {
forall k in 1..size {
grid4 = grid[i,k];
forall j in 1..size {
grid3[i,j] += grid4 * grid2[k,j];
}
}
}
Ideally, a good Chapel editor mode would help protect you from such issues, but at present, only a few such editor modes are available, and their quality varies. In the Chapel installation, you'll find emacs and vim modes in $CHPL_HOME/highlight
(where the emacs mode tends to be less mature than the vim mode). I believe there is also an atom mode available online (and potentially others), but am not personally as familiar with them.
But, as I say, a very safe way of programming is to always use curly brackets with control flow in which case simple mistakes will typically show up as syntax errors due to imbalanced curly brackets.
Upvotes: 2