Matrix发送MPI

时间:2017-06-14 18:51:50

标签: c matrix mpi

将rank = 0的进程发送到其他进程时,我遇到了问题。输出应为:

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

double **A = (double **)malloc(SIZE_N*sizeof(double *));
double **B = (double **)malloc(SIZE_N*sizeof(double *));
double **C = (double **)malloc(SIZE_N*sizeof(double *)); 

int g = 0;

for (g = 0; g < SIZE_N; g++) {
    A[g] = (double *)malloc(SIZE_N * sizeof(double));
}

int g1 = 0;

for (g1 = 0; g1 < SIZE_N; g1++) {
    B[g1] = (double *)malloc(SIZE_N * sizeof(double));
}

int g2 = 0;

for (g2 = 0; g2 < SIZE_N; g2++) {
    C[g2] = (double *)malloc(SIZE_N * sizeof(double));
}

for (i = 0; i < SIZE_N; i++) {
    for (j = 0; j < SIZE_N; j++) {
        *(*(A+i)+j) = i+2;
        *(*(B+i)+j) = i+j+3;
    }
}

if (rank == 0) {
    start_time = MPI_Wtime();
    for (i = 1; i < size; i++) {
        offset = SIZE_N/(size-1);
        bottom = (i-1)*offset;

        // (i+1)==size indica l'ultimo nodo
        //se sei all'ultimo nodo e se ci sono righe in eccesso da elaborare
            //assegnale tutte all'ultimo nodo
        if((i+1)==size && SIZE_N%(size-1)!=0){
            top = SIZE_N;
        } 
        else{
            top=bottom+offset;
        }
        MPI_Send(&bottom,1,MPI_INT,i,1,MPI_COMM_WORLD);
        MPI_Send(&top,1,MPI_INT,i,2,MPI_COMM_WORLD);
    }
    i = 0;
    for (i = 1; i < size; i++) {
        MPI_Recv(&bottom,1,MPI_INT,i,4,MPI_COMM_WORLD,&status);
        MPI_Recv(&top,1,MPI_INT,i,5,MPI_COMM_WORLD,&status);
        MPI_Recv(&C[bottom][0],(top-bottom)*SIZE_N,MPI_DOUBLE,i,6,MPI_COMM_WORLD,&status);
        MPI_Recv(&weak_time,1,MPI_DOUBLE,i,7,MPI_COMM_WORLD,&status);
        last_weak += weak_time;
        //printf("Weak_time process %d> %f\nLast_weak> %f\n",i,weak_time,last_weak);
    }
    end_time = MPI_Wtime();

    i = 0;
    for (i = 0; i < SIZE_N; i++) {
        for (j = 0; j < SIZE_N; j++){
            printf("[%.0f]", C[i][j]);
        }
        printf("\n");
    }
    printf("\n\nTempo di calcolo compreso di stampa = %f\n\n",(end_time-start_time));
    fflush(stdout);
}

if (rank > 0) {
    double start,end,time_result;
    start = MPI_Wtime();

    MPI_Recv(&bottom,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
    MPI_Recv(&top,1,MPI_INT,0,2,MPI_COMM_WORLD,&status);

    for (i = bottom; i < top; i++) {
        for (j = 0; j < SIZE_N; j++) {
            for (k = 0; k < SIZE_N; k++) {
                C[i][j] = C[i][j] + (A[i][k] * B[k][j]);
            }
        }
    }
    MPI_Send(&bottom,1,MPI_INT,0,4,MPI_COMM_WORLD);
    MPI_Send(&top,1,MPI_INT,0,5,MPI_COMM_WORLD);
    MPI_Send(&C[bottom][0],(top-bottom)*SIZE_N,MPI_DOUBLE,0,6,MPI_COMM_WORLD);

    end = MPI_Wtime();
    time_result = end-start;
    MPI_Send(&time_result,1,MPI_DOUBLE,0,7,MPI_COMM_WORLD);
}

MPI_Finalize();
return 0;}`



但它返回:

{{1}}
你能帮帮我吗?

{{1}}

0 个答案:

没有答案