-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpi_section.cpp
More file actions
104 lines (49 loc) · 1.34 KB
/
mpi_section.cpp
File metadata and controls
104 lines (49 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
#include<mpi.h>
#include<unistd.h> // this has being included to use the function sleep
// The function if will be used to control which function is called by which process in the MPI process team.
void times_table(int n)
{
int rank = MPI::COMM_WORLD.Get_rank(); // this return the rank of the calling process
for (int i=1;i<=n; ++i)
{
int i_times_n = i*n;
std::cout<< "Process " << rank <<" says "<< i << " times "<< n<< " equals " << i_times_n << std::endl;
sleep(1); //to add one second pause
}
}
void count_down()
{
int rank = MPI::COMM_WORLD.Get_rank();
for (int i=10; i>=1; --i){
std::cout<<"Process "<< rank <<" says " << i << "...\n";
sleep(1);
}
std:: cout << "Process " << rank << " says \"Lift off!\"n";
}
void long_loop()
{
double sum= 0;
int rank =MPI:: COMM_WORLD.Get_rank();
for (int i=1; i<=10;++i)
{
sum +=(i*i);
sleep(1);
}
std::cout<<"Process " << rank<<" says the sum of the long loop is " << sum << std::endl;
}
int main(int argc, char **argv){
MPI::Init(argc,argv);
int rank = MPI::COMM_WORLD.Get_rank();
if(rank==0){
std::cout<<"This is the main process \n ";
times_table(12);
}
else if (rank=1){
count_down();
}
else if(rank=2){ long_loop();}
else { std::cout<<"I am not needed ...\n";}
MPI::Finalize();
return 0;
}