MPI (Message Passing Interface) l mt th vin cha cc hm c vit sn nhm h tr cho
vic vit cc chng trnh chy song song. MPI c th s dng trong chng trnh C/C++ hoc Fortran chia nh phn tnh ton no cho nhiu my tnh cng thc hin. Bi ny hng dn dng Visual Studio 2008 vit v debug mt chng trnh MPI. Bc 1: vo trang http://www.mcs.anl.gov/research/projects/mpich2/downloads/index.php?s=downloads download gi MPICH2 ph hp. V d ti ang xi Win7 32bits th down gi MPICH2 Windows IA32 (binary) (8M) Bc 2: ci t gi va down, th mc mc nh l C:\Program Files\MPICH2, ci xong file smpd.exe s t ng chy v hi bn fire wall c cho access khng th bn chn yes nh, nu smpd.exe khng t chy th bn t khi ng n ln (n nm trong folder bin ) Bc 3: chy file wmpiregister.exe nhp vo account trong my bn (v d Administrator) v password, sau click nt register Bc 4: Trong Visual Studio 2008 chn Tools>Options, thit lp nh 2 hnh sau:
Hnh trn l Include files, hnh di l library files. Bc 5: To 1 New empty project (Win32 Console Application), to 1 file main.cpp vi ni dung nh sau: #include <stdio.h> #include <mpi.h> int main(int argc, char *argv[]) { int numprocs, rank, namelen; char processor_name[MPI_MAX_PROCESSOR_NAME]; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &numprocs); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Get_processor_name(processor_name, &namelen); printf(Process %d on %s out of %d\n, rank, processor_name, numprocs); MPI_Finalize(); } Bc 6: vo Project>Properties, thit lp nh cc hnh sau:
Dng -np 2 $(TargetPath) > $(OutDir)\out.txt c ngha l s c 2 process cng chy song song vi nhau, kt qu s ghi vo file out.txt nm trong folder Debug chung vi file exe ca chng trnh (trong VS 2008, khi bn chn Debug chng trnh, khi chy xong cmd s t ng exit, do khng th thy c ci mnh cn in ra mn hnh) Bc 7: chn Debug>Start debugging, m file out.txt xem kt qu (CAT y l tn my tnh ca tui) Process 0 on CAT out of 2 Process 1 on CAT out of 2