Identifying temporary object creation in Eigen

半腔热情 提交于 2021-02-19 06:51:42

问题


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?


回答1:


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);



来源:https://stackoverflow.com/questions/52918480/identifying-temporary-object-creation-in-eigen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!