NVIDIA Morpheus (24.10.01)

C++ Morpheus 模块

有关 Morpheus 模块的介绍,请参阅简单 Python 模块

以下示例将创建一个简单的 C++ 模块,该模块在不修改的情况下传递输入数据。此模块将用 C++ 编写,并将编译到 Morpheus 核心库中。

注意:关于 C++ 模块,有一点不同之处在于,它们默认被假定为无状态的,这意味着模块本身可以在调用初始化函数后释放。如果您需要一个状态在管道生命周期内持久存在的模块,您还需要从 PersistentModule 类继承,这将导致管道保持对模块的引用,直到管道被销毁。

my_test_module.hpp

复制
已复制!
            

#pragma once #include <morpheus/export.h> #include <mrc/modules/properties/persistent.hpp> #include <mrc/modules/segment_modules.hpp> #include <nlohmann/json.hpp> namespace morpheus { class MORPHEUS_EXPORT MyTestModule: public mrc::modules::SegmentModule, public mrc::modules::PersistentModule { using type_t = MyTestModule; public: DataLoaderModule(std::string module_name); DataLoaderModule(std::string module_name, nlohmann::json _config); protected: void initialize(mrc::segment::Builder& builder) override; std::string module_type_name() const override; private: int my_persistent_value{0}; }; } // namespace morpheus

my_test_module.cpp

复制
已复制!
            

#include <mrc/modules/segment_modules.hpp> #include <mrc/segment/builder.hpp> #include <mrc/utils/type_utils.hpp> #include <nlohmann/json.hpp> #include <string> namespace morpheus { MyTestModule::MyTestModule(std::string module_name) : SegmentModule(module_name) {} MyTestModule::MyTestModule(std::string module_name, nlohmann::json _config) : SegmentModule(std::move(module_name), std::move(_config)) {} void MyTestModule::initialize(mrc::segment::Builder& builder) { auto passthrough_node = builder.make_node<std::shared_ptr<MyDataType>>("passthrough_node", rxcpp::operators::map([this](std::shared_ptr<MyDataType> data) { return data; })); register_input_port("input_0", passthrough_node); register_output_port("output_0", passthrough_node); } std::string MyTestModule::module_type_name() const { return std::string(::mrc::boost_type_name<type_t>()); } }

my_test_module_registration.cpp

复制
已复制!
            

#include "mrc/modules/module_registry.hpp" #include "my_test_module.hpp" int main(int argc, char** argv) { const std::vector<unsigned int> release_version = {1, 0, 0}; auto module_constructor = [](std::string module_name, nlohmann::json config) { return std::make_shared<morpheus::MyTestModule>(module_name, config); }; ModuleRegistry::register_module("my_test_module", "my_module_namespace", release_version, module_constructor); }

动态模块创建和加载

复制
已复制!
            

#include "mrc/version.hpp" #include "mrc/modules/module_registry.hpp" #include "my_test_module.hpp" extern "C" { const std::vector<unsigned int> TestModuleVersion{mrc_VERSION_MAJOR, mrc_VERSION_MINOR, mrc_VERSION_PATCH}; const char* MODULES[] = {"my_test_module::my_module_namespace"}; bool MRC_MODULE_entrypoint_load() // NOLINT { using namespace mrc::modules; try { ModuleRegistry::register_module( "my_test_module", "my_module_namespace" TestModuleVersion, [](std::string module_name, nlohmann::json config) { return std::make_shared<mrc::modules::MyTestModule>(std::move(module_name), std::move(config)); }); } catch (...) { return false; } return true; } bool MRC_MODULE_entrypoint_unload() // NOLINT { using namespace mrc::modules; try { ModuleRegistry::unregister_module("my_test_module", "my_module_namespace"); } catch (...) { return false; } return true; } unsigned int MRC_MODULE_entrypoint_list(const char** result) // NOLINT { *result = (const char*)(&MODULES); return 1; // Number of modules }

以上代码是如何声明可以在运行时加载的共享模块的示例。如果我们假设此代码段编译为 my_test_module.so,我们可以使用以下方法在运行时动态加载模块

复制
已复制!
            

#include "mrc/modules/module_registry.hpp" std::string get_modules_path() { return std::string{YOUR_MODULES_PATH}; } int main(int argc, char** argv) { using namespace mrc::modules; auto plugin = PluginModule::create_or_acquire("my_test_module.so"); plugin->set_library_directory(get_modules_path()); plugin->load(); std::string module_namespace{"my_module_namespace"}; std::string module_name{"my_test_module"}; ModuleRegistry::contains_namespace(module_namespace); // Should be true ModuleRegistry::contains(module_name, module_namespace); // Should be true }

上一个 Python Morpheus 模块
下一个 Morpheus 控制消息
© 版权所有 2024,NVIDIA。 上次更新于 2024 年 12 月 3 日。