From 56764af24f772eb4ee5b1e2b9c65e6eb1b2fc421 Mon Sep 17 00:00:00 2001 From: "Yuhsiang M. Tsai" Date: Mon, 7 Nov 2022 01:41:28 +0800 Subject: [PATCH] clear up function, update example Co-authored-by: Pratik Nayak Co-authored-by: Terry Cojean --- core/solver/multigrid.cpp | 28 +++++++++---------- cuda/CMakeLists.txt | 1 + .../mixed-multigrid-solver.cpp | 24 +++++++++------- include/ginkgo/core/solver/multigrid.hpp | 2 -- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/core/solver/multigrid.cpp b/core/solver/multigrid.cpp index 9d5c6040ca1..91b56ea62b8 100644 --- a/core/solver/multigrid.cpp +++ b/core/solver/multigrid.cpp @@ -160,6 +160,14 @@ void handle_list( } +template +void clear_and_reserve(Vec& vec, size_type` size) +{ + vec.clear(); + vec.reserve(size); +} + + } // namespace @@ -178,20 +186,12 @@ void MultigridState::generate(const LinOp* system_matrix_in, auto mg_level_list = multigrid->get_mg_level_list(); auto list_size = mg_level_list.size(); auto cycle = multigrid->get_cycle(); - r_list.clear(); - g_list.clear(); - e_list.clear(); - b_list.clear(); - x_list.clear(); - next_one_list.clear(); - one_list.clear(); - neg_one_list.clear(); - r_list.reserve(list_size); - g_list.reserve(list_size); - e_list.reserve(list_size); - one_list.reserve(list_size); - next_one_list.reserve(list_size); - neg_one_list.reserve(list_size); + clear_and_reserve(r_list, list_size); + clear_and_reserve(g_list, list_size); + clear_and_reserve(e_list, list_size); + clear_and_reserve(one_list, list_size); + clear_and_reserve(next_one_list, list_size); + clear_and_reserve(neg_one_list, list_size); // Allocate memory first such that reusing allocation in each iter. for (int i = 0; i < mg_level_list.size(); i++) { auto next_nrows = mg_level_list.at(i)->get_coarse_op()->get_size()[0]; diff --git a/cuda/CMakeLists.txt b/cuda/CMakeLists.txt index 2bf5ffa1104..46946b3b696 100644 --- a/cuda/CMakeLists.txt +++ b/cuda/CMakeLists.txt @@ -110,6 +110,7 @@ target_include_directories(ginkgo_cuda target_include_directories(ginkgo_cuda PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/..) target_link_libraries(ginkgo_cuda PRIVATE ${CUDA_RUNTIME_LIBS} ${CUBLAS} ${CUSPARSE} ${CURAND} ${CUFFT}) +target_link_libraries(ginkgo_cuda PUBLIC ginkgo_device) target_compile_options(ginkgo_cuda PRIVATE "$<$:${GINKGO_CUDA_ARCH_FLAGS}>") # we handle CUDA architecture flags for now, disable CMake handling diff --git a/examples/mixed-multigrid-solver/mixed-multigrid-solver.cpp b/examples/mixed-multigrid-solver/mixed-multigrid-solver.cpp index 47303859e85..3b5f67f950e 100644 --- a/examples/mixed-multigrid-solver/mixed-multigrid-solver.cpp +++ b/examples/mixed-multigrid-solver/mixed-multigrid-solver.cpp @@ -45,19 +45,19 @@ int main(int argc, char* argv[]) { // Some shortcuts using ValueType = double; - using ValueType2 = float; + using MixedType = float; using IndexType = int; using vec = gko::matrix::Dense; using mtx = gko::matrix::Csr; using fcg = gko::solver::Fcg; - using cg = gko::solver::Cg; + using cg = gko::solver::Cg; using ir = gko::solver::Ir; - using ir2 = gko::solver::Ir; + using ir2 = gko::solver::Ir; using mg = gko::solver::Multigrid; using bj = gko::preconditioner::Jacobi; - using bj2 = gko::preconditioner::Jacobi; + using bj2 = gko::preconditioner::Jacobi; using pgm = gko::multigrid::Pgm; - using pgm2 = gko::multigrid::Pgm; + using pgm2 = gko::multigrid::Pgm; // Print version information std::cout << gko::version_info::get() << std::endl; @@ -87,7 +87,7 @@ int main(int argc, char* argv[]) // executor where Ginkgo will perform the computation const auto exec = exec_map.at(executor_string)(); // throws if not valid const bool use_mixed = argc >= 5; - std::cout << "Use Mixed: " << use_mixed << std::endl; + std::cout << "Using mixed precision? " << use_mixed << std::endl; // Read data auto A = share(gko::read(std::ifstream(argv[2]), exec)); // Create RHS as 1 and initial guess as 0 @@ -140,7 +140,7 @@ int main(int argc, char* argv[]) auto smoother_gen2 = gko::share( ir2::build() .with_solver(bj2::build().with_max_block_size(1u).on(exec)) - .with_relaxation_factor(static_cast(0.9)) + .with_relaxation_factor(static_cast(0.9)) .with_criteria( gko::stop::Iteration::build().with_max_iters(1u).on(exec)) .on(exec)); @@ -160,14 +160,13 @@ int main(int argc, char* argv[]) auto coarsest_solver_gen2 = gko::share( ir2::build() .with_solver(bj2::build().with_max_block_size(1u).on(exec)) - .with_relaxation_factor(static_cast(0.9)) + .with_relaxation_factor(static_cast(0.9)) .with_criteria( gko::stop::Iteration::build().with_max_iters(4u).on(exec)) .on(exec)); // Create multigrid factory std::shared_ptr multigrid_gen; if (use_mixed) { - std::cout << "USE" << std::endl; multigrid_gen = mg::build() .with_max_levels(10u) @@ -177,7 +176,12 @@ int main(int argc, char* argv[]) .with_mg_level(mg_level_gen, mg_level_gen2) .with_level_selector([](const gko::size_type level, const gko::LinOp*) -> gko::size_type { - std::cout << "level " << level << std::endl; + // The first (index 0) level will use the first + // mg_level_gen, smoother_gen which are the factories with + // ValueType. The rest of levels (>= 1) will use the second + // (index 1) mg_level_gen2 and smoother_gen2 which use the + // MixedType. The rest of levels will use different type + // than the normal multigrid. return level >= 1 ? 1 : 0; }) .with_coarsest_solver(coarsest_solver_gen2) diff --git a/include/ginkgo/core/solver/multigrid.hpp b/include/ginkgo/core/solver/multigrid.hpp index 7b77326e661..a20e4022e48 100644 --- a/include/ginkgo/core/solver/multigrid.hpp +++ b/include/ginkgo/core/solver/multigrid.hpp @@ -162,8 +162,6 @@ struct MultigridState { // current level's nrows x nrhs std::vector> r_list; - std::vector> x_list; - std::vector> b_list; // next level's nrows x nrhs std::vector> g_list; std::vector> e_list;