MPI,流程结束没有输出?

时间:2014-12-03 13:29:16

标签: mpi

我有一个非常奇怪的问题。我在mpi中写了一个con,一个进程应该打印一些东西,但令人惊讶的是代码终止而没有任何输出。我无法理解它的错误......

PS:这段代码应该乘以两个矩阵。

int main( int argc, char *argv[] )
{
    int M = atoi(argv[1]);
    // N = 2 ^ M
    N = (unsigned int) pow (2.0, M); //you need to modify this code!
    int my_rank, comm_sz,mt;
    int rows,offset,extra,averow ,dest;
    int i,j,k;
    srand(time(0));

time_t t1, t2;
double dt; //t2-t1
double tavg=0.0;


//input array
A = (double*) malloc ( sizeof(double) * N * N );
B = (double*) malloc ( sizeof(double) * N * N );
C = (double*) malloc ( sizeof(double) * N * N );

//int r; for (r = 0; r < REP; r++)
//{
    //fill in matrix A and B with random numbers

    //t1
        //t1 = time(0);
            MPI_Init(&argc, &argv);
            MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
            MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

        if (my_rank =0){
             printf("mpi_mm has started with %d tasks.\n",comm_sz);
             printf("Initializing arrays...\n");
        fillmatrix(A,N);
        fillmatrix(B,N);
        averow = N/comm_sz;
        extra = N%comm_sz;
        offset = 0;
        mt = 0;
        MPI_Bcast(B,N*N,MPI_DOUBLE,0,MPI_COMM_WORLD);
        for ( dest=1;dest<=comm_sz;dest++){
        rows = (dest <=extra) ? averow+1 : averow;
        MPI_Send(&offset,1,MPI_INT,dest,mt,MPI_COMM_WORLD);
        MPI_Send(&rows,1,MPI_INT,dest,mt,MPI_COMM_WORLD);
        MPI_Send(&A[offset*N],rows*N,MPI_DOUBLE,dest,mt,MPI_COMM_WORLD);
        offset=offset+rows;}

        mt = 1;
        for (i=1; i<=comm_sz; i++){

     MPI_Recv(&offset, 1, MPI_INT, i, mt, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
     MPI_Recv(&rows, 1, MPI_INT, i, mt, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
     MPI_Recv(&C[offset*N], rows*N, MPI_DOUBLE, i, mt, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    printf("Received results from task %d\n",i);
    }

          /* Print results */
  printf("******************************************************\n");
  printf("Result Matrix:\n");
  for (i=0; i<N; i++)
  {
     printf("\n"); 
     for (j=0; j<N; j++) 
        printf("%6.2f   ", C[i*N+j]);
  }
  printf("\n******************************************************\n");
  printf ("Done.\n");


        }

        if(my_rank !=0){
        MPI_Bcast(B,N*N,MPI_DOUBLE,0,MPI_COMM_WORLD);
        mt = 0;
        MPI_Recv(&offset,1,MPI_INT,0,mt,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        MPI_Recv(&rows,1,MPI_INT,0,mt,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        MPI_Recv(&A,rows*N,MPI_DOUBLE,0,mt,MPI_COMM_WORLD,MPI_STATUS_IGNORE);

         for(i=0;i<N;i++)
            for(j=0;j<rows;j++){
                C[j*N+i] =0.0;
                for(k=0;k<N;k++)
                    C[j*N+i] += A[j*N+k]*B[k*N+i];
                    }
        mt = 1;
        MPI_Send(&offset, 1, MPI_INT, 0, mt, MPI_COMM_WORLD);
        MPI_Send(&rows, 1, MPI_INT, 0, mt, MPI_COMM_WORLD);
        MPI_Send(&C, rows*N, MPI_DOUBLE, 0, mt, MPI_COMM_WORLD);
        }

    MPI_Finalize();

1 个答案:

答案 0 :(得分:0)

找到它。

你说

if (my_rank =0)

这应该是

if (my_rank == 0)

- )