MPI_Scatterv不能正常工作

时间:2017-01-15 12:14:10

标签: c parallel-processing mpi

我想在4个节点中分散布尔随机数组的行,但只是节点0正常工作,以下是运行的结果,请帮助我。 问候

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;
}

运行的结果是:

0 个答案:

没有答案