Blitz logo

Blitz Support :

From: Sam Kaplan (skaplan_at_[hidden])
Date: 2003-04-11 16:00:11


Hello,

I have a question concerning sum(...) vs. looping for a matrix-matrix
product. It appears that looping is faster.

In particular, please consider the following two code fragments:

1) using blitz++, sum(...):

...

Array<double,2> A(m,m);
Array<double,2> B(m,m);
Array<double,2> C(m,m);

...

firstIndex i;
secondIndex j;
thirdIndex k;

C = sum(A(i,k)*B(k,j),k);

...

2) looping:

...

Array<double,2> A(m,m);
Array<double,2> B(m,m);
Array<double,2> C(m,m);

...

int i,j,k;

for (i = 0; i < m; i++){
  for (j = 0; j < m; j++){
    for (k = 0; k < m; k++){
      C(i,j) += A(i,k)*B(k,j);
    }
  }
}

...

I've attached some code for benchmarking these matrix-matrix
multiplications. One result is:

with m = 1000:

blitz with sum(...): 676.89 seconds
blitz with looping: 554.84 seconds
C style arrays: 554.57 seconds

The code was compiled with: g++ -ftemplate-depth-30 -O3

I quite like the condensed notation using sum(...). However, the speed
issue is bothersome. Have other people noticed this. If so, how do you
deal with this problem?

Thanks.

Sam