Skip to content

Commit c395e86

Browse files
committed
WIP
1 parent 48dd58c commit c395e86

1 file changed

Lines changed: 48 additions & 27 deletions

File tree

examples/multigrid-preconditioned-solver/multigrid-preconditioned-solver.cpp

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ int main(int argc, char *argv[])
4747
using IndexType = int;
4848
using vec = gko::matrix::Dense<ValueType>;
4949
using mtx = gko::matrix::Csr<ValueType, IndexType>;
50+
using fcg = gko::solver::Fcg<ValueType>;
5051
using cg = gko::solver::Cg<ValueType>;
52+
using ir = gko::solver::Ir<ValueType>;
5153
using mg = gko::solver::Multigrid<ValueType>;
5254
using bj = gko::preconditioner::Jacobi<ValueType, IndexType>;
5355
using amgx_pgm = gko::multigrid::AmgxPgm<ValueType, IndexType>;
@@ -73,17 +75,20 @@ int main(int argc, char *argv[])
7375
}
7476

7577
// Read data
76-
auto A = share(gko::read<mtx>(std::ifstream("data/A.mtx"), exec));
78+
auto A = share(gko::read<mtx>(std::ifstream("data/A.mtx"), exec,
79+
std::make_shared<mtx::automatical>()));
7780
// Create RHS and initial guess as 1
7881
gko::size_type size = A->get_size()[0];
7982
auto host_x = vec::create(exec->get_master(), gko::dim<2>(size, 1));
83+
auto host_b = vec::create(exec->get_master(), gko::dim<2>(size, 1));
8084
for (auto i = 0; i < size; i++) {
81-
host_x->at(i, 0) = 1.;
85+
host_x->at(i, 0) = 0.;
86+
host_b->at(i, 0) = 1.;
8287
}
8388
auto x = vec::create(exec);
8489
auto b = vec::create(exec);
8590
x->copy_from(host_x.get());
86-
b->copy_from(host_x.get());
91+
b->copy_from(host_b.get());
8792

8893
// Calculate initial residual by overwriting b
8994
auto one = gko::initialize<vec>({1.0}, exec);
@@ -93,60 +98,70 @@ int main(int argc, char *argv[])
9398
b->compute_norm2(lend(initres));
9499

95100
// copy b again
96-
b->copy_from(host_x.get());
97-
const gko::remove_complex<ValueType> reduction_factor = 1e-13;
101+
b->copy_from(host_b.get());
102+
const gko::remove_complex<ValueType> tolerance = 1e-8;
98103
auto iter_stop =
99-
gko::stop::Iteration::build().with_max_iters(10000u).on(exec);
100-
auto tol_stop = gko::stop::ResidualNormReduction<ValueType>::build()
101-
.with_reduction_factor(reduction_factor)
104+
gko::stop::Iteration::build().with_max_iters(100u).on(exec);
105+
auto tol_stop = gko::stop::AbsoluteResidualNorm<ValueType>::build()
106+
.with_tolerance(tolerance)
102107
.on(exec);
103108

104109
std::shared_ptr<const gko::log::Convergence<ValueType>> logger =
105110
gko::log::Convergence<ValueType>::create(exec);
106111
iter_stop->add_logger(logger);
107112
tol_stop->add_logger(logger);
108113

109-
// Create smoother factory
110-
auto smoother_gen = bj::build().with_max_block_size(1u).on(exec);
114+
// Create smoother factory (ir with bj)
115+
auto inner_solver_gen = bj::build().with_max_block_size(1u).on(exec);
116+
auto smoother_gen = gko::share(
117+
ir::build()
118+
.with_solver(gko::share(inner_solver_gen))
119+
.with_relaxation_factor(0.9)
120+
.with_criteria(
121+
gko::stop::Iteration::build().with_max_iters(2u).on(exec))
122+
.on(exec));
111123
// Create RestrictProlong factory
112-
auto rstr_prlg_gen = amgx_pgm::build().on(exec);
124+
auto rstr_prlg_gen = amgx_pgm::build().with_deterministic(true).on(exec);
113125
// Create CoarsesSolver factory
114126
auto coarsest_solver_gen =
115127
cg::build()
116128
.with_criteria(
117-
gko::stop::Iteration::build().with_max_iters(4u).on(exec),
118-
gko::stop::ResidualNormReduction<ValueType>::build()
119-
.with_reduction_factor(reduction_factor)
120-
.on(exec))
129+
gko::stop::Iteration::build().with_max_iters(4u).on(exec))
121130
.on(exec);
122131
// Create multigrid factory
123132
auto multigrid_gen =
124133
mg::build()
125-
.with_max_levels(2u)
134+
.with_max_levels(9u)
126135
.with_pre_smoother(gko::share(smoother_gen))
127136
.with_post_uses_pre(true)
128137
.with_rstr_prlg(gko::share(rstr_prlg_gen))
129-
.with_coarsest_solver(gko::share(coarsest_solver_gen))
130-
.with_criteria(
131-
gko::stop::Iteration::build().with_max_iters(4u).on(exec))
132-
.on(exec);
133-
// Create solver factory
134-
auto solver_gen =
135-
cg::build()
138+
.with_coarsest_solver(gko::share(smoother_gen))
136139
.with_criteria(gko::share(iter_stop), gko::share(tol_stop))
137-
// Add preconditioner, these 2 lines are the only
138-
// difference from the simple solver example
139-
.with_preconditioner(gko::share(multigrid_gen))
140140
.on(exec);
141+
// Create solver factory
142+
// auto solver_gen =
143+
// cg::build()
144+
// .with_criteria(gko::share(iter_stop), gko::share(tol_stop))
145+
// // Add preconditioner, these 2 lines are the only
146+
// // difference from the simple solver example
147+
// // .with_preconditioner(gko::share(multigrid_gen))
148+
// .on(exec);
141149
// Create solver
142-
auto solver = solver_gen->generate(A);
150+
std::chrono::nanoseconds gen_time(0);
151+
auto gen_tic = std::chrono::steady_clock::now();
152+
auto solver = multigrid_gen->generate(A);
153+
exec->synchronize();
154+
auto gen_toc = std::chrono::steady_clock::now();
155+
gen_time +=
156+
std::chrono::duration_cast<std::chrono::nanoseconds>(gen_toc - gen_tic);
143157

144158

145159
// Solve system
146160
exec->synchronize();
147161
std::chrono::nanoseconds time(0);
148162
auto tic = std::chrono::steady_clock::now();
149163
solver->apply(lend(b), lend(x));
164+
exec->synchronize();
150165
auto toc = std::chrono::steady_clock::now();
151166
time += std::chrono::duration_cast<std::chrono::nanoseconds>(toc - tic);
152167

@@ -163,6 +178,12 @@ int main(int argc, char *argv[])
163178
// Print solver statistics
164179
std::cout << "CG iteration count: " << logger->get_num_iterations()
165180
<< std::endl;
181+
std::cout << "CG generation time [ms]: "
182+
<< static_cast<double>(gen_time.count()) / 1000000.0 << std::endl;
166183
std::cout << "CG execution time [ms]: "
167184
<< static_cast<double>(time.count()) / 1000000.0 << std::endl;
185+
std::cout << "CG execution time per iteraion[ms]: "
186+
<< static_cast<double>(time.count()) / 1000000.0 /
187+
logger->get_num_iterations()
188+
<< std::endl;
168189
}

0 commit comments

Comments
 (0)