|
| 1 | +#include <iostream> |
| 2 | +#include <mpi.h> |
| 3 | +#include <string> |
| 4 | +#include <ctime> |
| 5 | +#include <cstdlib> |
| 6 | +#include <assert.h> |
| 7 | + |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +int main(int argc, char *argv[]) |
| 11 | +{ |
| 12 | + srand((int)time(0)); |
| 13 | + |
| 14 | + double* myVector; |
| 15 | + int size; |
| 16 | + |
| 17 | + string sizeStr; |
| 18 | + sizeStr = argv[1]; |
| 19 | + size = atoi(sizeStr.c_str()); |
| 20 | + |
| 21 | + myVector = new double[size]; |
| 22 | + |
| 23 | + // for parallel block |
| 24 | + double myResult = 0; |
| 25 | + double startTime = 0; |
| 26 | + double endTime; |
| 27 | + double myTime = 0; |
| 28 | + double mySum = 0; |
| 29 | + int remainderDiv = 0; |
| 30 | + int flag; |
| 31 | + int myId, numProcs; |
| 32 | + |
| 33 | + // for line block |
| 34 | + double myResult_ = 0; |
| 35 | + double startTime_ = 0; |
| 36 | + double endTime_ = 0; |
| 37 | + double myTime_ = 0; |
| 38 | + |
| 39 | + // Initialize the MPI environment |
| 40 | + |
| 41 | + MPI_Init(&argc, &argv); |
| 42 | + MPI_Initialized(&flag); // check |
| 43 | + if (!flag) { |
| 44 | + std::cout << "Error: MPI_Init"; |
| 45 | + } |
| 46 | + |
| 47 | + // Description of the communicator |
| 48 | + // communicator manages groups of parallel processes |
| 49 | + MPI_Comm_size(MPI_COMM_WORLD, &numProcs); // determining the number of processes in a group |
| 50 | + MPI_Comm_rank(MPI_COMM_WORLD, &myId); // determining the rank of a process in a group |
| 51 | + |
| 52 | + if (myId == 0) { |
| 53 | + |
| 54 | + for (int i = 0; i < size; i++) { |
| 55 | + myVector[i] = (double)(rand()%5) / ((double)(rand()%10) + 1); |
| 56 | + } |
| 57 | + //*LINE BLOCK* |
| 58 | + startTime_ = MPI_Wtime(); |
| 59 | + for (int i = 0; i < size; i++) { |
| 60 | + myResult_ += myVector[i]; |
| 61 | + } |
| 62 | + endTime_ = MPI_Wtime(); |
| 63 | + cout << endl << "*LINE*" << endl; |
| 64 | + cout << "Result: " << myResult_ << endl; |
| 65 | + myTime_ = endTime_ - startTime_; |
| 66 | + cout << "Time: " << myTime_ << " sec" << endl; |
| 67 | + //*END |
| 68 | + |
| 69 | + //*PARALLEL BLOCK* |
| 70 | + startTime = MPI_Wtime(); // started counting time in 0 proccess |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + MPI_Bcast(myVector, size , MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 75 | + |
| 76 | + for (int i = size / numProcs * myId; i < size / numProcs + size / numProcs * myId; i++) { |
| 77 | + mySum += myVector[i]; |
| 78 | + } |
| 79 | + |
| 80 | + MPI_Reduce(&mySum, &myResult, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); |
| 81 | + |
| 82 | + if (myId == 0) { |
| 83 | + remainderDiv = size % numProcs; |
| 84 | + if (remainderDiv) { |
| 85 | + for (int i = 0; i < remainderDiv; i++) |
| 86 | + myResult += myVector[size - remainderDiv + i]; |
| 87 | + } |
| 88 | + |
| 89 | + //Parallel Results |
| 90 | + endTime = MPI_Wtime(); |
| 91 | + cout << endl <<"*PARALLEL*" << endl; |
| 92 | + cout << "Result: " << myResult << endl; |
| 93 | + myTime = endTime - startTime; |
| 94 | + cout << "Time: " << myTime << " sec" << endl << endl; |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + MPI_Finalize(); |
| 99 | + //*END PARALLEL BLOCK* |
| 100 | + |
| 101 | + delete[]myVector; |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
0 commit comments