MPI的并行程序设计

使用VC8.0MPICH2-1.0.5 for Windows进行MPI并行程序设计.下面我们将编写一个简单的并行求和 程序,实现一个非常简单的功能 ,将一个文件中的1000个数相加.这里面用到了数据的广播与进程值的归约.

程序1 将1000个随机数相加

#include "mpi.h"          //建议将这个头文件置于所有头文件之前

#include <iostream>

#include <fstream>

#include <stdio.h>

 

#define   BUFSIZE 256

#define   MAXSIZE 10000

using namespace std;

int main(int argc, char* argv[])                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

{

        int myid,numprocs;

        int namelen;

        char processor_name[MPI_MAX_PROCESSOR_NAME];

        int i,x,low,high;

        int data[MAXSIZE],myresult=0,result=0;

        fstream f("D:\\rand_data.txt"); //数据存放在文件D:\\rand_data.txt中

MPI_Init(&argc,&argv); //初始化MPI通信层,MPI_INIT 必须在其他MPI函数

//调用前调用

        MPI_Comm_rank(MPI_COMM_WORLD,&myid);

        MPI_Comm_size(MPI_COMM_WORLD,&numprocs);

/*函数 MPI_COMM_SIZE 和 MPI_COMM_RANK 查询

MPI_COMM_WORLD 以获得应用中MPI进程的总数并为这些MPI进程分别获得一个唯一的编号。获取的值放在变量 numprocs和 myid中*/

/* MPI_COMM_WORLD 是一个预定义的MPI通信字(communicator)----一个 MPI进程的有序集合,也是一个唯一的通信上下文。 MPI_COMM_WORLD 在 MPI_INIT 函数执行后被赋予意义*/

 

        MPI_Get_processor_name(processor_name,&namelen);

//得到运行本程序的机器名

 

        if(myid==0)

        {

               if(!f){

                      cerr<<"Cannot open the input file :D:\\rand_data.txt"<<endl<<endl;

                      exit(1);

               }

               for(i=0;i<MAXSIZE;i++){

                      f>>data[i];

               }

        }

        MPI_Bcast(data,MAXSIZE,MPI_INT,0,MPI_COMM_WORLD);

        //将文件中的数据广播到所有节点上

        x=MAXSIZE/numprocs;

        low=myid*x;

        high=low+x;

        for(i=low;i<high;i++)

               myresult +=data[i];       //将数据分配到每个节点

        cerr<<"I got "<<myresult<<" from "<<myid<<endl<<endl;

        fflush(stdin);

        MPI_Reduce(&myresult,&result,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);    //归约各个节点的结果到result中去

        if(myid==0)

        {

               cerr<<"The sum is "<<result<<endl;

               fflush(stdin);

        }

        if (myid == 0)

        {

               cerr<<"\nPress a key and exit.\n";

               fflush(stdin);

               fgetc(stdin);

        }

MPI_Finalize();/*执行MPI进程中MPI层的清理和关闭工作。在 MPI_FINALIZE 后面再调用 MPI函数都是不合法的*/

        return 0;

}

你可能感兴趣的:(windows,工作,F#)