-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtranspose_helper.cc
More file actions
executable file
·410 lines (326 loc) · 14 KB
/
Copy pathtranspose_helper.cc
File metadata and controls
executable file
·410 lines (326 loc) · 14 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#define TRANSPOSE_HELPER_FILE
#include "transpose_helper.h"
#include <iostream>
#define DEBUG_T 0
#define DEBUG_TT 0
#define RRANK 7
namespace RRR{
using namespace std;
/*creates a 2D matrix from a list of multidimensional tensor tiles
this function works in the following way. It takes each block and
places its data in the appropriate position in the 2D matrix. Each
tensor block has multiple indices, which can be divided into row and
column indices. The input tiles are arranges so that the row indices
appear first and the column indices appear later. The input variable
num_row_dim gives the number of indices that corresponds to rows.
Using this, a row rank and a column rank is generated for each tile
which is then used to indetify the correct position of the data in
each tile in the 2D matrix. */
void create_big_matrix(Tensor* &T, double* &tensor_tiles, int* &permuted_address,
int* &p_map_T, int num_tiles,int row_per_block, int col_per_block,
int num_row_dim, map<int, map<int, int> >* &big_matrix_map,
double* &big_matrix)
{
big_matrix_map = new map<int, map<int, int> >();
//Pack the individual blocks of C to a big 2D matrix with rows from A and columns from B
//This packing allows calling of dgemm on similarly created matrix A and matrix B
//The goal is to run a single parallel huge dgemm instead of running multiple dgemms
int dims_T = T->get_dims();
//create a permuted tensor grid for C based on the permutation map.
//this allows easily accessing ranks of different tiles of C based on tile
//address. Ranks of tiles are simply a single dimensional integer address
//used to identify blocks
int* grid_T = new int[dims_T];
for(int d = 0; d< dims_T; d++)
{
grid_T[p_map_T[d]] = T->get_vgrid()[d];
}
Block_Grid* T_grid = new Block_Grid(dims_T, grid_T);
//temp_T_addr holds the address of the tile of C in the permuted order
//permuted order is the one where the external indices of A appear first
//then the external indices of B appear later for example C[i,m,n,j]
//where i and j are from A and m and n are from B gets permuted to C[i,j,m,n]
int* temp_T_addr = new int[dims_T];
if(DEBUG_T) {
cout<<"Rank : "<<T->get_rank()<<" ";
print_tile_addrs(dims_T, permuted_address, num_tiles);
cout<<"Rank : "<<T->get_rank()<<" Num Row Dim "<<num_row_dim<<endl;
}
//for each tile in C create a map to the placement in the large matrix
for(int i=0; i < num_tiles; i++)
{
int* T_addr = permuted_address + i * dims_T;
//only the external indices of A are kept since these get
//translated into row indices
memset(temp_T_addr, 0 , dims_T * sizeof(int));
//if(T->get_rank() == 0) cout<<"Num row dims is "<<num_row_dim<<endl;
//find the ordering for column
for(int j = 0; j<num_row_dim; j++)
{
temp_T_addr[j] = T_addr[j];
}
int T_row_rank = T_grid->get_block_rank(temp_T_addr);
//if(T->get_rank() == 0) cout<<"T row rank "<<T_row_rank<<endl;
memset(temp_T_addr, 0 , dims_T * sizeof(int));
//find the ordering for column
for(int j = num_row_dim; j<dims_T; j++)
{
temp_T_addr[j] = T_addr[j];
}
int T_column_rank = T_grid->get_block_rank(temp_T_addr);
//insert the row and column address in the big_matrix_map
if( big_matrix_map->find(T_row_rank) == big_matrix_map->end())
{
//if(T->get_rank() == 0) cout<<"first if "<<T_column_rank<<endl;
map<int, int> col_T;
col_T[T_column_rank] = i;
//col_T.insert(std::pair<int,int>(T_column_rank, i));
big_matrix_map->insert(std::pair<int, map<int, int> >(T_row_rank, col_T));
}
else
{
//if(T->get_rank() == 0) cout<<"else "<<T_column_rank<<endl;
big_matrix_map->find(T_row_rank)
->second.insert(std::pair<int,int>(T_column_rank, i));
}
}
big_matrix = new double[num_tiles * T->get_block_size()];
if(DEBUG_TT) cout<<"Rank : "<<T->get_rank()<<" size created "<<num_tiles * T->get_block_size()<<endl;
int row_count =0;
int column_count =0;
int total_rows = big_matrix_map->size() * row_per_block;
int total_column = big_matrix_map->begin()->second.size() * col_per_block;
int previous_col_count, current_col_count;
previous_col_count = total_column / col_per_block;
//Copy data from the tile representation to the big C matrix
for (std::map<int,map <int,int> >::iterator row_it=big_matrix_map->begin(); row_it!=big_matrix_map->end(); ++row_it)
{
map <int, int> col = row_it->second;
current_col_count = col.size();
//makes sure the number of column stays the same
assert(current_col_count == previous_col_count);
previous_col_count = current_col_count;
column_count = 0;
//Copy a single column
for (std::map<int,int>::iterator col_it=col.begin(); col_it!=col.end(); ++col_it)
{
int tile_number = col_it->second;
double* current_tile = tensor_tiles + tile_number * T->block_size;
//Prints out the block indices in the order used to create the big matrix
if(DEBUG_T && T->get_rank()==RRANK)
{
int row_rank = row_it->first;
int col_rank = col_it->first;
int *row_address, *col_address, *full_addr;
T_grid->get_block_addr(row_rank, row_address);
T_grid->get_block_addr(col_rank, col_address);
full_addr = new int[dims_T];
for(int i = 0; i<dims_T; i++)
{
full_addr[i] = row_address[i] + col_address[i];
}
print_tile_addr(dims_T, full_addr);
}
for(int row_num = row_count; row_num < row_count + row_per_block; row_num++)
{
double* source = current_tile + (row_num - row_count) * col_per_block;
double* dest = big_matrix + row_num * total_column + column_count;
memcpy(dest, source, col_per_block * sizeof(double));
}
column_count += col_per_block;
}
if(DEBUG_T && T->get_rank()==RRANK)
{
cout<<endl;
}
row_count += row_per_block;
}
}
/*creates a 2D matrix from a list of multidimensional tensor tiles
this function works in the following way. It takes each block and
places its data in the appropriate position in the 2D matrix. Each
tensor block has multiple indices, which can be divided into row and
column indices. The input tiles are arranges so that the row indices
appear first and the column indices appear later. The input variable
num_row_dim gives the number of indices that corresponds to rows.
Using this, a row rank and a column rank is generated for each tile
which is then used to indetify the correct position of the data in
each tile in the 2D matrix. */
void create_big_matrix_nl(Tensor* &T, double* &tensor_tiles, int* &permuted_address,
int* &p_map_T, int num_tiles,int row_per_block, int col_per_block,
int num_row_dim, map<int, map<int, int> >* &big_matrix_map,
double* &big_matrix, map<int,int>* &address_to_location)
{
big_matrix_map = new map<int, map<int, int> >();
//Pack the individual blocks of C to a big 2D matrix with rows from A and columns from B
//This packing allows calling of dgemm on similarly created matrix A and matrix B
//The goal is to run a single parallel huge dgemm instead of running multiple dgemms
int dims_T = T->get_dims();
//create a permuted tensor grid for C based on the permutation map.
//this allows easily accessing ranks of different tiles of C based on tile
//address. Ranks of tiles are simply a single dimensional integer address
//used to identify blocks
int* grid_T = new int[dims_T];
for(int d = 0; d< dims_T; d++)
{
grid_T[p_map_T[d]] = T->get_vgrid()[d];
}
Block_Grid* T_grid = new Block_Grid(dims_T, grid_T);
//temp_T_addr holds the address of the tile of C in the permuted order
//permuted order is the one where the external indices of A appear first
//then the external indices of B appear later for example C[i,m,n,j]
//where i and j are from A and m and n are from B gets permuted to C[i,j,m,n]
int* temp_T_addr = new int[dims_T];
int location, tile_rank;
//for each tile in C create a map to the placement in the large matrix
for(int i=0; i < num_tiles; i++)
{
int* T_addr = permuted_address + i * dims_T;
//only the external indices of A are kept since these get
//translated into row indices
memset(temp_T_addr, 0 , dims_T * sizeof(int));
//find the ordering for column
for(int j = 0; j<num_row_dim; j++)
{
temp_T_addr[j] = T_addr[j];
}
int T_row_rank = T_grid->get_block_rank(temp_T_addr);
memset(temp_T_addr, 0 , dims_T * sizeof(int));
//find the ordering for column
for(int j = num_row_dim; j<dims_T; j++)
{
temp_T_addr[j] = T_addr[j];
}
int T_column_rank = T_grid->get_block_rank(temp_T_addr);
//tile rank gives the rank of the current tile under scrutiny
tile_rank = T_grid->get_block_rank(T_addr);
/* the address_to_location map needs to have a location
* for this tile. Otherwise something must have gone
* wrong*/
assert(address_to_location->find(tile_rank)
!= address_to_location->end());
/*Store the location of this tile. The location number is
* used to locate the data tile to create the big matrix*/
location = address_to_location->find(tile_rank)->second;
//insert the row and column address in the big_matrix_map
if( big_matrix_map->find(T_row_rank) == big_matrix_map->end())
{
map<int, int> col_T;
col_T[T_column_rank] = location;
//col_T.insert(std::pair<int,int>(T_column_rank, location));
big_matrix_map->insert(std::pair<int, map<int, int> >(T_row_rank, col_T));
}
else
{
big_matrix_map->find(T_row_rank)
->second.insert(std::pair<int,int>(T_column_rank, location));
}
}
big_matrix = new double[num_tiles * T->get_block_size()];
int row_count =0;
int column_count =0;
int total_rows = big_matrix_map->size() * row_per_block;
int total_column = big_matrix_map->begin()->second.size() * col_per_block;
int previous_col_count, current_col_count;
previous_col_count = total_column / col_per_block;
//Copy data from the tile representation to the big C matrix
for (std::map<int,map <int,int> >::iterator row_it=big_matrix_map->begin(); row_it!=big_matrix_map->end(); ++row_it)
{
map <int, int> col = row_it->second;
current_col_count = col.size();
//makes sure the number of column stays the same
assert(current_col_count == previous_col_count);
previous_col_count = current_col_count;
column_count = 0;
//Copy a single column
for (std::map<int,int>::iterator col_it=col.begin(); col_it!=col.end(); ++col_it)
{
int tile_number = col_it->second;
double* current_tile = tensor_tiles + tile_number * T->block_size;
//Prints out the block indices in the order used to create the big matrix
if(DEBUG_T)
{
int row_rank = row_it->first;
int col_rank = col_it->first;
int *row_address, *col_address;
T_grid->get_block_addr(row_rank, row_address);
T_grid->get_block_addr(col_rank, col_address);
print_tile_addr(dims_T, row_address);
print_tile_addr(dims_T, col_address);
cout<<endl;
}
for(int row_num = row_count; row_num < row_count + row_per_block; row_num++)
{
double* source = current_tile + (row_num - row_count) * col_per_block;
double* dest = big_matrix + row_num * total_column + column_count;
memcpy(dest, source, col_per_block * sizeof(double));
}
column_count += col_per_block;
}
row_count += row_per_block;
}
}
/*Convert the 2D matrix, back to the multidimensional tile*/
void revert_big_matrix(Tensor* &T, map<int, map<int, int> >* &big_matrix_map,
double* &big_matrix, int row_per_block, int col_per_block,
int* &p_map_T, double* &tensor_tiles)
{
//Pack the individual blocks of C to a big 2D matrix with rows from A and columns from B
//This packing allows calling of dgemm on similarly created matrix A and matrix B
//The goal is to run a single parallel huge dgemm instead of running multiple dgemms
int dims_T = T->get_dims();
//create a permuted tensor grid for C based on the permutation map.
//this allows easily accessing ranks of different tiles of C based on tile
//address. Ranks of tiles are simply a single dimensional integer address
//used to identify blocks
int* grid_T = new int[dims_T];
for(int d = 0; d< dims_T; d++)
{
grid_T[p_map_T[d]] = T->get_vgrid()[d];
}
Block_Grid* T_grid = new Block_Grid(dims_T, grid_T);
int row_count =0;
int column_count =0;
int total_rows = big_matrix_map->size() * row_per_block;
int total_column = big_matrix_map->begin()->second.size() * col_per_block;
//they are used to verify that the number of column blocks stays the same
int previous_col_count, current_col_count;
previous_col_count = total_column / col_per_block;
//Copy data from the tile representation to the big C matrix
for (std::map<int,map <int,int> >::iterator row_it=big_matrix_map->begin(); row_it!=big_matrix_map->end(); ++row_it)
{
map <int, int> col = row_it->second;
current_col_count = col.size();
//makes sure the number of column stays the same
assert(current_col_count == previous_col_count);
previous_col_count = current_col_count;
column_count = 0;
//Copy a single column
for (std::map<int,int>::iterator col_it=col.begin(); col_it!=col.end(); ++col_it)
{
int tile_number = col_it->second;
double* current_tile = tensor_tiles + tile_number * T->block_size;
//Prints out the block indices in the order used to create the big matrix
if(DEBUG_T && T->get_rank()==0)
{
int row_rank = row_it->first;
int col_rank = col_it->first;
int *row_address, *col_address;
T_grid->get_block_addr(row_rank, row_address);
T_grid->get_block_addr(col_rank, col_address);
print_tile_addr(dims_T, row_address);
print_tile_addr(dims_T, col_address);
cout<<endl;
}
for(int row_num = row_count; row_num < row_count + row_per_block; row_num++)
{
double* source = big_matrix + row_num * total_column + column_count;
double* dest = current_tile + (row_num - row_count) * col_per_block;
memcpy(dest, source, col_per_block * sizeof(double));
}
column_count += col_per_block;
}
row_count += row_per_block;
}
}
}