MPI ¤»¤j°ò¥»«ü¥O¤Î½d¨Òµ{¦¡¬ãŪ
´£¿ô¤j®aª`·N¤@¤U¡AMPI ªº«ü¥O³£¬O©I¥s°Æµ{¦¡ªº¤è¦¡¡A¦]¦¹¤Þ¼Æùرªº¦WºÙ¬O¥i¥H¦Û¥Ñ©R¦Wªº¡A¦ì¸m«h³£n¹ï±o¤W¡C
¥t¥~¡A¥Ñ©ó³o¨Ç°Æµ{¦¡¤]¬O Fortran¡A¤j¤p¼g¬Oµ¥¦P¤£°Ï¤Àªº¡A³oùئ³¤j¤p¼g¥u¬O¬°¤FÅý±Ð§÷ªí¥Ü±o§ó²M·¡¡C
mpif.h ¤º¤w¸g«Å§i¤F«Ü¦h MPI ±M¥ÎªºÅܼơA¦]¦¹§Ṳ́ޥΪº®ÉÔ§¹¥þ¤£¥²¦A«Å§i¡A¦p¤U±¨Ò¤l¤¤ªº mpi_data_type = {MPI_INTEGER or MPI_REAL or MPI_DOUBLE_PRECISION}¡Fcommunicator = MPI_COMM_WORLD¡F status = MPI_STATUS¡F mpi_op_type = {MPI_SUM or MPI_PRODUCT} ¡C
MPI_Init (ierr)
±Ò°Ê MPI¡A©Ò¦³ MPI «ü¥O¤§«en¦³³oÓªì©l¤Æ«ü¥O¡A°ß¤@Ӥ޼ƬO¥²¶·«Å§i¦¨¾ã¼Æªº ierr¡A¦pªGªì©l¤Æ¦¨¥\¡A«h ierr ªº¶Ç¦^ȬO¹s¡C
MPI_Finalize (ierr)
Ãö³¬ MPI¡Aµ²§ô¦hÓ CPU ¨ó¦P¤u§@ªºª¬ºA¡C¥¿±`Ãö³¬ªº¸Ü«h¾ã¼Æ ierr ªº¦^¶ÇȬO¹s¡C
MPI_Comm_rank (communicator, my_rank, ierr)
Àò±o¦¹ CPU ¦b¥þÅé¦h CPU ¤¤ªº±Æ¦æ¶¶§Ç¡A±Æ¦Wªº½s¸¹·|¦s¦b¾ã¼ÆÅÜ¼Æ my_rank ùØ¡]±q¹s¶}©l¡^¡A¨CÓ°Ñ»Pªº CPU ³£·|¤À¨ì¤@Ó³s¸¹¦ý¤£«ÂЪº¼Æ¦r¡]¹³¨ì¶l§½¿ì¨Æ»â¸¹½XµP¨º¼Ë¡^¡Ccommunicator ¬O¦b¿ï³q°T¸Ë¸m¡A¤@¯ë³£¬O¥Î¹w³]ªº MPI_COMM_WORLD ¤£¥Î¦Û¤v§ï¡]°Ñ¨£½d¨Ò¡^¡A¦Ü©ó ierr «h¬O©M«e±¤@ºØ§@¬°¿ù»~©Î¥¿±`ªººX¼Ð¡C
MPI_Comm_size (communicator, nproc, ierr)
Àò±o¥þÅ馳¦h¤ÖÓ CPU ªº¼Æ¥Ø¡]³q±`¥Î©ó¤Á³Î¤u§@¥÷¶qªº°ò¦¡^¡A¾ã¼ÆÅÜ¼Æ nproc ¦^¶Çµ¹¨C¤@Ó CPU ¦P¼Ëªº¼ÆÈ¡A§Y¦@¦³¦h¤ÖÓ CPU ¥¿¦b¤@°_¤u§@¡Cierr ¬O¿ù»~¦^¶ÇÀˬd¥Î¡Acommunicator ªº©w¸q»P¥Îªk¸ò«e±ªº©w¸q¤@¼Ë¡C
MPI_Send (buf, count, datatype, dest, tag, communicator, ierr)
°e¥X¡]¥Ñ²{¦b³oÓ CPU °e¥X¤@¦ê¸ê®Æµ¹¥t¤@Ó CPU¡^¡Abuf ¬O«ün¶Ç°eªº¨º¤@¦ê¸ê®ÆÅܼƪº¦WºÙ¡]¤]´N¬O²Ä¤@µ§¸êªº¦ì§}¡^¡Acount ¬O¸ê®Æªºªø«×¡]±q«e±©wªº°_ÂI¶}©lºâ°_¡^¡Adatatype ¬On³Q¶Ç°e¤§¸ê®Æªº¼ÆÈ«¬ºA¡A¥u¦³´XºØ±`¨£ªº©T©wÃþ«¬¡A¦p MPI_REAL¡BMPI_INTEGER µ¥¡F¾ã¼Æ dest ¬On¶Çªº¥Øªº¦a CPU ªº rank ½s¸¹¡A¦Ü©ó ¾ã¼Æ tag «h¬O¼gµ{¦¡ªº¤H¦Û¤w³]©wªº¼ÐÅÒ¡C¼ÐÅÒªº¥\¯à¦b³oùØ¥i¥H»¡©ú¤@¤U¡A·í¨Ó·½ºÝ CPU µo°e¸ê®Æ®É¡A°e¥X¨Óªº¸ê®Æ¥¼¥²·|³Q¥Øªº¦a CPU ¦¬¨ì¬Û¦P¦WºÙªºÅܼÆùØ¡A¦³¥i¯à¤£¤î°e¤@¥]¸ê®Æ¡A¤]¦³¥i¯à¨ì¹Fªº¥ý«á¶¶§Ç·|¥´¶Ã¡A¦]¦¹¤@©wn§Q¥Î¼Ðñ¨Ó³]©w¨C¤@¥]¸ê®Æ¦ê¦³ºë·Ç³Q±µ¦¬¨ì¡C
MPI_Send «ü¥O¬O¤@Óªý¾×©Ê (blocking) ªº«ü¥O¡A¤]´N¬O»¡¥¦nµ¥¨ì¦¬¨ì¥¦¦bµ¥«ÝªºªF¦è¤~·|ÄÀ©ñ±±¨îÅvµ¹¥Dµ{¦¡¥h°õ¦æ¤U¤@Ó«ü¥O¡C
MPI_Recv (buf, count, datatype, source, tag, communicator, status, ierr)
±µ¦¬¡C¨ä¤¤«e¤TÓ¤Þ¼Æ buf¡Bcount¡Bdatatype »P«e±©w¸q¤@¼Ë¡Asouce ¬O¨Ó·½ CPU ªº½s¸¹¡Atag «h¬On»P°e¤è«ü¥O¬Û¦Pªº¾ã¼ÆÈ¡A§@¬°±µ¦¬ªº»{ÃÒ¡CÀH«áªº communicator »P ierr »P«e±¦P¡Cȱoª`·N¬O status ¡A¥¦¬O¥Î¨Óªí¥Ü±µ¦¬ªºª¬ºA¡C±µ¦¬»P°e¥X¦³¤@ÂIÂI¤£¦P¡A°e¥X¬O¥i¥H¥ß§Y°Ê§@ªº¡AµM¦Ó±µ¦¬´N¨Ã¤£¬O¤U¤F±µ¦¬«ü¥O´N¥i¥H°¨¤W¶i¦æ§¹¦¨¡A¦]¬°Á`¬Onµ¥«Ý¸ê®Æ¨ì¹F¡C
¨ä¥L¦³¥Îªº MPI «ü¥O
MPI_Bcast(buf,n,mpi_datatype,i_src_node,MPI_COMM_WORLD,ierr)
¼s¼½ (Broadcast)¡A·N«ü¤@Ó CPU ©ñ°e¸ê®Æ¥X¨Ó¡A¨ä¥L CPU ±µ¦¬¡A¦@¦P¨ú±o¸Ó¤ñ¸ê®Æªº¤º®e¡C±ý¶Ç°e¸ê®Æªº±Ò©l¦ì¸m¬O buf ¡Bªø«×¡]¸ê®Æµ§¼Æ¡^¬O n ¡B i_src_node ¬O µo°e·½ ¤§ CPU ½s¸¹¡A¨ä¥L mpi_datatype ¡BMPI_COMM_WORLD¡Bierr «h¬O»P«e±ªº©w¸qªº¤@¼Ë
MPI_Reduce(buf,result,n,mpi_datatype,mpi_op_type,i_trgt_node, MPI_COMM_WORLD,ierr)
¦¬¶°¦UÓ CPU ¤Wªºµ²ªG¡C¨ä¤¤ mpi_op_type ¥i¥H¬O MPI_SUM ©Î MPI_PRODUCT¡A ¦Óµ²ªG«h¦¬¶°¨ì i_trgt_node ªº result ÅܼƤW¡]¨Ã¤£¬O¨CÓ node ªº result ³£§ó·s¡^¡C
MPI_Get_processor_name (nodename,nchar,ierr)
nodename ¬O¦r¦êÅܼơA¥¦·|¦^¶Ç¸Ó CPU ©Ò¦bªº¾÷¾¹¦WºÙ¡F nchar ¬O¾ã¼Æ¡A¦^¶Ç nodename ¤º§tªº¦r¤¸¼Æ
¾\Ū½d¨Òµ{¦¡
Examples : ¨Ï¥Î MPI_Comm_rank ¨Ó°Ï¤À master »P slave CPU ¨Ó°µ¤£¦P¤u§@ªº¨Ò¤l
Examples : ¨Ï¥Î MPI_Bcast ¤Î MPI_Reduce ¨Ópºâ¶ê©P²vªº¨Ò¤l
¦p¦ó½sĶ¤Î°õ¦æ MPI Fortran µ{¦¡
½sĶ§A¦Û¤wªº MPI µ{¦¡ªº¤èªk
º¥ý¥²¶·½T©w mpich ¥Ø¿ý¦b¸Ó¨t²Î¤¤ªº¦ì¸m¡A¨Ò¦p¦b S105 ±Ð«Ç¬O¸m©ó /NFS_home/mhl/mpich-1.2.7 ¤§¤U¡AY¬O¨Ï¥ÎªÌ¦Û¦æ¦b¦Û¤w¥Ø¿ý¤U«Øºc«h°Ñ·Ó¸Ó¥Ø¿ý¡A¦b MPICH ªº¥D¥Ø¿ý¤U§AÀ³¸Ó·|¬Ý¨ì $MPI_DIR/bin/mpif77 ¤Î $MPI_DIR/bin/mpif90¡An½sĶ¥¦æµ{¦¡®É¡A¥u»Ýn¥´¸Ó mpif77 ©Î mpif90 ªº§¹¾ã¸ô®|¡A«á±¦A±µÀɦW¡A¦³»Ýn«ü©w¥i°õ¦æÀɪº¦UºÙ«h¥Î -o ¡B «ü©w·j´Mµ{¦¡®w¥Ø¿ý¥Î -L ¡B «ü©w¨Ï¥Î¤§µ{¦¡®w¥Î -l ¡A»P¤@¯ë½sĶ Fortran ªº²ßºD¤@¼Ë¡C
µù¡G¦b S105 ¹q¸£±Ð«ÇªºÀô¹Ò¡AMPICH ªº¥D¥Ø¿ý¬O¦b /NFS_home/mhl/mpich-1.2.7 ¡C
°õ¦æ¤@Ó MPI µ{¦¡
°O±o½sĶ¥X¨Óªº¥i°õ¦æÀÉ a.out ¡An¥Î $MPI_DIR/bin/mpirun -np 4 a.out ³oºØ¤è¦¡¨Ó±Ò°Ê°õ¦æ¡C¨ä¤¤ $MPI_DIR ¥Nªí MPICH ¥D¥Ø¿ýªº§¹¾ã¸ô®|¡C
Yn«ü©w (¦h¥x¦U§O) ¾÷¾¹¡A«hn¥[¥Î -machinefile my_machine_file °Ñ¼Æ©ó mpirun ©R¥O¤¤¡A¨Ï¦¨¬°¡G
$MPI_DIR/bin/mpirun -machinefile my_machine_file -np 4 a.out
¨ä¤¤ my_machine_file ¬O¤@Ó´¶³qªº¤å¦rÀÉ¡A¤º®e¥i¥H¹³¬O¡G
proton2
proton3
proton4
proton8©Î¬O
proton2:2
proton4:2«áªÌ¾÷¾¹©úºÙ«á©Ò¥[¤§ :2 ¥Nªí§iª¾¸Ó¾÷¾¹¦³¨âÁû CPU¡C¥t¥~«Ü«nªº¤@ÂI¬O¡A¤WzÀɮפ¤¤§¾÷¾¹¦WºÙ¥²¶·¬O¥´ hostname «ü¥O°Ý¨t²Î©Ò·|§e²{ªº¡A¤£¬OÀH«Kºô¸ô¥i³s½uªº¦WºÙ©Î IP ¦a§}´N¥i¥H¡C
²M³æ¤¤ªº¾÷¾¹¤ñ mpirun -np # ©Òn¨Dªº CPU ¼ÆÁ٤֮ɡA·|½ü¦^¨ì²Ä¤@¥x¦A¥[¥á¤@Ó¤u§@³æ¤¸¡C
½m²ß¡G
½Ð§Q¥Î mpif77 ¡]¥Îªk¦p¤W©Òz¡^±N«e±½d¨Òµ{¦¡ init.f¡Badd_mpi.f¡Bfpi.f ½sĶ¥X¥i°õ¦æÀÉ¡A¨Ã¥B¦b¨â¥x¤£¦Pªº¾÷¾¹¤W°õ¦æ¡C¡]°O±o³]©w¾÷¾¹¥i¥Î -machinefile my_machine_file °Ñ¼Æ¡^