-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpmatLocal.hpp
More file actions
607 lines (485 loc) · 15.8 KB
/
SpmatLocal.hpp
File metadata and controls
607 lines (485 loc) · 15.8 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#pragma once
#include <iostream>
#include <memory>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <algorithm>
#include <parallel/algorithm>
#include <mkl_spblas.h>
#include <mpi.h>
#include <string.h>
#include "common.h"
#include "CombBLAS/CombBLAS.h"
using namespace Eigen;
using namespace combblas;
using namespace std;
#define TAG_MULTIPLIER 10000
typedef enum {csr, coo, both} ShiftMode;
/**
* Some notes about ParallelReadMM given a 2D grid:
* - It re-indexes the local sparse matrices
* - The trailing processor along each dimension is slightly larger than
* the other processors.
*/
/*
* This class handles block distributions.
*/
class NonzeroDistribution {
public:
MPI_Comm world;
int rows_in_block, cols_in_block;
virtual int blockOwner(int row_block, int col_block) = 0;
/*
* Returns the processor that is supposed to own a particular nonzero.
*/
int getOwner(int r, int c, int transpose) {
if(! transpose) {
return blockOwner(r / rows_in_block, c / cols_in_block);
}
else {
return blockOwner(c / rows_in_block, r / cols_in_block);
}
}
};
class CSRHandle {
public:
vector<double> values;
vector<MKL_INT> col_idx;
vector<MKL_INT> rowStart;
vector<MKL_INT> row_idx;
sparse_matrix_t mkl_handle;
};
class CSRLocal {
public:
MKL_INT rows, cols;
int max_nnz, num_coords;
bool transpose;
int active;
CSRHandle* buffer;
/*
* TODO: Need to check this function for memory leaks!
*/
CSRLocal(MKL_INT rows, MKL_INT cols, MKL_INT max_nnz, spcoord_t* coords, int num_coords, bool transpose) {
this->transpose = transpose;
this->num_coords = num_coords;
this->rows = rows;
this->cols = cols;
this->buffer = new CSRHandle[2];
// This setup is really clunky, but I don't have time to fix it.
vector<MKL_INT> rArray(num_coords, 0.0);
vector<MKL_INT> cArray(num_coords, 0.0);
vector<double> vArray(num_coords, 0.0);
// Put a dummy value in if the number of coordinates is 0, so that everything doesn't
// blow up
if(num_coords == 0) {
rArray.push_back(0);
cArray.push_back(0);
vArray.push_back(0.0);
}
#pragma omp parallel for
for(int i = 0; i < num_coords; i++) {
rArray[i] = coords[i].r;
cArray[i] = coords[i].c;
vArray[i] = coords[i].value;
}
sparse_operation_t op;
if(transpose) {
op = SPARSE_OPERATION_TRANSPOSE;
}
else {
op = SPARSE_OPERATION_NON_TRANSPOSE;
}
sparse_matrix_t tempCOO, tempCSR;
mkl_sparse_d_create_coo(&tempCOO, SPARSE_INDEX_BASE_ZERO, rows, cols, max(num_coords, 1), rArray.data(), cArray.data(), vArray.data());
mkl_sparse_convert_csr(tempCOO, op, &tempCSR);
mkl_sparse_destroy(tempCOO);
vector<MKL_INT>().swap(rArray);
vector<MKL_INT>().swap(cArray);
vector<double>().swap(vArray);
sparse_index_base_t indexing;
MKL_INT *rows_start, *rows_end, *col_idx;
double *values;
mkl_sparse_d_export_csr(tempCSR,
&indexing,
&(this->rows),
&(this->cols),
&rows_start,
&rows_end,
&col_idx,
&values
);
int rv = 0;
for(int i = 0; i < num_coords; i++) {
while(rv < this->rows && i >= rows_start[rv + 1]) {
rv++;
}
coords[i].r = rv;
coords[i].c = col_idx[i];
coords[i].value = values[i];
}
active = 0;
assert(num_coords <= max_nnz);
for(int t = 0; t < 2; t++) {
buffer[t].values.resize(max_nnz == 0 ? 1 : max_nnz);
buffer[t].col_idx.resize(max_nnz == 0 ? 1 : max_nnz);
buffer[t].row_idx.resize(max_nnz == 0 ? 1 : max_nnz);
buffer[t].rowStart.resize(this->rows + 1);
// Copy over row indices
#pragma omp parallel for
for(int i = 0; i < num_coords; i++) {
buffer[t].row_idx[i] = coords[i].r;
}
memcpy(buffer[t].values.data(), values, sizeof(double) * max(num_coords, 1));
memcpy(buffer[t].col_idx.data(), col_idx, sizeof(MKL_INT) * max(num_coords, 1));
memcpy(buffer[t].rowStart.data(), rows_start, sizeof(MKL_INT) * this->rows);
buffer[t].rowStart[this->rows] = max(num_coords, 1);
mkl_sparse_d_create_csr(&(buffer[t].mkl_handle),
SPARSE_INDEX_BASE_ZERO,
this->rows,
this->cols,
buffer[t].rowStart.data(),
buffer[t].rowStart.data() + 1,
buffer[t].col_idx.data(),
buffer[t].values.data()
);
// This madness is just trying to get around the inspector routine
if(num_coords == 0) {
buffer[t].rowStart[this->rows] = 0;
}
}
mkl_sparse_destroy(tempCSR);
}
~CSRLocal() {
for(int t = 0; t < 2; t++) {
mkl_sparse_destroy(buffer[t].mkl_handle);
}
delete[] buffer;
}
/*
* Note: Input tag should be no greater than 10,000
*/
void shiftCSR(int src, int dst, MPI_Comm comm, int nnz_to_receive, int tag,
ShiftMode mode) {
CSRHandle* send = buffer + active;
CSRHandle* recv = buffer + 1 - active;
int proc_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &proc_rank);
MPI_Status stat;
int nnz_to_send = num_coords;
int recv_verify;
/*MPI_Sendrecv(&nnz_to_send, 1, MPI_INT,
dst, 0,
&recv_verify, 1, MPI_INT,
src, 0,
comm, &stat);
assert(recv_verify == nnz_to_receive);*/
MPI_Request vRequestSend, cRequestSend, ridxRequestSend, rRequestSend;
MPI_Request vRequestReceive, cRequestReceive, ridxRequestReceive, rRequestReceive;
MPI_Isend(send->values.data(), num_coords, MPI_DOUBLE, dst, tag * TAG_MULTIPLIER, comm, &vRequestSend);
MPI_Isend(send->col_idx.data(), num_coords, MPI_LONG, dst, tag * TAG_MULTIPLIER + 1, comm, &cRequestSend);
if(mode == csr || mode == both) {
MPI_Isend(send->rowStart.data(), rows + 1, MPI_LONG, dst, tag * TAG_MULTIPLIER + 2, comm, &ridxRequestSend);
}
if(mode == coo || mode == both) {
MPI_Isend(send->row_idx.data(), num_coords, MPI_LONG, dst, tag * TAG_MULTIPLIER + 3, comm, &rRequestSend);
}
if(mode == csr || mode == both) {
MPI_Irecv(recv->rowStart.data(), rows + 1, MPI_LONG, src, tag * TAG_MULTIPLIER + 2, comm, &ridxRequestReceive);
}
if(mode == coo || mode == both) {
MPI_Irecv(recv->row_idx.data(), nnz_to_receive, MPI_LONG, src, tag * TAG_MULTIPLIER + 3, comm, &rRequestReceive);
}
MPI_Irecv(recv->values.data(), nnz_to_receive, MPI_DOUBLE, src, tag * TAG_MULTIPLIER, comm, &vRequestReceive);
MPI_Irecv(recv->col_idx.data(), nnz_to_receive, MPI_LONG, src, tag * TAG_MULTIPLIER + 1, comm, &cRequestReceive);
MPI_Wait(&vRequestSend, &stat);
MPI_Wait(&vRequestReceive, &stat);
MPI_Wait(&cRequestSend, &stat);
MPI_Wait(&cRequestReceive, &stat);
if(mode == csr || mode == both) {
MPI_Wait(&ridxRequestSend, &stat);
MPI_Wait(&ridxRequestReceive, &stat);
}
else if (mode == coo || mode == both) {
MPI_Wait(&rRequestSend, &stat);
MPI_Wait(&rRequestReceive, &stat);
}
num_coords = nnz_to_receive;
active = 1 - active;
}
CSRHandle* getActive() {
return buffer + active;
}
};
class SpmatLocal {
public:
// This is redundant, but it makes coding more convenient.
// These are unzipped versions of the sparse matrix G.
vector<spcoord_t> coords;
/*
* Global properties of the distributed sparse matrix.
*/
uint64_t M;
uint64_t N;
uint64_t dist_nnz;
bool initialized;
// These are more specialized parameters
// A contiguous interval of coordinates that this processor is responsible for in its input;
// need to duplicate this for the transpose.
int owned_coords_start, owned_coords_end;
vector<int> layer_coords_start, layer_coords_sizes;
bool coordinate_ownership_initialized;
bool csr_initialized;
vector<uint64_t> blockStarts;
vector<CSRLocal*> csr_blocks;
SpmatLocal() {
initialized = false;
coordinate_ownership_initialized = false;
csr_initialized = false;
}
~SpmatLocal() {
for(int i = 0; i < csr_blocks.size(); i++) {
if(csr_blocks[i] != nullptr) {
delete csr_blocks[i];
}
}
}
/*
* We do not support shifting multiple blocks of nonzeros owned by processors, only
* a single block.
*/
void initializeCSRBlocks(int blockRows, int blockCols, int max_nnz, bool transpose) {
if(max_nnz == -1) {
for(int i = 0; i < blockStarts.size() - 1; i++) {
int num_coords = blockStarts[i + 1] - blockStarts[i];
if(num_coords > 0) {
CSRLocal* block
= new CSRLocal(blockRows, blockCols, num_coords, coords.data() + blockStarts[i], num_coords, transpose);
csr_blocks.push_back(block);
}
else {
csr_blocks.push_back(nullptr);
}
}
}
else {
int num_coords = blockStarts[1] - blockStarts[0];
CSRLocal* block = new CSRLocal(blockRows, blockCols, max_nnz, coords.data(), num_coords, transpose);
csr_blocks.push_back(block);
}
csr_initialized = true;
}
void own_all_coordinates() {
owned_coords_start = 0;
owned_coords_end = coords.size();
layer_coords_start.push_back(0);
layer_coords_start.push_back(coords.size());
layer_coords_sizes.push_back(coords.size());
coordinate_ownership_initialized = true;
}
void shard_across_layers(int num_layers, int current_layer) {
divideIntoSegments(coords.size(), num_layers, layer_coords_start, layer_coords_sizes);
owned_coords_start = layer_coords_start[current_layer];
owned_coords_end = layer_coords_start[current_layer + 1];
coordinate_ownership_initialized = true;
}
void unpack_tuples( SpTuples<int64_t,int> &tups,
vector<spcoord_t> &unpacked) {
tuple<int64_t, int64_t, int>* values = tups.tuples;
new (&unpacked) vector<spcoord_t>;
unpacked.resize(tups.getnnz());
for(int i = 0; i < tups.getnnz(); i++) {
unpacked[i].r = get<0>(values[i]);
unpacked[i].c = get<1>(values[i]);
unpacked[i].value = get<2>(values[i]);
}
}
/*
* This is a bad prefix sum function.
*/
void prefix_sum(vector<int> &values, vector<int> &offsets) {
int sum = 0;
for(int i = 0; i < values.size(); i++) {
offsets.push_back(sum);
sum += values[i];
}
}
/*
* Redistributes nonzeros according to the provided distribution, optionally transposing the matrix
* in the process. Works either in-place, or returns an entirely new sparse matrix.
*
* TODO: Write extra code to amortize away the process of a transpose.
*/
SpmatLocal* redistribute_nonzeros(NonzeroDistribution* dist, bool transpose, bool in_place) {
int num_procs, proc_rank;
MPI_Comm_size(dist->world, &num_procs);
MPI_Comm_rank(dist->world, &proc_rank);
vector<int> sendcounts(num_procs, 0);
vector<int> recvcounts(num_procs, 0);
vector<int> offsets, bufindices;
spcoord_t* sendbuf = new spcoord_t[coords.size()];
#pragma omp parallel for
for(int i = 0; i < coords.size(); i++) {
int owner = dist->getOwner(coords[i].r, coords[i].c, transpose);
#pragma omp atomic update
sendcounts[owner]++;
}
prefix_sum(sendcounts, offsets);
bufindices = offsets;
#pragma omp parallel for
for(int i = 0; i < coords.size(); i++) {
int owner = dist->getOwner(coords[i].r, coords[i].c, transpose);
int idx;
#pragma omp atomic capture
idx = bufindices[owner]++;
sendbuf[idx].r = transpose ? coords[i].c : coords[i].r;
sendbuf[idx].c = transpose ? coords[i].r : coords[i].c;
sendbuf[idx].value = coords[i].value;
}
// Broadcast the number of nonzeros that each processor is going to receive
MPI_Alltoall(sendcounts.data(), 1, MPI_INT, recvcounts.data(), 1,
MPI_INT, dist->world);
vector<int> recvoffsets;
prefix_sum(recvcounts, recvoffsets);
// Use the sizing information to execute an AlltoAll
int total_received_coords =
std::accumulate(recvcounts.begin(), recvcounts.end(), 0);
SpmatLocal* result;
if(in_place) {
result = this;
}
else {
result = new SpmatLocal();
}
result->M = transpose ? this->N : this->M;
result->N = transpose ? this->M : this->N;
result->dist_nnz = this->dist_nnz;
result->initialized = true;
(result->coords).resize(total_received_coords);
MPI_Alltoallv(sendbuf, sendcounts.data(), offsets.data(),
SPCOORD, (result->coords).data(), recvcounts.data(), recvoffsets.data(),
SPCOORD, dist->world
);
// TODO: Parallelize the sort routine?
//std::sort((result->coords).begin(), (result->coords).end(), column_major);
__gnu_parallel::sort((result->coords).begin(), (result->coords).end(), column_major);
delete[] sendbuf;
return result;
}
/*
* Distributes tuples arbitrarily among all processors.
*/
void loadTuples(bool readFromFile,
int logM,
int nnz_per_row,
string filename) {
MPI_Comm WORLD;
MPI_Comm_dup(MPI_COMM_WORLD, &WORLD);
int proc_rank, num_procs;
MPI_Comm_rank(WORLD, &proc_rank);
MPI_Comm_size(WORLD, &num_procs);
shared_ptr<CommGrid> simpleGrid;
simpleGrid.reset(new CommGrid(WORLD, num_procs, 1));
PSpMat_s32p64_Int * G;
uint64_t nnz;
if(readFromFile) {
G = new PSpMat_s32p64_Int(simpleGrid);
G->ParallelReadMM(filename, true, maximum<double>());
// Apply a random permutation for load balance
// Taken from CombBLAS (see Aydin's email)
//FullyDistVec<int64_t, array<char, MAXVERTNAME> > perm
// = G->ReadGeneralizedTuples(filename, maximum<double>());
nnz = G->getnnz();
if(proc_rank == 0) {
cout << "File reader read " << nnz << " nonzeros." << endl;
}
}
else { // This uses the Graph500 R-mat generator
DistEdgeList<int64_t> * DEL = new DistEdgeList<int64_t>(simpleGrid);
double initiator[4] = {0.25, 0.25, 0.25, 0.25};
unsigned long int scale = logM;
DEL->GenGraph500Data(initiator, scale, nnz_per_row);
PermEdges(*DEL);
RenameVertices(*DEL);
G = new PSpMat_s32p64_Int(*DEL, false);
delete DEL;
nnz = G->getnnz();
if(proc_rank == 0) {
cout << "R-mat generator created " << nnz << " nonzeros." << endl;
}
}
SpTuples<int64_t,int> tups(G->seq());
unpack_tuples(tups, coords);
this->M = G->getnrow();
this->N = G->getncol();
this->dist_nnz = nnz;
int rowIncrement = this->M / num_procs;
for(int i = 0; i < coords.size(); i++) {
coords[i].r += rowIncrement * proc_rank;
}
initialized = true;
delete G;
}
/*
* This method assumes the tuples are sorted in a column major order,
* and it also changes the column coordinates. DO NOT call this function
* unless you're sure there is no more work to do with the current sparse
* matrix.
*/
void divideIntoBlockCols(int blockWidth, int targetDivisions, bool modIndex) {
blockStarts.clear();
// Locate block starts within the local sparse matrix (i.e. divide a long
// block row into subtiles)
int currentStart = 0;
for(uint64_t i = 0; i < coords.size(); i++) {
while(coords[i].c >= currentStart) {
blockStarts.push_back(i);
currentStart += blockWidth;
}
// This modding step helps indexing.
if(modIndex) {
coords[i].c %= blockWidth;
}
}
assert(blockStarts.size() <= targetDivisions + 1);
while(blockStarts.size() < targetDivisions + 1) {
blockStarts.push_back(coords.size());
}
}
void monolithBlockColumn() {
blockStarts.clear();
blockStarts.push_back(0);
blockStarts.push_back(coords.size());
}
void setCSRValues(VectorXd &values) {
for(int i = 0; i < blockStarts.size() - 1; i++) {
if(csr_blocks[i] != nullptr) {
memcpy(csr_blocks[i]->getActive()->values.data(),
values.data() + blockStarts[i],
sizeof(double) * (blockStarts[i + 1] - blockStarts[i]));
}
}
}
VectorXd getCSRValues() {
VectorXd values = VectorXd::Constant(blockStarts[blockStarts.size() -1], 0.0);
for(int i = 0; i < blockStarts.size() - 1; i++) {
if(csr_blocks[i] != nullptr) {
memcpy(values.data() + blockStarts[i],
csr_blocks[i]->getActive()->values.data(),
sizeof(double) * (blockStarts[i + 1] - blockStarts[i]));
}
}
return values;
}
void setValuesConstant(double cval) {
for(int i = 0; i < blockStarts.size() - 1; i++) {
if(csr_blocks[i] != nullptr) {
// This may be too slow, we maybe should optimize this...
#pragma omp parallel for
for(int j = 0; j < blockStarts[i+1] - blockStarts[i]; j++) {
csr_blocks[i]->getActive()->values[j] = cval;
}
}
}
}
};