-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_mpi_program.cpp
163 lines (146 loc) · 3.79 KB
/
example_mpi_program.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <cmath>
#include <mpi.h>
#include <cassert>
#include <random>
//enum communication
//{
// Chain,
//};
const int MAX_LOOP_LEN=100000000; // maximum of send_size
const int MAX_LOOPS=1000000; // MAX LOOPS LIMIMT
int send_size=100000000; // comm time
int loop_len=1; // time of every calc()
int cnt_loops=100; // real loop times
double calc_time=1;
int mpi_size,mpi_rank,mpi_size_per_node;
double start_time,start_time2,end_time;
struct Timestamp
{
double before_calc, before_comm, before_PMPI, after_comm;
}local_time[MAX_LOOPS];
int cnt_iter;
double calc_result[MAX_LOOP_LEN];
double recv_buffer[MAX_LOOP_LEN];
std::minstd_rand rand_engine;
//int calc_op() //{
// int x=1,y=2,z=3;
// for (int i=0;i<100000;++i)
// {
// z=x*y;
// x=y*z;
// y=x*z;
// }
// return x+y+z;
//}
int calc_smallop() {
int x=1,y=2,z=3;
for (int i=0;i<100000;++i)
{
z=x*y;
x=y*z;
y=x*z;
}
return x+y+z;
}
int calc_op()// cost 0.6s
{
int t;
int sum=0;
for (int i=0;i<20*calc_time/0.6;++i)
{
t=calc_smallop();
sum+=t;
}
return sum;
}
void calc()
{
calc_result[0]=0;
// DEBUG: skewed overload
static int cnt=0;
++cnt;
int r = 0;
if (mpi_rank < 128)
{
r = loop_len * 40 / (cnt / 10 + 4);
} else
{
r = loop_len * 40 / (0 + 4);
}
for (int i = 0; i < r; ++i)
// for (int i = 0; i < loop_len * 40 / (rand()%mpi_size + 4); ++i)
{
//calc_result[j]+=int(calc_result[j-1]+int(sin(sin(rand_engine())))*10000)%123123;
calc_result[i] = calc_op();
}
}
void comm(int iter)
{
cnt_iter=iter;
// fprintf(stderr, "rank=%d target rank=%d\n", mpi_rank, mpi_rank + mpi_size_per_node);
#ifdef SEND_RECV
if (mpi_rank<mpi_size_per_node)
{
MPI_Send(calc_result, send_size, MPI_DOUBLE, mpi_rank + mpi_size_per_node, 0, MPI_COMM_WORLD);
}
else
{
MPI_Status status;
MPI_Recv(recv_buffer, send_size, MPI_DOUBLE, mpi_rank - mpi_size_per_node, 0, MPI_COMM_WORLD, &status);
}
#else // ALL_REDUCE
MPI_Allreduce(calc_result, recv_buffer, send_size, MPI_DOUBLE, MPI_SUM,
MPI_COMM_WORLD);
#endif
}
int main(int argc, char* argv[])
{
if (argc!=4)
{
fprintf(stderr,"argc != 4");
return -1;
}
MPI_Init(nullptr, nullptr);
MPI_Barrier(MPI_COMM_WORLD);
send_size=atoi(argv[1]);
//double send_time=atof(argv[1]);
//loop_len=atoi(argv[2]);
calc_time=atof(argv[2]);
cnt_loops=atoi(argv[3]);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Comm_size(MPI_COMM_WORLD,&mpi_size);
MPI_Comm_rank(MPI_COMM_WORLD,&mpi_rank);
mpi_size_per_node=mpi_size/2;
MPI_Barrier(MPI_COMM_WORLD);
start_time=MPI_Wtime();
MPI_Barrier(MPI_COMM_WORLD);
start_time2=MPI_Wtime();
for (int i=0;i<cnt_loops;++i)
{
// fprintf(stderr,"start calc %d\n",i);//DEBUG
local_time[i].before_calc=MPI_Wtime();
calc();
local_time[i].before_comm=MPI_Wtime();
comm(i);
local_time[i].after_comm=MPI_Wtime();
// fprintf(stderr,"finish calc %d\n",i);//DEBUG
}
MPI_Barrier(MPI_COMM_WORLD);
end_time=MPI_Wtime();
MPI_Datatype mpi_timestamp;
int length[1]={4};
MPI_Aint disp[1]={0};
MPI_Datatype types[1]={MPI_DOUBLE};
MPI_Type_create_struct(1, length, disp, types, &mpi_timestamp);
MPI_Type_commit(&mpi_timestamp);
if (mpi_rank==0)
{
fprintf(stderr, "Wall time: %lf\n", end_time - start_time);
//fprintf(stdout, "Wall time: %lf\n", end_time - start_time);
fprintf(stderr, "3~Wall time: %lf\n", end_time - local_time[2].before_calc);
}
MPI_Finalize();
//fprintf(stderr, "Rank %d finalized.\n", mpi_rank);
return 0;
}