7

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?

Max Hutchinson
  • 3,051
  • 16
  • 29

1 Answers1

5

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

Juan M. Bello-Rivas
  • 3,994
  • 16
  • 24