430 *
template <
typename ValueType,
typename IndexType,
typename MemorySpace>
431 *
struct mapper<device_matrix_data<ValueType, IndexType>, MemorySpace> {
432 *
using index_mapper = mapper<array<IndexType>, MemorySpace>;
433 *
using value_mapper = mapper<array<ValueType>, MemorySpace>;
444 *
template <
typename ValueType_c,
typename IndexType_c>
446 *
using index_array =
typename index_mapper::template type<IndexType_c>;
447 *
using value_array =
typename value_mapper::template type<ValueType_c>;
460 *
static type map(size_type size, IndexType_c* row_idxs,
461 * IndexType_c* col_idxs, ValueType_c* values)
463 *
return {index_mapper::map(row_idxs, size),
464 * index_mapper::map(col_idxs, size),
465 * value_mapper::map(values, size)};
468 * index_array row_idxs;
469 * index_array col_idxs;
470 * value_array values;
473 *
static type<ValueType, IndexType> map(
474 * device_matrix_data<ValueType, IndexType>& md)
476 * assert_compatibility<MemorySpace>(md);
477 *
return type<ValueType, IndexType>::map(
478 * md.get_num_stored_elements(), md.get_row_idxs(), md.get_col_idxs(),
482 *
static type<const ValueType, const IndexType> map(
483 *
const device_matrix_data<ValueType, IndexType>& md)
485 * assert_compatibility<MemorySpace>(md);
486 *
return type<const ValueType, const IndexType>::map(
487 * md.get_num_stored_elements(), md.get_const_row_idxs(),
488 * md.get_const_col_idxs(), md.get_const_values());
496 *
template <
typename ValueType,
typename IndexType>
499 *
auto exec = matrix->get_executor();
500 *
const auto discretization_points = matrix->get_size()[0];
503 * discretization_points * 3);
505 *
auto k_md = gko::ext::kokkos::map_data(md);
507 * Kokkos::parallel_for(
508 *
"generate_stencil_matrix", md.get_num_stored_elements(),
509 * KOKKOS_LAMBDA(
int i) {
510 *
const ValueType coefs[] = {-1, 2, -1};
511 *
auto ofs =
static_cast<IndexType
>((i % 3) - 1);
512 *
auto row =
static_cast<IndexType
>(i / 3);
513 *
auto col = row + ofs;
516 *
static_cast<IndexType
>(0 <= col && col < discretization_points);
518 * k_md.row_idxs[i] = mask * row;
519 * k_md.col_idxs[i] = mask * col;
520 * k_md.values[i] = mask * coefs[ofs + 1];
523 * md.sum_duplicates();
525 * matrix->read(std::move(md));
529 *
template <
typename Closure,
typename ValueType>
530 *
void generate_rhs(Closure&& f, ValueType u0, ValueType u1,
533 *
const auto discretization_points =
rhs->get_size()[0];
534 *
auto k_rhs = gko::ext::kokkos::map_data(rhs);
535 * Kokkos::parallel_for(
536 *
"generate_rhs", discretization_points, KOKKOS_LAMBDA(
int i) {
537 *
const ValueType h = 1.0 / (discretization_points + 1);
538 *
const ValueType xi = ValueType(i + 1) * h;
539 * k_rhs(i, 0) = -f(xi) * h * h;
543 *
if (i == discretization_points - 1) {
550 *
template <
typename Closure,
typename ValueType>
551 *
double calculate_error(
int discretization_points,
553 * Closure&& correct_u)
555 *
auto k_u = gko::ext::kokkos::map_data(u);
557 * Kokkos::parallel_reduce(
558 *
"calculate_error", discretization_points,
559 * KOKKOS_LAMBDA(
int i,
double& lsum) {
560 *
const auto h = 1.0 / (discretization_points + 1);
561 *
const auto xi = (i + 1) * h;
562 * lsum += Kokkos::abs((k_u(i, 0) - correct_u(xi)) /
563 * Kokkos::abs(correct_u(xi)));
570 *
int main(
int argc,
char* argv[])
572 *
using ValueType = double;
574 *
using IndexType = int;
581 *
if (argc == 2 && (std::string(argv[1]) ==
"--help")) {
582 * std::cerr <<
"Usage: " << argv[0]
583 * <<
" [discretization_points] [kokkos-options]" << std::endl;
584 * Kokkos::ScopeGuard kokkos(argc, argv);
588 * Kokkos::ScopeGuard kokkos(argc, argv);
590 *
const auto discretization_points =
591 *
static_cast<gko::size_type>(argc >= 2 ? std::atoi(argv[1]) : 100u);
593 *
auto exec = gko::ext::kokkos::create_default_executor();
595 *
auto correct_u = [] KOKKOS_FUNCTION(ValueType x) {
return x * x * x; };
596 *
auto f = [] KOKKOS_FUNCTION(ValueType x) {
return ValueType{6} * x; };
597 *
auto u0 = correct_u(0);
598 *
auto u1 = correct_u(1);
601 * generate_rhs(f, u0, u1,
rhs.get());
605 *
auto A =
share(mtx::create(
606 * exec,
gko::dim<2>{discretization_points, discretization_points}));
607 * generate_stencil_matrix(A.get());
609 *
const RealValueType reduction_factor{1e-7};
612 * gko::stop::Iteration::build().with_max_iters(discretization_points),
615 * .with_preconditioner(bj::build())
620 * std::cout <<
"\nSolve complete."
621 * <<
"\nThe average relative error is "
622 * << calculate_error(discretization_points, u.get(), correct_u) /
623 * discretization_points
Definition device_matrix_data.hpp:36
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
std::size_t size_type
Definition types.hpp:89
detail::shared_type< OwningPointer > share(OwningPointer &&p)
Definition utils_helper.hpp:224
typename detail::remove_complex_s< T >::type remove_complex
Definition math.hpp:260