0

I want to scatter rows of an boolean random array among 4 nodes but just node 0 work properly ,The following is the result of the run, please help me. regards

int main(int argc, char *argv[]) {


    int size, rank, divided_pop_size, sum = 0, root = 0,procgridsize;
    const int num_vertices = 3;
    int **indivisual, **recbuf;
    int *sendcounts;     //specifying the number of elements to send to each processor
    int *displs;         //Entry i specifies the displacement   
    MPI_Status status;
    int offset,rows;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    divided_pop_size = (n_initial_pop / size)*num_vertices;
    rows = n_initial_pop / size;
    //~~~~~~~~~~~~~~~~~~~~~~~decleration of receive buffer
    recbuf = new int*[n_initial_pop / size];

    for (int i = 0; i < n_initial_pop / size; i++)
        recbuf[i] = new int[num_vertices]; 


    if (rank == root)
    {

        indivisual = new int*[n_initial_pop];

        for (int i = 0; i < n_initial_pop; i++)
            indivisual[i] = new int[num_vertices];

        for (int i = 0; i < n_initial_pop; i++)
        {
            for (int j = 0; j < num_vertices; j++)
            {
                indivisual[i][j] = rand() % 2;
            }
        }
        printf("indivisual array is:\n");
        for (int i = 0; i < n_initial_pop; i++)
        {
            printf("\n");
            for (int j = 0; j < num_vertices; j++)
                printf("  %d", indivisual[i][j]);
        }
    }

    sendcounts = (int*)malloc(sizeof(int)*size);
    displs = (int*)malloc(sizeof(int)*size);

    if (rank == 0) {
        for (int i = 0; i<size; i++) {
            sendcounts[i] = divided_pop_size;

            displs[i] = sum;
            sum += sendcounts[i];

        }
    }
    if (rank == 0) {
        for (int i = 0; i < size; i++) {
            printf("sendcounts[%d] = %d\tdispls[%d] = %d\n", i, sendcounts[i], i, displs[i]);
        }
    }
    MPI_Scatterv(indivisual, sendcounts, displs, MPI_INT, recbuf,
        sendcounts[rank], MPI_INT,
        0, MPI_COMM_WORLD);

    for (int p = 0; p<size; p++) {
        //printf("\nrank : %d", rank);
        if (rank == p) {
            printf("Local process on rank %d is:\n", rank);
            for (int i = 0; i<n_initial_pop / size; i++) {
                putchar('|');
                for (int j = 0; j<num_vertices; j++) {
                    printf("%d ", recbuf[i][j]);
                }
                printf("|\n");
            }
        }
        MPI_Barrier(MPI_COMM_WORLD);
    }



    MPI_Finalize();

    return 0;
}

The result of run is:

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
sami
  • 7
  • 1
  • Welcome to Stackoverflow! There is another way to allocate the 2D array `indivisual` which eases the use of `MPI_Scatterv()`. Indeed, in the code you posted, the array is not contiguous in memory since rows are allocated one by one. It make the computation of the displacement less obvious... See http://stackoverflow.com/questions/25628321/dynamic-memory-allocation-in-mpi/25629279#25629279 or http://stackoverflow.com/questions/5901476/sending-and-receiving-2d-array-over-mpi – francis Jan 15 '17 at 14:36
  • Thank you, My problem was solved. – sami Jan 19 '17 at 09:43

0 Answers0