HPC&Geophysics攻城獅
阿新 • • 發佈:2018-12-22
本在做地震勘探數值模擬,由於大計算量的問題,試著探索MPI平行計算。小有收穫,在此給出一個簡單的例子,希望對大家有益處。
#include <stdio.h> #include "time.h" #include "par.h" #include "su.h" #include "segy.h" #include <mpi.h> int main(int argc, char **argv) { int verbose; int ix,iz,it,is,igrid; /* counters */ int nx,nz; /* x,z,t,tsizes */ float *wavelet; float *xs; /* array of source x coordinates */ float *zs; /* array of source z coordinates */ float **h_s; /************** MPI parameters **************/ int myid,npros; int dblock; char proname[20]; int resultlen; MPI_Comm comm; int s_beg,s_end; /* hook up getpar to handle the parameters */ initargs(argc,argv); requestdoc(0); /* MPI initializing */ MPI_Init(&argc, &argv); MPI_Comm_dup(MPI_COMM_WORLD,&comm); MPI_Comm_size(comm,&npros); MPI_Comm_rank(comm,&myid); MPI_Get_processor_name(proname,&resultlen); /* get required parameters */ if (!getparint("verbose",&verbose)) err(" must specify verbose!"); if (!getparint("nx",&nx)) err(" must specify nx!"); if (!getparint("nz",&nz)) err(" must specify nz!"); MPI_Barrier(comm); if (myid==0 ) { warn(" ***************** Parameters *********************** "); warn(" nz=%d; dz=%f; fz=%f",nz,dz,fz); warn(" nx=%d; dx=%f; fx=%f",nx,dx,fx); } /* allocate memory space on host */ xs = alloc1float(ns); zs = alloc1float(ns);; h_s = alloc2float(nz,nx); wavelet = alloc1float(nt); memset((void *) h_s[0],0,FSIZE*nz*nx); /* determine source coordinates */ for (is=0;is<ns;is++) { xs[is] = fxs+dxs*is; zs[is] = fzs+dzs*is; } MPI_Barrier(comm); /* delete host memory */ free1float(xs); free1float(zs); free2float(h_s); free1float(wavelet); MPI_Barrier(comm); MPI_Finalize(); return(0); }