【发布时间】:2011-03-01 07:28:07
【问题描述】:
int n, j, i, i2, i3, rank, size, rowChunk, **cells, **cellChunk;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if(!rank){
printf("\nEnter board size:\n");
fflush(stdout);
scanf("%d", &n);
printf("\nEnter the total iterations to play:\n");
fflush(stdout);
scanf("%d", &j);
srand(3);
rowChunk = n/size; //how many rows each process will get
for(i=1; i<size; i++){
MPI_Send(&n,1, MPI_INT, i, 0, MPI_COMM_WORLD);
MPI_Send(&j,1, MPI_INT, i, 7, MPI_COMM_WORLD);
}
cells = (int**) malloc(n*sizeof(int*)); //create main 2D array
for(i=0; i<n; i++){
cells[i] = (int*) malloc(n*sizeof(int));
}
for(i=0; i<n; i++){
for(i2=0; i2<n; i2++){ //fill array with random data
cells[i][i2] = rand() % 2;
}
}
for(i=1; i<size; i++){ //send blocks of rows to each process
for(i2=0; i2<rowChunk; i2++){ //this works for all n
MPI_Send(cells[i2+(rowChunk*i)], n, MPI_INT, i, i2, MPI_COMM_WORLD);
}
}
cellChunk = (int**) malloc(rowChunk*sizeof(int*));
for(i=0; i<rowChunk; i++){ //declare 2D array for process zero's array chunk
cellChunk[i] = (int*) malloc(n*sizeof(int));
}
for(i=0; i<rowChunk; i++){ //give process zero it's proper chunk of the array
for(i2=0; i2<n; i2++){
cellChunk[i][i2] = cells[i][i2];
}
}
for(i3=1; i3<=j; i3++){
MPI_Send(cellChunk[0], n, MPI_INT, size-1,1,MPI_COMM_WORLD); //Hangs here if n >256
MPI_Send(cellChunk[rowChunk-1], n, MPI_INT, 1,2,MPI_COMM_WORLD); //also hangs if n > 256
... //Leaving out code that works
如果 n(数组大小)小于或等于 256,则此代码可以完美运行。任何更大,它都会挂在第一个 MPI_Send 上。此外,当将数组行块发送给其他进程时,(首先 MPI_Send)其他进程完美接收它们的数据,即使 n > 256。如果缓冲区大小超过 256,会导致这个 MPI_Send 挂起?
【问题讨论】:
-
你能发布完整的代码吗?我没有看到您发布的 sn-p 有任何问题。