Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPI_Bcast - One row in a 2D array

Tags:

arrays

c

mpi

I am trying to broadcast my first row in a 2D array. Here's the code:

double *chunkPtr = malloc(sizeof(double) * columns);
if (rank == 0) {
    chunkPtr = &chunk[0][0];
}

MPI_Bcast(chunkPtr, columns, MPI_DOUBLE, 0, MPI_COMM_WORLD);

With chunk being a 2D double array (double **chunk) and columns is the number of doubles in one row.

Here's my problem: When I do the broadcast from the process with rank 0 (like the code I provided before), the other processes don't get the broadcasted double array. Instead, they get an array with values equal to 0.0 on every index.

Here's the output that I get when I print the values inside the arrays, when I run my program with 3 processes:

>>>>>>>>RANK IS 0
6.807000 5.249000 0.073000 3.658000 8.930000 1.272000 7.544000 0.878000 1.000000 
>>>>>>>>RANK IS 1
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 
>>>>>>>>RANK IS 2
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

Notice how process 0 has actual values in its array, while process 1 and 2 only have 0 values.

When I change my program to have the process with rank 1 broadcast its row, everything runs correctly:

Code:

if (rank == 1) {
    chunkPtr = &chunk[0][0];
}

MPI_Bcast(chunkPtr, columns, MPI_DOUBLE, 1, MPI_COMM_WORLD);

Output:

>>>>>>>>RANK IS 0
0.979000 9.149000 6.579000 8.821000 1.967000 0.672000 1.393000 9.336000 5.000000 
>>>>>>>>RANK IS 1
0.979000 9.149000 6.579000 8.821000 1.967000 0.672000 1.393000 9.336000 5.000000 
>>>>>>>>RANK IS 2
0.979000 9.149000 6.579000 8.821000 1.967000 0.672000 1.393000 9.336000 5.000000

What am I doing wrong?

like image 417
Nayzer Avatar asked May 21 '26 16:05

Nayzer


1 Answers

Turns out I had extra MP_Bcast in my code that were messing up my whole program.

Solution: Make sure all your bcasts are well setup!

like image 154
Nayzer Avatar answered May 23 '26 05:05

Nayzer