/home/docs/checkouts/readthedocs.org/user_builds/ginkgo-test/checkouts/latest/build/doc/doxygen/examples/custom-stopping-criterion.hpp Source File

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

Reference API: /home/docs/checkouts/readthedocs.org/user_builds/ginkgo-test/checkouts/latest/build/doc/doxygen/examples/custom-stopping-criterion.hpp Source File
Reference API
custom-stopping-criterion.hpp
1
402 * class ByInteraction
406 * using Criterion = gko::stop::Criterion;
407 *
408 * public:
409 * GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
410 * {
411 *
414 * std::add_pointer<volatile bool>::type GKO_FACTORY_PARAMETER_SCALAR(
415 * stop_iteration_process, nullptr);
416 * };
417 * GKO_ENABLE_CRITERION_FACTORY(ByInteraction, parameters, Factory);
418 * GKO_ENABLE_BUILD_METHOD(Factory);
419 *
420 * protected:
421 * bool check_impl(gko::uint8 stoppingId, bool setFinalized,
423 * bool* one_changed, const Criterion::Updater&) override
424 * {
425 * bool result = *(parameters_.stop_iteration_process);
426 * if (result) {
427 * this->set_all_statuses(stoppingId, setFinalized, stop_status);
428 * *one_changed = true;
429 * }
430 * return result;
431 * }
432 *
433 * explicit ByInteraction(std::shared_ptr<const gko::Executor> exec)
434 * : EnablePolymorphicObject<ByInteraction, Criterion>(std::move(exec))
435 * {}
436 *
437 * explicit ByInteraction(const Factory* factory,
438 * const gko::stop::CriterionArgs& args)
439 *
440 * : EnablePolymorphicObject<ByInteraction, Criterion>(
441 * factory->get_executor()),
442 * parameters_{factory->get_parameters()}
443 * {}
444 * };
445 *
446 *
447 * void run_solver(volatile bool* stop_iteration_process,
448 * std::shared_ptr<gko::Executor> exec)
449 * {
450 * using ValueType = double;
451 * using RealValueType = gko::remove_complex<ValueType>;
452 * using IndexType = int;
453 *
458 *
459 * auto A = share(gko::read<mtx>(std::ifstream("data/A.mtx"), exec));
460 * auto b = gko::read<vec>(std::ifstream("data/b.mtx"), exec);
461 * auto x = gko::read<vec>(std::ifstream("data/x0.mtx"), exec);
462 *
463 * auto solver =
464 * bicg::build()
465 * .with_criteria(ByInteraction::build().with_stop_iteration_process(
466 * stop_iteration_process))
467 * .on(exec)
468 * ->generate(A);
470 * gko::log::Logger::iteration_complete_mask, std::cout, true));
471 * solver->apply(b, x);
472 *
473 * std::cout << "Solver stopped" << std::endl;
474 *
475 * std::cout << "Solution (x): \n";
476 * write(std::cout, x);
477 *
478 * auto one = gko::initialize<vec>({1.0}, exec);
479 * auto neg_one = gko::initialize<vec>({-1.0}, exec);
480 * auto res = gko::initialize<real_vec>({0.0}, exec);
481 * A->apply(one, x, neg_one, b);
482 * b->compute_norm2(res);
483 *
484 * std::cout << "Residual norm sqrt(r^T r): \n";
485 * write(std::cout, res);
486 * }
487 *
488 *
489 * int main(int argc, char* argv[])
490 * {
491 * std::cout << gko::version_info::get() << std::endl;
492 *
493 * if (argc == 2 && (std::string(argv[1]) == "--help")) {
494 * std::cerr << "Usage: " << argv[0] << " [executor]" << std::endl;
495 * std::exit(-1);
496 * }
497 *
498 * const auto executor_string = argc >= 2 ? argv[1] : "reference";
499 *
500 * std::map<std::string, std::function<std::shared_ptr<gko::Executor>()>>
501 * exec_map{
502 * {"omp", [] { return gko::OmpExecutor::create(); }},
503 * {"cuda",
504 * [] {
505 * return gko::CudaExecutor::create(0,
507 * }},
508 * {"hip",
509 * [] {
511 * }},
512 * {"dpcpp",
513 * [] {
516 * }},
517 * {"reference", [] { return gko::ReferenceExecutor::create(); }}};
518 *
519 * const auto exec = exec_map.at(executor_string)(); // throws if not valid
520 *
521 * volatile bool stop_iteration_process{};
522 *
523 * std::thread t(run_solver, &stop_iteration_process, exec);
524 *
525 * std::cout << "Type 'stop' to stop the iteration process" << std::endl;
526 * std::string command;
527 * while (std::cin >> command) {
528 * if (command == "stop") {
529 * break;
530 * } else {
531 * std::cout << "Unknown command" << std::endl;
532 * }
533 * }
534 * std::cout << "User input command 'stop' - The solver will stop!"
535 * << std::endl;
536 * stop_iteration_process = true;
537 * t.join();
538 * }
539 * @endcode
540*/
Definition custom-stopping-criterion.cpp:17
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)
Definition polymorphic_object.hpp:668
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
Definition array.hpp:166
Definition stream.hpp:30
Definition csr.hpp:123
Definition bicgstab.hpp:53
Definition criterion.hpp:36
static const version_info & get()
Definition version.hpp:139
#define GKO_CREATE_FACTORY_PARAMETERS(_parameters_name, _factory_name)
Definition abstract_factory.hpp:280
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Definition abstract_factory.hpp:445
#define GKO_ENABLE_BUILD_METHOD(_factory_name)
Definition abstract_factory.hpp:394
constexpr T one()
Definition math.hpp:630
std::uint8_t uint8
Definition types.hpp:118
void write(StreamType &&os, MatrixPtrType &&matrix, layout_type layout=detail::mtx_io_traits< std::remove_cv_t< detail::pointee< MatrixPtrType > > >::default_layout)
Definition mtx_io.hpp:295
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 criterion.hpp:205