Class Operator
Defined in File operator.hpp
Base Type
public holoscan::ComponentBase
(Class ComponentBase)
Derived Types
public holoscan::ops::AsyncPingRxOp
(Class AsyncPingRxOp)public holoscan::ops::AsyncPingTxOp
(Class AsyncPingTxOp)public holoscan::ops::BayerDemosaicOp
(Class BayerDemosaicOp)public holoscan::ops::FormatConverterOp
(Class FormatConverterOp)public holoscan::ops::ForwardOp
(Class ForwardOp)public holoscan::ops::GXFOperator
(Class GXFOperator)public holoscan::ops::HolovizOp
(Class HolovizOp)public holoscan::ops::InferenceOp
(Class InferenceOp)public holoscan::ops::InferenceProcessorOp
(Class InferenceProcessorOp)public holoscan::ops::PingRxOp
(Class PingRxOp)public holoscan::ops::PingTensorRxOp
(Class PingTensorRxOp)public holoscan::ops::PingTensorTxOp
(Class PingTensorTxOp)public holoscan::ops::PingTxOp
(Class PingTxOp)public holoscan::ops::SegmentationPostprocessorOp
(Class SegmentationPostprocessorOp)public holoscan::ops::V4L2VideoCaptureOp
(Class V4L2VideoCaptureOp)public holoscan::ops::VideoStreamRecorderOp
(Class VideoStreamRecorderOp)public holoscan::ops::VideoStreamReplayerOp
(Class VideoStreamReplayerOp)public holoscan::ops::VirtualOperator
(Class VirtualOperator)
-
class Operator : public holoscan::ComponentBase
Base class for all operators.
An operator is the most basic unit of work in Holoscan SDK. An Operator receives streaming data at an input port, processes it, and publishes it to one of its output ports.
This class is the base class for all operators. It provides the basic functionality for all operators.
NoteThis class is not intended to be used directly. Inherit from this class to create a new operator.
Subclassed by holoscan::ops::AsyncPingRxOp, holoscan::ops::AsyncPingTxOp, holoscan::ops::BayerDemosaicOp, holoscan::ops::FormatConverterOp, holoscan::ops::ForwardOp, holoscan::ops::GXFOperator, holoscan::ops::HolovizOp, holoscan::ops::InferenceOp, holoscan::ops::InferenceProcessorOp, holoscan::ops::PingRxOp, holoscan::ops::PingTensorRxOp, holoscan::ops::PingTensorTxOp, holoscan::ops::PingTxOp, holoscan::ops::SegmentationPostprocessorOp, holoscan::ops::V4L2VideoCaptureOp, holoscan::ops::VideoStreamRecorderOp, holoscan::ops::VideoStreamReplayerOp, holoscan::ops::VirtualOperator
Public Types
-
enum class OperatorType
Operator type used by the executor.
Values:
-
enumerator kNative
Native operator.
-
enumerator kGXF
GXF operator.
-
enumerator kVirtual
Virtual operator. (for internal use, not intended for use by application authors)
-
enumerator kNative
Public Functions
-
template<typename ArgT, typename ...ArgsT, typename = std::enable_if_t<!std::is_base_of_v<holoscan::Operator, std::decay_t<ArgT>> && (std::is_same_v<holoscan::Arg, std::decay_t<ArgT>> || std::is_same_v<holoscan::ArgList, std::decay_t<ArgT>> || std::is_base_of_v<holoscan::Condition, typename holoscan::type_info<ArgT>::derived_type> || std::is_base_of_v<holoscan::Resource, typename holoscan::type_info<ArgT>::derived_type>)>>
inline explicit Operator(ArgT &&arg, ArgsT&&... args) Construct a new Operator object.
- Parameters
args – The arguments to be passed to the operator.
-
Operator() = default
-
~Operator() override = default
-
inline OperatorType operator_type() const
Get the operator type.
- Returns
The operator type.
-
inline Operator &id(int64_t id)
Set the Operator ID.
- Parameters
id – The ID of the operator.
- Returns
The reference to this operator.
-
inline Operator &name(const std::string &name)
Set the name of the operator.
- Parameters
name – The name of the operator.
- Returns
The reference to this operator.
-
inline Operator &fragment(Fragment *fragment)
Set the fragment of the operator.
- Parameters
fragment – The pointer to the fragment of the operator.
- Returns
The reference to this operator.
Set the operator spec.
- Parameters
spec – The operator spec.
- Returns
The reference to this operator.
-
inline OperatorSpec *spec()
Get the operator spec.
- Returns
The operator spec.
Get the shared pointer to the operator spec.
- Returns
The shared pointer to the operator spec.
Get a shared pointer to the Condition object.
- Parameters
name – The name of the condition.
- Returns
The reference to the Condition object. If the condition does not exist, return the nullptr.
-
inline std::unordered_map<std::string, std::shared_ptr<Condition>> &conditions()
Get the conditions of the operator.
- Returns
The conditions of the operator.
Get a shared pointer to the Resource object.
- Parameters
name – The name of the resource.
- Returns
The reference to the Resource object. If the resource does not exist, returns the nullptr.
-
inline std::unordered_map<std::string, std::shared_ptr<Resource>> &resources()
Get the resources of the operator.
- Returns
The resources of the operator.
Add a condition to the operator.
- Parameters
arg – The condition to add.
Add a condition to the operator.
- Parameters
arg – The condition to add.
Add a resource to the operator.
- Parameters
arg – The resource to add.
Add a resource to the operator.
- Parameters
arg – The resource to add.
-
inline virtual void setup(OperatorSpec &spec)
Define the operator specification.
- Parameters
spec – The reference to the operator specification.
-
bool is_root()
Returns whether the operator is a root operator based on its fragment’s graph.
- Returns
True, if the operator is a root operator; false, otherwise
-
bool is_user_defined_root()
Returns whether the operator is a user-defined root operator i.e., the first operator added to the graph.
- Returns
True, if the operator is a user-defined root operator; false, otherwise
-
bool is_leaf()
Returns whether the operator is a leaf operator based on its fragment’s graph.
- Returns
True, if the operator is a leaf operator; false, otherwise
-
std::string qualified_name()
Returns the fully qualified name of the operator including the name of the fragment.
- Returns
std::string fully qualified name of the operator in the format: “<fragment_name>.<operator_name>”
-
virtual void initialize() override
Initialize the operator.
This function is called when the fragment is initialized by Executor::initialize_fragment().
-
inline virtual void start()
Implement the startup logic of the operator.
This method is called multiple times over the lifecycle of the operator according to the order defined in the lifecycle, and used for heavy initialization tasks such as allocating memory resources.
-
inline virtual void stop()
Implement the shutdown logic of the operator.
This method is called multiple times over the lifecycle of the operator according to the order defined in the lifecycle, and used for heavy deinitialization tasks such as deallocation of all resources previously assigned in start.
-
inline virtual void compute(InputContext &op_input, OutputContext &op_output, ExecutionContext &context)
Implement the compute method.
This method is called by the runtime multiple times. The runtime calls this method until the operator is stopped.
- Parameters
op_input – The input context of the operator.
op_output – The output context of the operator.
context – The execution context of the operator.
-
virtual YAML::Node to_yaml_node() const override
Get a YAML representation of the operator.
- Returns
YAML node including type, specs, conditions and resources of the operator in addition to the base component properties.
-
inline std::shared_ptr<nvidia::gxf::GraphEntity> graph_entity()
Get the GXF GraphEntity object corresponding to this operator.
- Returns
graph entity corresponding to the operator
-
inline std::shared_ptr<MetadataDictionary> metadata()
Get a shared pointer to the dynamic metadata of this operator.
Note: currently this metadata dictionary is only active if explicitly enabled for the application by setting
Fragment::is_metadata_enabled(true)
. When metadata is disabled the dictionary will not be populated by receive calls and will not be emitted on emit calls.This metadata dictionary is always empty at the start of each compute call. It is populated by metadata received on input ports during
InputContext::receive()
calls and can be modified as desired by the operator during the compute call. Any metadata corresponding to this object will be sent on the output ports by anyOutputContext::emit()
calls.- Returns
The metadata dictionary for this operator.
-
bool is_metadata_enabled() const
Determine if metadata is enabled for this operator.
- Returns
Boolean indicating if metadata is enabled (returns
fragment()->is_metadata_enabled()
ifenable_metadata
was not explicitly called for the operator.
-
inline void enable_metadata(bool enable)
Enable or disable metadata for this operator.
If this method has not been used to explicitly enable or disable metadata, the value for
is_metadata_enabled()
will be determined byFragment::is_metadata_enabled()
when the operator is initialized.- Parameters
enable – Boolean indicating if metadata should be enabled.
-
inline MetadataPolicy metadata_policy() const
Get the metadata update policy used by this operator.
- Returns
The metadata update policy used by this operator.
-
inline void metadata_policy(MetadataPolicy policy)
Set the metadata update policy used by this operator.
The metadata policy determines how metadata is merged across multiple receive calls:
MetadataPolicy::kUpdate
: Update the existing value when a key already exists.MetadataPolicy::kInplaceUpdate
: Update the existing MetadataObject’s value in-place when a key already exists.MetadataPolicy::kReject
: Do not modify the existing value if a key already exists.MetadataPolicy::kRaise
: Raise an exception if a key already exists (default).
- Parameters
policy – The metadata update policy to be used by this operator.
-
void add_cuda_stream_pool(int32_t dev_id = 0, uint32_t stream_flags = 0, int32_t stream_priority = 0, uint32_t reserved_size = 1, uint32_t max_size = 0)
Add no CudaStreamPool parameter or argument already exists, add a default one.
This method is available to be called by derived classes to add a default CudaStreamPool in the case that the user did not pass one in as an argument to
make_operator
.This function will not add an additional CUDA stream pool if one was already passed in as an argument to
make_operator
(i.e. it is in resources_) or if a “cuda_stream_pool” parameter already exists in the operator spec.- Parameters
dev_id – The device id for the CudaStreamPool.
stream_flags – The stream flags for any streams allocated by the pool.
stream_priority – The stream priority for any streams allocated by the pool.
reserved_size – The number of initial streams to reserve in the pool.
max_size – The maximum number of streams that can be allocated by the pool (0 = unbounded).
-
std::optional<std::shared_ptr<Receiver>> receiver(const std::string &port_name)
Return the Receiver corresponding to a specific input port.
- Parameters
port_name – The name of the input port.
- Returns
The Receiver corresponding to the input port, if it exists. Otherwise, return nullopt.
-
std::optional<std::shared_ptr<Transmitter>> transmitter(const std::string &port_name)
Return the Transmitter corresponding to a specific output port.
- Parameters
port_name – The name of the output port.
- Returns
The Transmitter corresponding to the output port, if it exists. Otherwise, return nullopt.
-
void queue_policy(const std::string &port_name, IOSpec::IOType port_type = IOSpec::IOType::kInput, IOSpec::QueuePolicy policy = IOSpec::QueuePolicy::kFault)
Set the queue policy to be used by an input or output port.
The following IOSpec::QueuePolicy values are supported:
QueuePolicy::kPop - If the queue is full, pop the oldest item, then add the new one.
QueuePolicy::kReject - If the queue is full, reject (discard) the new item.
QueuePolicy::kFault - If the queue is full, log a warning and reject the new item.
- Parameters
port_name – The name of the port.
port_type – Enum flag indicating whether
port_name
specifies an input or output port.policy – The queue policy to set for the port.
-
const std::shared_ptr<IOSpec> &input_exec_spec()
-
const std::shared_ptr<IOSpec> &output_exec_spec()
-
const std::function<void(const std::shared_ptr<Operator>&)> &dynamic_flow_func()
-
const std::vector<std::shared_ptr<FlowInfo>> &next_flows()
Get the list of next flows connected to this operator.
- Returns
A vector of FlowInfo objects representing the flows to downstream operators.
Add a dynamic flow from this operator to another operator using a FlowInfo object.
- Parameters
flow – The flow information object describing the connection between operators.
Add multiple dynamic flows from this operator using a list of FlowInfo objects.
- Parameters
flows – List of flow information objects describing the connections between operators.
Add a dynamic flow from this operator to another operator with specified output port.
- Parameters
curr_output_port_name – The name of the output port on this operator to connect from.
next_op – The downstream operator to connect to.
next_input_port_name – The name of the input port on the downstream operator to connect to. If not specified, the first available input port will be used.
Add a dynamic flow from this operator to another operator using default output port.
- Parameters
next_op – The downstream operator to connect to.
next_input_port_name – The name of the input port on the downstream operator to connect to. If not specified, the first available input port will be used.
-
const std::shared_ptr<std::vector<std::shared_ptr<FlowInfo>>> &dynamic_flows()
Get the list of dynamic flows that have been added to this operator.
- Returns
A shared pointer to a vector of FlowInfo objects representing the dynamic flows.
Locate a flow info in the operator’s next flows based on a given predicate.
Find all FlowInfo objects in the operator’s next flows that match a given condition.
-
std::shared_ptr<holoscan::AsynchronousCondition> async_condition()
Get the internal asynchronous condition for the operator.
- Returns
A shared pointer to the internal asynchronous condition.
-
void stop_execution()
Stop the execution of the operator.
This method is used to stop the execution of the operator by setting the internal async condition to EVENT_NEVER state, which sets the scheduling condition to NEVER. Once stopped, the operator will not be scheduled for execution (the
compute()
method will not be called).Note that executing this method does not trigger the operator’s
stop()
method. Thestop()
method is called only when the scheduler deactivates all operators together.
-
virtual std::shared_ptr<holoscan::ExecutionContext> execution_context() const
Get the ExecutionContext object.
- Returns
The shared pointer to the ExecutionContext object.
-
void ensure_contexts()
Ensure the contexts (input/output/execution) for the operator.
This method is called by the GXFExecutor when the operator is initialized.
-
virtual void release_internal_resources()
Internal method to clean up operator resources and prevent circular references.
This is an internal method called automatically by the GXFWrapper during operator shutdown. It resets std::shared_ptr fields and std::function objects such as
input_exec_spec_
,output_exec_spec_
,next_flows_
,dynamic_flows_
, anddynamic_flow_func_
to break potential circular references between connected Operator objects.WarningThis is an internal method that should never be called directly by user code. Improper use can lead to undefined behavior.
-
inline int64_t id() const
Get the identifier of the component.
By default, the identifier is set to -1. It is set to a valid value when the component is initialized.
With the default executor (GXFExecutor), the identifier is set to the GXF component ID.
- Returns
The identifier of the component.
-
inline const std::string &name() const
Get the name of the component.
- Returns
The name of the component.
-
inline Fragment *fragment()
Get a pointer to Fragment object.
- Returns
The Pointer to Fragment object.
-
inline void add_arg(const Arg &arg)
Add an argument to the component.
- Parameters
arg – The argument to add.
-
inline void add_arg(Arg &&arg)
Add an argument to the component.
- Parameters
arg – The argument to add.
-
inline void add_arg(const ArgList &arg)
Add a list of arguments to the component.
- Parameters
arg – The list of arguments to add.
-
inline void add_arg(ArgList &&arg)
Add a list of arguments to the component.
- Parameters
arg – The list of arguments to add.
Public Static Functions
-
static bool is_all_operator_successor_virtual(OperatorNodeType op, OperatorGraph &graph)
Returns whether all the successors of an operator are virtual operators.
- Parameters
op – The shared_ptr to the operator for which the check is to be performed
graph – The graph of operators. fragment()->graph() can usually be used to get this graph.
- Returns
true if the operator has all virtual operator successors, false otherwise
-
static bool is_all_operator_predecessor_virtual(OperatorNodeType op, OperatorGraph &graph)
Returns whether all the predecessors of an operator are virtual operators.
- Parameters
op – The shared_ptr to the operator for which the check is to be performed
graph – The graph of operators. fragment()->graph() can usually be used to get this graph.
- Returns
true if the operator has all virtual operator predecessors, false otherwise
-
static std::pair<std::string, std::string> parse_port_name(const std::string &op_port_name)
Return operator name and port name from a string in the format of “<op_name>[.<port_name>]”.
-
template<typename typeT>
static inline void register_codec(const std::string &codec_name, bool overwrite = true) Register the codec for serialization/deserialization of a custom type.
If the operator has an argument with a custom type, the codec must be registered using this method.
For example, suppose we want to emit using the following custom struct type:
namespace holoscan { struct Coordinate { int16_t x; int16_t y; int16_t z; } } // namespace holoscan
Then, we can define codec<Coordinate> as follows where the serialize and deserialize methods would be used for serialization and deserialization of this type, respectively.
namespace holoscan { template <> struct codec<Coordinate> { static expected<size_t, RuntimeError> serialize(const Coordinate& value, Endpoint* endpoint) { return serialize_trivial_type<Coordinate>(value, endpoint); } static expected<Coordinate, RuntimeError> deserialize(Endpoint* endpoint) { return deserialize_trivial_type<Coordinate>(endpoint); } }; } // namespace holoscan
In this case, since this is a simple struct with a static size, we can use the existing serialize_trivial_type and deserialize_trivial_type implementations.
Finally, to register this custom codec at runtime, we need to make the following call within the setup method of our Operator.
register_codec<Coordinate>("Coordinate");
- Template Parameters
typeT – The type of the argument to register.
- Parameters
codec_name – The name of the codec (must be unique unless overwrite is true).
overwrite – If true and codec_name already exists, the codec will be overwritten.
Public Static Attributes
-
static constexpr const char *kInputExecPortName = "__input_exec__"
Default input execution port name.
-
static constexpr const char *kOutputExecPortName = "__output_exec__"
Default output execution port name.
Protected Functions
-
gxf_uid_t initialize_graph_entity(void *context, const std::string &entity_prefix = "")
This function creates a GraphEntity corresponding to the operator.
- Parameters
context – The GXF context.
entity_prefix – prefix to add to the operator’s name when creating the GraphEntity.
- Returns
The GXF entity eid corresponding to the graph entity.
-
void initialize_async_condition()
Initialize the internal asynchronous condition to control the operator execution.
This method is called by the GXFExecutor when the operator is initialized.
-
virtual gxf_uid_t add_codelet_to_graph_entity()
Add this operator as the codelet in the GXF GraphEntity.
- Returns
The codelet component id corresponding to GXF codelet.
-
void initialize_conditions()
Initialize conditions and add GXF conditions to graph_entity_.
-
void initialize_resources()
Initialize resources and add GXF resources to graph_entity_.
-
void update_params_from_args()
Update parameters based on the specified arguments.
-
void update_connector_arguments()
Replace any “receiver” supplied as a string with the actual receiver of that name.
Can only be called after GXFExecutor::create_input_port so the input ports (receivers) exist.
-
void find_ports_used_by_condition_args()
Determine ports whose transmitter or receiver are associated with an Arg for a Condition.
Should be called before GXFExecutor::create_input_port or GXFExecutor::create_output_port.
-
virtual void set_parameters()
Set the parameters based on defaults (sets GXF parameters for GXF operators)
-
MessageLabel get_consolidated_input_label()
This function returns a consolidated MessageLabel for all the input ports of an Operator. If there is no input port (root Operator), then a new MessageLabel with the current Operator and default receive timestamp is returned.
- Returns
The consolidated MessageLabel
-
inline void update_input_message_label(std::string input_name, MessageLabel m)
Update the input_message_labels map with the given MessageLabel a corresponding input_name.
- Parameters
input_name – The input port name for which the MessageLabel is updated
m – The new MessageLabel that will be set for the input port
-
inline void delete_input_message_label(std::string input_name)
Delete the input_message_labels map entry for the given input_name.
- Parameters
input_name – The input port name for which the MessageLabel is deleted
-
inline void reset_input_message_labels()
Reset the input message labels to clear all its contents. This is done for a leaf operator when it finishes its execution as it is assumed that all its inputs are processed.
-
inline std::map<std::string, uint64_t> num_published_messages_map()
Get the number of published messages for each output port indexed by the output port name.
The function is utilized by the DFFTCollector to update the DataFlowTracker with the number of published messages for root operators.
- Returns
The map of the number of published messages for every output name.
-
void update_published_messages(std::string output_name)
This function updates the number of published messages for a given output port.
- Parameters
output_name – The name of the output port
-
virtual void reset_graph_entities()
Reset the GXF GraphEntity of any components associated with this operator.
-
void initialize_next_flows()
Initialize the next flows for the operator.
-
inline std::vector<std::string> &non_default_input_ports()
-
inline std::vector<std::string> &non_default_output_ports()
-
void update_params_from_args(std::unordered_map<std::string, ParameterWrapper> ¶ms)
Update parameters based on the specified arguments.
Protected Attributes
-
bool is_initialized_ = false
Whether the operator is initialized.
-
OperatorType operator_type_ = OperatorType::kNative
The type of the operator.
-
std::shared_ptr<OperatorSpec> spec_
The operator spec of the operator.
-
std::unordered_map<std::string, std::shared_ptr<Condition>> conditions_
The conditions of the operator.
-
std::unordered_map<std::string, std::shared_ptr<Resource>> resources_
The resources used by the operator.
-
std::shared_ptr<nvidia::gxf::GraphEntity> graph_entity_
GXF graph entity corresponding to the Operator
-
std::shared_ptr<holoscan::AsynchronousCondition> internal_async_condition_
The asynchronous condition to control the operator execution.
-
std::shared_ptr<ExecutionContext> execution_context_ = {}
The execution context for the operator.
-
std::shared_ptr<MetadataDictionary> dynamic_metadata_ = std::make_shared<MetadataDictionary>()
The metadata dictionary for the operator.
-
std::optional<bool> is_metadata_enabled_ = std::nullopt
Flag to enable or disable metadata for the operator. If not set, the value from the Fragment is used.
-
std::shared_ptr<IOSpec> input_exec_spec_
The input execution port specification.
-
std::shared_ptr<IOSpec> output_exec_spec_
The output execution port specification.
-
std::function<void(const std::shared_ptr<Operator>&)> dynamic_flow_func_ = nullptr
-
std::shared_ptr<std::vector<std::shared_ptr<FlowInfo>>> next_flows_
-
std::shared_ptr<std::vector<std::shared_ptr<FlowInfo>>> dynamic_flows_
Friends
- friend class AnnotatedDoubleBufferReceiver
- friend class AnnotatedDoubleBufferTransmitter
- friend class HoloscanUcxTransmitter
- friend class HoloscanUcxReceiver
- friend class DFFTCollector
- friend class holoscan::gxf::GXFExecutor
- friend class Fragment
-
friend gxf_result_t deannotate_message(gxf_uid_t *uid, const gxf_context_t &context, Operator *op, const char *name)
This function de-annotates a message and extracts the MessageLabel timestamp. It then updates necessary data flow tracking information in DataFlowTracker object.
- Parameters
uid – The entity ID of the message.
context – The GXF context.
op – The operator that is receiving the message.
receiver_name – The name of the receiver which is receiving the message.
-
friend gxf_result_t annotate_message(gxf_uid_t uid, const gxf_context_t &context, Operator *op, const char *name)
This function annotates a message with a MessageLabel timestamp.
- Parameters
uid – The entity ID of the message.
context – The GXF context.
op – The operator that is transmitting the message.
transmitter_name – The name of the transmitter from which the message is being published.
- Returns
gxf_result_t The result of the annotation.
-
struct FlowInfo
Information about a flow connection between two operators.
This struct encapsulates all the necessary information about a connection between two operators, including the source and destination operators, their respective ports, and port specifications.
Public Functions
Construct a new FlowInfo object.
- Parameters
curr_operator – The source operator of the flow.
output_port_name – The name of the output port on the source operator.
next_operator – The destination operator of the flow.
input_port_name – The name of the input port on the destination operator.
Public Members
-
const std::shared_ptr<Operator> curr_operator
The source operator of the flow connection.
-
const std::string output_port_name
The name of the output port on the source operator.
-
const std::shared_ptr<IOSpec> output_port_spec
The specification of the output port.
-
const std::shared_ptr<Operator> next_operator
The destination operator of the flow connection.
-
const std::string input_port_name
The name of the input port on the destination operator.
-
const std::shared_ptr<IOSpec> input_port_spec
The specification of the input port.
-
enum class OperatorType