-
Notifications
You must be signed in to change notification settings - Fork 112
/
mpi_solver.cpp
607 lines (505 loc) · 18 KB
/
mpi_solver.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
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
607
#include <iostream>
#include <vector>
#include <string>
#include <boost/program_options.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <amgcl/backend/builtin.hpp>
#include <amgcl/value_type/static_matrix.hpp>
#include <amgcl/adapter/crs_tuple.hpp>
#include <amgcl/adapter/block_matrix.hpp>
#if defined(SOLVER_BACKEND_VEXCL)
# include <amgcl/backend/vexcl.hpp>
# include <amgcl/backend/vexcl_static_matrix.hpp>
#elif defined(SOLVER_BACKEND_CUDA)
# include <amgcl/backend/cuda.hpp>
# include <amgcl/relaxation/cusparse_ilu0.hpp>
#else
# ifndef SOLVER_BACKEND_BUILTIN
# define SOLVER_BACKEND_BUILTIN
# endif
#endif
#include <amgcl/mpi/util.hpp>
#include <amgcl/mpi/make_solver.hpp>
#include <amgcl/mpi/preconditioner.hpp>
#include <amgcl/mpi/solver/runtime.hpp>
#include <amgcl/io/mm.hpp>
#include <amgcl/io/binary.hpp>
#include <amgcl/profiler.hpp>
#ifndef AMGCL_BLOCK_SIZES
# define AMGCL_BLOCK_SIZES (3)(4)
#endif
namespace amgcl {
profiler<> prof;
}
namespace math = amgcl::math;
//---------------------------------------------------------------------------
ptrdiff_t assemble_poisson3d(amgcl::mpi::communicator comm,
ptrdiff_t n, int block_size,
std::vector<ptrdiff_t> &ptr,
std::vector<ptrdiff_t> &col,
std::vector<double> &val,
std::vector<double> &rhs)
{
ptrdiff_t n3 = n * n * n;
ptrdiff_t chunk = (n3 + comm.size - 1) / comm.size;
if (chunk % block_size != 0) {
chunk += block_size - chunk % block_size;
}
ptrdiff_t row_beg = std::min(n3, chunk * comm.rank);
ptrdiff_t row_end = std::min(n3, row_beg + chunk);
chunk = row_end - row_beg;
ptr.clear(); ptr.reserve(chunk + 1);
col.clear(); col.reserve(chunk * 7);
val.clear(); val.reserve(chunk * 7);
rhs.resize(chunk);
std::fill(rhs.begin(), rhs.end(), 1.0);
const double h2i = (n - 1) * (n - 1);
ptr.push_back(0);
for (ptrdiff_t idx = row_beg; idx < row_end; ++idx) {
ptrdiff_t k = idx / (n * n);
ptrdiff_t j = (idx / n) % n;
ptrdiff_t i = idx % n;
if (k > 0) {
col.push_back(idx - n * n);
val.push_back(-h2i);
}
if (j > 0) {
col.push_back(idx - n);
val.push_back(-h2i);
}
if (i > 0) {
col.push_back(idx - 1);
val.push_back(-h2i);
}
col.push_back(idx);
val.push_back(6 * h2i);
if (i + 1 < n) {
col.push_back(idx + 1);
val.push_back(-h2i);
}
if (j + 1 < n) {
col.push_back(idx + n);
val.push_back(-h2i);
}
if (k + 1 < n) {
col.push_back(idx + n * n);
val.push_back(-h2i);
}
ptr.push_back( col.size() );
}
return chunk;
}
//---------------------------------------------------------------------------
ptrdiff_t read_matrix_market(
amgcl::mpi::communicator comm,
const std::string &A_file, const std::string &rhs_file, int block_size,
std::vector<ptrdiff_t> &ptr,
std::vector<ptrdiff_t> &col,
std::vector<double> &val,
std::vector<double> &rhs)
{
amgcl::io::mm_reader A_mm(A_file);
ptrdiff_t n = A_mm.rows();
ptrdiff_t chunk = (n + comm.size - 1) / comm.size;
if (chunk % block_size != 0) {
chunk += block_size - chunk % block_size;
}
ptrdiff_t row_beg = std::min(n, chunk * comm.rank);
ptrdiff_t row_end = std::min(n, row_beg + chunk);
chunk = row_end - row_beg;
A_mm(ptr, col, val, row_beg, row_end);
if (rhs_file.empty()) {
rhs.resize(chunk);
std::fill(rhs.begin(), rhs.end(), 1.0);
} else {
amgcl::io::mm_reader rhs_mm(rhs_file);
rhs_mm(rhs, row_beg, row_end);
}
return chunk;
}
//---------------------------------------------------------------------------
ptrdiff_t read_binary(
amgcl::mpi::communicator comm,
const std::string &A_file, const std::string &rhs_file, int block_size,
std::vector<ptrdiff_t> &ptr,
std::vector<ptrdiff_t> &col,
std::vector<double> &val,
std::vector<double> &rhs)
{
ptrdiff_t n = amgcl::io::crs_size<ptrdiff_t>(A_file);
ptrdiff_t chunk = (n + comm.size - 1) / comm.size;
if (chunk % block_size != 0) {
chunk += block_size - chunk % block_size;
}
ptrdiff_t row_beg = std::min(n, chunk * comm.rank);
ptrdiff_t row_end = std::min(n, row_beg + chunk);
chunk = row_end - row_beg;
amgcl::io::read_crs(A_file, n, ptr, col, val, row_beg, row_end);
if (rhs_file.empty()) {
rhs.resize(chunk);
std::fill(rhs.begin(), rhs.end(), 1.0);
} else {
ptrdiff_t rows, cols;
amgcl::io::read_dense(rhs_file, rows, cols, rhs, row_beg, row_end);
}
return chunk;
}
//---------------------------------------------------------------------------
template <class Backend, class Matrix>
std::shared_ptr< amgcl::mpi::distributed_matrix<Backend> >
partition(amgcl::mpi::communicator comm, const Matrix &Astrip,
typename Backend::vector &rhs, const typename Backend::params &bprm,
amgcl::runtime::mpi::partition::type ptype, int block_size = 1)
{
typedef typename Backend::value_type val_type;
typedef typename amgcl::math::rhs_of<val_type>::type rhs_type;
typedef amgcl::mpi::distributed_matrix<Backend> DMatrix;
using amgcl::prof;
auto A = std::make_shared<DMatrix>(comm, Astrip);
if (comm.size == 1 || ptype == amgcl::runtime::mpi::partition::merge)
return A;
prof.tic("partition");
boost::property_tree::ptree prm;
prm.put("type", ptype);
amgcl::runtime::mpi::partition::wrapper<Backend> part(prm);
auto I = part(*A, block_size);
auto J = transpose(*I);
A = product(*J, *product(*A, *I));
#if defined(SOLVER_BACKEND_BUILTIN)
amgcl::backend::numa_vector<rhs_type> new_rhs(J->loc_rows());
#elif defined(SOLVER_BACKEND_VEXCL)
vex::vector<rhs_type> new_rhs(bprm.q, J->loc_rows());
#elif defined(SOLVER_BACKEND_CUDA)
thrust::device_vector<rhs_type> new_rhs(J->loc_rows());
#endif
J->move_to_backend(bprm);
amgcl::backend::spmv(1, *J, rhs, 0, new_rhs);
rhs.swap(new_rhs);
prof.toc("partition");
return A;
}
//---------------------------------------------------------------------------
#if defined(SOLVER_BACKEND_BUILTIN) || defined(SOLVER_BACKEND_VEXCL)
template <int B>
void solve_block(
amgcl::mpi::communicator comm,
ptrdiff_t chunk,
const std::vector<ptrdiff_t> &ptr,
const std::vector<ptrdiff_t> &col,
const std::vector<double> &val,
const boost::property_tree::ptree &prm,
const std::vector<double> &f,
amgcl::runtime::mpi::partition::type ptype
)
{
typedef amgcl::static_matrix<double, B, B> val_type;
typedef amgcl::static_matrix<double, B, 1> rhs_type;
#if defined(SOLVER_BACKEND_BUILTIN)
typedef amgcl::backend::builtin<val_type> Backend;
#elif defined(SOLVER_BACKEND_VEXCL)
typedef amgcl::backend::vexcl<val_type> Backend;
#endif
typedef
amgcl::mpi::make_solver<
amgcl::runtime::mpi::preconditioner<Backend>,
amgcl::runtime::mpi::solver::wrapper<Backend>
>
Solver;
using amgcl::prof;
typename Backend::params bprm;
#if defined(SOLVER_BACKEND_BUILTIN)
amgcl::backend::numa_vector<rhs_type> rhs(
reinterpret_cast<const rhs_type*>(&f[0]),
reinterpret_cast<const rhs_type*>(&f[0]) + chunk / B
);
#elif defined(SOLVER_BACKEND_VEXCL)
vex::Context ctx(vex::Filter::Env);
bprm.q = ctx;
vex::scoped_program_header header(ctx,
amgcl::backend::vexcl_static_matrix_declaration<double,B>());
if (comm.rank == 0) std::cout << ctx << std::endl;
vex::vector<rhs_type> rhs(ctx, chunk / B, reinterpret_cast<const rhs_type*>(&f[0]));
#endif
prof.tic("setup");
std::shared_ptr<Solver> solve;
if (ptype) {
auto A = partition<Backend>(comm,
amgcl::adapter::block_matrix<val_type>(std::tie(chunk, ptr, col, val)),
rhs, bprm, ptype, prm.get("precond.coarsening.aggr.block_size", 1));
solve = std::make_shared<Solver>(comm, A, prm, bprm);
chunk = A->loc_rows();
} else {
solve = std::make_shared<Solver>(comm,
amgcl::adapter::block_matrix<val_type>(std::tie(chunk, ptr, col, val)),
prm, bprm);
}
prof.toc("setup");
if (comm.rank == 0) {
std::cout << *solve << std::endl;
}
#if defined(SOLVER_BACKEND_BUILTIN)
amgcl::backend::numa_vector<rhs_type> x(chunk);
#elif defined(SOLVER_BACKEND_VEXCL)
vex::vector<rhs_type> x(ctx, chunk);
x = math::zero<rhs_type>();
#endif
int iters;
double error;
prof.tic("solve");
std::tie(iters, error) = (*solve)(rhs, x);
prof.toc("solve");
if (comm.rank == 0) {
std::cout
<< "Iterations: " << iters << std::endl
<< "Error: " << error << std::endl
<< prof << std::endl;
}
}
#endif
//---------------------------------------------------------------------------
void solve_scalar(
amgcl::mpi::communicator comm,
ptrdiff_t chunk,
const std::vector<ptrdiff_t> &ptr,
const std::vector<ptrdiff_t> &col,
const std::vector<double> &val,
const boost::property_tree::ptree &prm,
const std::vector<double> &f,
amgcl::runtime::mpi::partition::type ptype
)
{
#if defined(SOLVER_BACKEND_BUILTIN)
typedef amgcl::backend::builtin<double> Backend;
#elif defined(SOLVER_BACKEND_VEXCL)
typedef amgcl::backend::vexcl<double> Backend;
#elif defined(SOLVER_BACKEND_CUDA)
typedef amgcl::backend::cuda<double> Backend;
#endif
typedef
amgcl::mpi::make_solver<
amgcl::runtime::mpi::preconditioner<Backend>,
amgcl::runtime::mpi::solver::wrapper<Backend>
>
Solver;
using amgcl::prof;
typename Backend::params bprm;
#if defined(SOLVER_BACKEND_BUILTIN)
amgcl::backend::numa_vector<double> rhs(f);
#elif defined(SOLVER_BACKEND_VEXCL)
vex::Context ctx(vex::Filter::Env);
bprm.q = ctx;
if (comm.rank == 0) std::cout << ctx << std::endl;
vex::vector<double> rhs(ctx, f);
#elif defined(SOLVER_BACKEND_CUDA)
cusparseCreate(&bprm.cusparse_handle);
thrust::device_vector<double> rhs(f);
#endif
prof.tic("setup");
std::shared_ptr<Solver> solve;
if (ptype) {
auto A = partition<Backend>(comm,
std::tie(chunk, ptr, col, val), rhs, bprm, ptype,
prm.get("precond.coarsening.aggr.block_size", 1));
solve = std::make_shared<Solver>(comm, A, prm, bprm);
chunk = A->loc_rows();
} else {
solve = std::make_shared<Solver>(comm, std::tie(chunk, ptr, col, val), prm, bprm);
}
prof.toc("setup");
if (comm.rank == 0) {
std::cout << *solve << std::endl;
}
#if defined(SOLVER_BACKEND_BUILTIN)
amgcl::backend::numa_vector<double> x(chunk);
#elif defined(SOLVER_BACKEND_VEXCL)
vex::vector<double> x(ctx, chunk);
x = 0.0;
#elif defined(SOLVER_BACKEND_CUDA)
thrust::device_vector<double> x(chunk, 0.0);
#endif
int iters;
double error;
prof.tic("solve");
std::tie(iters, error) = (*solve)(rhs, x);
prof.toc("solve");
if (comm.rank == 0) {
std::cout
<< "Iterations: " << iters << std::endl
<< "Error: " << error << std::endl
<< prof << std::endl;
}
}
//---------------------------------------------------------------------------
int main(int argc, char *argv[]) {
amgcl::mpi::init_thread mpi(&argc, &argv);
amgcl::mpi::communicator comm(MPI_COMM_WORLD);
if (comm.rank == 0)
std::cout << "World size: " << comm.size << std::endl;
using amgcl::prof;
// Read configuration from command line
namespace po = boost::program_options;
po::options_description desc("Options");
desc.add_options()
("help,h", "show help")
("matrix,A",
po::value<std::string>(),
"System matrix in the MatrixMarket format. "
"When not specified, a Poisson problem in 3D unit cube is assembled. "
)
(
"rhs,f",
po::value<std::string>()->default_value(""),
"The RHS vector in the MatrixMarket format. "
"When omitted, a vector of ones is used by default. "
"Should only be provided together with a system matrix. "
)
(
"Ap",
po::value< std::vector<std::string> >()->multitoken(),
"Pre-partitioned matrix (single file per MPI process)"
)
(
"fp",
po::value< std::vector<std::string> >()->multitoken(),
"Pre-partitioned RHS (single file per MPI process)"
)
(
"binary,B",
po::bool_switch()->default_value(false),
"When specified, treat input files as binary instead of as MatrixMarket. "
"It is assumed the files were converted to binary format with mm2bin utility. "
)
(
"block-size,b",
po::value<int>()->default_value(1),
"The block size of the system matrix. "
"When specified, the system matrix is assumed to have block-wise structure. "
"This usually is the case for problems in elasticity, structural mechanics, "
"for coupled systems of PDE (such as Navier-Stokes equations), etc. "
)
(
"partitioner,r",
po::value<amgcl::runtime::mpi::partition::type>()->default_value(
#if defined(AMGCL_HAVE_SCOTCH)
amgcl::runtime::mpi::partition::ptscotch
#elif defined(AMGCL_HAVE_PARMETIS)
amgcl::runtime::mpi::partition::parmetis
#else
amgcl::runtime::mpi::partition::merge
#endif
),
"Repartition the system matrix"
)
(
"size,n",
po::value<ptrdiff_t>()->default_value(128),
"domain size"
)
("prm-file,P",
po::value<std::string>(),
"Parameter file in json format. "
)
(
"prm,p",
po::value< std::vector<std::string> >()->multitoken(),
"Parameters specified as name=value pairs. "
"May be provided multiple times. Examples:\n"
" -p solver.tol=1e-3\n"
" -p precond.coarse_enough=300"
)
;
po::positional_options_description p;
p.add("prm", -1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
po::notify(vm);
if (vm.count("help")) {
if (comm.rank == 0) std::cout << desc << std::endl;
return 0;
}
boost::property_tree::ptree prm;
if (vm.count("prm-file")) {
read_json(vm["prm-file"].as<std::string>(), prm);
}
if (vm.count("prm")) {
for(const std::string &v : vm["prm"].as<std::vector<std::string> >()) {
amgcl::put(prm, v);
}
}
ptrdiff_t n;
std::vector<ptrdiff_t> ptr;
std::vector<ptrdiff_t> col;
std::vector<double> val;
std::vector<double> rhs;
int block_size = vm["block-size"].as<int>();
int aggr_block = prm.get("precond.coarsening.aggr.block_size", 1);
bool binary = vm["binary"].as<bool>();
amgcl::runtime::mpi::partition::type ptype = vm["partitioner"].as<amgcl::runtime::mpi::partition::type>();
if (vm.count("matrix")) {
prof.tic("read");
if (binary) {
n = read_binary(comm,
vm["matrix"].as<std::string>(),
vm["rhs"].as<std::string>(),
block_size * aggr_block, ptr, col, val, rhs);
} else {
n = read_matrix_market(comm,
vm["matrix"].as<std::string>(),
vm["rhs"].as<std::string>(),
block_size * aggr_block, ptr, col, val, rhs);
}
prof.toc("read");
} else if (vm.count("Ap")) {
prof.tic("read");
ptype = static_cast<amgcl::runtime::mpi::partition::type>(0);
std::vector<std::string> Aparts = vm["Ap"].as<std::vector<std::string>>();
comm.check(Aparts.size() == static_cast<size_t>(comm.size),
"--Ap should have single entry per MPI process");
if (binary) {
amgcl::io::read_crs(Aparts[comm.rank], n, ptr, col, val);
} else {
ptrdiff_t m;
std::tie(n, m) = amgcl::io::mm_reader(Aparts[comm.rank])(ptr, col, val);
}
if (vm.count("fp")) {
std::vector<std::string> fparts = vm["fp"].as<std::vector<std::string>>();
comm.check(fparts.size() == static_cast<size_t>(comm.size),
"--fp should have single entry per MPI process");
ptrdiff_t rows;
ptrdiff_t cols;
if (binary) {
amgcl::io::read_dense(fparts[comm.rank], rows, cols, rhs);
} else {
std::tie(rows, cols) = amgcl::io::mm_reader(fparts[comm.rank])(rhs);
}
comm.check(rhs.size() == static_cast<size_t>(n), "Wrong RHS size");
} else {
rhs.resize(n, 1);
}
prof.toc("read");
} else {
prof.tic("assemble");
n = assemble_poisson3d(comm,
vm["size"].as<ptrdiff_t>(),
block_size * aggr_block, ptr, col, val, rhs);
prof.toc("assemble");
}
switch(block_size) {
#if defined(SOLVER_BACKEND_BUILTIN) || defined(SOLVER_BACKEND_VEXCL)
# define AMGCL_CALL_BLOCK_SOLVER(z, data, B) \
case B: \
solve_block<B>(comm, n, ptr, col, val, prm, rhs, ptype); \
break;
BOOST_PP_SEQ_FOR_EACH(AMGCL_CALL_BLOCK_SOLVER, ~, AMGCL_BLOCK_SIZES)
# undef AMGCL_CALL_BLOCK_SOLVER
#endif
case 1:
solve_scalar(comm, n, ptr, col, val, prm, rhs, ptype);
break;
default:
if (comm.rank == 0)
std::cout << "Unsupported block size!" << std::endl;
}
}