-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.c
261 lines (235 loc) · 7.51 KB
/
bench.c
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <sys/time.h>
#include <unistd.h>
#include <ctype.h>
#include <getopt.h>
#include "assignments.h"
#define INNER_COMM 0
#define OUTER_COMM 1
double Alltoall(MPI_Comm comm, int num_ranks, int rank, int msg_size, int num_time_steps){
//declare, initialize message space
char send[msg_size*num_ranks];
char recv[msg_size*num_ranks];
for(int i = 0; i < msg_size*num_ranks; i++) send[i] = rand() % 256;
//perform all-to-all
double start = MPI_Wtime();
for(int i = 0; i < num_time_steps; i++){
MPI_Alltoall(send, msg_size, MPI_CHAR, recv, msg_size, MPI_CHAR, comm);
}
double end = MPI_Wtime();
return end - start;
}
int main(int argc, char**argv){
int num_ranks, rank, split_num_ranks, split_rank;
int outer_ranks, inner_ranks;
int new_comm_id;
int msg_size, loops;
int slurm_id, run_index;
MPI_Comm split_comm;
FILE * timings, * configs;
int assignment;
int custom;
char c;
while ((c = getopt (argc, argv, "s:r:l:i:ac:")) != -1){
switch (c)
{
case 's':
sscanf(optarg, "%d", &msg_size);
break;
case 'r':
sscanf(optarg, "%d", &inner_ranks);
break;
case 'l':
sscanf(optarg, "%d", &loops);
break;
case 'i':
sscanf(optarg, "%d", &run_index);
break;
case 'a':
sscanf(optarg, "%d", &assignment);
assignment = 0;
break;
case 'c':
sscanf(optarg, "%d", &custom);
break;
default:
printf("Unrecognized option: %c\n", optopt);
break;
}
if(c != 's' && c != 'i' && c != 'l' && c != 'r' ){break;}
}
timings = fopen("timings.out", "a");
char configs_buf[128] = {0};
sprintf(configs_buf, "config-%d.out", run_index);
configs = fopen(configs_buf, "a");
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &num_ranks);
if(num_ranks == 0){
printf("MPI_Comm_size failure\n");
exit(1);
}
outer_ranks = num_ranks - inner_ranks;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
//get node names
char name[MPI_MAX_PROCESSOR_NAME] = {0};
char * recv = (char*)calloc(MPI_MAX_PROCESSOR_NAME*num_ranks, sizeof(char));
int proc_len;
MPI_Get_processor_name(name, &proc_len);
name[proc_len] = 0;
MPI_Gather(name, MPI_MAX_PROCESSOR_NAME, MPI_CHAR, recv, MPI_MAX_PROCESSOR_NAME, MPI_CHAR, 0, MPI_COMM_WORLD);
int * splitter = (int*)malloc(sizeof(int)*num_ranks);
for(int i = 0; i < num_ranks; i++) splitter[i] = OUTER_COMM;
if(!custom){
if(rank == 0){
if(assignment == RANDOM){
int num_assigned = 0;
while(num_assigned < inner_ranks){
int val = rand() % num_ranks;
if(splitter[val] == INNER_COMM){
continue;
}else{
splitter[val] = INNER_COMM;
num_assigned += 1;
}
}
}else if(assignment == APLANES){
for(int i = 0; i < num_ranks; i++){
int dims[5] = {0};
get_dim(recv + i*MPI_MAX_PROCESSOR_NAME, dims);
if (dims[0] == 0 || dims[0] == 2){
splitter[i] = INNER_COMM;
}
}
}else if(assignment == APLANES_COARSE){
for(int i = 0; i < num_ranks; i++){
int dims[5] = {0};
get_dim(recv + i*MPI_MAX_PROCESSOR_NAME, dims);
if (dims[0] == 0 || dims[0] == 1){
splitter[i] = INNER_COMM;
}
}
}else if(assignment == BPLANES){
for(int i = 0; i < num_ranks; i++){
int dims[5] = {0};
get_dim(recv + i*MPI_MAX_PROCESSOR_NAME, dims);
if (dims[1] == 0 || dims[1] == 2){
splitter[i] = INNER_COMM;
}
}
}else if(assignment == CPLANES){
for(int i = 0; i < num_ranks; i++){
int dims[5] = {0};
get_dim(recv + i*MPI_MAX_PROCESSOR_NAME, dims);
if (dims[2] == 0 || dims[2] == 2){
splitter[i] = INNER_COMM;
}
}
}else if(assignment == DPLANES){
for(int i = 0; i < num_ranks; i++){
int dims[5] = {0};
get_dim(recv + i*MPI_MAX_PROCESSOR_NAME, dims);
if (dims[3] == 0 || dims[3] == 2){
splitter[i] = INNER_COMM;
}
}
}else if(assignment == EPLANES){
for(int i = 0; i < num_ranks; i++){
int dims[5] = {0};
get_dim(recv + i*MPI_MAX_PROCESSOR_NAME, dims);
if (dims[4] == 0){
splitter[i] = INNER_COMM;
}
}
}else if(assignment == SQUAREAB1){
for(int i = 0; i < num_ranks; i++){
int dims[5] = {0};
get_dim(recv + i*MPI_MAX_PROCESSOR_NAME, dims);
if (((dims[0] == 0 || dims[0] == 1) && (dims[1] == 0 || dims[1] == 1)) ||
((dims[0] == 2 || dims[0] == 3) && (dims[1] == 2 || dims[1] == 3))){
splitter[i] = INNER_COMM;
}
}
}else if(assignment == SQUAREAB2){
for(int i = 0; i < num_ranks; i++){
int dims[5] = {0};
get_dim(recv + i*MPI_MAX_PROCESSOR_NAME, dims);
if (((dims[0] == 0 || dims[0] == 2) && (dims[1] == 0 || dims[1] == 2)) ||
((dims[0] == 1 || dims[0] == 3) && (dims[1] == 1 || dims[1] == 3))){
splitter[i] = INNER_COMM;
}
}
}else if(assignment == ALTERABC_NONE){
for(int i = 0; i < num_ranks; i++){
int dims[5] = {0};
get_dim(recv + i*MPI_MAX_PROCESSOR_NAME, dims);
if (((dims[0] == 0 || dims[0] == 1) && (dims[1] == 0 || dims[1] == 1) && (dims[2] == 0 || dims[2] == 1)) ||
((dims[0] == 2 || dims[0] == 3) && (dims[1] == 2 || dims[1] == 3) && (dims[2] == 0 || dims[2] == 1)) ||
((dims[0] == 0 || dims[0] == 1) && (dims[1] == 2 || dims[1] == 3) && (dims[2] == 2 || dims[2] == 3)) ||
((dims[0] == 2 || dims[0] == 3) && (dims[1] == 0 || dims[1] == 1) && (dims[2] == 2 || dims[2] == 3))) {
splitter[i] = INNER_COMM;
}
}
}else if(assignment == ALTERABC_ALL){
for(int i = 0; i < num_ranks; i++){
int dims[5] = {0};
get_dim(recv + i*MPI_MAX_PROCESSOR_NAME, dims);
if (((dims[0] == 0 || dims[0] == 2) && (dims[1] == 0 || dims[1] == 2) && (dims[2] == 0 || dims[2] == 2)) ||
((dims[0] == 1 || dims[0] == 3) && (dims[1] == 1 || dims[1] == 3) && (dims[2] == 0 || dims[2] == 2)) ||
((dims[0] == 0 || dims[0] == 2) && (dims[1] == 1 || dims[1] == 3) && (dims[2] == 1 || dims[2] == 3)) ||
((dims[0] == 1 || dims[0] == 3) && (dims[1] == 0 || dims[1] == 2) && (dims[2] == 1 || dims[2] == 3))) {
splitter[i] = INNER_COMM;
}
}
}
}
}else{ //using custon mapping in map.out
for(int i = 0; i < num_ranks/2; i++){
splitter[i] = INNER_COMM;
}
}
MPI_Bcast(splitter, num_ranks, MPI_INT, 0, MPI_COMM_WORLD);
//split communicator
MPI_Comm_split(MPI_COMM_WORLD, splitter[rank], 1, &split_comm);
MPI_Comm_size(split_comm, &split_num_ranks);
MPI_Comm_rank(split_comm, &split_rank);
MPI_Barrier(MPI_COMM_WORLD);
//print names to file
if(rank == 0){
fprintf(configs,"rank,comm,node\n");
for(int i = 0; i < num_ranks; i++){
fprintf(configs,"%d,%d,%s\n", i, splitter[i], recv + i*MPI_MAX_PROCESSOR_NAME);
}
}
//run the inner communicator as a warm-up, seems to reduce variance
if(splitter[rank] == INNER_COMM){
Alltoall(split_comm, split_num_ranks, split_rank, msg_size, loops);
}
MPI_Barrier(MPI_COMM_WORLD);
//run the inside alone, as a baseline
//start network counters region 1
MPI_Pcontrol(1);
float run1;
if(splitter[rank] == INNER_COMM){
run1 = Alltoall(split_comm, split_num_ranks, split_rank, msg_size, loops);
}
MPI_Barrier(MPI_COMM_WORLD);
//start network counters region 2
MPI_Pcontrol(2);
//run both communicators
float run2;
if(splitter[rank] == INNER_COMM){
run2 = Alltoall(split_comm, split_num_ranks, split_rank, msg_size, loops);
}else{
Alltoall(split_comm, split_num_ranks, split_rank, msg_size, loops);
}
//stop network counters
MPI_Pcontrol(0);
//print timings
if(splitter[rank] == INNER_COMM && split_rank==0) fprintf(timings, "%d,%f,%f\n", run_index, run1, run2);
//free(recv);
free(splitter);
MPI_Finalize();
exit(0);
}