Skip to content

Commit aa029b0

Browse files
author
Nesterov Alexander
authored
Merge pull request #26 from J-win/task_1
Вдовин Евгений. Задача 1. Подсчёт числа слов в строке.
2 parents b10bb17 + 6a64a84 commit aa029b0

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ script:
2525
- mpirun -np 4 ./modules/task_1/Yakovlev_Pavel_mul_vect/Yakovlev_Pavel_mul_vect 100000
2626
- mpirun -np 4 ./modules/task_1/Perov_Dima_task1_SumElVect/Perov_Dima_task1_SumElVect 10000
2727
- mpirun -np 4 ./modules/task_1/vikhrev_array_sum/vikhrev_array_sum 10 10
28+
- mpirun -np 4 ./modules/task_1/Vdovin_Eugene_task1_NumWords/Vdovin_Eugene_task1_NumWords 300000000

appveyor.yml

+1
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ build_script:
4949
- cmd: mpiexec.exe -np 4 modules\task_1\Perov_Dima_task1_SumElVect\%configuration%\Perov_Dima_task1_SumElVect.exe 10000
5050
- cmd: mpiexec.exe -np 4 modules\task_1\vikhrev_array_sum\%configuration%\vikhrev_array_sum.exe 10 10
5151
- cmd: mpiexec.exe -np 4 modules\task_1\Yakovlev_Pavel_mul_vect\%configuration%\Yakovlev_Pavel_mul_vect.exe 100000
52+
- cmd: mpiexec.exe -np 4 modules\task_1\Vdovin_Eugene_task1_NumWords\%configuration%\Vdovin_Eugene_task1_NumWords.exe 300000000

modules/task_1/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ message(STATUS "Task 1")
22
add_subdirectory( Perov_Dima_task1_SumElVect )
33
add_subdirectory( vikhrev_array_sum )
44
add_subdirectory( Yakovlev_Pavel_mul_vect )
5+
add_subdirectory( Vdovin_Eugene_task1_NumWords )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
project( Vdovin_Eugene_task1_NumWords )
2+
3+
message( STATUS "-- " ${PROJECT_NAME} )
4+
add_executable( ${PROJECT_NAME} main.cpp )
5+
6+
if( MPI_COMPILE_FLAGS )
7+
set_target_properties( ${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${MPI_COMPILE_FLAGS}" )
8+
endif( MPI_COMPILE_FLAGS )
9+
10+
if( MPI_LINK_FLAGS )
11+
set_target_properties( ${PROJECT_NAME} PROPERTIES LINK_FLAGS "${MPI_LINK_FLAGS}" )
12+
endif( MPI_LINK_FLAGS )
13+
14+
target_link_libraries( ${PROJECT_NAME} ${MPI_LIBRARIES} )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <iostream>
2+
#include <mpi.h>
3+
#include <string.h>
4+
#include <string>
5+
#include <ctime>
6+
#include <cstdlib>
7+
#include <assert.h>
8+
using namespace std;
9+
10+
int converter_in_number(const string &s)
11+
{
12+
int len = s.length();
13+
int a = 0;
14+
int i = 0;
15+
for (i = 0; (i < len); i++)
16+
a = a * 10 + (s[i] - '0');
17+
return a;
18+
}
19+
20+
int main(int argc, char *argv[])
21+
{
22+
srand((int)time(0));
23+
24+
string size = argv[1];
25+
string l = "qwertyuiop asdfghjkl zxcvbnm";
26+
int lenl = l.length();
27+
int len = converter_in_number(size);
28+
len = len * 3;
29+
char* s = new char[len + 1];
30+
int k = 0;
31+
while (k < len - 2)
32+
{
33+
s[k] = 'a';
34+
s[k + 1] = l[(int)(rand() % lenl)];
35+
s[k + 2] = 'a';
36+
k = k + 3;
37+
}
38+
39+
int ProcNum, ProcRank;
40+
double stime = 0.0;
41+
double etime = 0.0;
42+
double stime_ = 0.0;
43+
double etime_ = 0.0;
44+
int nword = 0;
45+
int nword_ = 0;
46+
int nresword = 0;
47+
int residue = 0;
48+
49+
MPI_Init(&argc, &argv);
50+
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
51+
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
52+
53+
if (ProcRank == 0)
54+
{
55+
56+
stime = MPI_Wtime();
57+
for (int i = 0; i < len; i++)
58+
if (s[i] == ' ')
59+
nword++;
60+
nword++;
61+
etime = MPI_Wtime();
62+
cout << "1 process" << endl;
63+
cout << "Number words: " << nword << endl;
64+
cout << "Time: " << etime - stime << " sec" << endl;
65+
stime_ = MPI_Wtime();
66+
}
67+
68+
MPI_Bcast(s, len, MPI_CHAR, 0, MPI_COMM_WORLD);
69+
70+
for (int i = len / ProcNum * ProcRank; i < len / ProcNum + len / ProcNum * ProcRank; i++)
71+
if (s[i] == ' ')
72+
nword_++;
73+
74+
MPI_Reduce(&nword_, &nresword, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
75+
76+
if (ProcRank == 0)
77+
{
78+
residue = len % ProcNum;
79+
if (residue)
80+
for (int i = len - residue; i < len; i++)
81+
if (s[i] == ' ')
82+
nresword++;
83+
nresword++;
84+
etime_ = MPI_Wtime();
85+
cout << ProcNum << " process" << endl;
86+
cout << "Number words: " << nresword << endl;
87+
cout << "Time: " << etime_ - stime_ << " sec" << endl;
88+
}
89+
90+
MPI_Finalize();
91+
delete[] s;
92+
return 0;
93+
}

0 commit comments

Comments
 (0)