Skip to content

Commit

Permalink
add extension: header only json config
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed May 8, 2024
1 parent 4037ca5 commit 3571dd6
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 6 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ if(GINKGO_BUILD_TESTS)
endif()
if(GINKGO_BUILD_BENCHMARKS)
find_package(gflags 2.2.2 QUIET)
endif()
if(GINKGO_BUILD_TESTS OR GINKGO_BUILD_BENCHMARKS)
find_package(nlohmann_json 3.9.1 QUIET)
endif()

Expand All @@ -265,6 +267,11 @@ endif()
# Bundled third party libraries
add_subdirectory(third_party) # Third-party tools and libraries

if(GINKGO_BUILD_TESTS)
# Non core directories and targets
add_subdirectory(extensions)
endif()

if(MSVC)
if(BUILD_SHARED_LIBS)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
Expand Down Expand Up @@ -312,8 +319,6 @@ if(GINKGO_BUILD_TESTS)
add_subdirectory(test) # Tests running on all executors
endif()

# Non core directories and targets
add_subdirectory(extensions)

if(GINKGO_BUILD_EXAMPLES)
add_subdirectory(examples)
Expand Down
5 changes: 1 addition & 4 deletions extensions/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@ if (Kokkos_FOUND)
add_subdirectory(kokkos)
endif ()

find_package(nlohmann_json 3.9.1 QUIET)
if (nlohmann_json_FOUND)
add_subdirectory(config)
endif ()
add_subdirectory(config)
2 changes: 2 additions & 0 deletions extensions/test/config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ginkgo_create_test(json_config ADDITIONAL_LIBRARIES nlohmann_json::nlohmann_json)
configure_file("test.json" test.json COPYONLY)
79 changes: 79 additions & 0 deletions extensions/test/config/json_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

#include <gtest/gtest.h>
#include <nlohmann/json.hpp>


#include <ginkgo/core/config/property_tree.hpp>
#include <ginkgo/extensions/config/json_config.hpp>


#include "core/test/utils.hpp"


TEST(JsonConfig, ReadMap)
{
const char json[] = R"({"test": "A", "bool": true})";
auto d = nlohmann::json::parse(json);

auto ptree = gko::ext::config::parse_json(d);

ASSERT_EQ(ptree.get_map().size(), 2);
ASSERT_EQ(ptree.get("test").get_string(), "A");
ASSERT_EQ(ptree.get("bool").get_boolean(), true);
}


TEST(JsonConfig, ReadArray)
{
const char json[] = R"(["A", "B", "C"])";
auto d = nlohmann::json::parse(json);

auto ptree = gko::ext::config::parse_json(d);

ASSERT_EQ(ptree.get_array().size(), 3);
ASSERT_EQ(ptree.get(0).get_string(), "A");
ASSERT_EQ(ptree.get(1).get_string(), "B");
ASSERT_EQ(ptree.get(2).get_string(), "C");
}


TEST(JsonConfig, ReadInput)
{
const char json[] =
R"({"item": 4,
"array": [3.0, 4.5],
"map": {"bool": false}})";

auto d = nlohmann::json::parse(json);

auto ptree = gko::ext::config::parse_json(d);

auto& child_array = ptree.get("array").get_array();
auto& child_map = ptree.get("map").get_map();
ASSERT_EQ(ptree.get_map().size(), 3);
ASSERT_EQ(ptree.get("item").get_integer(), 4);
ASSERT_EQ(child_array.size(), 2);
ASSERT_EQ(child_array.at(0).get_real(), 3.0);
ASSERT_EQ(child_array.at(1).get_real(), 4.5);
ASSERT_EQ(child_map.size(), 1);
ASSERT_EQ(child_map.at("bool").get_boolean(), false);
}


TEST(JsonConfig, ReadInputFromFile)
{
auto ptree = gko::ext::config::parse_json_file("test.json");

auto& child_array = ptree.get("array").get_array();
auto& child_map = ptree.get("map").get_map();
ASSERT_EQ(ptree.get_map().size(), 3);
ASSERT_EQ(ptree.get("item").get_integer(), 4);
ASSERT_EQ(child_array.size(), 2);
ASSERT_EQ(child_array.at(0).get_real(), 3.0);
ASSERT_EQ(child_array.at(1).get_real(), 4.5);
ASSERT_EQ(child_map.size(), 1);
ASSERT_EQ(child_map.at("bool").get_boolean(), false);
}
10 changes: 10 additions & 0 deletions extensions/test/config/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"item": 4,
"array": [
3.0,
4.5
],
"map": {
"bool": false
}
}
8 changes: 8 additions & 0 deletions include/ginkgo/extensions/config/json_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ namespace gko {
namespace ext {
namespace config {


/**
* parse_json takes the nlohmann json object to generate the property tree
* object
*/
inline gko::config::pnode parse_json(const nlohmann::json& input)
{
const auto& dom = input;
Expand Down Expand Up @@ -65,6 +70,9 @@ inline gko::config::pnode parse_json(const nlohmann::json& input)
}


/**
* parse_json_file takes the json file to generate the property tree object
*/
inline gko::config::pnode parse_json_file(std::string filename)
{
std::ifstream fstream(filename);
Expand Down
3 changes: 3 additions & 0 deletions third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ if(GINKGO_BUILD_BENCHMARKS)
if (NOT gflags_FOUND)
add_subdirectory(gflags)
endif()
endif()

if(GINKGO_BUILD_TESTS OR GINKGO_BUILD_BENCHMARKS)
if (NOT nlohmann_json_FOUND)
add_subdirectory(nlohmann_json)
endif()
Expand Down

0 comments on commit 3571dd6

Please sign in to comment.