C++使用MPI Conway's Game of Life

C++ using MPI Conway's Game of Life

本文关键字:Game of Life 使用 MPI Conway C++      更新时间:2023-10-16

有人能帮我修复这个使用MPI的源代码吗?我不太熟悉c++并行编程的这一领域,我需要一些关于我需要添加或更改什么的建议。我试着自己做,但它显示了很多错误,例如以下错误:=>https://i.stack.imgur.com/pyIoA.jpg这是康威游戏的序列码=>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=%dn" ,iterations);
        system("pause");
    }
    MPI_Finalize();

}

我会尽力帮你一点,我只是在读错误:

  • 我认为您希望使用MPI_Comm_size而不是不存在的MPI_Comm_dim
  • 您缺少MPI_File_set_view的参数,请参阅文档以正确使用
  • MPI_File_write缺少两个参数请参阅文档以获得正确使用
  • 修复您的警告,因为以下行中多余的;if (last_line > DIM); { last_line = DIM; }有臭味
  • 您缺少一些从downright = A[i+1][j] + A[i][j+1]行开始的;
  • dimof我怀疑您确实搜索并用dim替换了size你从你的一个同学那里复制了这个代码吗?)
  • print_matrix:在尝试制作更复杂的MPI程序之前,学习如何用C(这不是C++)正确地进行代码和调试