1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include "mpi.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
int N=8;
int main(int argc, char **argv)
{
int rank,
nprocs,
*x,*r,
count,
*w,
i=0;
x = (int*)malloc(N*sizeof(int));
r = (int*)malloc(N*sizeof(int));
/* MPI Initialization */
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // get process number for this process
MPI_Comm_size(MPI_COMM_WORLD, &nprocs); // number of processes
count = N/nprocs;
w = (int*)malloc(count*sizeof(int));
if (rank == 0) {
// Initialize the vector x
for (i=0; i<N; i++)
x[i]= 1;
} // end master
// distribute vector partitions among processes using MPI_Scatter
MPI_Scatter(x, count, MPI_INT, x, N, MPI_INT, 0, MPI_COMM_WORLD);
// computation (MASTER AND SLAVES)
for (i=0;i<count;i++){
w[i] = x[i] + 10;
}
// gather partial results from processes using MPI_Gather
MPI_Gather(&w, count, MPI_INT, r, count, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0) {
cout << "\n\nFINISH\n\n";
cout << "\n\n";
for (i=0; i < N; i++) {
r[i] = w[i];
cout << "r[" << i << "] = " << r[i] << "\n";
}
} // end master
MPI_Finalize();
}
|