I am trying to implement the Strassen algorithm in C++. I want to partition the square matrix 'hA' into 4 equal blocks.
// Initialize matrices on the host
float hA[N][N],ha11[N / 2][N / 2], ha12[N / 2][N / 2], ha21[N / 2][N / 2],
ha22[N / 2][N / 2];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
hA[i][j] = i;
//hB[i][j] = i;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("\n%d,%d\n", i, j);
if (i < N / 2 & j < N / 2) {
ha11[i][j] = hA[i][j];
} else if (i < N / 2 & j >= N / 2) {
ha12[i][j] = hA[i][j];
} else if (i >= N / 2 & j < N / 2) {
ha21[i][j] = hA[i][j];
} else if (i >= N / 2 & j >= N / 2) {
ha22[i][j] = hA[i][j]; //faulty!
}
}
}
I used above method for partitioning and it gets faulty as shown in the output below. But it works fine when I remove the last comparison in the 'if else' ladder.
Why does 'i' have a wrong value that is even outside the loop condition? Is there a more convenient way to do the partitioning than this way?
Your arrays should be N x N, not N/2 x N/2.
Your use of the bitwise operator & is unusual but works. I mistakenly thought you need a logical and ( && ) instead. Still, for readability I'd suggest the &&. You get the short circuiting with too.
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("\n%d,%d\n", i, j);
if (i < N / 2 & j < N / 2) {
ha11[i][j] = hA[i][j];
} else if (i < N / 2 & j >= N / 2) {
ha12[i][j] = hA[i][j];
} else if (i >= N / 2 & j < N / 2) {
ha21[i][j] = hA[i][j];
} else if (i >= N / 2 & j >= N / 2) {
ha22[i][j] = hA[i][j]; //faulty!
}
}
}