I'm trying to work with 2D arrays and MPI_Scatterv. When I call MPI_Scatterv I get
================================================================================
= BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
= PID 5790 RUNNING AT ubuntu
= EXIT CODE: 139
= CLEANING UP REMAINING PROCESSES
= YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
================================================================================
YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11)
This typically refers to a problem with your application.
Please see the FAQ page for debugging suggestions
If I use C99 2D arrays it works, but not with malloc. I want to know where I'm wrong with malloc. I can't use linearized 2D array, so I can't create array like array[i*columns+j]
Here is a test program:
int **alloc2d(int n, int m) {
int i;
int **array = malloc(n * sizeof(int*));
array[0] = malloc(n * m * sizeof(int));
for(i = 1; i < n; i++)
array[i] = array[i-1] + m;
return array;
}
int *genSendc(int dim, int numprocs) {
int* sendc = (int*)malloc(sizeof(int)*numprocs);
int i;
int subsize = dim/numprocs;
for(i=0; i<numprocs; ++i)
sendc[i] = subsize;
for(i=0; i<dim-subsize*numprocs; ++i)
sendc[i]+=1;
return sendc;
}
int *genDispl(int numprocs, int*sendc) {
int* displ = (int*)malloc(sizeof(int)*numprocs);
int i;
displ[0]=0;
for(i=1; i<numprocs; ++i)
displ[i] = displ[i-1]+sendc[i-1];
return displ;
}
int main(int argc, char *argv[]){
int numprocs, rank, i, j, N=5, M=4;
int* displMat, *sendcMat;
int **txMatrix, **rxMatrix;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
sendcMat = genSendc(N, numprocs);
for(i=0; i<numprocs; ++i)
sendcMat[i] *= M;
displMat = genDispl(numprocs, sendcMat);
rxMatrix = alloc2d(sendcMat[rank]/M, M);
if (rank == 0) {
srand(time(NULL));
txMatrix = alloc2d(N, M);
for (i=0; i < N; ++i)
for(j=0; j < M; ++j)
txMatrix [i][j] = (rand() % 10)+1;
}
MPI_Scatterv(&txMatrix[0][0], sendcMat, displMat, MPI_INT, &rxMatrix[0][0], sendcMat[rank], MPI_INT, 0, MPI_COMM_WORLD);
MPI_Finalize();
}
If I print rxMatrix
after MPI_Scatterv
, the program prints Rank0 sub-matrix and then it crashes with segmentation fault. Where am I wrong?
This expression invokes undefined behavior if txMatrix
is not properly initialized.
&txMatrix[0][0]
While the first argument to MPI_Scatterv
is inconsequential on non-root ranks*, just evaluating the expression can cause a segfault. Just use an if/else for root/nonroot and pass NULL
for the latter.
*: at least per the standard, I've seen this be bugged in MPI implementations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.