From c402f6ff0d83c8c7087f0c73a2c7e6c4bed18d1a Mon Sep 17 00:00:00 2001 From: Gregor Olenik Date: Tue, 12 Mar 2024 11:32:29 +0100 Subject: [PATCH 01/90] treat local_mtx and non_local_mtx as single interface mtx --- DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H b/DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H index 75f838306..f3545a9c8 100644 --- a/DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H +++ b/DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H @@ -178,16 +178,17 @@ struct MatrixInitFunctor { auto exec = exec_.get_ref_exec(); auto num_rows = partition_.get_local_size(); - gko::device_matrix_data A_data( + std::vector A_data_v; + A_data_v.emplace_back( exec, gko::dim<2>(num_rows, num_rows), *rows.get(), *cols.get(), *coeffs.get()); - gko::device_matrix_data non_local_A_data( + std::vector non_local_A_data_v; + non_local_A_data.emplace_back( exec, gko::dim<2>(num_rows, non_local_cols->get_size()), *non_local_rows.get(), *non_local_cols.get(), *non_local_coeffs.get()); - auto comm = exec_.get_gko_mpi_host_comm(); auto dist_A = generate_dist_mtx_with_inner_type( exec_.get_ref_exec(), exec_.get_gko_mpi_host_comm()); @@ -201,7 +202,8 @@ struct MatrixInitFunctor { auto send_sizes = sparse_comm->get_send_sizes(); auto send_offs = sparse_comm->get_send_offsets(); - dist_A->read_distributed(A_data, non_local_A_data, sparse_comm); + // TODO this needs a std::vector version + dist_A->read_distributed(A_data_v, non_local_A_data_v, sparse_comm); auto device_mat = generate_dist_mtx_with_inner_type( exec_.get_device_exec(), exec_.get_gko_mpi_device_comm()); From 8abaa113cf80a1389308adf902d2c04d58d2bf97 Mon Sep 17 00:00:00 2001 From: Gregor Olenik Date: Tue, 12 Mar 2024 11:45:40 +0100 Subject: [PATCH 02/90] update remode branch --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 895020167..90ba391e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,7 +49,7 @@ if((NOT OGL_ALLOW_REFERENCE_ONLY) AND (NOT OGL_USE_EXTERNAL_GINKGO)) endif() set(GINKGO_CHECKOUT_VERSION - "fc86d48b78cebd2b2c5833a2dcf0fe40f615cf19" + "sparse-communicator-cmake-modernization-combination-interface-ogl" CACHE STRING "Use specific version of ginkgo") include(CheckIncludeFileCXX) From 2488c0c1ba427866d4408ccc72b9d72670747a34 Mon Sep 17 00:00:00 2001 From: Gregor Olenik Date: Fri, 8 Mar 2024 10:55:39 +0100 Subject: [PATCH 03/90] move to FetchContent Build of OF --- CMakeLists.txt | 12 -------- third_party/ginkgo/CMakeLists.txt | 51 +++++++++++++++++++------------ 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 90ba391e6..f86fabdd8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,18 +63,6 @@ ginkgo_find_package(Ginkgo "Ginkgo::ginkgo" FALSE 1.7.0) add_subdirectory(third_party) -# If ginkgo is not found at this point, look for the third_party installation -if(NOT ${OGL_USE_EXTERNAL_GINKGO}) - find_package( - Ginkgo - CONFIG - 1.7.0 - REQUIRED - PATHS - $ENV{FOAM_USER_LIBBIN}/cmake/Ginkgo - ${CMAKE_CURRENT_BINARY_DIR}/third_party/ginkgo/build/install/lib/cmake) -endif() - # C++ 14 standard or later is required to interface with Ginkgo OGL itself uses # some c++ 17 features diff --git a/third_party/ginkgo/CMakeLists.txt b/third_party/ginkgo/CMakeLists.txt index 758716bd3..db1c9670a 100644 --- a/third_party/ginkgo/CMakeLists.txt +++ b/third_party/ginkgo/CMakeLists.txt @@ -3,22 +3,35 @@ # SPDX-License-Identifier: GPL-3.0-or-later # TODO pass flags from main CMAKE file -ginkgo_load_git_package( - ginkgo_external - "https://github.com/ginkgo-project/ginkgo.git" - ${GINKGO_CHECKOUT_VERSION} - "-DGINKGO_BUILD_CUDA=${GINKGO_BUILD_CUDA}" - "-DGINKGO_BUILD_HIP=${GINKGO_BUILD_HIP}" - "-DGINKGO_BUILD_OMP=${GINKGO_BUILD_OMP}" - "-DGINKGO_BUILD_MPI=ON" - "-DGINKGO_BUILD_TESTS=OFF" - "-DGINKGO_BUILD_EXAMPLES=OFF" - "-DGINKGO_BUILD_BENCHMARKS=OFF" - "-DGINKGO_BUILD_REFERENCE=${GINKGO_BUILD_REFERENCE}" - "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}" - "-DCMAKE_CUDA_ARCHITECTURES=${OGL_CUDA_ARCHITECTURES}" - "-DGINKGO_BUILD_HWLOC=OFF" - "-DGINKGO_FORCE_GPU_AWARE_MPI=${GINKGO_FORCE_GPU_AWARE_MPI}" - "-DCMAKE_INSTALL_PREFIX=$ENV{FOAM_USER_LIBBIN}/.." - "-DBUILD_SHARED_LIBS=ON" - "-DCMAKE_CXX_STANDARD=17") +include(FetchContent) + +if(NOT ${OGL_USE_EXTERNAL_GINKGO}) + FetchContent_Declare( + Ginkgo + QUITE + GIT_SHALLOW ON + GIT_REPOSITORY "https://github.com/ginkgo-project/ginkgo.git" + GIT_TAG ${GINKGO_CHECKOUT_VERSION} + ) + FetchContent_MakeAvailable(Ginkgo) +endif() + +#ginkgo_load_git_package( +# ginkgo_external +# "https://github.com/ginkgo-project/ginkgo.git" +# ${GINKGO_CHECKOUT_VERSION} +# "-DGINKGO_BUILD_CUDA=${GINKGO_BUILD_CUDA}" +# "-DGINKGO_BUILD_HIP=${GINKGO_BUILD_HIP}" +# "-DGINKGO_BUILD_OMP=${GINKGO_BUILD_OMP}" +# "-DGINKGO_BUILD_MPI=ON" +# "-DGINKGO_BUILD_TESTS=OFF" +# "-DGINKGO_BUILD_EXAMPLES=OFF" +# "-DGINKGO_BUILD_BENCHMARKS=OFF" +# "-DGINKGO_BUILD_REFERENCE=${GINKGO_BUILD_REFERENCE}" +# "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}" +# "-DCMAKE_CUDA_ARCHITECTURES=${OGL_CUDA_ARCHITECTURES}" +# "-DGINKGO_BUILD_HWLOC=OFF" +# "-DGINKGO_FORCE_GPU_AWARE_MPI=${GINKGO_FORCE_GPU_AWARE_MPI}" +# "-DCMAKE_INSTALL_PREFIX=$ENV{FOAM_USER_LIBBIN}/.." +# "-DBUILD_SHARED_LIBS=ON" +# "-DCMAKE_CXX_STANDARD=17") From a489a112d2218ebb9131d4d60c6fa00706ab2bac Mon Sep 17 00:00:00 2001 From: Gregor Olenik Date: Fri, 8 Mar 2024 22:04:59 +0100 Subject: [PATCH 04/90] add a version that compiles with recent cmake --- CMakeLists.txt | 40 +++++++++++++++++++------------ CMakePresets.json | 3 ++- third_party/ginkgo/CMakeLists.txt | 37 ++++++++-------------------- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f86fabdd8..2d17e2862 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,8 +14,11 @@ if(NOT DEFINED ENV{FOAM_SRC}) message(FATAL_ERROR "You must source OpenFOAM before building OGL") endif() -# Configuration options +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) +# Configuration options include(cmake/build_type_helpers.cmake) list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/Modules/") @@ -26,17 +29,23 @@ option(OGL_USE_FOAM_FOUNDATION_VERSION "Build OGL for the OpenFOAM Foundation version" FALSE) option(OGL_BUILD_UNITTEST "Build OGL unittests" FALSE) option(OGL_CUDA_ARCHITECTURES "Which cuda architecture to build for" "native") -option(GINKGO_BUILD_CUDA "Build Ginkgo with cuda backend" FALSE) -option(GINKGO_BUILD_HIP "Build Ginkgo with hip backend" FALSE) -option(GINKGO_BUILD_SYCL "Build Ginkgo with sycl backend" FALSE) -option(GINKGO_BUILD_OMP "Build Ginkgo with omp backend" FALSE) option(GINKGO_BUILD_REFERENCE "Build Ginkgo with reference backend" TRUE) option(OGL_ALLOW_REFERENCE_ONLY "Enable builds of Ginkgo with reference backend only" FALSE) -option(GINKGO_FORCE_GPU_AWARE_MPI "Build Ginkgo using device aware MPI" TRUE) option(GINKGO_WITH_OGL_EXTENSIONS "Whether ginkgo was build with OGL extension" FALSE) +option(GINKGO_FORCE_GPU_AWARE_MPI "Build Ginkgo using device aware MPI" TRUE) +option(GINKGO_BUILD_CUDA "Build Ginkgo with cuda backend" FALSE) +option(GINKGO_BUILD_HIP "Build Ginkgo with hip backend" FALSE) +option(GINKGO_BUILD_SYCL "Build Ginkgo with sycl backend" FALSE) +option(GINKGO_BUILD_OMP "Build Ginkgo with omp backend" FALSE) + +set(GINKGO_BUILD_MPI ON CACHE INTERNAL "") +set(GINKGO_BUILD_TESTS OFF CACHE INTERNAL "") +set(GINKGO_BUILD_EXAMPLES OFF CACHE INTERNAL "") +set(GINKGO_BUILD_BENCHMARKS OFF CACHE INTERNAL "") + if((NOT OGL_ALLOW_REFERENCE_ONLY) AND (NOT OGL_USE_EXTERNAL_GINKGO)) if((NOT GINKGO_BUILD_CUDA) AND (NOT GINKGO_BUILD_HIP) @@ -63,12 +72,6 @@ ginkgo_find_package(Ginkgo "Ginkgo::ginkgo" FALSE 1.7.0) add_subdirectory(third_party) -# C++ 14 standard or later is required to interface with Ginkgo OGL itself uses -# some c++ 17 features - -set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -O0 -ggdb") set(CMAKE_CXX_FLAGS_RELEASE @@ -191,8 +194,7 @@ target_include_directories( target_link_libraries( OGL PRIVATE $ENV{FOAM_LIBBIN}/libOpenFOAM.so $ENV{FOAM_LIBBIN}/libfiniteVolume.so - $ENV{FOAM_LIBBIN}/$ENV{FOAM_MPI}/libPstream.so - PUBLIC Ginkgo::ginkgo stdc++fs) + $ENV{FOAM_LIBBIN}/$ENV{FOAM_MPI}/libPstream.so Ginkgo::ginkgo stdc++fs) if(${GINKGO_WITH_OGL_EXTENSIONS}) target_compile_definitions(OGL PRIVATE GINKGO_WITH_OGL_EXTENSIONS=1) @@ -224,4 +226,12 @@ add_custom_target( COMMAND find . -name CMakeLists.txt -exec cmake-format -i {} + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}) -install(TARGETS OGL DESTINATION $ENV{FOAM_USER_LIBBIN}) +install(TARGETS OGL + ginkgo + ginkgo_device + ginkgo_hip + ginkgo_cuda + ginkgo_omp + ginkgo_dpcpp + ginkgo_reference + DESTINATION $ENV{FOAM_USER_LIBBIN}) diff --git a/CMakePresets.json b/CMakePresets.json index 890dd7613..b62aed332 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -13,7 +13,8 @@ "generator": "Ninja", "binaryDir": "${sourceDir}/build/Release", "cacheVariables": { - "CMAKE_BUILD_TYPE": "Release" + "CMAKE_BUILD_TYPE": "Release", + "CMAKE_INSTALL_PREFIX": "$env{FOAM_USER_LIBBIN}/.." } }, { diff --git a/third_party/ginkgo/CMakeLists.txt b/third_party/ginkgo/CMakeLists.txt index db1c9670a..ca0facdfd 100644 --- a/third_party/ginkgo/CMakeLists.txt +++ b/third_party/ginkgo/CMakeLists.txt @@ -6,32 +6,15 @@ include(FetchContent) if(NOT ${OGL_USE_EXTERNAL_GINKGO}) - FetchContent_Declare( - Ginkgo - QUITE - GIT_SHALLOW ON - GIT_REPOSITORY "https://github.com/ginkgo-project/ginkgo.git" - GIT_TAG ${GINKGO_CHECKOUT_VERSION} - ) - FetchContent_MakeAvailable(Ginkgo) + FetchContent_Declare( + Ginkgo + QUITE + GIT_SHALLOW ON + GIT_REPOSITORY "https://github.com/ginkgo-project/ginkgo.git" + GIT_TAG ${GINKGO_CHECKOUT_VERSION} + ) + + FetchContent_MakeAvailable(Ginkgo) + endif() -#ginkgo_load_git_package( -# ginkgo_external -# "https://github.com/ginkgo-project/ginkgo.git" -# ${GINKGO_CHECKOUT_VERSION} -# "-DGINKGO_BUILD_CUDA=${GINKGO_BUILD_CUDA}" -# "-DGINKGO_BUILD_HIP=${GINKGO_BUILD_HIP}" -# "-DGINKGO_BUILD_OMP=${GINKGO_BUILD_OMP}" -# "-DGINKGO_BUILD_MPI=ON" -# "-DGINKGO_BUILD_TESTS=OFF" -# "-DGINKGO_BUILD_EXAMPLES=OFF" -# "-DGINKGO_BUILD_BENCHMARKS=OFF" -# "-DGINKGO_BUILD_REFERENCE=${GINKGO_BUILD_REFERENCE}" -# "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}" -# "-DCMAKE_CUDA_ARCHITECTURES=${OGL_CUDA_ARCHITECTURES}" -# "-DGINKGO_BUILD_HWLOC=OFF" -# "-DGINKGO_FORCE_GPU_AWARE_MPI=${GINKGO_FORCE_GPU_AWARE_MPI}" -# "-DCMAKE_INSTALL_PREFIX=$ENV{FOAM_USER_LIBBIN}/.." -# "-DBUILD_SHARED_LIBS=ON" -# "-DCMAKE_CXX_STANDARD=17") From f6f71819e67aae2c6924bd07d284d3e271a62ff9 Mon Sep 17 00:00:00 2001 From: Gregor Olenik Date: Wed, 13 Mar 2024 08:32:46 +0100 Subject: [PATCH 05/90] add in matrix, and create dist_mtx with Combination --- DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H b/DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H index f3545a9c8..61fb72d62 100644 --- a/DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H +++ b/DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H @@ -139,6 +139,9 @@ struct MatrixInitFunctor { std::shared_ptr exec, std::shared_ptr comm) const { + return dist_mtx::create(exec, *comm.get(), + gko::with_matrix_type()); + if (matrix_format_ == "Csr") { return dist_mtx::create(exec, *comm.get(), gko::with_matrix_type()); @@ -203,7 +206,8 @@ struct MatrixInitFunctor { auto send_offs = sparse_comm->get_send_offsets(); // TODO this needs a std::vector version - dist_A->read_distributed(A_data_v, non_local_A_data_v, sparse_comm); + dist_A->read_distributed<>(A_data_v, non_local_A_data_v, sparse_comm, + gko::matrix::Coo::create()); auto device_mat = generate_dist_mtx_with_inner_type( exec_.get_device_exec(), exec_.get_gko_mpi_device_comm()); From ba17e2f2403d437245df0d274cbd426a4e2db287 Mon Sep 17 00:00:00 2001 From: Gregor Olenik Date: Wed, 13 Mar 2024 13:20:52 +0100 Subject: [PATCH 06/90] add basic custom matrix implementation --- CMakeLists.txt | 7 ++-- .../CsrMatrixWrapper/CsrMatrixWrapper.H | 41 ++++++++++--------- DevicePersistent/Partition/Partition.H | 2 +- .../LduMatrix}/GKOACG/GKOACG.C | 0 .../LduMatrix}/GKOACG/GKOACG.H | 0 lduLduBase/lduLduBase.H | 2 +- unitTests/test_HostMatrix.C | 3 +- 7 files changed, 29 insertions(+), 26 deletions(-) rename {LduMatrix => MatrixWrapper/LduMatrix}/GKOACG/GKOACG.C (100%) rename {LduMatrix => MatrixWrapper/LduMatrix}/GKOACG/GKOACG.H (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d17e2862..f213ab068 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,8 +156,8 @@ target_sources( Preconditioner/Preconditioner.C BaseWrapper/lduBase/GKOlduBase.C BaseWrapper/CoupledLduBase/GKOCoupledLduBase.C - HostMatrix/HostMatrix.C - HostMatrix/HostMatrixFreeFunctions.C + MatrixWrapper/HostMatrix/HostMatrix.C + MatrixWrapper/HostMatrix/HostMatrixFreeFunctions.C Solver/CG/GKOCG.C Solver/BiCGStab/GKOBiCGStab.C # Solver / IR / GKOIR.C Solver / Multigrid / GKOMultigrid.C @@ -166,7 +166,8 @@ target_sources( PUBLIC common/common.H StoppingCriterion/StoppingCriterion.H lduLduBase/lduLduBase.H - HostMatrix/HostMatrix.H + MatrixWrapper/HostMatrix/HostMatrix.H + MatrixWrapper/GkoCombinationMatrix/GkoCombinationMatrix.H DevicePersistent/Base/Base.H DevicePersistent/Partition/Partition.H DevicePersistent/Array/Array.H diff --git a/DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H b/DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H index 61fb72d62..c09a609b8 100644 --- a/DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H +++ b/DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H @@ -9,6 +9,8 @@ #include "DevicePersistent/ExecutorHandler/ExecutorHandler.H" #include "DevicePersistent/Partition/Partition.H" +#include "MatrixWrapper/GkoCombinationMatrix/GkoCombinatinoMatrix.H" + #include "fvCFD.H" namespace Foam { @@ -139,25 +141,28 @@ struct MatrixInitFunctor { std::shared_ptr exec, std::shared_ptr comm) const { - return dist_mtx::create(exec, *comm.get(), - gko::with_matrix_type()); - if (matrix_format_ == "Csr") { - return dist_mtx::create(exec, *comm.get(), - gko::with_matrix_type()); + return dist_mtx::create( + exec, *comm.get(), + gko::with_matrix_type< + CombinationMatrix>()); } if (matrix_format_ == "Ell") { - return dist_mtx::create(exec, *comm.get(), - gko::with_matrix_type()); + return dist_mtx::create( + exec, *comm.get(), + gko::with_matrix_type < + CombinationMatrix()); + } + if (matrix_format_ == "Coo") { + return dist_mtx::create( + exec, *comm.get(), + gko::with_matrix_type < + CombinationMatrix()); } // if (matrix_format_ == "Hybrid") { // return dist_mtx::create( // *comm.get(), gko::with_matrix_type()); // } - if (matrix_format_ == "Coo") { - return dist_mtx::create(exec, *comm.get(), - gko::with_matrix_type()); - } FatalErrorInFunction << "Matrix format " << matrix_format_ << " not supported " << abort(FatalError); @@ -181,16 +186,13 @@ struct MatrixInitFunctor { auto exec = exec_.get_ref_exec(); auto num_rows = partition_.get_local_size(); - std::vector A_data_v; - A_data_v.emplace_back( - exec, gko::dim<2>(num_rows, num_rows), *rows.get(), *cols.get(), - *coeffs.get()); + device_matrix_data A_data{exec, gko::dim<2>(num_rows, num_rows), + *rows.get(), *cols.get(), *coeffs.get()}; - std::vector non_local_A_data_v; - non_local_A_data.emplace_back( + device_matrix_data non_local_A_data{ exec, gko::dim<2>(num_rows, non_local_cols->get_size()), *non_local_rows.get(), *non_local_cols.get(), - *non_local_coeffs.get()); + *non_local_coeffs.get()}; auto comm = exec_.get_gko_mpi_host_comm(); auto dist_A = generate_dist_mtx_with_inner_type( @@ -206,8 +208,7 @@ struct MatrixInitFunctor { auto send_offs = sparse_comm->get_send_offsets(); // TODO this needs a std::vector version - dist_A->read_distributed<>(A_data_v, non_local_A_data_v, sparse_comm, - gko::matrix::Coo::create()); + dist_A->read_distributed(local_A_data, non_local_A_data, sparse_comm); auto device_mat = generate_dist_mtx_with_inner_type( exec_.get_device_exec(), exec_.get_gko_mpi_device_comm()); diff --git a/DevicePersistent/Partition/Partition.H b/DevicePersistent/Partition/Partition.H index 52ca51d77..7d57da673 100644 --- a/DevicePersistent/Partition/Partition.H +++ b/DevicePersistent/Partition/Partition.H @@ -10,7 +10,7 @@ #include "DevicePersistent/Base/Base.H" #include "DevicePersistent/ExecutorHandler/ExecutorHandler.H" -#include "HostMatrix/HostMatrix.H" +#include "MatrixWrapper/HostMatrix/HostMatrix.H" #include "common/common.H" namespace Foam { diff --git a/LduMatrix/GKOACG/GKOACG.C b/MatrixWrapper/LduMatrix/GKOACG/GKOACG.C similarity index 100% rename from LduMatrix/GKOACG/GKOACG.C rename to MatrixWrapper/LduMatrix/GKOACG/GKOACG.C diff --git a/LduMatrix/GKOACG/GKOACG.H b/MatrixWrapper/LduMatrix/GKOACG/GKOACG.H similarity index 100% rename from LduMatrix/GKOACG/GKOACG.H rename to MatrixWrapper/LduMatrix/GKOACG/GKOACG.H diff --git a/lduLduBase/lduLduBase.H b/lduLduBase/lduLduBase.H index cf77709fc..2a2b9a3a2 100644 --- a/lduLduBase/lduLduBase.H +++ b/lduLduBase/lduLduBase.H @@ -6,7 +6,7 @@ #include "DevicePersistent/CsrMatrixWrapper/CsrMatrixWrapper.H" #include "DevicePersistent/Vector/Vector.H" -#include "HostMatrix/HostMatrix.H" +#include "MatrixWrapper/HostMatrix/HostMatrix.H" #include "Preconditioner/Preconditioner.H" #include "common/common.H" diff --git a/unitTests/test_HostMatrix.C b/unitTests/test_HostMatrix.C index 8b4dcd548..99438c2f5 100644 --- a/unitTests/test_HostMatrix.C +++ b/unitTests/test_HostMatrix.C @@ -2,7 +2,8 @@ // // SPDX-License-Identifier: GPL-3.0-or-later -#include "HostMatrix/HostMatrix.H" +#include "MatrixWrapper/HostMatrix/HostMatrix.H" + #include "gtest/gtest.h" TEST(HostMatrixConversion, symmetric_update) From 6a9f12320c40281de06c38725d4ef9756ee95026 Mon Sep 17 00:00:00 2001 From: Gregor Olenik Date: Wed, 13 Mar 2024 13:22:54 +0100 Subject: [PATCH 07/90] add missing MatrixWrapper files --- .../GkoCombinationMatrix.H | 78 ++ MatrixWrapper/HostMatrix/HostMatrix.C | 764 ++++++++++++++++++ MatrixWrapper/HostMatrix/HostMatrix.H | 465 +++++++++++ .../HostMatrix/HostMatrixFreeFunctions.C | 199 +++++ 4 files changed, 1506 insertions(+) create mode 100644 MatrixWrapper/GkoCombinationMatrix/GkoCombinationMatrix.H create mode 100644 MatrixWrapper/HostMatrix/HostMatrix.C create mode 100644 MatrixWrapper/HostMatrix/HostMatrix.H create mode 100644 MatrixWrapper/HostMatrix/HostMatrixFreeFunctions.C diff --git a/MatrixWrapper/GkoCombinationMatrix/GkoCombinationMatrix.H b/MatrixWrapper/GkoCombinationMatrix/GkoCombinationMatrix.H new file mode 100644 index 000000000..53facf2f9 --- /dev/null +++ b/MatrixWrapper/GkoCombinationMatrix/GkoCombinationMatrix.H @@ -0,0 +1,78 @@ +#include + + +// The CombinationMatrix class is a wrapper around Ginkgos combination linear +// operator. It provides an interface for creating linear combinations of a +// giveen inner matrix type and a read implementation +template +class CombinationMatrix + : public gko::EnableLinOp>, + public gko::EnableCreateMethod>, + public gko::ReadableFromMatrixData { +public: + // This constructor will be called by the create method. + // Here we initialize, the interface_ranges to create the + // the linear combination during the read call + CombinationMatrix(std::shared_ptr exec, + std::vector interface_ranges) + : gko::EnableLinOp(exec, gko::dim<2>{size}), + interface_ranges_(interface_ranges), + comb_(gko::share(gko::Combination::create(exec, size))) + {} + +protected: + using vec = gko::matrix::Dense; + using coef_type = gko::array; + + void read(const device_matrix_data &data) override + { + auto exec = this->get_executor(); + for (auto &interface : interface_ranges_) { + auto op gko::share(InnerMatrixType::create(this->exec)) + + auto sub_array_view = [interface, exec](auto array_data) { + make_const_array_view(exec, interface.length(), + array_data + interface.begin()) + }; + + auto row_idxs = sub_array_view(data.get_const_row_idxs()); + auto col_idxs = sub_array_view(data.get_const_col_idxs()); + auto vals = sub_array_view(data.get_const_values()); + + device_matrix_data in(exec, this->get_size(), row_idxs, col_idxs, + vals); + + op->read(std::move(interface_view)) + + combination->add_operators( + gko::initialize>({1}, exec), + in); + } + } + + // Here we implement the application of the linear operator, x = A * b. + // apply_impl will be called by the apply method, after the arguments have + // been moved to the correct executor and the operators checked for + // conforming sizes. + // + // For simplicity, we assume that there is always only one right hand side + // and the stride of consecutive elements in the vectors is 1 (both of these + // are always true in this example). + void apply_impl(const gko::LinOp *b, gko::LinOp *x) const override { + this->comb_->apply_impl(b,x); + } + + // There is also a version of the apply function which does the operation + // x = alpha * A * b + beta * x. This function is commonly used and can + // often be better optimized than implementing it using x = A * b. However, + // for simplicity, we will implement it exactly like that in this example. + void apply_impl(const gko::LinOp *alpha, const gko::LinOp *b, + const gko::LinOp *beta, gko::LinOp *x) const override + { + this->comb_->apply_impl(alpha,b, beta, x); + } + +private: + std::vector interface_ranges_; + std::shared_ptr comb_; +}; diff --git a/MatrixWrapper/HostMatrix/HostMatrix.C b/MatrixWrapper/HostMatrix/HostMatrix.C new file mode 100644 index 000000000..383d1739d --- /dev/null +++ b/MatrixWrapper/HostMatrix/HostMatrix.C @@ -0,0 +1,764 @@ +/*---------------------------------------------------------------------------*\ +License + This file is part of OGL. + + OGL is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OGL is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OGL. If not, see . + + +Author: Gregor Olenik + +SourceFiles + HostMatrix.H + +\*---------------------------------------------------------------------------*/ + +#include "HostMatrix.H" + +#include "cyclicFvPatchField.H" +#include "lduMatrix.H" +#include "processorFvPatch.H" + +#include + +namespace Foam { + +template +HostMatrixWrapper::HostMatrixWrapper( + const objectRegistry &db, const MatrixType &matrix, + // coeffs for cells on boundaries + const FieldField &interfaceBouCoeffs, + // coeffs for internal cells + const FieldField &interfaceIntCoeffs, + // pointers to interfaces can be used to access concrete + // functions such as transferring indices, patch neighbours etc + const lduInterfaceFieldPtrsList &interfaces, + const dictionary &solverControls, const word &fieldName) + : MatrixType::solver(fieldName, matrix, interfaceBouCoeffs, + interfaceIntCoeffs, interfaces, solverControls), + exec_{db, solverControls, fieldName}, + device_id_guard_{db, fieldName, exec_.get_device_exec()}, + verbose_(solverControls.lookupOrDefault