MPI関数の挿入の仕方がわかりません。
並列処理を行いたいのは、このプログラムの計算処理部分です。
特にMPI関数のMPI_Send()MPI_Recv()の挿入の仕方がわかりませんのでご教授頂けると幸いです。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <time.h>
#include "mpi.h"
#define DT 0.001
#define TIMELIMIT 200.0
#define R 0.1
#define BUFSIZE 256
#define N 256
struct coordinate {
double x;/*x座標*/
double y;/*y座標*/
};
struct charge {
struct coordinate qxy;/*電荷の位置*/
double q;/*電荷*/
};
int getdouble(double *x);/*実数の読み込み*/
int inputq(struct charge qi[]);/*電荷入力*/
int main(int argc,char* argv[])
{
struct coordinate v,x;/*質点の速度と位置*/
struct charge qi[N];
int nofq;
clock_t start,end;
double t=0;
double h=DT;
double rx,ry,r,rmin;
int i;
MPI_Init(&argc,&argv);
fprintf(stderr,"初速度v0x\n");
if(getdouble(&v.x)==EOF)exit(1);
fprintf(stderr,"初速度v0y\n");
if(getdouble(&v.y)==EOF)exit(1);
fprintf(stderr,"初期位置x.x\n");
if(getdouble(&x.x)==EOF)exit(1);
fprintf(stderr,"初期位置x.y\n");
if(getdouble(&x.y)==EOF)exit(1);
nofq=inputq(qi);
printf("%f\t%f\t%f\t%f\t%f\n",t,x.x,x.y,v.x,v.y);
start = clock();
printf("開始時間:%f\n",start);
while(t<=TIMELIMIT){
t+=h;
rmin=DBL_MAX;
for(i=0;i<nofq;++i){
rx=qi[i].qxy.x-x.x;
ry=qi[i].qxy.y-x.y;
r=sqrt(rx*rx+ry*ry);
if(r<rmin) rmin=r;
v.x+=(rx/r/r/r*qi[i].q)*h;
v.y+=(ry/r/r/r*qi[i].q)*h;
}
end = clock();
printf("処理%f\n",(double)(end-start)/CLOCKS_PER_SEC);
x.x+=v.x*h;
x.y+=v.y*h;
printf("%f\t%f\t%f\t%f\t%f\n",t,x.x,x.y,v.x,v.y);
if(rmin<R) break;
}
MPI_Finalize();
return 0;
}
int inputq(struct charge qi[])
{
int i;
for(i=0;i<N;++i){
fprintf(stderr,"電荷%d\n",i);
fprintf(stderr,"電荷の配置箇所qxy.x\n");
if(getdouble(&qi[i].qxy.x)==EOF)break;
fprintf(stderr,"電荷の配置箇所qxy.y\n");
if(getdouble(&qi[i].qxy.y)==EOF)break;
fprintf(stderr,"電荷の値q\n");
if(getdouble(&qi[i].q)==EOF)break;
}
return i;
}
int getdouble(double *x)
{
char linebuf[BUFSIZE];
int result=0;
if(fgets(linebuf,BUFSIZE,stdin)!=NULL){
if(sscanf(linebuf,"%lf",x)<=0)
result=EOF;
}else{
result=EOF;
}
return result ;
}