Skip to content

Commit

Permalink
Merge #1662 Add file config example
Browse files Browse the repository at this point in the history
This PR adds file config example which configures solvers entirely from the config.
Moreover, the configs are mapped to the existing examples.

Related PR: #1662
  • Loading branch information
yhmtsai authored Oct 23, 2024
2 parents a521e1d + 05742fe commit 272b4ff
Show file tree
Hide file tree
Showing 49 changed files with 588 additions and 32 deletions.
9 changes: 8 additions & 1 deletion doc/examples/examples.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,14 @@
* @ref mixed_multigrid_solver
* </td>
* </tr>

*
* <tr valign="top">
* <td> Configure a solver from a config file
* </td>
* <td>@ref file_config_solver
* </td>
* </tr>
*
* <tr valign="top">
* <td> Distributed
* </td>
Expand Down
16 changes: 16 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set(EXAMPLES_EXEC_LIST
set(EXAMPLES_LIST
${EXAMPLES_EXEC_LIST}
custom-stopping-criterion
file-config-solver
ginkgo-overhead
minimal-cuda-solver
mixed-spmv
Expand Down Expand Up @@ -102,4 +103,19 @@ if(GINKGO_BUILD_TESTS)
"${CMAKE_CURRENT_SOURCE_DIR}/${example}")
endforeach()
endforeach()

file(GLOB config_list RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" file-config-solver/config/*.json)
foreach(config IN LISTS config_list)
get_filename_component(config_name "${config}" NAME_WE)
foreach(executor IN LISTS executors)
add_test(NAME example_file-config-solver_${config_name}_${executor}
COMMAND
"$<TARGET_FILE:file-config-solver>"
"${executor}" "${CMAKE_CURRENT_SOURCE_DIR}/file-config-solver/config/${config_name}.json"
"${CMAKE_CURRENT_SOURCE_DIR}/file-config-solver/data/A.mtx"
WORKING_DIRECTORY
"$<TARGET_FILE_DIR:ginkgo>")
endforeach()
endforeach()

endif()
2 changes: 1 addition & 1 deletion examples/adaptiveprecision-blockjacobi/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" &>/dev/null && pwd )
source ${THIS_DIR}/../build-setup.sh

# build
${CXX} -std=c++14 -o ${THIS_DIR}/adaptiveprecision-blockjacobi \
${CXX} -std=c++17 -o ${THIS_DIR}/adaptiveprecision-blockjacobi \
${THIS_DIR}/adaptiveprecision-blockjacobi.cpp \
-I${THIS_DIR}/../../include -I${BUILD_DIR}/include \
-L${THIS_DIR} ${LINK_FLAGS}
2 changes: 1 addition & 1 deletion examples/cb-gmres/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ mkdir -p data
cp ${THIS_DIR}/../../matrices/test/ani1.mtx data/A.mtx

# build
${CXX} -std=c++14 -o ${THIS_DIR}/cb-gmres ${THIS_DIR}/cb-gmres.cpp \
${CXX} -std=c++17 -o ${THIS_DIR}/cb-gmres ${THIS_DIR}/cb-gmres.cpp \
-I${THIS_DIR}/../../include -I${BUILD_DIR}/include \
-L${THIS_DIR} ${LINK_FLAGS}
2 changes: 1 addition & 1 deletion examples/custom-logger/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" &>/dev/null && pwd )
source ${THIS_DIR}/../build-setup.sh

# build
${CXX} -std=c++14 -o ${THIS_DIR}/custom-logger ${THIS_DIR}/custom-logger.cpp \
${CXX} -std=c++17 -o ${THIS_DIR}/custom-logger ${THIS_DIR}/custom-logger.cpp \
-I${THIS_DIR}/../../include -I${BUILD_DIR}/include -L${THIS_DIR} \
${LINK_FLAGS}
2 changes: 1 addition & 1 deletion examples/custom-matrix-format/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ CXX="nvcc"
LINK_FLAGS="${LINK_FLAGS/-Wl,-rpath,/-Xlinker -rpath -Xlinker }"

# build
${CXX} -std=c++14 -o ${THIS_DIR}/custom-matrix-format \
${CXX} -std=c++17 -o ${THIS_DIR}/custom-matrix-format \
${THIS_DIR}/custom-matrix-format.cpp ${THIS_DIR}/stencil_kernel.cu \
-I${THIS_DIR}/../../include -I${BUILD_DIR}/include \
-L${THIS_DIR} ${LINK_FLAGS}
2 changes: 1 addition & 1 deletion examples/custom-stopping-criterion/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ source ${THIS_DIR}/../build-setup.sh
LINK_FLAGS="${LINK_FLAGS} -lpthread"

# build
${CXX} -std=c++14 -o ${THIS_DIR}/custom-stopping-criterion \
${CXX} -std=c++17 -o ${THIS_DIR}/custom-stopping-criterion \
${THIS_DIR}/custom-stopping-criterion.cpp \
-I${THIS_DIR}/../../include -I${BUILD_DIR}/include \
-L${THIS_DIR} ${LINK_FLAGS}
19 changes: 19 additions & 0 deletions examples/file-config-solver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.16)
project(file-config-solver)

# We only need to find Ginkgo/nlohmann_json if we build this example stand-alone
if (NOT GINKGO_BUILD_EXAMPLES)
find_package(Ginkgo 1.9.0 REQUIRED)
find_package(nlohmann_json 3.9.1 REQUIRED)
endif()

add_executable(file-config-solver file-config-solver.cpp)
target_link_libraries(file-config-solver Ginkgo::ginkgo nlohmann_json::nlohmann_json)

# Copy the data files to the execution directory
configure_file(data/A.mtx data/A.mtx COPYONLY)
# 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()
16 changes: 16 additions & 0 deletions examples/file-config-solver/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# set up script
if [ $# -ne 1 ]; then
echo -e "Usage: $0 GINKGO_BUILD_DIRECTORY"
exit 1
fi
BUILD_DIR=$1
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" &>/dev/null && pwd )

source ${THIS_DIR}/../build-setup.sh

# build
${CXX} -std=c++17 -o ${THIS_DIR}/file-config-solver ${THIS_DIR}/file-config-solver.cpp \
-I${THIS_DIR}/../../include -I${BUILD_DIR}/include \
-L${THIS_DIR} ${LINK_FLAGS}
17 changes: 17 additions & 0 deletions examples/file-config-solver/config/blockjacobi-cg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"type": "solver::Cg",
"preconditioner": {
"type": "preconditioner::Jacobi",
"max_block_size": 8
},
"criteria": [
{
"type": "Iteration",
"max_iters": 20
},
{
"type": "ResidualNorm",
"reduction_factor": 1e-7
}
]
}
13 changes: 13 additions & 0 deletions examples/file-config-solver/config/cg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "solver::Cg",
"criteria": [
{
"type": "Iteration",
"max_iters": 20
},
{
"type": "ResidualNorm",
"reduction_factor": 1e-7
}
]
}
22 changes: 22 additions & 0 deletions examples/file-config-solver/config/ir.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"type": "solver::Ir",
"solver": {
"type": "solver::Cg",
"criteria": [
{
"type": "ResidualNorm",
"reduction_factor": 1e-2
}
]
},
"criteria": [
{
"type": "Iteration",
"max_iters": 10000
},
{
"type": "ResidualNorm",
"reduction_factor": 1e-12
}
]
}
84 changes: 84 additions & 0 deletions examples/file-config-solver/config/mixed-pgm-multigrid-cg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"type": "solver::Cg",
"preconditioner": {
"type": "solver::Multigrid",
"max_levels": 10,
"min_coarse_rows": 2,
"pre_smoother": [
{
"type": "solver::Ir",
"relaxation_factor": 0.9,
"solver": {
"type": "preconditioner::Jacobi",
"max_block_size": 1
},
"criteria": [
{
"type": "Iteration",
"max_iters": 1
}
]
},
{
"type": "solver::Ir",
"value_type": "float32",
"relaxation_factor": 0.9,
"solver": {
"type": "preconditioner::Jacobi",
"max_block_size": 1
},
"criteria": [
{
"type": "Iteration",
"max_iters": 1
}
]
}
],
"post_uses_pre": true,
"mg_level": [
{
"type": "multigrid::Pgm",
"deterministic": true
},
{
"type": "multigrid::Pgm",
"value_type": "float32",
"deterministic": true
}
],
"coarsest_solver": {
"type": "solver::Ir",
"value_type": "float32",
"relaxation_factor": 0.9,
"solver": {
"type": "preconditioner::Jacobi",
"max_block_size": 1
},
"criteria": [
{
"type": "Iteration",
"max_iters": 4
}
]
},
"default_initial_guess": "zero",
"criteria": [
{
"type": "Iteration",
"max_iters": 1
}
]
},
"criteria": [
{
"type": "Iteration",
"max_iters": 100
},
{
"type": "ResidualNorm",
"reduction_factor": 1e-8,
"baseline": "absolute"
}
]
}
21 changes: 21 additions & 0 deletions examples/file-config-solver/config/parilu-gmres.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "solver::Gmres",
"preconditioner": {
"type": "preconditioner::Ilu",
"l_solver_type": "solver::LowerTrs",
"reverse_apply": false,
"factorization": {
"type": "factorization::ParIlu"
}
},
"criteria": [
{
"type": "Iteration",
"max_iters": 1000
},
{
"type": "ResidualNorm",
"reduction_factor": 1e-7
}
]
}
30 changes: 30 additions & 0 deletions examples/file-config-solver/config/pgm-multigrid-cg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"type": "solver::Cg",
"preconditioner": {
"type": "solver::Multigrid",
"min_coarse_rows": 2,
"mg_level": [
{
"type": "multigrid::Pgm",
"deterministic": true
}
],
"criteria": [
{
"type": "Iteration",
"max_iters": 1
}
]
},
"criteria": [
{
"type": "Iteration",
"max_iters": 100
},
{
"type": "ResidualNorm",
"reduction_factor": 1e-8,
"baseline": "absolute"
}
]
}
Loading

0 comments on commit 272b4ff

Please sign in to comment.