1、
#include <stdio.h>
#include <mpi.h>
int main (int argc,char *argv[])
{
int rank, size;
MPI_Init (&argc, &argv); /* starts MPI */
MPI_Comm_rank (MPI_COMM_WORLD, &rank); /* get current process id */
MPI_Comm_size (MPI_COMM_WORLD, &size); /* get number of processes */
printf( "Hello world from process %d of %d\n", rank, size );
MPI_Finalize();
return 0;
}
2、
//摘自 http://blog.sina.com.cn/s/blog_8a5f025a01012juz.html
#include<iostream>
#include<mpi.h> // 包含mpi头文件
int main(int argc, char** argv)
{
int nThisNode = -1 ; // 节点
int nTotalNodes = -1 ; // 节点总数
MPI_Init( &argc , &argv ) ;
MPI_Comm_size( MPI_COMM_WORLD , &nTotalNodes ) ;
MPI_Comm_rank( MPI_COMM_WORLD , &nThisNode ) ;
//---仅仅测试---------------------------
int n = 0 ;
while ( n++ < 900000 )
std::cout << "" ;
//------------------------------
char szMsg[128] = { 0 } ;
sprintf( szMsg , "来自%d的节点的问候,一共有%d个节点。\n" , nThisNode , nTotalNodes ) ;
std::cout << szMsg ;
MPI_Finalize( ) ;
return 0;
}
将1或2编译生成exe文件后,使用MPICH执行即可。