C++ C++;使用MPI康威&x27;人生的游戏
有人能帮我修复这个使用MPI的源代码吗?我不太熟悉C++并行编程的这个领域,我需要一些关于我需要添加或更改的建议。 我试着自己做,但它显示了很多错误,比如以下错误:=> 这是康威游戏的序列码=>C++ C++;使用MPI康威&x27;人生的游戏,c++,parallel-processing,mpi,C++,Parallel Processing,Mpi,有人能帮我修复这个使用MPI的源代码吗?我不太熟悉C++并行编程的这个领域,我需要一些关于我需要添加或更改的建议。 我试着自己做,但它显示了很多错误,比如以下错误:=> 这是康威游戏的序列码=> #包括 #包括 #包括 #包括 #包括“math.h” #包括 #包括“mpi.h” #定义尺寸7 #定义文件名“life.dat” 作废打印矩阵(重新加倍[DIM+2][DIM+2]){ int i,j; 对于(i=0;i我将尽力帮助您,我只是阅读错误: 我想您应该使用MPI\u Comm\u si
#包括
#包括
#包括
#包括
#包括“math.h”
#包括
#包括“mpi.h”
#定义尺寸7
#定义文件名“life.dat”
作废打印矩阵(重新加倍[DIM+2][DIM+2]){
int i,j;
对于(i=0;i我将尽力帮助您,我只是阅读错误:
- 我想您应该使用
MPI\u Comm\u size
而不是不存在的MPI\u Comm\u dim
MPI\u File\u set\u视图缺少一个参数
有关正确使用的信息,请参阅
MPI\u File\u write
缺少两个参数。有关正确使用,请参阅
- 修复您的警告,因为以下行中额外的
;
:if(last_-line>DIM);{last_-line=DIM;}
很臭
- 您缺少了一些
;
,从行开始,直接=A[i+1][j]+A[i][j+1]
dimof
?我怀疑你用dim
搜索并替换了size
,dim
你从你的一个同学那里复制了这段代码吗;)
print_matrix
:在尝试制作更复杂的MPI程序之前,先学习如何正确地用C(这不是C++)进行编码和调试
嗯..难道你不能通过阅读错误来解决这个问题吗?编译器消息很清楚…检查MPI文档。请不要在你的帖子中直接提供引用错误的图像链接。对不起..我不知道,这是我第一次使用此网站。关于代码..缺少一些东西,我无法理解我需要从我的代码中添加或删除什么,这是主要的problem@IonLeahu请提供一个完整的文本,特别是错误消息。有很好的理由限制您发布图片!@IonLeahu逐步保姆不是您可以期望的,抱歉!@IonLeahu“我在这里做错了什么…”你必须从更广泛的角度来看这里。该网站不是作为你的个人帮助台,而是一个有用的FAQ库,用于长期研究。因此,请在这些条件下检查你的问题,它仍然有用吗?
#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();