holoscan::Subgraph
holoscan::Subgraph
A reusable subgraph that directly populates a Fragment’s operator graph.
Subgraph receives Fragment* during construction and directly adds operators and flows to the Fragment’s main graph during compose().
Usage example:
Example
Constructors
Subgraph
Construct Subgraph with target Fragment.
Parameters
Target Fragment to populate with operators
Unique instance name for operator qualification
Optional path to a YAML configuration file for this subgraph. If provided, the configuration is loaded before compose() is called, making from_config() available during composition.
Destructor
~Subgraph
Methods
compose
Define the internal structure of the subgraph.
This method should create operators and flows, which will be directly added to the Fragment’s main graph with qualified names.
name
Get the name for this subgraph.
instance_name
Get the instance name for this subgraph.
Deprecated
Use name() instead. This method will be removed in a future release.
fragment
Mutable
Const
Get the Fragment that this subgraph belongs to.
Returns: Pointer to the fragment this subgraph belongs to
get_qualified_name
Create qualified operator name: subgraph_name + ”_” + operator_name.
make_operator
Overload 1
Overload 2
make_condition
Overload 1
Overload 2
make_resource
Overload 1
Overload 2
register_service
Register an existing service instance with the fragment.
Registers an already created service instance with the fragment. This allows the service to be retrieved later using Fragment::service().
Returns: true if the service was successfully registered, false otherwise.
Template parameters
The type of the service (must inherit from Resource or FragmentService).
Parameters
The shared pointer to the service instance to register.
The identifier for the service registration. If empty, uses the service type or resource name as identifier.
make_subgraph
Create a nested subgraph within this subgraph.
This enables hierarchical Subgraph composition. The nested Subgraph will use the same Fragment* and will have its operators added directly to the Fragment’s main graph with hierarchical qualified names (parent_name_child_name_operator).
Returns: Shared pointer to the nested subgraph
Template parameters
The nested subgraph class type
Parameters
Name for the nested subgraph
Additional arguments for the nested subgraph constructor
add_operator
Add an operator to the Fragment’s main graph with qualified name.
This directly calls fragment_->add_operator() with a qualified name, eliminating the need for intermediate graph storage.
add_subgraph
Add a pre-constructed subgraph as a nested subgraph, taking ownership.
This method stores the subgraph in nested_subgraphs_ for lifetime management and interface port resolution.
Two paths are supported:
- Not yet composed (C++ factory pattern): The subgraph’s name is qualified with this parent’s name prefix, then compose() is called. The subgraph should be constructed with an unqualified name.
- Already composed (Python path): The name must already be qualified with this parent’s prefix (e.g. constructed with this subgraph as the parent). The subgraph is stored as-is.
Example (C++ factory pattern):
Throws: std::runtime_error if a nested subgraph with the same name already exists.
Throws: std::runtime_error if already composed but the name is not properly qualified.
Parameters
The subgraph to be added.
Example
add_flow
Overload 1
Overload 2
Overload 3
Overload 4
Overload 5
Overload 6
Overload 7
Overload 8
Overload 9
Overload 10
Add a flow between two operators directly in the Fragment’s main graph.
This directly calls fragment_->add_flow(), eliminating the need for intermediate flow storage.
bind_input_topic
Bind an input interface port to a Pub/Sub topic.
Applies the topic binding to every internal operator port mapped from this interface port.
Parameters
Interface port name on this subgraph.
Topic name to subscribe to.
Optional QoS profile override. When nullopt, any QoS already configured on the mapped internal ports is preserved.
If true, replace explicitly non-PubSub connectors on mapped internal ports with Pub/Sub connectors.
bind_output_topic
Bind an output interface port to a Pub/Sub topic.
Applies the topic binding to every internal operator port mapped from this interface port.
Parameters
Interface port name on this subgraph.
Topic name to publish to.
Optional QoS profile override. When nullopt, any QoS already configured on the mapped internal ports is preserved.
If true, replace explicitly non-PubSub connectors on mapped internal ports with Pub/Sub connectors.
set_dynamic_flows
Set a callback function to define dynamic flows for an operator at runtime.
This method allows operators to modify their connections with other operators during execution. The callback function is called after the operator executes and can add dynamic flows using the operator’s add_dynamic_flow() methods.
Parameters
The operator to set dynamic flows for
The callback function that defines the dynamic flows. Takes a shared pointer to the operator as input and returns void.
add_data_logger
Add a data logger to the fragment.
This method dispatches to the fragment’s add_data_logger method.
Parameters
The data logger to add.
add_interface_port
Overload 1
Overload 2
Add an interface port that can be connected from external Subgraphs/Operators.
Validates that the internal operator has the specified port. If is_input is not specified, the method automatically detects whether the port is an input or output. If the port name exists in both inputs and outputs, a runtime_error is thrown requiring explicit specification.
Parameters
The name of the interface port (used in add_flow calls)
The internal operator that owns the actual port
The port name on the internal operator (defaults to external_name if not specified)
Whether this is an input port (true) or output port (false). If not specified, the port direction is auto-detected from the operator’s port definitions.
add_input_interface_port
Add an input interface port (convenience method)
Overload 2
Add an input interface port (convenience method).
Parameters
The name of the interface port (used in add_flow calls)
The internal operator that owns the actual port
The port name on the internal operator (defaults to external_name if not specified)
add_output_interface_port
Add an output interface port (convenience method)
Overload 2
Add an output interface port (convenience method).
Parameters
The name of the interface port (used in add_flow calls)
The internal operator that owns the actual port
The port name on the internal operator (defaults to external_name if not specified)
add_input_exec_interface_port
Overload 1
Overload 2
Add an input execution interface port for control flow.
Exposes an execution port from an internal operator for control flow connections. The internal operator must be a Native operator with an input execution spec.
Parameters
The name of the interface port (used in add_flow calls)
The internal operator that has the execution port
add_output_exec_interface_port
Overload 1
Overload 2
Add an output execution interface port for control flow.
Exposes an execution port from an internal operator for control flow connections. The internal operator must be a Native operator with an output execution spec.
Parameters
The name of the interface port (used in add_flow calls)
The internal operator that has the execution port
interface_ports
Get data interface ports.
Returns a map of interface port names to InterfacePort objects. Each InterfacePort can contain multiple mappings for broadcast input ports.
exec_interface_ports
Get execution interface ports.
get_interface_operator_port
Get the first operator/port for a data interface port name.
This method first checks local interface ports, then recursively checks nested subgraphs for hierarchical port resolution.
For broadcast input ports that have multiple mappings, this returns only the first mapping. Access the InterfacePort directly via interface_ports() to get all mappings.
Returns: Pair of (operator, actual_port_name) or (nullptr, "") if not found
Parameters
The interface port name
get_exec_interface_operator_port
Get the operator/port for an execution interface port name.
This method first checks local exec interface ports, then recursively checks nested subgraphs for hierarchical port resolution.
Returns: Pair of (operator, actual_port_name) or (nullptr, "") if not found
Parameters
The exec interface port name
is_composed
Check if the subgraph has been composed.
set_composed
Set the composed state of the subgraph.
operators
Get all operators belonging to this subgraph and its nested subgraphs.
This method returns all operators whose names are prefixed with this subgraph’s name followed by an underscore. This includes operators from nested subgraphs since their names are also prefixed with the parent subgraph’s name.
Returns: Vector of shared pointers to the operators.
nested_subgraphs
Get the nested subgraphs directly owned by this subgraph.
Returns subgraphs added via make_subgraph() or add_subgraph() within this subgraph. Does not recursively include subgraphs nested further down the hierarchy.
Returns: A const reference to the vector of nested subgraphs.
config
Get the configuration of the subgraph
Set the configuration of the subgraph from a file
Get the configuration of the subgraph.
Returns: The reference to the configuration of the subgraph (Config object.)
config_shared
Get the shared pointer to the configuration of the subgraph.
Returns: The shared pointer to the configuration of the subgraph.
from_config
Get the value of a configuration key as an ArgList.
This method retrieves the value from the subgraph’s configuration for the given key. You can use ’.’ (dot) to access nested fields. The returned ArgList can be passed directly to make_operator() or other methods that accept configuration arguments.
Example usage:
Returns: The argument list of the configuration for the key.
Parameters
The key of the configuration.
Example
config_keys
Determine the set of keys present in the subgraph’s config.
Returns: The set of valid keys.
format_port_list
Efficiently format a port list for error messages.
Uses fmt::memory_buffer for O(n) string building instead of O(n²) string concatenation. This provides better performance and avoids repeated memory reallocations.
Returns: Formatted string with port names like “‘port1’, ‘port2’, ‘port3’”
Parameters
The port collection (inputs or outputs from OperatorSpec)
validate_operator_port
Validate that an operator has a port with the correct type.
Returns: true if the port exists and has the correct type, false otherwise
Parameters
The operator to validate
The name of the port to check
Whether we expect this to be an input port (true) or output port (false)
validate_operator_exec_port
Validate that an operator can be used for execution control flow.
Checks that the operator is a Native operator. Execution specs are created automatically by the GXF executor during initialization.
Returns: true if the operator can be used for execution control flow, false otherwise
Parameters
The operator to validate
check_exec_port_name_available
Check if an exec interface port name is already in use.
Checks both data and exec interface port maps for name conflicts.
Returns: true if the name is available (not already used)
Throws: std::runtime_error if the name is already in use
Parameters
The name to check
register_exec_interface_port
Register an exec interface port.
Creates and stores an InterfacePort in exec_interface_ports_ with the given parameters.
Parameters
The external interface port name
The internal operator
The internal port name (kInputExecPortName or kOutputExecPortName)
Whether this is an input port
resolve_nested_exec_port
Resolve a nested subgraph’s exec interface port to an operator and port.
Looks up the specified exec interface port in the nested subgraph and validates that it has the expected direction (input vs output).
Returns: Pair of (operator, port_name) or (nullptr, "") if resolution fails
Parameters
The name for this subgraph’s interface port (for error messages)
The nested subgraph to query
The exec interface port name to look up
Whether we expect an input port (true) or output port (false)