Reputation: 131
I am learning MPI. I have two Windows machines on each I have VS2015 and I have installed Microsoft HPC Pack 2012, Microsoft HPC Pack 2012 SDK 8.1, Microsoft MPI 8.1 and I am able to run the following code on each machine separately.
I want to connect the two machines to communicate through MPI and run this code while having the ability to specify which process ID runs on which machine. What is the next step to achieve this?
#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();
}
Upvotes: 0
Views: 656