I am trying to create MPI parallelized program for vector multiplication : c = a[i]*b[i]
, a,b
are std::vector
s and c
is double. Here is an example code:
#include "stdafx.h"
#include<iostream>
#include<mpi.h>
#include<vector>
#include <chrono>
int main(int argc, char*argv[])
{
MPI_Init(&argc, &argv);
int mynode = { 1 }, totalnodes = { 2 };
MPI_Comm_size(MPI_COMM_WORLD, &totalnodes);
MPI_Comm_rank(MPI_COMM_WORLD, &mynode);
MPI_Status status;
//creating clock start
std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
std::vector<double> a, b;
int startval, endval;
int end_range = { 20000 };
double sum = { 0.0 }, accumulate = { 0.0 };
//creating vectors
for (int i = 0; i < end_range; ++i)
{
a.push_back(i);
b.push_back(i);
}
//creating a chuck of vector : assigning start and end value for each process
startval = (mynode)*(end_range/ totalnodes);
endval = (mynode)*(end_range / totalnodes) + ((end_range / totalnodes) - 1);
if(mynode==(totalnodes-1))
endval = (end_range - 1);
//vector multiplication of given range
for (int i = startval; i <= endval; ++i)
{
sum = sum + ( a[i] * b[i]);
}
//if process is not 0 then send sum to process 0, else receive answer from each process and sum them up
if (mynode != 0)
{
MPI_Send(&sum, 1, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD);
}
else
{
for (int j = 1; j < totalnodes; ++j)
{
MPI_Recv(&accumulate, 1, MPI_DOUBLE, j, 1, MPI_COMM_WORLD, &status);
sum = sum + accumulate;
}
}
if (mynode == 0)
std::cout << "a_i * b_i = " << sum << std::endl;
MPI_Finalize();
std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
std::cout << "Program execution time in microseconds = " << duration << std::endl;
return 0;
}
I compiled using Microsoft Visual C++ compiler and here is the output:
mpiexec -n 1 .\Mult.exe
a_i * b_i = 2.66647e+12
Program execution time in microseconds = 64998
mpiexec -n 2 .\Mult.exe
a_i * b_i = 2.66647e+12
Program execution time in microseconds = 70649
Program execution time in microseconds = 70662
mpiexec -n 3 .\Mult.exe
a_i * b_i = 2.66647e+12
Program execution time in microseconds = 100134
Program execution time in microseconds = 102985
Program execution time in microseconds = 103125
mpiexec -n 4 .\Mult.exe
a_i * b_i = 2.66647e+12
Program execution time in microseconds = 104111
Program execution time in microseconds = 104106
Program execution time in microseconds = 104159
Program execution time in microseconds = 105269
So, it can be seen that smallest amount of time is when using single process. And surprisingly time increases with increase in number of process. Vector multiplication is completely parallel operation, but not sure why the execution time contradicts. Any comments?
Aucun commentaire:
Enregistrer un commentaire