使用 mpic++ 的总线错误但不使用 g++
我有一些用 C++ 编写的代码,在使用 g++ 时编译和运行良好(具有合理的输出),但是当我尝试使用 mpic++ 时,出现运行时总线错误。我已经设法找出总线错误发生的位置,但不知道原因。这是我的代码:
one = (double *) malloc(sizeof(&one) * nx * nx * nt);
two = (double **) malloc(sizeof(&two) * nx * nx);
rho_exp = (double ***) malloc(sizeof(&rho_exp) * nx);
for(i = 0; i < nx * nx; i++)
two[i] = &one[i * nt];
for(i = 0; i < nx; i++)
rho_exp[i] = &two[i * nx];
for(i = 0; i < nx; i++)
for(j = 0; j < nx; j++)
for(k = 0; k < nt; k++)
rho_exp[i][j][k] = 0;
在三个嵌套 for 循环期间发生总线错误。我的问题有两个:一是我认为我搞砸了 3D 矩阵的分配;二是我认为我已经搞砸了 3D 矩阵的分配。我应该怎么做呢?第二,为什么这适用于 gcc 和 g++,但不适用于 mpic++?
I have some code written in C++ that compiles and runs fine (with reasonable output) when using g++, but when I try to use mpic++, I get a runtime bus error. I've managed to figure out where the bus error is occuring, but not why. Here is my code:
one = (double *) malloc(sizeof(&one) * nx * nx * nt);
two = (double **) malloc(sizeof(&two) * nx * nx);
rho_exp = (double ***) malloc(sizeof(&rho_exp) * nx);
for(i = 0; i < nx * nx; i++)
two[i] = &one[i * nt];
for(i = 0; i < nx; i++)
rho_exp[i] = &two[i * nx];
for(i = 0; i < nx; i++)
for(j = 0; j < nx; j++)
for(k = 0; k < nt; k++)
rho_exp[i][j][k] = 0;
The bus error is occurring during the three nested for loops. My question is two-fold: One, I assume, I've screwed up my allocation for the 3D matrix; how should I have done it? Two, why did this work with gcc and g++, but not with mpic++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
跳出来的一件事是,当您可能指的是
sizeof(*foo)
(指向 foo 的指针的大小)时,您正在使用sizeof(&foo)
(指向 foo 的指针的大小) foo 指向的东西)。我敢打赌,mpic++ 正在编译为 32 位目标,其中双精度数的大小为 64 位,地址的大小为 32 位。这会导致不匹配并导致问题。您的 g++ 可能针对 64 位系统,其中这些大小是相同的(都是 64 位)。
编辑:
这是您应该使用的代码:
老实说,您正在重新发明轮子。您应该使用供应商提供的 BLAS 库,该库具有针对您的平台优化的快速矩阵运算(可能比你写的任何东西快数百倍......)。
One thing that jumps out is that you are using
sizeof(&foo)
(size of a pointer to foo) when you probably meansizeof(*foo)
(size of the thing that foo points to).I bet what is happening is that mpic++ is compiling to a 32-bit target, where the size of doubles is 64 bits and the size of addresses is 32 bits. That gives you a mismatch and causes a problem. Your g++ is probably targeting a 64-bit system, where those sizes are the same (both 64 bits).
EDIT:
Here's the code you should be using:
And in all honesty, you are reinventing the wheel. You should be using your vendor-provided BLAS library, which has fast matrix operations optimized for your platform (probably hundreds of times faster than whatever you write...).
下面是动态分配 X x Y x Z 数组的一般过程:没关系,这是针对 C 的;由于您使用的是 C++,因此请如上所述使用
new
。Here's the general procedure for dynamically allocating an X x Y x Z array:Never mind, that's for C; since you're using C++, use
new
as described above.