2

As the documentation in Eigen C++ library points out at many places, to get the maximum performance in terms of the computation time, we need to avoid temporary objects as far as possible. In my application, I deals with Dynamic size matrices. I would like to know the creation of temporary matrices in my calculations. Is there any general method to identify the creation of the temporary matrices?

For example,

Eigen::MatrixXf B, C, D;
....some initialization for B, C, D
Eigen::MatrixXf A = B*C+D;

How to check how many temporary matrices are created while realising this operation?

Soo
  • 885
  • 7
  • 26

1 Answers1

3

You can use the plugin mechanism for that. Before including any Eigen headers, define the following:

static long int nb_temporaries;
static long int nb_temporaries_on_assert = -1;
inline void on_temporary_creation(long int size) {
  // here's a great place to set a breakpoint when debugging failures in this test!
  if(size!=0) nb_temporaries++;
  if(nb_temporaries_on_assert>0) assert(nb_temporaries<nb_temporaries_on_assert);
}

#define EIGEN_DENSE_STORAGE_CTOR_PLUGIN { on_temporary_creation(size); }

This mechanism is used in the test-suite at several places, most noticeably in product_notemporary.cpp.

If you are mostly concerned about temporary memory allocations, you can also check for that, by compiling with -DEIGEN_RUNTIME_NO_MALLOC and allow/disallow dynamic allocations with Eigen::internal::set_is_malloc_allowed(bool);

chtz
  • 17,329
  • 4
  • 26
  • 56