### Instantiate and Install Ginkgo Device Library Source: https://github.com/ginkgo-project/ginkgo/blob/develop/devices/CMakeLists.txt Example of using the custom CMake functions to add the 'ginkgo_device' library with specified source files and then install it. ```cmake ginkgo_add_library(ginkgo_device machine_topology.cpp device.cpp) ginkgo_install_library(ginkgo_device) ``` -------------------------------- ### Navigate to Ginkgo Examples Directory Source: https://github.com/ginkgo-project/ginkgo/wiki/Tutorial-1:-Getting-Started Change your current directory to the 'examples' folder to access and run Ginkgo examples. Ensure Ginkgo was compiled with example support enabled. ```sh cd examples/ ``` -------------------------------- ### Install Ginkgo Source: https://github.com/ginkgo-project/ginkgo/blob/develop/INSTALL.md Execute this command in the build folder to install Ginkgo to the specified folder. Use 'sudo' if the installation prefix is not writable. ```shell make install ``` -------------------------------- ### Conditionally Add Examples Subdirectory Source: https://github.com/ginkgo-project/ginkgo/blob/develop/doc/CMakeLists.txt Includes the 'examples' subdirectory only if the GINKGO_DOC_GENERATE_EXAMPLES option is enabled. ```cmake if(GINKGO_DOC_GENERATE_EXAMPLES) add_subdirectory(examples) endif() ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/ginkgo-project/ginkgo/blob/develop/test/test_pkgconfig/CMakeLists.txt Standard CMake setup including minimum version, project name, and C++ standard. This forms the foundation for integrating libraries like Ginkgo. ```cmake cmake_minimum_required(VERSION 3.16) project(GinkgoExportBuildWithPkgConfigTest LANGUAGES CXX) # Here, we use test install without any data. We instantiate the # interface only. add_executable(test_pkgconfig ../test_install/test_install.cpp) target_compile_features(test_pkgconfig PUBLIC cxx_std_17) ``` -------------------------------- ### Create Test Installation Directories Source: https://github.com/ginkgo-project/ginkgo/blob/develop/CMakeLists.txt Ensures the necessary directories for test installation exist. ```cmake file(MAKE_DIRECTORY "${GINKGO_TEST_INSTALL_BIN_DIR}") file(MAKE_DIRECTORY "${GINKGO_TEST_EXPORTBUILD_BIN_DIR}") ``` -------------------------------- ### Install Ginkgo with Sudo Privileges Source: https://github.com/ginkgo-project/ginkgo/wiki/Tutorial-1:-Getting-Started Install Ginkgo using sudo if the installation prefix is not writable by the current user. ```sh sudo make install ``` -------------------------------- ### Minimal CUDA Solver Example in C++ Source: https://github.com/ginkgo-project/ginkgo/wiki/Release-Notes-v1.0.0 Demonstrates solving a linear system using a preconditioned Conjugate Gradient (CG) solver on a CUDA accelerator. Requires data to be read from standard input and results written to standard output. Ensure Ginkgo library is installed and headers are included. ```cpp #include #include int main() { // Instantiate a CUDA executor auto gpu = gko::CudaExecutor::create(0, gko::OmpExecutor::create()); // Read data auto A = gko::read>(std::cin, gpu); auto b = gko::read>(std::cin, gpu); auto x = gko::read>(std::cin, gpu); // Create the solver auto solver = gko::solver::Cg<>::build() .with_preconditioner(gko::preconditioner::Jacobi<>::build().on(gpu)) .with_criteria( gko::stop::Iteration::build().with_max_iters(1000u).on(gpu), gko::stop::ResidualNormReduction<>::build() .with_reduction_factor(1e-15) .on(gpu)) .on(gpu); // Solve system solver->generate(give(A))->apply(lend(b), lend(x)); // Write result write(std::cout, lend(x)); } ``` -------------------------------- ### Benchmark matrix list file example Source: https://github.com/ginkgo-project/ginkgo/blob/develop/BENCHMARKING.md Example content for a matrix list file used to specify which matrices to benchmark. ```plaintext 1903 Freescale/circuit5M thermal2 ``` -------------------------------- ### CMakeLists.txt for Heat Equation Example Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/heat-equation/CMakeLists.txt Configures the build for the heat equation example. It specifies the minimum CMake version, project name, finds necessary packages (Ginkgo and OpenCV), defines the executable, links libraries, and copies data files. ```cmake cmake_minimum_required(VERSION 3.16) project(heat-equation) ``` ```cmake # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() ``` ```cmake find_package(OpenCV REQUIRED) ``` ```cmake add_executable(heat-equation heat-equation.cpp) ``` ```cmake target_link_libraries(heat-equation Ginkgo::ginkgo ${OpenCV_LIBS}) ``` ```cmake # Copy the data files to the execution directory configure_file( ../../matrices/examples/gko_logo_2d.mtx data/gko_logo_2d.mtx COPYONLY ) ``` ```cmake configure_file( ../../matrices/examples/gko_text_2d.mtx data/gko_text_2d.mtx COPYONLY ) ``` -------------------------------- ### Build Example with Ginkgo Dependency Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/preconditioner-export/CMakeLists.txt Configures the build system to find and link against the Ginkgo library. This is necessary when building the example as a standalone project. ```cmake cmake_minimum_required(VERSION 3.16) project(preconditioner-export) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() add_executable(preconditioner-export preconditioner-export.cpp) target_link_libraries(preconditioner-export Ginkgo::ginkgo) # Copy the data files to the execution directory configure_file(data/A.mtx data/A.mtx COPYONLY) ``` -------------------------------- ### CMakeLists.txt Configuration for Ginkgo Example Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/par-ilu-convergence/CMakeLists.txt This snippet configures the CMake build for the parallel ILU convergence example. It finds the required Ginkgo library version and links it to the executable. It also copies necessary data files. ```cmake cmake_minimum_required(VERSION 3.16) project(par-ilu-convergence) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() add_executable(par-ilu-convergence par-ilu-convergence.cpp) target_link_libraries(par-ilu-convergence Ginkgo::ginkgo) # Copy the data files to the execution directory configure_file(data/A.mtx data/A.mtx COPYONLY) ``` -------------------------------- ### Install Ginkgo Core Library Source: https://github.com/ginkgo-project/ginkgo/blob/develop/core/CMakeLists.txt Installs the Ginkgo core library. ```cmake ginkgo_install_library(${ginkgo_core}) ``` -------------------------------- ### Setup CPack Source: https://github.com/ginkgo-project/ginkgo/blob/develop/CMakeLists.txt Configures CPack for packaging the Ginkgo library, setting description, license, and icon files. ```cmake set(CPACK_PACKAGE_DESCRIPTION_FILE "${Ginkgo_SOURCE_DIR}/README.md") set(CPACK_RESOURCE_FILE_LICENSE "${Ginkgo_SOURCE_DIR}/LICENSE") set(CPACK_PACKAGE_ICON "${Ginkgo_SOURCE_DIR}/assets/logo.png") set(CPACK_PACKAGE_CONTACT "ginkgo.library@gmail.com") include(CPack) ``` -------------------------------- ### CMakeLists.txt for cb-gmres Example Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/cb-gmres/CMakeLists.txt This CMakeLists.txt file configures the build for the cb-gmres example. It finds the Ginkgo library, adds the executable, links against Ginkgo, and copies a matrix data file. ```cmake cmake_minimum_required(VERSION 3.16) project(cb-gmres) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() add_executable(cb-gmres cb-gmres.cpp) target_link_libraries(cb-gmres Ginkgo::ginkgo) # Copy the data files to the execution directory configure_file( "${Ginkgo_SOURCE_DIR}/matrices/test/ani1.mtx" data/A.mtx COPYONLY ) ``` -------------------------------- ### Build and Install Ginkgo with CMake Source: https://github.com/ginkgo-project/ginkgo/blob/develop/README.md Standard CMake procedure for building and installing Ginkgo. Ensure you are in the build directory before executing these commands. ```sh mkdir build; cd build cmake -G "Unix Makefiles" .. && cmake --build . cmake --install . ``` -------------------------------- ### CMakeLists.txt Configuration for Ginkgo Example Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/performance-debugging/CMakeLists.txt This CMake script sets up the build environment for a Ginkgo performance debugging example. It ensures the Ginkgo library is found and linked, and prepares necessary data files for execution. ```cmake cmake_minimum_required(VERSION 3.16) project(performance-debugging) ``` ```cmake # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() ``` ```cmake add_executable(performance-debugging performance-debugging.cpp) ``` ```cmake target_link_libraries(performance-debugging Ginkgo::ginkgo) ``` ```cmake # Copy the data files to the execution directory configure_file(data/A.mtx data/A.mtx COPYONLY) ``` -------------------------------- ### Install Binaries and Data Source: https://github.com/ginkgo-project/ginkgo/blob/develop/test/test_install/CMakeLists.txt Installs the compiled executables and data files to a specified directory structure within the Ginkgo installation prefix. This includes conditional installation of CUDA and HIP executables. ```cmake set(TESTINSTALL_INSTALL_DIR "${GINKGO_INSTALL_PREFIX}/smoke_tests") set(TESTINSTALL_INSTALL_DATADIR "${TESTINSTALL_INSTALL_DIR}/data") install(TARGETS test_install RUNTIME DESTINATION ${TESTINSTALL_INSTALL_DIR}) if(GINKGO_BUILD_CUDA) install( TARGETS test_install_cuda RUNTIME DESTINATION ${TESTINSTALL_INSTALL_DIR} ) endif() if(GINKGO_BUILD_HIP) install( TARGETS test_install_hip RUNTIME DESTINATION ${TESTINSTALL_INSTALL_DIR} ) endif() install( DIRECTORY "${TestInstall_BINARY_DIR}/data/" DESTINATION "${TESTINSTALL_INSTALL_DATADIR}" ) ``` -------------------------------- ### Installation Error: Permissions Source: https://github.com/ginkgo-project/ginkgo/wiki/Frequently-asked-questions-(FAQ) Permission errors during 'make install' often occur when trying to write to system directories like '/usr/include'. Specify a custom installation path using CMAKE_INSTALL_PREFIX. ```bash cmake .. -DCMAKE_INSTALL_PREFIX=/your/desired/path make install ``` ```bash sudo make install ``` -------------------------------- ### Preconditioner Export Tool Example Execution Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/preconditioner-export/doc/results.dox This snippet demonstrates a typical execution of the preconditioner export tool, showing the input matrix file being read and the output file being written. ```cpp Reading data/A.mtx Writing data/A.mtx.jacobi ``` -------------------------------- ### Configure Ginkgo Ranges Example Build Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/ginkgo-ranges/CMakeLists.txt This CMakeLists.txt file sets up the build for the ginkgo-ranges executable. It finds the Ginkgo library and links it to the target executable. The example is configured to find Ginkgo 1.12.0. ```cmake cmake_minimum_required(VERSION 3.16) project(ginkgo-ranges) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() add_executable(ginkgo-ranges ginkgo-ranges.cpp) target_link_libraries(ginkgo-ranges Ginkgo::ginkgo) ``` -------------------------------- ### Install Ginkgo DPCPP Library Source: https://github.com/ginkgo-project/ginkgo/blob/develop/dpcpp/CMakeLists.txt Installs the compiled ginkgo_dpcpp library. This makes the library available for use in other projects or for deployment. ```cmake ginkgo_install_library(ginkgo_dpcpp) ``` -------------------------------- ### Install CUDA Library Source: https://github.com/ginkgo-project/ginkgo/blob/develop/cuda/CMakeLists.txt Installs the ginkgo_cuda library. This makes the compiled CUDA backend library available for use in other projects or for system-wide installation. ```cmake ginkgo_install_library(ginkgo_cuda) ``` -------------------------------- ### Include Statement Grouping Example Source: https://github.com/ginkgo-project/ginkgo/wiki/Contributing-guidelines Provides an example of how include statements should be ordered in groups with two blank lines separating each group, following a specific hierarchy. ```c++ #include #include #include #include #include #include #include "third_party/blas/cblas.hpp" #include "third_party/lapack/lapack.hpp" #include #include #include #include "core/base/my_file_kernels.hpp" ``` -------------------------------- ### CMakeLists.txt Configuration for IR-ILU Solver Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/ir-ilu-preconditioned-solver/CMakeLists.txt Configures the build for the IR-ILU preconditioned solver example. It finds the Ginkgo library and links the executable against it. It also copies necessary data files. ```cmake cmake_minimum_required(VERSION 3.16) project(ir-ilu-preconditioned-solver) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() add_executable(ir-ilu-preconditioned-solver ir-ilu-preconditioned-solver.cpp) target_link_libraries(ir-ilu-preconditioned-solver Ginkgo::ginkgo) # Copy the data files to the execution directory configure_file(data/A.mtx data/A.mtx COPYONLY) ``` -------------------------------- ### CMake Configuration for Mixed SPMV Example Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/mixed-spmv/CMakeLists.txt This CMake script sets up the build for the mixed-spmv example. It finds the Ginkgo library and links it to the executable. It also copies necessary data files. ```cmake cmake_minimum_required(VERSION 3.16) project(mixed-spmv) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() add_executable(mixed-spmv mixed-spmv.cpp) target_link_libraries(mixed-spmv Ginkgo::ginkgo) # Copy the data files to the execution directory configure_file(data/A.mtx data/A.mtx COPYONLY) ``` -------------------------------- ### CMakeLists.txt for Mixed Multigrid Solver Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/mixed-multigrid-preconditioned-solver/CMakeLists.txt Configures the build for the mixed multigrid solver example. It finds the Ginkgo library and links it to the executable. It also copies necessary data files. ```cmake cmake_minimum_required(VERSION 3.16) project(mixed-multigrid-preconditioned-solver) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() add_executable( mixed-multigrid-preconditioned-solver mixed-multigrid-preconditioned-solver.cpp ) target_link_libraries(mixed-multigrid-preconditioned-solver Ginkgo::ginkgo) # Copy the data files to the execution directory configure_file(data/A.mtx data/A.mtx COPYONLY) ``` -------------------------------- ### CMakeLists.txt for Inverse Iteration Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/inverse-iteration/CMakeLists.txt Configures the build for the inverse iteration example, finding the Ginkgo library and linking it to the executable. ```cmake cmake_minimum_required(VERSION 3.16) project(inverse-iteration) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() add_executable(inverse-iteration inverse-iteration.cpp) target_link_libraries(inverse-iteration Ginkgo::ginkgo) # Copy the data files to the execution directory configure_file(data/A.mtx data/A.mtx COPYONLY) ``` -------------------------------- ### Matrix Output Example in C++ Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/ginkgo-ranges/doc/results.dox This snippet shows the expected output format for matrices in Ginkgo, commonly used for results or intermediate computations. ```cpp L = [ 1.00 0.00 0.00 2.00 1.00 0.00 3.00 4.00 1.00 ] U = [ 2.00 4.00 5.00 0.00 3.00 2.00 0.00 0.00 1.00 ] ``` -------------------------------- ### Example Loggable Object Implementation (Cg Solver) Source: https://github.com/ginkgo-project/ginkgo/wiki/PAPI-Logger-draft Provides an example implementation of a loggable object, specifically a Conjugate Gradient (Cg) solver. It inherits from `EnableLogging` and calls `log_iteration_complete` within its `apply` method to notify registered loggers. ```c++ class Cg : public BasicLinOp, public EnableLogging { public: void apply(/* parameters */) { // setup for (int i = 0; i < num_iters; ++i) { // do iteration this->log_iteration_complete(/* parameters */); // doesn't have to know about PAPI } } }; ``` -------------------------------- ### Define Ginkgo Test Installation Command Source: https://github.com/ginkgo-project/ginkgo/blob/develop/CMakeLists.txt Sets the command for the test_install target, incorporating configuration prefixes. ```cmake set(GINKGO_CONFIG_PREFIX "$<$:$/$/>") set(GINKGO_TEST_INSTALL_CMD ${GINKGO_TEST_INSTALL_BIN_DIR}/${GINKGO_CONFIG_PREFIX}test_install ) ``` -------------------------------- ### CMakeLists.txt for Nine-Point Stencil Solver Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/nine-pt-stencil-solver/CMakeLists.txt Configures the build for the nine-point stencil solver example. It specifies the minimum CMake version, project name, finds the Ginkgo library, and links the executable against it. Use this file when building the example as a standalone project. ```cmake cmake_minimum_required(VERSION 3.16) project(nine-pt-stencil-solver) ``` ```cmake # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() ``` ```cmake add_executable(nine-pt-stencil-solver nine-pt-stencil-solver.cpp) target_link_libraries(nine-pt-stencil-solver Ginkgo::ginkgo) ``` -------------------------------- ### Include Statement Grouping Example Source: https://github.com/ginkgo-project/ginkgo/blob/develop/CONTRIBUTING.md Shows the recommended order and grouping for include statements in a C++ file, with blank lines separating each group. ```c++ #include "ginkgo/core/base/my_file.hpp" #include #include #include #include #include #include #include #include #include #include #include "core/base/my_file_kernels.hpp" ``` -------------------------------- ### CMake Project Configuration Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/schroedinger-splitting/CMakeLists.txt Sets up the CMake build environment, defines the project name, and finds necessary external libraries like Ginkgo and OpenCV. This is essential for building the example. ```cmake cmake_minimum_required(VERSION 3.16) project(schroedinger-splitting) ``` ```cmake # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() find_package(OpenCV REQUIRED) ``` ```cmake add_executable(schroedinger-splitting schroedinger-splitting.cpp) target_link_libraries(schroedinger-splitting Ginkgo::ginkgo ${OpenCV_LIBS}) ``` ```cmake # Copy the data files to the execution directory configure_file( ../../matrices/examples/gko_logo_2d.mtx data/gko_logo_2d.mtx COPYONLY ) configure_file( ../../matrices/examples/gko_text_2d.mtx data/gko_text_2d.mtx COPYONLY ) ``` -------------------------------- ### Configure Poisson Solver Project Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/poisson-solver/CMakeLists.txt Sets up the minimum CMake version and project name. It finds the Ginkgo library if the example is not built as part of the main Ginkgo build. Finally, it adds the executable and links the Ginkgo library. ```cmake cmake_minimum_required(VERSION 3.16) project(poisson-solver) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() add_executable(poisson-solver poisson-solver.cpp) target_link_libraries(poisson-solver Ginkgo::ginkgo) ``` -------------------------------- ### CMakeLists.txt for Batched Solver Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/batched-solver/CMakeLists.txt Configures the build for the batched solver example. It finds the Ginkgo library and links it to the executable. The Ginkgo library is only found if Ginkgo is not built as part of the same project. ```cmake cmake_minimum_required(VERSION 3.16) project(batched-solver) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() add_executable(batched-solver batched-solver.cpp) target_link_libraries(batched-solver Ginkgo::ginkgo) ``` -------------------------------- ### CMakeLists.txt for ILU Preconditioned Solver Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/ilu-preconditioned-solver/CMakeLists.txt This CMake script configures the build for the ILU preconditioned solver example. It finds the Ginkgo library and links it to the executable. It also ensures necessary data files are copied to the execution directory. ```cmake cmake_minimum_required(VERSION 3.16) project(ilu-preconditioned-solver) ``` ```cmake # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() ``` ```cmake add_executable(ilu-preconditioned-solver ilu-preconditioned-solver.cpp) target_link_libraries(ilu-preconditioned-solver Ginkgo::ginkgo) ``` ```cmake # Copy the data files to the execution directory configure_file(data/A.mtx data/A.mtx COPYONLY) configure_file(data/b.mtx data/b.mtx COPYONLY) configure_file(data/x0.mtx data/x0.mtx COPYONLY) ``` -------------------------------- ### CMake Configuration for Kokkos Assembly Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/kokkos-assembly/CMakeLists.txt Sets up the CMake build for an example that depends on Ginkgo and Kokkos. It finds the necessary packages and links them to the executable. ```cmake cmake_minimum_required(VERSION 3.16) project(kokkos-assembly CXX) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() find_package(Kokkos 4.1.00 REQUIRED) # Kokkos doesn't handle any compiler launcher well, so it's disable it unset(CMAKE_CXX_COMPILER_LAUNCHER) add_executable(kokkos-assembly kokkos-assembly.cpp) target_link_libraries(kokkos-assembly Ginkgo::ginkgo Kokkos::kokkos) ``` -------------------------------- ### Run Ginkgo Benchmarks and Aggregate Results Source: https://github.com/ginkgo-project/ginkgo/blob/develop/BENCHMARKING.md Execute benchmark scripts and aggregate results into JSON files. Ensure Python 3 is installed. ```bash ./build-list . > list.json ./agregate < list.json > agregate.json ./represent . > represent.json ``` -------------------------------- ### Minimal Ginkgo C++ Example Source: https://context7.com/ginkgo-project/ginkgo/llms.txt A basic C++ program demonstrating how to use Ginkgo for solving a linear system. It requires matrix and vector data files (A.mtx, b.mtx, x0.mtx) and uses the OpenMP executor. ```cpp // main.cpp — minimal working example using installed Ginkgo #include #include #include int main() { using ValueType = double; using IndexType = int; using mtx = gko::matrix::Csr; using vec = gko::matrix::Dense; using cg = gko::solver::Cg; auto exec = gko::OmpExecutor::create(); auto A = gko::share(gko::read(std::ifstream("A.mtx"), exec)); auto b = gko::read(std::ifstream("b.mtx"), exec); auto x = gko::read(std::ifstream("x0.mtx"), exec); cg::build() .with_criteria( gko::stop::Iteration::build().with_max_iters(1000u), gko::stop::ResidualNorm::build() .with_reduction_factor(1e-8)) .on(exec) ->generate(A) ->apply(b, x); gko::write(std::cout, x); return 0; } ``` -------------------------------- ### Ginkgo Solver Loggers Example Source: https://context7.com/ginkgo-project/ginkgo/llms.txt Demonstrates attaching different types of loggers (stream, convergence, record, custom, performance hint) to a Ginkgo solver and executor. Ensure necessary matrix and vector data are loaded before creating the solver. ```cpp #include #include using ValueType = double; using vec = gko::matrix::Dense; using mtx = gko::matrix::Csr; auto exec = gko::OmpExecutor::create(); auto A = gko::share(gko::read(std::ifstream("A.mtx"), exec)); auto b = gko::read(std::ifstream("b.mtx"), exec); auto x = gko::read(std::ifstream("x0.mtx"), exec); // --- Stream logger: prints iteration info to stdout --- auto stream_logger = gko::log::Stream::create( gko::log::Logger::iteration_complete_mask, std::cout, true /* print residual */); // --- Convergence logger: captures final iteration count and residual --- auto conv_logger = gko::log::Convergence::create(); // --- Record logger: stores all events in memory --- auto record_logger = gko::log::Record::create(); auto solver = gko::solver::Cg::build() .with_criteria( gko::stop::Iteration::build().with_max_iters(100u), gko::stop::ResidualNorm::build() .with_reduction_factor(1e-8)) .on(exec) ->generate(A); // Attach loggers to the generated solver solver->add_logger(stream_logger); solver->add_logger(conv_logger); solver->apply(b, x); // Access convergence info std::cout << "Iterations: " << conv_logger->get_num_iterations() << std::endl; auto res_norm = gko::as(conv_logger->get_residual_norm()); gko::write(std::cout, res_norm); // --- Custom logger: implement on_iteration_complete --- template struct MyLogger : gko::log::Logger { MyLogger() : gko::log::Logger(gko::log::Logger::iteration_complete_mask) {} void on_iteration_complete( const gko::LinOp* solver, const gko::LinOp* b, const gko::LinOp* solution, const gko::size_type& iteration, const gko::LinOp* residual, const gko::LinOp* residual_norm, const gko::LinOp* implicit_sq_residual_norm, const gko::array*, bool) const override { if (residual_norm) { auto norm = gko::as>>( residual_norm); std::cout << "Iter " << iteration << " residual: " << norm->get_executor()->copy_val_to_host( norm->get_const_values()) << "\n"; } } }; // --- Performance hint logger --- auto perf_logger = gko::log::PerformanceHint::create(std::cout); exec->add_logger(perf_logger); // attach to executor, not solver ``` -------------------------------- ### Create Ginkgo Executors Source: https://context7.com/ginkgo-project/ginkgo/llms.txt Examples of creating different Ginkgo executors for various hardware backends. The executor determines where computation takes place. Runtime selection allows choosing a backend dynamically. ```cpp #include // Single-threaded reference executor (for testing / debugging) auto ref = gko::ReferenceExecutor::create(); // Multi-threaded OpenMP executor (CPU) auto omp = gko::OmpExecutor::create(); // NVIDIA GPU executor (device 0, with OMP as host executor) auto cuda = gko::CudaExecutor::create(0, gko::OmpExecutor::create()); // AMD GPU executor (device 0) auto hip = gko::HipExecutor::create(0, gko::OmpExecutor::create()); // Intel GPU/CPU executor via SYCL/DPC++ auto dpcpp = gko::DpcppExecutor::create(0, gko::OmpExecutor::create()); // Runtime selection const std::string backend = "cuda"; // or "omp", "hip", "dpcpp", "reference" std::map()>> exec_map{ {"omp", [] { return gko::OmpExecutor::create(); }}, {"cuda", [] { return gko::CudaExecutor::create(0, gko::OmpExecutor::create()); }}, {"hip", [] { return gko::HipExecutor::create(0, gko::OmpExecutor::create()); }}, {"dpcpp", [] { return gko::DpcppExecutor::create(0, gko::OmpExecutor::create()); }}, {"reference", [] { return gko::ReferenceExecutor::create(); }} }; auto exec = exec_map.at(backend)(); // Synchronize device (needed before reading results after async operations) exec->synchronize(); // Get host (CPU) executor from a device executor auto master = exec->get_master(); // Print version std::cout << gko::version_info::get() << std::endl; ``` -------------------------------- ### CMake Configuration for Mixed Multigrid Solver Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/mixed-multigrid-solver/CMakeLists.txt Sets up the CMake build system for the mixed multigrid solver example. It finds the Ginkgo library and defines the executable, linking it against Ginkgo. ```cmake cmake_minimum_required(VERSION 3.16) project(mixed-multigrid-solver) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() add_executable(mixed-multigrid-solver mixed-multigrid-solver.cpp) target_link_libraries(mixed-multigrid-solver Ginkgo::ginkgo) # Copy the data files to the execution directory configure_file(data/A.mtx data/A.mtx COPYONLY) ``` -------------------------------- ### CMakeLists.txt for Three-Point Stencil Solver Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/three-pt-stencil-solver/CMakeLists.txt Configures the build for the three-point stencil solver example. It sets the minimum CMake version, project name, finds the Ginkgo library, and defines the executable and its linking dependencies. ```cmake cmake_minimum_required(VERSION 3.16) project(three-pt-stencil-solver) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() add_executable(three-pt-stencil-solver three-pt-stencil-solver.cpp) target_link_libraries(three-pt-stencil-solver Ginkgo::ginkgo) ``` -------------------------------- ### Find Ginkgo Package with CMake Source: https://github.com/ginkgo-project/ginkgo/blob/develop/INSTALL.md After installation, CMake can find the Ginkgo package using this command. An example is provided in the test_install directory. ```cmake find_package(Ginkgo) ``` -------------------------------- ### Build Ginkgo with Debug Configuration Source: https://github.com/ginkgo-project/ginkgo/blob/develop/INSTALL.md Example of building Ginkgo in debug mode with various development tools and parallelization backends enabled. Ensure the build directory is specified correctly. ```cmake cmake .. -BDebug -DCMAKE_BUILD_TYPE=Debug -DGINKGO_DEVEL_TOOLS=ON \ -DGINKGO_BUILD_TESTS=ON -DGINKGO_BUILD_REFERENCE=ON -DGINKGO_BUILD_OMP=ON \ -DGINKGO_BUILD_CUDA=ON -DGINKGO_BUILD_HIP=ON cmake --build Debug ``` -------------------------------- ### CMake Integration for Downstream Projects in CMake Source: https://context7.com/ginkgo-project/ginkgo/llms.txt Example CMakeLists.txt file for a downstream project that uses Ginkgo. It demonstrates how to find the installed Ginkgo package using `find_package` and link it to the executable. ```cmake # CMakeLists.txt of downstream project cmake_minimum_required(VERSION 3.16) project(MySolver LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) # Find installed Ginkgo find_package(Ginkgo REQUIRED) add_executable(my_solver main.cpp) ``` -------------------------------- ### Initialize Ginkgo Solvers and Data Source: https://context7.com/ginkgo-project/ginkgo/llms.txt Sets up the Ginkgo executor and loads matrices and vectors from files. Ensure 'data/A.mtx', 'data/b.mtx', and 'data/x0.mtx' exist. ```cpp #include #include using ValueType = double; using RealValueType = gko::remove_complex; using IndexType = int; using mtx = gko::matrix::Csr; using vec = gko::matrix::Dense; auto exec = gko::CudaExecutor::create(0, gko::OmpExecutor::create()); auto A = gko::share(gko::read(std::ifstream("data/A.mtx"), exec)); auto b = gko::read(std::ifstream("data/b.mtx"), exec); auto x = gko::read(std::ifstream("data/x0.mtx"), exec); ``` -------------------------------- ### Navigate and Create Build Directory Source: https://github.com/ginkgo-project/ginkgo/wiki/Tutorial-1:-Getting-Started Enter the cloned Ginkgo directory and create a build subdirectory for compilation. ```sh cd ginkgo mkdir build cd build ``` -------------------------------- ### Running Solver Benchmarks with Custom Preconditioners Source: https://github.com/ginkgo-project/ginkgo/blob/develop/BENCHMARKING.md Demonstrates how to run solver benchmarks using specific preconditioners by setting the PRECONDS environment variable. Multiple preconditioners can be specified. ```shell export PRECONDS=jacobi,ic,ilu ``` -------------------------------- ### Find Ginkgo Package Source: https://github.com/ginkgo-project/ginkgo/blob/develop/test/test_install/CMakeLists.txt Finds the installed Ginkgo package. Specify the installation directory using `CMAKE_PREFIX_PATH`. ```cmake find_package( Ginkgo REQUIRED PATHS # The Path where ginkgo was installed # Alternatively, use `cmake -DCMAKE_PREFIX_PATH=` to specify the install directory ) ``` -------------------------------- ### Add test_install Custom Target Source: https://github.com/ginkgo-project/ginkgo/blob/develop/CMakeLists.txt Configures a custom target for testing the installation process, including building and running installed binaries. ```cmake add_custom_target( test_install COMMAND ${CMAKE_COMMAND} -G${CMAKE_GENERATOR} ${TOOLSET} -S${GINKGO_TEST_INSTALL_SRC_DIR} -B${GINKGO_TEST_INSTALL_BIN_DIR} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_PREFIX_PATH=${CMAKE_INSTALL_PREFIX} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_CUDA_COMPILER=${CMAKE_CUDA_COMPILER} -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} # `--config cfg` is ignored by single-configuration generator. # `$` is always be the same as `CMAKE_BUILD_TYPE` in # single-configuration generator. COMMAND ${CMAKE_COMMAND} --build ${GINKGO_TEST_INSTALL_BIN_DIR} --config $ COMMAND ${GINKGO_TEST_INSTALL_CMD} COMMAND ${GINKGO_TEST_INSTALL_CUDA_CMD} COMMAND ${GINKGO_TEST_INSTALL_HIP_CMD} WORKING_DIRECTORY ${GINKGO_TEST_INSTALL_BIN_DIR} COMMENT "Running a test on the installed binaries. " "This requires running `(sudo) make install` first." ) ``` -------------------------------- ### Standard CMake Build Source: https://github.com/ginkgo-project/ginkgo/blob/develop/INSTALL.md Use this command to initiate the build process. Replace [OPTIONS] with desired CMake flags. For Visual Studio, use `--config ` to specify the build type (e.g., Debug, Release). ```sh mkdir build; cd build cake [OPTIONS] .. && cmake --build . ``` -------------------------------- ### CMakeLists.txt Configuration for PAPI Logging Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/papi-logging/CMakeLists.txt This CMakeLists.txt file sets up the build for the PAPI logging example. It finds the Ginkgo library, checks for PAPI support, and links the executable against Ginkgo and PAPI. It also copies necessary data files. ```cmake cmake_minimum_required(VERSION 3.16) project(papi-logging) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() if(NOT GINKGO_HAVE_PAPI_SDE) message(FATAL_ERROR "This example needs Ginkgo built with PAPI support") endif() add_executable(papi-logging papi-logging.cpp) target_link_libraries(papi-logging ginkgo PAPI::PAPI) # Copy the data files to the execution directory configure_file(data/A.mtx data/A.mtx COPYONLY) configure_file(data/b.mtx data/b.mtx COPYONLY) configure_file(data/x0.mtx data/x0.mtx COPYONLY) ``` -------------------------------- ### Kokkos Assembly Example Output Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/kokkos-assembly/doc/results.dox This snippet shows the typical output when running the Kokkos assembly example. The average relative error is hardware-dependent. ```text > ./kokkos-assembly Solve complete. The average relative error is 1.05488e-11 ``` -------------------------------- ### Querying Script Help Source: https://github.com/ginkgo-project/ginkgo/blob/develop/CONTRIBUTING.md Use this command to view the available options for the create_new_algorithm.sh script. ```bash ./create_new_algorithm.sh --help ``` -------------------------------- ### CMakeLists.txt for File Configuration Solver Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/file-config-solver/CMakeLists.txt This CMakeLists.txt file sets up the project, finds dependencies like Ginkgo and nlohmann_json, builds the executable, and copies data and configuration files. ```cmake cmake_minimum_required(VERSION 3.16) project(file-config-solver) ``` ```cmake # We only need to find Ginkgo/nlohmann_json if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) find_package(nlohmann_json 3.9.1 REQUIRED) endif() ``` ```cmake add_executable(file-config-solver file-config-solver.cpp) target_link_libraries( file-config-solver Ginkgo::ginkgo nlohmann_json::nlohmann_json ) ``` ```cmake # Copy the data files to the execution directory configure_file(data/A.mtx data/A.mtx COPYONLY) ``` ```cmake # Copy the config files to the execution directory file(GLOB config_list RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" config/*.json) foreach(config IN LISTS config_list) configure_file("${config}" "${config}" COPYONLY) endforeach() ``` -------------------------------- ### Create Dense Vectors and Initialize Source: https://github.com/ginkgo-project/ginkgo/wiki/Tutorial-2:-Implement:-Matrices Demonstrates creating dense vectors for the right-hand side and solution, initializing the solution vector with zeros, and using lambda functions for mathematical definitions. ```c++ #include int main(int argc, char *argv[]) { . . . using vec = gko::matrix::Dense; auto correct_u = [](double x) { return x * x * x; }; auto f = [](double x) { return 6 * x; }; auto u0 = correct_u(0); auto u1 = correct_u(1); auto rhs = vec::create(exec, gko::dim<2>(discretization_points, 1)); generate_rhs(f, u0, u1, lend(rhs)); auto u = vec::create(exec, gko::dim<2>(discretization_points, 1)); for (int i = 0; i < u->get_size()[0]; ++i) { u->get_values()[i] = 0.0; } } ``` -------------------------------- ### CMakeLists.txt Configuration for Multigrid Solver Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/multigrid-preconditioned-solver-customized/CMakeLists.txt This CMake script sets up the build environment for the multigrid preconditioned solver example. It finds the required Ginkgo library version and links it to the executable. It also configures the copying of necessary data files. ```cmake cmake_minimum_required(VERSION 3.16) project(multigrid-preconditioned-solver-customized) # We only need to find Ginkgo if we build this example stand-alone if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() add_executable( multigrid-preconditioned-solver-customized multigrid-preconditioned-solver-customized.cpp ) target_link_libraries(multigrid-preconditioned-solver-customized Ginkgo::ginkgo) # Copy the data files to the execution directory configure_file(data/A.mtx data/A.mtx COPYONLY) ``` -------------------------------- ### Conditional Test Install HIP Command Source: https://github.com/ginkgo-project/ginkgo/blob/develop/CMakeLists.txt Defines the test_install_hip command only if GINKGO_BUILD_HIP is enabled. ```cmake if(GINKGO_BUILD_HIP) set(GINKGO_TEST_INSTALL_HIP_CMD ${GINKGO_TEST_INSTALL_BIN_DIR}/${GINKGO_CONFIG_PREFIX}test_install_hip ) endif() ``` -------------------------------- ### Conditional Test Install CUDA Command Source: https://github.com/ginkgo-project/ginkgo/blob/develop/CMakeLists.txt Defines the test_install_cuda command only if GINKGO_BUILD_CUDA is enabled. ```cmake if(GINKGO_BUILD_CUDA) set(GINKGO_TEST_INSTALL_CUDA_CMD ${GINKGO_TEST_INSTALL_BIN_DIR}/${GINKGO_CONFIG_PREFIX}test_install_cuda ) endif() ``` -------------------------------- ### Find Ginkgo Library Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/simple-solver/CMakeLists.txt Finds the Ginkgo library if the example is not built as part of the Ginkgo build. Requires Ginkgo version 1.12.0. ```cmake if(NOT GINKGO_BUILD_EXAMPLES) find_package(Ginkgo 1.12.0 REQUIRED) endif() ``` -------------------------------- ### Print Configuration Summary Source: https://github.com/ginkgo-project/ginkgo/blob/develop/CMakeLists.txt Reads and prints a detailed or minimal build configuration summary to the console based on GINKGO_CONFIG_LOG_DETAILED. ```cmake if(GINKGO_CONFIG_LOG_DETAILED) file(READ ${PROJECT_BINARY_DIR}/detailed.log GINKGO_LOG_SUMMARY) else() file(READ ${PROJECT_BINARY_DIR}/minimal.log GINKGO_LOG_SUMMARY) endif() message(STATUS "${GINKGO_LOG_SUMMARY}") ``` -------------------------------- ### Build Distributed Solver Executable Source: https://github.com/ginkgo-project/ginkgo/blob/develop/examples/distributed-solver/CMakeLists.txt Defines the distributed-solver executable and links it against the Ginkgo library. Ensure Ginkgo is installed and discoverable by CMake. ```cmake add_executable(distributed-solver distributed-solver.cpp) target_link_libraries(distributed-solver Ginkgo::ginkgo) ``` -------------------------------- ### Configure ILUT Preconditioner with Threshold Dropping in C++ Source: https://context7.com/ginkgo-project/ginkgo/llms.txt Sets up an ILUT (Incomplete LU with threshold) factorization. Parameters like fill-in limit and factorization iterations can be specified to control sparsity. ```cpp // --- ILUT (ILU with threshold dropping) --- auto par_ilut = gko::share( gko::factorization::ParIlut::build() .with_fill_in_limit(2.0) // fill-in factor .with_factorization_iterations(10u) .on(exec) ->generate(A)); ``` -------------------------------- ### Configure Documentation Generation Options Source: https://github.com/ginkgo-project/ginkgo/blob/develop/doc/CMakeLists.txt Sets boolean options to control the generation of PDF, internal development, and example documentation. Defaults are provided. ```cmake option(GINKGO_DOC_GENERATE_PDF "Generate PDF documentation" OFF) option(GINKGO_DOC_GENERATE_DEV "Generate internal documentation" OFF) option(GINKGO_DOC_GENERATE_EXAMPLES "Generate example documentation" ON) ```