Skip to content

Commit 5e55e2f

Browse files
author
Dima Perov
committed
SumElVect
1 parent dca4dfa commit 5e55e2f

File tree

3 files changed

+78
-30
lines changed

3 files changed

+78
-30
lines changed

.travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ before_install:
1414

1515
install:
1616
- sudo apt-get install libcr-dev mpich2 mpich2-doc
17-
- sudo apt-get install libopencv-*
1817

1918
script:
2019
- mkdir build
@@ -23,4 +22,4 @@ script:
2322
- make -j4
2423

2524
- mpirun -np 4 ./modules/test_task/test_task
26-
- mpirun -np 4 ./modules/task_1/Perov_Dima_task1_SumElVect/Perov_Dima_task1_SumElVect
25+
- mpirun -np 4 ./modules/task_1/Perov_Dima_task1_SumElVect/Perov_Dima_task1_SumElVect 10000

appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ build_script:
4646
- cmd: msbuild ALL_BUILD.vcxproj
4747

4848
- cmd: mpiexec.exe -np 4 modules\test_task\%configuration%\test_task.exe
49-
- cmd: mpiexec.exe -np 4 modules\task_1\Perov_Dima_task1_SumElVect\%configuration%\Perov_Dima_task1_SumElVect.exe
49+
- cmd: mpiexec.exe -np 4 modules\task_1\Perov_Dima_task1_SumElVect\%configuration%\Perov_Dima_task1_SumElVect.exe 10000
5050

Original file line numberDiff line numberDiff line change
@@ -1,59 +1,108 @@
11
#include <iostream>
22
#include <mpi.h>
3-
4-
const int SIZE = 10000;
3+
#include <string>
4+
#include <ctime>
55

66
using namespace std;
77

88
int main(int argc, char *argv[])
99
{
10-
double myVector[SIZE];
11-
int myId, numProcs;
12-
double startTime = 0;
13-
double endTime;
10+
srand(time(0));
1411

15-
for (int i = 0; i < SIZE; i++) {
16-
myVector[i] = 1;
17-
}
12+
double* myVector;
13+
int size;
14+
15+
string sizeStr;
16+
sizeStr = argv[1];
17+
size = atoi(sizeStr.c_str());
1818

19-
int flag; // Ôëàã èíöèàëèçàöèè
20-
MPI_Status status;
19+
myVector = new double[size];
20+
21+
22+
23+
24+
// for parallel block
25+
double myResult = 0;
26+
double startTime = 0;
27+
double endTime;
28+
double myTime = 0;
2129
double mySum = 0;
22-
double myResult = 0;
30+
int remainderDiv = 0;
31+
int flag;
32+
int myId, numProcs;
2333

24-
MPI_Init(&argc, &argv); // Èíöèàëèçèðóåì MPI-îêðóæåíèå
25-
MPI_Initialized(&flag); // Ïðîâåðêà
34+
// for line block
35+
double myResult_ = 0;
36+
double startTime_ = 0;
37+
double endTime_ = 0;
38+
double myTime_ = 0;
39+
40+
// Initialize the MPI environment
41+
42+
MPI_Init(&argc, &argv);
43+
MPI_Initialized(&flag); // check
2644
if (!flag) {
2745
std::cout << "Error: MPI_Init";
2846
}
2947

30-
// Îïèñàíèå êîììóíèêàòîðà
31-
// Êîììóíèêàòîð óïðàâëÿåò ãðóïïàìè ïàðàëëåëüíûõ ïðîöåcñîâ
32-
MPI_Comm_size(MPI_COMM_WORLD, &numProcs); // Îïðåäåëåíèå êîëè÷åñòâî ïðîöåññîâ â ãðóïïå
33-
MPI_Comm_rank(MPI_COMM_WORLD, &myId); // Îïðåäåëåíèå ðàíãà ïðîöåññà â ãðóïïå
48+
49+
// Description of the communicator
50+
// communicator manages groups of parallel processes
51+
MPI_Comm_size(MPI_COMM_WORLD, &numProcs); // determining the number of processes in a group
52+
MPI_Comm_rank(MPI_COMM_WORLD, &myId); // determining the rank of a process in a group
3453

3554

3655
if (myId == 0) {
37-
startTime = MPI_Wtime();
56+
57+
for (int i = 0; i < size; i++) {
58+
myVector[i] = (double)(rand()%5) / ((double)(rand()%10) + 1);
59+
}
60+
//*LINE BLOCK*
61+
startTime_ = MPI_Wtime();
62+
for (int i = 0; i < size; i++) {
63+
myResult_ += myVector[i];
64+
}
65+
endTime_ = MPI_Wtime();
66+
cout << endl << "*LINE*" << endl;
67+
cout << "Result: " << myResult_ << endl;
68+
myTime_ = endTime_ - startTime_;
69+
cout << "Time: " << myTime_ << " sec" << endl;
70+
//*END
71+
72+
//*PARALLEL BLOCK*
73+
startTime = MPI_Wtime(); // started counting time in 0 proccess
74+
3875
}
3976

40-
MPI_Bcast(&myVector[SIZE / numProcs * myId], SIZE / numProcs, MPI_DOUBLE, 0, MPI_COMM_WORLD);
41-
42-
for (int i = SIZE / numProcs * myId; i < SIZE / numProcs + SIZE / numProcs * myId; i++) {
77+
MPI_Bcast(myVector, size , MPI_DOUBLE, 0, MPI_COMM_WORLD);
78+
79+
for (int i = size / numProcs * myId; i < size / numProcs + size / numProcs * myId; i++) {
4380
mySum += myVector[i];
4481
}
4582

4683
MPI_Reduce(&mySum, &myResult, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
4784

48-
85+
4986
if (myId == 0) {
50-
cout << "Result: " << myResult << endl;
87+
remainderDiv = size % numProcs;
88+
if (remainderDiv) {
89+
for (int i = 0; i < remainderDiv; i++)
90+
myResult += myVector[size - remainderDiv + i];
91+
}
92+
93+
//Parallel Results
5194
endTime = MPI_Wtime();
52-
cout << "Time: " << (endTime - startTime) << " sec" << endl;
53-
//system("pause");
95+
cout << endl <<"*PARALLEL*" << endl;
96+
cout << "Result: " << myResult << endl;
97+
myTime = endTime - startTime;
98+
cout << "Time: " << myTime << " sec" << endl << endl;
5499
}
55-
100+
56101

57102
MPI_Finalize();
103+
//*END PARALLEL BLOCK*
104+
105+
delete[]myVector;
58106

107+
return 0;
59108
}

0 commit comments

Comments
 (0)