我目前正在尝试编写一个非常简单的程序,但似乎无法阻止seg-faulting。这是我的计划:
#include <stdio.h>
#include <mpi.h>
#include <time.h>
#include <cstdlib>
int main ( int argc, char **argv )
{
int my_id, num_procs;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
int r1, r2;
srand(time(NULL) * my_id);
for(int i = 0; i < 2; i++)
{
if(i == 0)
{
r1 = rand() % 100;
}
else{
r2 = rand() % 100;
}
}
printf("\nProcess #%i has numbers %i and %i \n",my_id,r1,r2);
printf("\n");
int sendArray[2];
//Put the nubers in sendArray
sendArray[0] = r1;
sendArray[1] = r2;
int size = 2 * num_procs;
int rbuf[size];
for(int i = 0;i < size;i++)
{
rbuf[i] = 0;
}
//rbuf = (int *)malloc(gsize*100*sizeof(int));
//Use MPI_Allgather
MPI_Allgather( sendArray, 2, MPI_INT, rbuf, size,MPI_INT,MPI_COMM_WORLD ); //segfault HERE, must be using parameters incorrectly
if(my_id == 0){
printf("-------------------------------------\n");
}
//printf("\nProcess #%i has number %i in pos0\n",my_id,rbuf[0]);
//printf("\nProcess #%i has number %i in pos1\n",my_id,rbuf[1]);
//printf("\nProcess #%i has number %i in pos2\n",my_id,rbuf[2]);
//printf("\nProcess #%i has number %i in pos3\n",my_id,rbuf[3]);
MPI_Finalize();
}
这个想法非常基础。在每个进程中创建两个随机数,将它们存储在发送缓冲区中,在我的情况下 - 它是sendArray
,然后调用MPI_Allgather将所有数字传播到每个进程,我试图将数字发送到接收缓冲区名为rbuf
。我不确定我在这里做错了什么,但这段代码肯定是我称之为MPI_Allgather
的段错误。