7
$\begingroup$

I am trying to replicate Fortran90 array syntax using a C++ library. The libraries themselves are discussed at length in this question. They can all do something like this:

D = alpha*A + beta*B + gamma*C

for vectors A, B, C and scalars alpha, beta, gamma. This could be translated as:

for (i = 0; i < D.length; i++)
  D[i] = alpha*A[i] + beta*B[i] + gamma*C[i]

which traverses each array a single time, or like:

for (i = 0; i < D.length; i++)
  D[i] = alpha*A[i] + beta*B[i]
for (i = 0; i < D.length; i++)
  D[i] += gamma*C[i]

The first way is much better for cache performance, and I assume is what fortran compilers do. Are C++ matrix libraries able to translate compound vector expressions into single loops like fortran, or will they emit multiple simple loops?

$\endgroup$
5
$\begingroup$

This is possible in C++ via expression templates. Section 1.9 of this technical report addresses your question.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.