C ++使用MPI Conway的生命游戏

时间:2015-12-31 01:19:21

标签: c++ parallel-processing mpi

有人可以帮我修复使用MPI的源代码吗?我对c ++并行编程的这个领域并不熟悉,我需要一些关于我需要添加或改变的建议。 我试图自己做,但它显示了很多错误,如下面的那些:=> http://i.imgur.com/JfWNHBA.jpg 以下是Conway游戏的序列我的代码=> http://codepad.org/V9i6oyQy

#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "math.h" 
#include <memory.h>
#include "mpi.h" 

#define DIM 7 
#define FILENAME "life.dat"

void print_matrix(double Anew[DIM+2][DIM+2])    {
int i,j;

for(i=0;i<=DIM+1;i++)   {
    for(j=0;j<=DIM+1;j++) {
        printf("%4.2F    ",Anew[i][j]);
    }
    printf("\n");
}
printf("....................................\n");
}



int main(int argc, char **argv) {
char * sFilename;
FILE *fid;
double Anew[100][100];
double A[100][100];
register int i,j;
int ok=0;
double up,down,left,right,upleft,downright,upright,downleft;
int iterations=0;
int rank, dim;
MPI_Status status;

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

          /* Open the file */
     MPI_File_open (MPI_COMM_WORLD, FILENAME, MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &myfile);
  /* Set the file view */
  MPI_File_set_view(myfile, MPI_INT, MPI_INT,"life.dat", MPI_INFO_NULL);
     /* Write buf to the file */
       MPI_File_write(myfile, MPI_INT, MPI_STATUS_IGNORE);
           /* Close the file */

/*Initialization*/
for  (i=0; i<=DIM+1; i++)   {
        for (j=0; j<=DIM+1; j++) {
            A[i][j]=1;
            Anew[i][j]=1;
    }
    Anew[i][0]=-1;
    Anew[i][DIM+1]=-1;
    A[i][0]=-1;
    A[i][DIM+1]=-1;
}

/*Find how much matrix rows are assigned to each process. */
int first_line=0, last_line=0;
double lines_per_proc_tmp = (double)(DIM+2)/dim ;
int lines_per_proc;
if( (double)((int)lines_per_proc_tmp)== lines_per_proc_tmp) {
    lines_per_proc=(int)lines_per_proc_tmp;
} else {
    lines_per_proc=(int)lines_per_proc_tmp+1;
}

/*First and last row for each process. */
first_line = lines_per_proc*rank;
last_line = first_line + lines_per_proc-1;

    /*For the last process adjust last line so it
    does not fall outside the matrix. */

    if (last_line > DIM); {
        last_line = DIM;
    }
    /*For the first process remove first line.
    (code optimization)*/
    if (first_line == 0)    {
        first_line = 1;
    }

    while(!ok)  {
        iterations++;

        /*Inform next process for my last line . */
        if (rank<dim-1) {
            MPI_Send(A[last_line], DIM+2, MPI_DOUBLE, rank+1, 1, MPI_COMM_WORLD);
        }
        /*be informed from the prev process for my pre first line. */
        if (rank > 0) { 
            MPI_Recv(A[first_line-1],DIM+2, MPI_DOUBLE, rank-1, 1, MPI_COMM_WORLD,&status);
        }
            /*inform previous process for my list line. */
            if (rank > 0) {
                MPI_Send(A[first_line], DIM+2, MPI_DOUBLE, rank-1, 2, MPI_COMM_WORLD);
            }
                /*Be informed from next process for my after-last line. */
                if (rank > dim-1) {
                    MPI_Recv(A[last_line+1], DIM+2, MPI_DOUBLE, rank+1, 2, MPI_COMM_WORLD,&status);
                }

                for (i=first_line; i<=last_line; i++){ /*Compute the elements in this process's part
                                                       from first_line to last_line. */
                    for(j=1; j<DIM; j++) {                 /*For all columns*/
                        up = A[i-1][j];
                        down = A[i+1][j];
                        left = A[i][j-1];
                        right = A[i][j+1];
                        upleft = A[i-1][j] + A[i][j-1];
                        downright = A[i+1][j] + A[i][j+1];  
                        upright = A[i-1][j] + A[i][j+1];
                        downleft = A[i+1][j] + A[i][j-1];
                        Anew[i][j] = (up + down + left + right + upleft + downright + upright + downleft)/4.0;
                    }
                }

                /*Process 0 decides termination. */ 
                if (rank==0) {
                    if (iterations>100) {
                        ok=1;
                    }           
                }   

                /*Process 0 broadcast ok. */    
            MPI_Bcast(&ok, 1, MPI_INT, 0, MPI_COMM_WORLD);

                /*Copy Anew to A. */
                memcpy(A, Anew, (dimof(double)*(DIM+2)*(DIM+2)));
            }   
            /*In the end all processes send their part to PO*/      
            //collect results
            if (rank==0) {
                for (i=1;i<dim;i++) {
                    MPI_Recv(Anew[i*lines_per_proc], (DIM+2)*lines_per_proc,         MPI_DOUBLE, i, 10, MPI_COMM_WORLD,&status);
                }
            } else {
        MPI_Send(Anew[first_line], (DIM+2)*lines_per_proc, MPI_DOUBLE, 0, 10, MPI_COMM_WORLD);
    }
    /*Show result from process 0*/  
    if(rank==0) {
        print_matrix(Anew);
        printf("iterations=%d\n" ,iterations);
        system("pause");
    }
    MPI_Finalize();

}

1 个答案:

答案 0 :(得分:1)

我会尽力帮助你,我只是在阅读错误:

  • 我认为您要使用MPI_Comm_size代替MPI_Comm_dim而不存在。
  • 您错过了MPI_File_set_view的参数,请参阅documentation以便正确使用。
  • 您缺少MPI_File_write的两个参数,请参阅documentation正确使用
  • 修正您的警告,因为以下行中的额外;if (last_line > DIM); { last_line = DIM; }有臭......
  • ;行开始,您遗漏了一些downright = A[i+1][j] + A[i][j+1]
  • dimof我怀疑您确实在size之前搜索并替换dim您是否从同学中复制了此代码;)?
  • print_matrix:在尝试制作更复杂的MPI程序之前,先了解如何在C中正确编码和调试(这不是C ++)。