Skip to content

Commit

Permalink
Adds missing functionality to allow dynamic modules to forward their …
Browse files Browse the repository at this point in the history
…configuration to nested children. (#224)

Adds missing functionality to allow dynamic modules to forward their configuration to nested children.

Closes #223

Authors:
  - Devin Robison (https://github.com/drobison00)

Approvers:
  - Michael Demoret (https://github.com/mdemoret-nv)

URL: #224
  • Loading branch information
drobison00 authored Nov 28, 2022
1 parent 6d46cb8 commit b4c7bd7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 35 deletions.
6 changes: 6 additions & 0 deletions include/srf/segment/builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ class Builder final
*/
void register_module_input(std::string input_name, std::shared_ptr<segment::ObjectProperties> object);

/**
* Get the json configuration for the current module under configuration.
* @return nlohmann::json object.
*/
nlohmann::json get_current_module_config();

/**
* Register an output port on the given module -- note: this in generally only necessary for dynamically
* created modules that use an alternate initializer function independent of the derived class.
Expand Down
32 changes: 2 additions & 30 deletions python/srf/_pysrf/include/pysrf/segment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <pybind11/pytypes.h>
#include <pybind11/stl.h> // IWYU pragma: keep

#include <cstddef>
#include <functional>
#include <memory>
#include <string>
Expand Down Expand Up @@ -176,16 +175,6 @@ class BuilderProxy
const std::string& name,
std::function<void(const pysrf::PyObjectObservable& obs, pysrf::PyObjectSubscriber& sub)> sub_fn);

static void make_py2cxx_edge_adapter(srf::segment::Builder& self,
std::shared_ptr<srf::segment::ObjectProperties> source,
std::shared_ptr<srf::segment::ObjectProperties> sink,
pybind11::object& sink_t);

static void make_cxx2py_edge_adapter(srf::segment::Builder& self,
std::shared_ptr<srf::segment::ObjectProperties> source,
std::shared_ptr<srf::segment::ObjectProperties> sink,
pybind11::object& source_t);

static void make_edge(srf::segment::Builder& self,
std::shared_ptr<srf::segment::ObjectProperties> source,
std::shared_ptr<srf::segment::ObjectProperties> sink);
Expand All @@ -210,26 +199,9 @@ class BuilderProxy
std::string output_name,
std::shared_ptr<segment::ObjectProperties> object);

static void init_module(srf::segment::Builder& self, std::shared_ptr<srf::modules::SegmentModule> module);

static std::shared_ptr<srf::segment::ObjectProperties> make_file_reader(srf::segment::Builder& self,
const std::string& name,
const std::string& filename);

static std::shared_ptr<srf::segment::ObjectProperties> debug_float_source(srf::segment::Builder& self,
const std::string& name,
std::size_t iterations);
static pybind11::dict get_current_module_config(srf::segment::Builder& self);

static std::shared_ptr<srf::segment::ObjectProperties> debug_float_passthrough(srf::segment::Builder& self,
const std::string& name);

static std::shared_ptr<PyNode> flatten_list(srf::segment::Builder& self, const std::string& name);

static std::shared_ptr<srf::segment::ObjectProperties> debug_string_passthrough(srf::segment::Builder& self,
const std::string& name);

static std::shared_ptr<srf::segment::ObjectProperties> debug_float_sink(srf::segment::Builder& self,
const std::string& name);
static void init_module(srf::segment::Builder& self, std::shared_ptr<srf::modules::SegmentModule> module);
};

#pragma GCC visibility pop
Expand Down
7 changes: 7 additions & 0 deletions python/srf/_pysrf/src/segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ void BuilderProxy::register_module_output(srf::segment::Builder& self,
self.register_module_output(std::move(output_name), object);
}

py::dict BuilderProxy::get_current_module_config(srf::segment::Builder& self)
{
auto json_config = self.get_current_module_config();

return cast_from_json(json_config);
}

void BuilderProxy::make_edge(srf::segment::Builder& self,
std::shared_ptr<srf::segment::ObjectProperties> source,
std::shared_ptr<srf::segment::ObjectProperties> sink)
Expand Down
2 changes: 2 additions & 0 deletions python/srf/core/segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ PYBIND11_MODULE(segment, module)
Builder.def(
"register_module_output", &BuilderProxy::register_module_output, py::arg("output_name"), py::arg("object"));

Builder.def("get_current_module_config", &BuilderProxy::get_current_module_config);

Builder.def("make_node_full", &BuilderProxy::make_node_full, py::return_value_policy::reference_internal);

/** Segment Module Interface Declarations **/
Expand Down
29 changes: 24 additions & 5 deletions python/tests/test_module_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,19 @@ def test_get_module_constructor():


def test_module_intitialize():

module_name = "test_py_source_from_cpp"
config = {"source_count": 42}
registry = srf.ModuleRegistry

def module_initializer(builder: srf.Builder):
local_config = builder.get_current_module_config()
assert ("source_count" in local_config)
assert (local_config["source_count"] == config["source_count"])

source_mod = builder.load_module("SourceModule", "srf_unittest", "ModuleSourceTest_mod1", config)
source_mod = builder.load_module("SourceModule", "srf_unittest", "ModuleSourceTest_mod1", local_config)
builder.register_module_output("source", source_mod.output_port("source"))

def init_wrapper(builder: srf.Builder):

global packet_count
packet_count = 0

Expand Down Expand Up @@ -200,6 +201,10 @@ def test_py_registered_nested_modules():
def module_initializer(builder: srf.Builder):
global packet_count

local_config = builder.get_current_module_config()
assert (isinstance(local_config, type({})))
assert (len(local_config.keys()) == 0)

def on_next(data):
global packet_count
packet_count += 1
Expand Down Expand Up @@ -246,6 +251,16 @@ def test_py_registered_nested_copied_modules():
global packet_count

def module_initializer(builder: srf.Builder):
local_config = builder.get_current_module_config()
assert (isinstance(local_config, type({})))
if ("test1" in local_config):
assert ("test2" not in local_config)
assert (local_config["test1"] == "module_1")
else:
assert ("test1" not in local_config)
assert ("test2" in local_config)
assert (local_config["test2"] == "module_2")

global packet_count

def on_next(data):
Expand All @@ -271,8 +286,12 @@ def on_complete():
def init_wrapper(builder: srf.Builder):
global packet_count
packet_count = 0
builder.load_module("test_py_registered_nested_copied_module", "srf_unittests", "my_loaded_module!", {})
builder.load_module("test_py_registered_nested_copied_module", "srf_unittests", "my_loaded_module_copy!", {})
builder.load_module("test_py_registered_nested_copied_module",
"srf_unittests",
"my_loaded_module!", {"test1": "module_1"})
builder.load_module("test_py_registered_nested_copied_module",
"srf_unittests",
"my_loaded_module_copy!", {"test2": "module_2"})

pipeline = srf.Pipeline()
pipeline.make_segment("ModuleAsSource_Segment", init_wrapper)
Expand Down
17 changes: 17 additions & 0 deletions src/public/segment/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,21 @@ void Builder::register_module_output(std::string output_name, sp_obj_prop_t obje
current_module->register_output_port(std::move(output_name), object);
}

nlohmann::json Builder::get_current_module_config()
{
if (m_module_stack.empty())
{
std::stringstream sstream;

sstream << "Failed to acquire module configuration -> no module context exists";
VLOG(2) << sstream.str();

throw std::invalid_argument(sstream.str());
}

auto current_module = m_module_stack.back();

return current_module->config();
}

} // namespace srf::segment

0 comments on commit b4c7bd7

Please sign in to comment.