509 * #include <iostream>
514 * #include <ginkgo/ginkgo.hpp>
517 *
template <
typename ValueType,
typename IndexType>
518 *
void generate_stencil_matrix(IndexType discretization_points,
519 * IndexType* row_ptrs, IndexType* col_idxs,
523 *
const ValueType coefs[] = {-1, 2, -1};
525 *
for (IndexType i = 0; i < discretization_points; ++i) {
526 *
for (
auto ofs : {-1, 0, 1}) {
527 *
if (0 <= i + ofs && i + ofs < discretization_points) {
528 * values[pos] = coefs[ofs + 1];
529 * col_idxs[pos] = i + ofs;
533 * row_ptrs[i + 1] = pos;
538 *
template <
typename Closure,
typename ValueType,
typename IndexType>
539 *
void generate_rhs(IndexType discretization_points, Closure f, ValueType u0,
540 * ValueType u1, ValueType* rhs)
542 *
const ValueType h = 1.0 / (discretization_points + 1);
543 *
for (IndexType i = 0; i < discretization_points; ++i) {
544 *
const ValueType xi = ValueType(i + 1) * h;
545 *
rhs[i] = -f(xi) * h * h;
548 *
rhs[discretization_points - 1] += u1;
552 *
template <
typename ValueType,
typename IndexType>
553 *
void print_solution(IndexType discretization_points, ValueType u0, ValueType u1,
554 *
const ValueType* u)
556 * std::cout << u0 <<
'\n';
557 *
for (IndexType i = 0; i < discretization_points; ++i) {
558 * std::cout << u[i] <<
'\n';
560 * std::cout << u1 << std::endl;
564 *
template <
typename Closure,
typename ValueType,
typename IndexType>
566 *
const ValueType* u,
569 *
const ValueType h = 1.0 / (discretization_points + 1);
571 *
for (IndexType i = 0; i < discretization_points; ++i) {
573 *
const ValueType xi = ValueType(i + 1) * h;
574 * error +=
abs(u[i] - correct_u(xi)) /
abs(correct_u(xi));
579 *
template <
typename ValueType,
typename IndexType>
580 *
void solve_system(
const std::string& executor_string,
581 * IndexType discretization_points, IndexType* row_ptrs,
582 * IndexType* col_idxs, ValueType* values, ValueType* rhs,
591 *
const auto& dp = discretization_points;
593 * std::map<std::string, std::function<std::shared_ptr<gko::Executor>()>>
610 * {
"reference", [] {
return gko::ReferenceExecutor::create(); }}};
612 *
const auto exec = exec_map.at(executor_string)();
613 *
const auto app_exec = exec->get_master();
617 * val_array::view(app_exec, 3 * dp - 2, values),
618 * idx_array::view(app_exec, 3 * dp - 2, col_idxs),
619 * idx_array::view(app_exec, dp + 1, row_ptrs));
622 * val_array::view(app_exec, dp, rhs), 1);
625 * val_array::view(app_exec, dp, u), 1);
629 * .with_criteria(gko::stop::Iteration::build().with_max_iters(
632 * .with_reduction_factor(reduction_factor))
633 * .with_preconditioner(bj::build())
641 *
int main(
int argc,
char* argv[])
643 *
using ValueType = double;
644 *
using IndexType = int;
648 *
if (argc == 2 && std::string(argv[1]) ==
"--help") {
649 * std::cerr <<
"Usage: " << argv[0]
650 * <<
" [executor] [DISCRETIZATION_POINTS]" << std::endl;
654 *
const auto executor_string = argc >= 2 ? argv[1] :
"reference";
655 *
const IndexType discretization_points =
656 * argc >= 3 ? std::atoi(argv[2]) : 100;
658 *
auto correct_u = [](ValueType x) {
return x * x * x; };
659 *
auto f = [](ValueType x) {
return ValueType(6) * x; };
660 *
auto u0 = correct_u(0);
661 *
auto u1 = correct_u(1);
663 * std::vector<IndexType> row_ptrs(discretization_points + 1);
664 * std::vector<IndexType> col_idxs(3 * discretization_points - 2);
665 * std::vector<ValueType> values(3 * discretization_points - 2);
666 * std::vector<ValueType>
rhs(discretization_points);
667 * std::vector<ValueType> u(discretization_points, 0.0);
670 * generate_stencil_matrix(discretization_points, row_ptrs.data(),
671 * col_idxs.data(), values.data());
672 * generate_rhs(discretization_points, f, u0, u1,
rhs.data());
674 * solve_system(executor_string, discretization_points, row_ptrs.data(),
675 * col_idxs.data(), values.data(),
rhs.data(), u.data(),
678 * std::cout <<
"The average relative error is "
679 * << calculate_error(discretization_points, u.data(), correct_u) /
680 * discretization_points
static std::shared_ptr< CudaExecutor > create(int device_id, std::shared_ptr< Executor > master, bool device_reset, allocation_mode alloc_mode=default_cuda_alloc_mode, CUstream_st *stream=nullptr)
static std::shared_ptr< DpcppExecutor > create(int device_id, std::shared_ptr< Executor > master, std::string device_type="all", dpcpp_queue_property property=dpcpp_queue_property::in_order)
static std::shared_ptr< HipExecutor > create(int device_id, std::shared_ptr< Executor > master, bool device_reset, allocation_mode alloc_mode=default_hip_alloc_mode, CUstream_st *stream=nullptr)
static std::shared_ptr< OmpExecutor > create(std::shared_ptr< CpuAllocatorBase > alloc=std::make_shared< CpuAllocator >())
Definition executor.hpp:1396
static std::unique_ptr< Dense > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size={}, size_type stride=0)
Definition jacobi.hpp:189
Definition residual_norm.hpp:113
static const version_info & get()
Definition version.hpp:139
constexpr std::enable_if_t<!is_complex_s< T >::value, T > abs(const T &x)
Definition math.hpp:931
std::size_t size_type
Definition types.hpp:89
std::remove_reference< OwningPointer >::type && give(OwningPointer &&p)
Definition utils_helper.hpp:247
typename detail::remove_complex_s< T >::type remove_complex
Definition math.hpp:260