text: /*******************************<GINKGO LICENSE>******************************
  Copyright (c) 2017-2022, the Ginkgo authors
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  
  1. Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
  
  2. Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.
  
  3. Neither the name of the copyright holder nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  ******************************<GINKGO LICENSE>*******************************/
  
  
  #include <ginkgo/ginkgo.hpp>
  
  
  #include <fstream>
  #include <iostream>
  #include <map>
  #include <string>
  
  
  int main(int argc, char* argv[])
  {
      // Some shortcuts
      using ValueType = double;
      using RealValueType = gko::remove_complex<ValueType>;
      using IndexType = int;
  
      using vec = gko::matrix::Dense<ValueType>;
      using real_vec = gko::matrix::Dense<RealValueType>;
      using mtx = gko::matrix::Csr<ValueType, IndexType>;
      using cg = gko::solver::Cg<ValueType>;
      using bj = gko::preconditioner::Jacobi<ValueType, IndexType>;
  
      // Print version information
      std::cout << gko::version_info::get() << std::endl;
  
      // Figure out where to run the code
      if (argc == 2 && (std::string(argv[1]) == "--help")) {
          std::cerr << "Usage: " << argv[0] << " [executor]" << std::endl;
          std::exit(-1);
      }
  
      // Figure out where to run the code
      const auto executor_string = argc >= 2 ? argv[1] : "reference";
      std::map<std::string, std::function<std::shared_ptr<gko::Executor>()>>
          exec_map{
              {"omp", [] { return gko::OmpExecutor::create(); }},
              {"cuda",
               [] {
                   return gko::CudaExecutor::create(0, gko::OmpExecutor::create(),
                                                    true);
               }},
              {"hip",
               [] {
                   return gko::HipExecutor::create(0, gko::OmpExecutor::create(),
                                                   true);
               }},
              {"dpcpp",
               [] {
                   return gko::DpcppExecutor::create(0,
                                                     gko::OmpExecutor::create());
               }},
              {"reference", [] { return gko::ReferenceExecutor::create(); }}};
  
      // executor where Ginkgo will perform the computation
      const auto exec = exec_map.at(executor_string)();  // throws if not valid
  
      // Read data
      auto A = share(gko::read<mtx>(std::ifstream("data/A.mtx"), exec));
      auto b = gko::read<vec>(std::ifstream("data/b.mtx"), exec);
      auto x = gko::read<vec>(std::ifstream("data/x0.mtx"), exec);
  
      const RealValueType reduction_factor{1e-7};
      // Create solver factory
      auto solver_gen =
          cg::build()
              .with_criteria(
                  gko::stop::Iteration::build().with_max_iters(20u).on(exec),
                  gko::stop::ResidualNorm<ValueType>::build()
                      .with_reduction_factor(reduction_factor)
                      .on(exec))
              // Add preconditioner, these 2 lines are the only
              // difference from the simple solver example
              .with_preconditioner(bj::build().with_max_block_size(8u).on(exec))
              .on(exec);
      // Create solver
      auto solver = solver_gen->generate(A);
  
      // Solve system
      solver->apply(lend(b), lend(x));
  
      // Print solution
      std::cout << "Solution (x):\n";
      write(std::cout, lend(x));
  
      // Calculate residual
      auto one = gko::initialize<vec>({1.0}, exec);
      auto neg_one = gko::initialize<vec>({-1.0}, exec);
      auto res = gko::initialize<real_vec>({0.0}, exec);
      A->apply(lend(one), lend(x), lend(neg_one), lend(b));
      b->compute_norm2(lend(res));
  
      std::cout << "Residual norm sqrt(r^T r):\n";
      write(std::cout, lend(res));
  }
  