Megumi Ai
Megumi Ai

Reputation: 3

Sharing an array of integers in MPI (C++)

I am trying to fake shared memory currently while using the MPI library in C++. I have an array A of size n+1, where n is given from the user, and have processor 0 generate the integers for that array. I need to share the array that processor 0 created with all the other processes. So as a result I Bcast it to the others... However when I go to have each processor print out their array I get a signal 11 (Segmentation Fault) from a processor that isn't zero. If I comment that section out it runs with no problems. I would like to be able to see that my array was sent and stored correctly in all of the processors.

int *A=new int[n+1];
if(my_rank==0)
{
    srand(1251);
    A[0]=0;
    for(int i=1; i<=n; i++){
    A[i]=rand()%100;}
    MPI_Bcast(&A, n+1, MPI_INT, 0, MPI_COMM_WORLD);
}
else {

    MPI_Bcast(&A, n+1, MPI_INT, 0, MPI_COMM_WORLD);

    }

cout<<"My rank is "<<my_rank<<" and this is my array:"<<endl;
for (int i=0; i<=n; i++)
{cout<<A[i]<<" "<<endl;}
cout<<endl;

Upvotes: 0

Views: 907

Answers (1)

Zulan
Zulan

Reputation: 22670

You are incorrectly passing &A as address to MPI_Bcast. This is the address of the pointer, MPI needs the address of the data i.e. A.

MPI_Bcast(A, n+1, MPI_INT, 0, MPI_COMM_WORLD);

Move that code outside of the if/else block. It is the same call for all ranks.

Upvotes: 2

Related Questions