/home/docs/checkouts/readthedocs.org/user_builds/ginkgo-test/checkouts/latest/build/doc/doxygen/examples/kokkos-assembly.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/ginkgo-test/checkouts/latest/build/doc/doxygen/examples/kokkos-assembly.hpp Source File#

Reference API: /home/docs/checkouts/readthedocs.org/user_builds/ginkgo-test/checkouts/latest/build/doc/doxygen/examples/kokkos-assembly.hpp Source File
Reference API
kokkos-assembly.hpp
1
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>;
434 *
435 *
444 * template <typename ValueType_c, typename IndexType_c>
445 * struct type {
446 * using index_array = typename index_mapper::template type<IndexType_c>;
447 * using value_array = typename value_mapper::template type<ValueType_c>;
448 *
449 *
460 * static type map(size_type size, IndexType_c* row_idxs,
461 * IndexType_c* col_idxs, ValueType_c* values)
462 * {
463 * return {index_mapper::map(row_idxs, size),
464 * index_mapper::map(col_idxs, size),
465 * value_mapper::map(values, size)};
466 * }
467 *
468 * index_array row_idxs;
469 * index_array col_idxs;
470 * value_array values;
471 * };
472 *
473 * static type<ValueType, IndexType> map(
474 * device_matrix_data<ValueType, IndexType>& md)
475 * {
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(),
479 * md.get_values());
480 * }
481 *
482 * static type<const ValueType, const IndexType> map(
483 * const device_matrix_data<ValueType, IndexType>& md)
484 * {
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());
489 * }
490 * };
491 *
492 *
493 * } // namespace gko::ext::kokkos::detail
494 *
495 *
496 * template <typename ValueType, typename IndexType>
497 * void generate_stencil_matrix(gko::matrix::Csr<ValueType, IndexType>* matrix)
498 * {
499 * auto exec = matrix->get_executor();
500 * const auto discretization_points = matrix->get_size()[0];
501 *
502 * gko::device_matrix_data<ValueType, IndexType> md(exec, matrix->get_size(),
503 * discretization_points * 3);
504 *
505 * auto k_md = gko::ext::kokkos::map_data(md);
506 *
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;
514 *
515 * auto mask =
516 * static_cast<IndexType>(0 <= col && col < discretization_points);
517 *
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];
521 * });
522 *
523 * md.sum_duplicates();
524 *
525 * matrix->read(std::move(md));
526 * }
527 *
528 *
529 * template <typename Closure, typename ValueType>
530 * void generate_rhs(Closure&& f, ValueType u0, ValueType u1,
532 * {
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;
540 * if (i == 0) {
541 * k_rhs(i, 0) += u0;
542 * }
543 * if (i == discretization_points - 1) {
544 * k_rhs(i, 0) += u1;
545 * }
546 * });
547 * }
548 *
549 *
550 * template <typename Closure, typename ValueType>
551 * double calculate_error(int discretization_points,
553 * Closure&& correct_u)
554 * {
555 * auto k_u = gko::ext::kokkos::map_data(u);
556 * auto error = 0.0;
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)));
564 * },
565 * error);
566 * return error;
567 * }
568 *
569 *
570 * int main(int argc, char* argv[])
571 * {
572 * using ValueType = double;
573 * using RealValueType = gko::remove_complex<ValueType>;
574 * using IndexType = int;
575 *
578 * using cg = gko::solver::Cg<ValueType>;
580 *
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); // print Kokkos help
585 * std::exit(1);
586 * }
587 *
588 * Kokkos::ScopeGuard kokkos(argc, argv);
589 *
590 * const auto discretization_points =
591 * static_cast<gko::size_type>(argc >= 2 ? std::atoi(argv[1]) : 100u);
592 *
593 * auto exec = gko::ext::kokkos::create_default_executor();
594 *
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);
599 *
600 * auto rhs = vec::create(exec, gko::dim<2>(discretization_points, 1));
601 * generate_rhs(f, u0, u1, rhs.get());
602 * auto u = vec::create(exec, gko::dim<2>(discretization_points, 1));
603 * u->fill(0.0);
604 *
605 * auto A = share(mtx::create(
606 * exec, gko::dim<2>{discretization_points, discretization_points}));
607 * generate_stencil_matrix(A.get());
608 *
609 * const RealValueType reduction_factor{1e-7};
610 * cg::build()
611 * .with_criteria(
612 * gko::stop::Iteration::build().with_max_iters(discretization_points),
613 * gko::stop::ResidualNorm<ValueType>::build().with_reduction_factor(
614 * reduction_factor))
615 * .with_preconditioner(bj::build())
616 * .on(exec)
617 * ->generate(A)
618 * ->apply(rhs, u);
619 *
620 * std::cout << "\nSolve complete."
621 * << "\nThe average relative error is "
622 * << calculate_error(discretization_points, u.get(), correct_u) /
623 * discretization_points
624 * << std::endl;
625 * }
626 * @endcode
627*/
Definition device_matrix_data.hpp:36
Definition csr.hpp:123
Definition dense.hpp:116
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 cg.hpp:49
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
Definition dim.hpp:26