Skip to content

Commit 51ea503

Browse files
author
Nesterov Alexander
authored
Merge pull request #14 from diper1998/branchOfDiper
Перов Дима. Задача 1. Сумма элементов вектора.
2 parents 473e031 + bd0dc06 commit 51ea503

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ script:
2222
- make -j4
2323

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

appveyor.yml

+2
Original file line numberDiff line numberDiff line change
@@ -46,3 +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 10000
50+

modules/task_1/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
message(STATUS "Task 1")
2+
add_subdirectory( Perov_Dima_task1_SumElVect )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
project( Perov_Dima_task1_SumElVect )
2+
message( STATUS "-- " ${PROJECT_NAME} )
3+
add_executable( ${PROJECT_NAME} main.cpp )
4+
if( MPI_COMPILE_FLAGS )
5+
set_target_properties( ${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${MPI_COMPILE_FLAGS}" )
6+
endif( MPI_COMPILE_FLAGS )
7+
if( MPI_LINK_FLAGS )
8+
set_target_properties( ${PROJECT_NAME} PROPERTIES LINK_FLAGS "${MPI_LINK_FLAGS}" )
9+
endif( MPI_LINK_FLAGS )
10+
target_link_libraries( ${PROJECT_NAME} ${MPI_LIBRARIES} )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)