在两台Windows计算机上运行MS-MPI应用程序

时间:2017-07-08 19:01:54

标签: c++ mpi hpc ms-mpi

我正在学习MPI。我有两台Windows机器,我有VS2015,我已经安装了Microsoft HPC Pack 2012,Microsoft HPC Pack 2012 SDK 8.1,Microsoft MPI 8.1,我可以分别在每台机器上运行以下代码。

我想连接两台机器通过MPI进行通信并运行此代码,同时能够指定在哪台机器上运行哪个进程ID。实现这一目标的下一步是什么?

#include<iostream>
#include "mpi.h"
#include <omp.h>

using namespace std;

int numOfProc, id, array_size, portion;
int *arr = NULL;
MPI_Status status;
const static int tag = 1;


int main(int argc, char *argv[])
{
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &numOfProc);
    MPI_Comm_rank(MPI_COMM_WORLD, &id);

    cout << "Hello from Process # " << id << '\n';

    if (id == 0)//master
    {
        cin >> array_size;
        arr = new int[array_size];

        for (int i = 0; i < array_size; i++)
        {
            arr[i] = i + 1;
        }

        portion = array_size / numOfProc;

        for (int p = 1; p < numOfProc; p++)
        {

            MPI_Send(&portion, 1, MPI_INT, p, tag, MPI_COMM_WORLD);
            MPI_Send(&arr[(p - 1)*portion], portion, MPI_INT, p, tag, MPI_COMM_WORLD);
        }
    }
    else // slaves 
    {

        //cout << "Hello from Process # " << id << '\n';

        MPI_Recv(&portion, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);

        arr = new int[portion];

        MPI_Recv(arr, portion, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);


    omp_set_num_threads(2);
#pragma omp parallel for
        for (int i = 0; i < portion; i++)
        {
            cout << "Thread [" << omp_get_thread_num() << "] is printing number " << arr[i] << "." << endl;
        }

    }

    MPI_Finalize();

}

0 个答案:

没有答案