【发布时间】:2016-04-05 00:44:27
【问题描述】:
有人可以帮我修复这个使用 MPI 的源代码吗?我对 c++ 并行编程的这个领域不太熟悉,我需要一些关于我需要添加或更改的建议。 我试图自己做,但它显示了很多错误,例如以下错误:=> http://i.imgur.com/JfWNHBA.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=%d\n" ,iterations);
system("pause");
}
MPI_Finalize();
}
【问题讨论】:
-
嗯.. 你不能通过阅读错误自己解决这个问题吗?编译器消息很清楚...检查 MPI 文档。并且请不要直接在您的帖子中提供指向错误的图片链接。
-
对不起..我不知道,这是我第一次使用这个网站。关于代码.. 缺少一些东西,我无法正确理解我需要从我的代码中添加或删除什么,这是主要问题
-
@IonLeahu 请提供minimal reproducible example,尤其是逐字逐句的错误消息。限制您发布图片的原因有很多!
-
@IonLeahu 一步一步的保姆不是你可以期待的,对不起!
-
@IonLeahu “我在这里做错了什么......”你必须从更广泛的 POV 来看这里。该站点并不是您的个人帮助台,而是一个有用的常见问题解答,例如用于长期研究的存储库。所以请在这些条件下检查您的问题,它仍然有帮助吗?
标签: c++ parallel-processing mpi