信号6退出流程(已中止)

时间:2017-11-26 07:20:12

标签: c mpi

我编写代码来获取矩阵并按块进行分发。 块大小可能并不总是相等。这些块工作正常,但是当我尝试运行它并且进程计数设置为3(例如)时,我收到错误,'mpirun注意到节点簇上的PID 8676的进程等级2退出信号6(已中止)。请查看附带的代码。我认为问题在于免费功能。

#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
#define COLUMN 4
#define ROW 10
#define  dp 100.0f

// Local start
#define chunk_low(commrank, commsize, nvert) \
    ((commrank) * (nvert) / (commsize))

// Local end
#define chunk_height(commrank, commsize, nvert) \
    (chunk_low((commrank) + 1, commsize, nvert) - 1)

// Local size
#define chunk_size(commrank, commsize, nvert) \
    (chunk_height(commrank, commsize, nvert) - \
    chunk_low(commrank, commsize, nvert) + 1)

// Matrix initialization function
void init_matrix(int column, int row, float *matrix)
{
    int j, i;
    printf("\nMatrix\n");
    for(i=0; i < row; i++){
        for(j=0; j < column; j++){
            matrix[i*column+j] = i * column + j; // (float)rand()/RAND_MAX * dp *2.0f - dp;
            printf(" %f ", matrix[i * column + j]);
        }
        printf("\n");
    }
    printf("\n");
}

int main(int argc, char **argv)
{
    int rank, size;
    int i, j;
    float *vm, *local_matrix;
    double time1, time2;
    int *displs, *rcounts, *scounts;

    vm = (float *)calloc(ROW * COLUMN, sizeof(float));

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

    /* Process 0 - master  */
    if (rank==0)
    {

        printf("\nNumbers of proccesses %d. \nElements in vector %d.\n", size, COLUMN);

        /* Init vector vA  */
        init_matrix(COLUMN, ROW, vm);

        //Time begining calculating of programm
        time1=MPI_Wtime();

    }
    /* End of work process 0  */

    displs = (int *)malloc(sizeof(int) * size);
    scounts = (int *)malloc(sizeof(int) * size);
    rcounts = (int *)malloc(sizeof(int) * size);

    for (i = 0; i < size; i++) {
        displs[i] = chunk_low(i, size, ROW) * COLUMN; // Position initialization
        rcounts[i] = scounts[i] = chunk_size(i, size, ROW) * COLUMN;
        printf("\ndispls[%d]=%d, scounts[%d]=%d\n",i , displs[i], i, scounts[i]);
    }

    local_matrix  = (float *)calloc(chunk_size(i, size, ROW) * COLUMN, sizeof(float));

    MPI_Scatterv(vm, scounts, displs, MPI_FLOAT, local_matrix,
                 rcounts[rank], MPI_FLOAT, 0, MPI_COMM_WORLD);


    printf("\nProcess=%d Displs=%d rcounts=%d\n", rank, displs[rank], rcounts[rank]);
    printf("Local Matrix\n");
    for(i=0; i < scounts[rank]; i++){
        printf(" %f ", local_matrix[i]);
        if (scounts[rank] % ROW == 0) {
            printf("%n");
        }
    }
    printf("\n");

    /* Only master-process  */
    if (rank==0)
    {
        //Time ending programm
        time2=MPI_Wtime();
        printf("\nTime parallel calculation = %f s.\n",time2-time1);
    }
    // End work of master-process

    /* Delete storage arrays of process */
    free(displs);
    free(scounts);
    free(rcounts);
    free(local_matrix);

    MPI_Finalize();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您似乎错误地分配了local_matrix

local_matrix  = (float *)calloc(chunk_size(i, size, ROW) * COLUMN, sizeof(float));

我认为你的意思是

local_matrix  = (float *)calloc(chunk_size(rank, size, ROW) * COLUMN, sizeof(float));