-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid.cc
executable file
·347 lines (307 loc) · 10.4 KB
/
grid.cc
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "grid.h"
#define RRANK 0
#define DEBUG_GRID 0
namespace RRR{
using namespace std;
// Constructor
Grid::Grid(int num_dims, int* phy_grid)
{
grid_dims = num_dims;
pgrid = new int[grid_dims+1];
memcpy(pgrid, phy_grid, grid_dims * sizeof(int));
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
#ifdef __bgq__
MPIX_Hardware(&hw);
bgq_torus_dims = hw.torus_dimension;
actual_grid = new int[bgq_torus_dims];
memcpy(actual_grid, hw.Size, bgq_torus_dims * sizeof(int));
is_torus = new int[bgq_torus_dims];
memcpy(is_torus, hw.isTorus, bgq_torus_dims * sizeof(int));
actual_addr = new int[bgq_torus_dims];
memcpy(actual_addr, hw.Coords, bgq_torus_dims * sizeof(int));
create_gdmap();
compute_exposed_addr(proc_addr, actual_addr);
#else
get_proc_addr(rank, proc_addr);
#endif
}
#ifdef __bgq__
// Create a mapping from allocated grid dimensions to the exposed grid dimensions
void Grid::create_gdmap()
{
gdmap = new int[bgq_torus_dims];
igdmap = new list<int>[grid_dims];
for(int i=0; i<bgq_torus_dims; i++) gdmap[i] = -1;
for(int i=0; i<grid_dims; i++) igdmap[i] = list<int>();
int* temp = new int[grid_dims];
for(int i=0; i<grid_dims; i++) temp[i] = 1;
// Look for the dimensions having end processors connected with a link
// and map them directly to the exposed grid dimensions, so that we
// use these links
for(int i=bgq_torus_dims-1; i>=0; i--)
{
if(is_torus[i] == 1)
{
for(int j=grid_dims-1; j>=0; j--)
{
if(actual_grid[i] == pgrid[j] && temp[j] < pgrid[j])
{
gdmap[i] = j;
igdmap[j].push_back(i);
temp[j] *= actual_grid[i];
break;
}
}
}
}
/*if(rank == 0)
{
cout << "actual grid: "; print_tile_addr(bgq_torus_dims, actual_grid);
cout << endl << "istorus: "; print_tile_addr(bgq_torus_dims, is_torus);
cout << endl << "temp: "; print_tile_addr(grid_dims, temp);
cout << endl << "GDMAP: "; print_tile_addr(bgq_torus_dims, gdmap);
cout << endl;
}*/
// Now look for unsatisfied dimensions in the exposed grid and map
// allocated grid dimensions to them
for(int i=grid_dims-1; i>=0; i--)
{
if(pgrid[i] != temp[i])
{
for(int j=bgq_torus_dims-1; j>=0; j--)
{
if(gdmap[j] == -1 && pgrid[i] >= temp[i] * actual_grid[j])
{
gdmap[j] = i;
igdmap[i].push_back(j);
temp[i] *= actual_grid[j];
}
}
}
}
if(rank==RRANK && DEBUG_GRID)
{
cout << "actual grid: "; print_tile_addr(bgq_torus_dims, actual_grid);
cout << endl << "istorus: "; print_tile_addr(bgq_torus_dims, is_torus);
cout << endl << "temp: "; print_tile_addr(grid_dims, temp);
cout << endl << "GDMAP: "; print_tile_addr(bgq_torus_dims, gdmap);
cout << endl << "IGDMAP: ";
cout << endl << "PGRID: "; print_tile_addr(grid_dims,pgrid);
for(int i=0; i<grid_dims; i++)
{
cout << " ";
for(list<int>::iterator it = igdmap[i].begin(); it != igdmap[i].end(); it++)
cout << *it << ",";
}
cout << endl;
}
// Assert that all dimensions are satisfied
for(int i=0; i<grid_dims; i++)
{
assert(pgrid[i] == temp[i]);
}
}
// Compute processor address in exposed grid given its actual address
void Grid::compute_exposed_addr(int* &exposed_address, int* &actual_address)
{
exposed_address = new int[grid_dims];
for(int i=0; i<grid_dims; i++)
{
// If the exposed grid dimension is mapped to only one actual grid dimension,
// just pick the index in the actual address
if(igdmap[i].size() == 1)
{
exposed_address[i] = actual_address[igdmap[i].front()];
}
// Otherwise multiple dimensions of actual grid correspond to one of exposed.
// Form an exposed index based on all the indices of mapped actual grid address dimensions
else
{
int multiplier = 1;
exposed_address[i] = 0;
for(list<int>::iterator it = igdmap[i].begin(); it != igdmap[i].end(); it++)
{
exposed_address[i] += actual_address[*it] * multiplier;
// If there is an element in the list after this one
// and the next one is odd, then we have to reverse the order of enumeration
if(distance(it,igdmap[i].begin()) < igdmap[i].size()-1 &&
actual_address[*(next_it(it,1))] % 2 == 1)
{
exposed_address[i] = exposed_address[i] - 2*actual_address[*it] + actual_grid[*it] - 1;
}
multiplier *= actual_grid[*it];
}
}
}
//cout << rank << " coords: "; print_tile_addr(bgq_torus_dims, actual_address);
//cout << " new coords: "; print_tile_addr(grid_dims, exposed_address); cout <<endl;
}
list<int>::iterator Grid::next_it(list<int>::iterator it, int n)
{
list<int>::iterator iter = it;
for(int i=0; i<n; i++)
iter++;
return iter;
}
// Compute processor address and rank in new grid
void Grid::compute_actual_addr(int* &actual_address, int* &exposed_address)
{
actual_address = new int[bgq_torus_dims];
bool* computed = new bool[bgq_torus_dims];
for(int i=0; i<bgq_torus_dims; i++) computed[i] = false;
for(int i=0; i<bgq_torus_dims; i++)
{
if(!computed[i])
{
// If the actual grid dimension is mapped to only one exposed grid dimension,
// just pick the index in the exposed address
if(igdmap[gdmap[i]].size() == 1)
{
actual_address[i] = exposed_address[gdmap[i]];
computed[i] = true;
}
// Otherwise multiple dimensions of actual grid correspond to one of exposed.
// Form the actual indices based on the index of mapped exposed grid address dimensions
else
{
int k = exposed_address[gdmap[i]];
int n = igdmap[gdmap[i]].size();
// Extract the problem into smaller and easier to manipulate set of variables
int* size = new int[n];
int* addr = new int[n];
int x = 1, j = 0;
// Extract size of the problem sub-grid
for(list<int>::iterator it = igdmap[gdmap[i]].begin(); it != igdmap[gdmap[i]].end(); it++)
{
size[j] = actual_grid[*it];
x *= size[j];
j++;
}
// Compute address
for(int j=n-1; j>=0; j--)
{
x /= size[j];
addr[j] = k/x;
k -= addr[j] * x;
if(j<n-1 && addr[j+1]%2 == 1)
{
addr[j] = size[j] - addr[j] - 1;
}
}
// Fill the address back in in the backward direction
// (because while computing exposed address, we computed that way)
j = 0;
for(list<int>::iterator it = igdmap[gdmap[i]].begin(); it != igdmap[gdmap[i]].end(); it++)
{
actual_address[*it] = addr[j++];
computed[*it] = true;
}
}
}
}
//cout << rank << " actual address: "; print_tile_addr(bgq_torus_dims, actual_addr);
//cout << " Recomputed actual address: "; print_tile_addr(bgq_torus_dims, actual_address); cout<<endl;
}
#endif
// Returns rank of a processor given its multi-dimensional address
int Grid::get_proc_rank(int* &address)
{
int* addr = address;
int dims = grid_dims;
int* grid = pgrid;
#ifdef __bgq__
compute_actual_addr(addr, address);
//cout << " actual_addr: "; print_tile_addr(bgq_torus_dims, addr); cout << endl;
dims = bgq_torus_dims;
grid = actual_grid;
#endif
int x = 1;
int r = 0;
for(int i=dims-1; i>=0; i--)
{
r += addr[i] * x;
x *= grid[i];
}
//cout << "rr = " << r << endl;
return r;
}
// Finds out ranks of n processors given their multi-dimensional addresses
void Grid::get_proc_ranks(int n, int** &addresses, int* &ranks)
{
ranks = new int[n];
for(int r=0; r<n; r++)
{
ranks[r] = get_proc_rank(addresses[r]);
}
}
// Computes a processor's multi-dimensional address given its rank
void Grid::get_proc_addr(int r, int* &address)
{
#ifndef __bgq__
address = new int[grid_dims];
#endif
int dims = grid_dims;
int* addr = address;
int* grid = pgrid;
#ifdef __bgq__
dims = bgq_torus_dims;
addr = new int[bgq_torus_dims];
grid = actual_grid;
#endif
int x = 1;
for(int i=0; i<dims; i++)
x *= grid[i];
for(int i=0; i<dims; i++)
{
x /= grid[i];
addr[i] = r/x;
r -= addr[i] * x;
}
#ifdef __bgq__
compute_exposed_addr(address, addr);
#endif
}
// Computes n processors' multi-dimensional addresses given their ranks
void Grid::get_proc_addrs(int n, int** &addresses, int* &ranks)
{
for(int r=0; r<n; r++)
{
get_proc_addr(ranks[r], addresses[r]);
}
}
// prints address of a processor given its rank
void Grid::print_proc_addr(int r)
{
cout << "node address is : " << get_proc_addr_str(r) << endl;
}
// Returns the string of the processor's multi-dimensional address, given its rank
string Grid::get_proc_addr_str(int r)
{
stringstream proc_addr_str;
int* address;
get_proc_addr(r, address);
proc_addr_str << "[ ";
for(int i=0; i<grid_dims; i++)
{
proc_addr_str << address[i];
if(i < grid_dims-1)
proc_addr_str << ", ";
}
proc_addr_str<<"]";
return proc_addr_str.str();
}
void Grid::printInfo(){
cout<<"NUmber of Processors is "<<nprocs<<endl;
cout<<"Grid Dimensions is "<<grid_dims<<endl;
cout<<"Processor rank is "<<rank<<endl;
cout<<"Grid size is [ ";
for(int i =0; i<grid_dims; i++)
cout<<pgrid[i]<<", ";
cout<<"]"<<endl;
cout<<"Proc address is [ ";
for(int i =0; i<grid_dims; i++)
cout<<proc_addr[i]<<", ";
cout<<"]"<<endl<<endl;
}
}