识别本征中的临时对象创建 [英] Identifying temporary object creation in Eigen

查看:72
本文介绍了识别本征中的临时对象创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如Eigen C ++库中的文档指出的那样,要在计算时间方面获得最大性能,我们需要尽可能避免使用临时对象.在我的应用程序中,我处理动态大小矩阵.我想知道在计算中创建临时矩阵.有什么通用方法可以识别临时矩阵的创建吗?

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?

例如

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?

推荐答案

您可以为此使用插件机制.在包含任何本征标题之前,请定义以下内容:

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.

如果您最关心临时内存分配,还可以通过使用 -DEIGEN_RUNTIME_NO_MALLOC 进行编译并使用 Eigen :: internal :: set_is_malloc_allowed(布尔);

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

这篇关于识别本征中的临时对象创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆