【问题标题】:NVCC compiles the Eigen library, and the resize of MatrixXd in the structure at runtime failsNVCC编译Eigen库,运行时结构中MatrixXd的resize失败
【发布时间】:2017-06-05 05:57:14
【问题描述】:

我在 ubuntu 16.04 LTS 中使用带有 g++ 5.4 和 CUDA 8.0 的 Eigen library version 3.3

在编写代码时发生了令人困惑的事情。 当我尝试在结构中调整 Eigen::MatrixXd 大小时发生崩溃

结构如下。

struct cudaCopy{
      struct s_path_info *nodes_parents
      struct s_path_info *nodes_children
      ...
}

s_path_info结构如下。

struct s_path_info{
    Eigen::MatrixXd supps;
    Eigen::MatrixXd residu;
    ...

问题如下。

struct cudaCopy *mem;
mem = (struct cudaCopy*)malloc(sizeof(struct cudaCopy));

mem->nodes_parents = (struct s_path_info*)malloc(50 * sizeof(struct s_path_info))
for(int i=0; i<50; i++){
    mem->nodes_parents[i].supps.resize(1, 1); // ERROR

这是 GDB 执行代码的回溯。

#0  __GI___libc_free (mem=0x1) at malloc.c:2949
#1  0x000000000040a538 in Eigen::internal::aligned_free (ptr=0x1) at ../Eigen/Eigen/src/Core/util/Memory.h:177
#2  0x000000000040f3e8 in Eigen::internal::conditional_aligned_free<true> (ptr=0x1) at ../Eigen/Eigen/src/Core/util/Memory.h:230
#3  0x000000000040cdd4 in Eigen::internal::conditional_aligned_delete_auto<double, true> (ptr=0x1, size=7209728) at ../Eigen/Eigen/src/Core/util/Memory.h:416
#4  0x000000000040d085 in Eigen::DenseStorage<double, -1, -1, -1, 0>::resize (this=0x6e1348, size=20, rows=20, cols=1) at ../Eigen/Eigen/src/Core/DenseStorage.h:406
#5  0x000000000040ba9e in Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >::resize (this=0x6e1348, rows=20, cols=1) at ../Eigen/Eigen/src/Core/PlainObjectBase.h:293
#6  0x000000000040697f in search::islsp_EstMMP_BF_reuse (this=0x7fffffffe4b0) at exam.cu:870
#7  0x0000000000404f33 in main () at exam.cu:422

有趣的是,以下效果很好。

mem->nodes_children = (struct s_path_info*)malloc(10*50*sizeof(struct s_path_info))
for(int i=0; i<10*50; i++){
    mem->nodes_children[i].supps.resize(1, 1); // OK   

我不明白为什么nodes_parents 不能调整大小。

我将不胜感激任何有关此事的 cmets。谢谢。

【问题讨论】:

  • 欢迎来到 Stack Overflow。请花时间阅读The Tour 并参考Help Center 中的材料,您可以在这里问什么以及如何问。

标签: c++ gdb g++ eigen nvcc


【解决方案1】:

您将nodes_parents 设置为未初始化的内存,这意味着永远不会调用s_path_info(及其成员)的构造函数。 而不是struct s_path_info *nodes_parents,你应该简单地写:

std::vector<s_path_info> nodes_parents;

而你的主要cudaCopy 应该只是一个堆栈变量:

cudaCopy mem;

一般情况下,不要在 C++ 中使用 malloc,尤其是对于非 POD。

注意:它与 nodes_children 一起工作的事实可能纯属巧合。

【讨论】:

    猜你喜欢
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2017-11-18
    相关资源
    最近更新 更多