JackSparrow
JackSparrow

Reputation: 707

Sending column through MPI_Bcast

I'm solving a problem in which one process do some calculation on a column and I'm saving that column in a temporary array and broadcasting it to all other processes using MPI_Bcast but always endings up in getting all zeros in the array in the receiving end.

Below is my code that I'm using:

//nProc = no of processors;
//helperA = 2D Array i.e. helperA[size][size]
// colK = 1D Array i.e. colK[size]

for (int k = 0; k < size; ++k) {
    if (k % nProc == rank) {
                    // One of the process will do this calculation
        int temp = 0;
        for (int j = k + 1; j < size; ++j) {
            helperA[j][k] = helperA[j][k]/helperA[k][k];
            colK[temp++] = helperA[j][k];
        }
    }

    MPI_Bcast(colK, size - k - 1, MPI_DOUBLE, rank, MPI_COMM_WORLD);    
    // After this other process should get the colK updated with the calculation

    for (int i = k + 1; i < size; ++i) {
        if (i % nProc == rank) {
            int temp = 0;
            for (int j = k + 1; j < size; ++j) {
                                    // Here colK is always zero
                printf("%d %f \n", rank, colK[temp]);
                helperA[j][i] = helperA[j][i] - (colK[temp++] * helperA[k][i]);
            }
        }
    }
}

I'm not sure what I am doing wrong here. Any help/suggestions please.

Upvotes: 0

Views: 400

Answers (1)

Wesley Bland
Wesley Bland

Reputation: 9062

(I really don't love debugging code via text boxes, so I'll assume you've already done the basic checks to be sure that there's actually data in colK before you send it and that size-k-1 is the correct value on all ranks.)

The rank value you're passing into the MPI_BCAST call should be the rank of the "root" process. That is, the process which has all of the data to be broadcast. This argument must be the same for all calling processes. You code appears to pass the rank of each calling process, so everyone will be trying to communicate with a different root process.

Upvotes: 1

Related Questions