我正在开发一个MPI程序,我的每个进程都有以下代码:
#define CELL(A,X,Y,MX,MY) (*(A+Y*MX+X))
int bs_x = 1;
int bs_y = 1;
int *this_data=calloc((bs_y+2)*(bs_x+2), sizeof(int));
//...
int *recv_top = calloc(bs_x, sizeof(int));
int *recv_left = calloc(bs_x, sizeof(int));
// Now I make some operations and I want to assign
// the value of recv_top and recv_left to this_data
for(i=0; i<bs_x; ++i) CELL(this_data, i+1, 0, bs_x+2, bs_y+2) = recv_top[i]; // Sets both (0,1) and (1,0) to recv_top!!
for(i=0; i<bs_x; ++i) CELL(this_data, 0, i+1, bs_x+2, bs_y+2) = recv_left[i]; // Again sets both (0,1) and (1,0) to recv_left!!
现在当我检查元素的值(1,0)时出现问题,我发现第一个调用也将recv_top的值保存在元素(0,1)中:
for(i=0; i<bs_x; ++i) CELL(this_data, i+1, 0, bs_x+2, bs_y+2) = recv_top[i];
if (rank == 4)
{
printf("Rank 4 value at (%d,0) = %d\n", 1, CELL(this_data,1,0,bs_x+2,bs_y+2));
printf("Rank 4 value at (0,%d) = %d\n", 1, CELL(this_data,0,1,bs_x+2,bs_y+2));
}
//Rank 4 value at (1,0) = 12
//Rank 4 value at (0,1) = 12
接下来:
for(i=0; i<bs_x; ++i) CELL(this_data, 0, i+1, bs_x+2, bs_y+2) = recv_left[i];
if (rank == 4)
{
printf("Rank 4 value at (%d,0) = %d\n", 1, CELL(this_data,1,0,bs_x+2,bs_y+2));
printf("Rank 4 value at (0,%d) = %d\n", 1, CELL(this_data,0,1,bs_x+2,bs_y+2));
}
//Rank 4 value at (1,0) = 1024
//Rank 4 value at (0,1) = 1024
更新(1,0)。
他们不应该这样做,因为他们不一样:
CELL(this_data, 0, i+1, bs_x+2, bs_y+2) = (*(this_data+(i+1)*(bs_x+2))
CELL(this_data, i+1, 0, bs_x+2, bs_y+2) = (*(this_data+i+1))
任何想法我做错了什么?
答案 0 :(得分:3)
代码#define CELL(A,X,Y,MX,MY) (*(A+Y*MX+X))
应为
#define CELL(A,X,Y,MX,MY) ( *( (A) + (Y) * (MX) + (X) ) )
请注意括号!
BTW,为什么不使用内联函数?使用内联函数可以获得更好的代码。
在您的版本中代码
CELL(this_data, 0, i+1, bs_x+2, bs_y+2)
扩展为:
*(this_data+i+1*bs_x+2+bs_y+2)
这似乎与您期望的不同
<强>更新强>
我仍然建议你把它变成一个内联函数!
inline void CELL(int* A, int X, int Y, int MX, int MY, int newValue)
{
int * cell = A + Y * MX + X;
*cell = newValue;
}