holoscan::OutputContext

Beta
View as Markdown

Class to hold the output context.

This class provides the interface to send data to the output ports of the operator.

#include <holoscan/io_context.hpp>

Constructors

OutputContext

holoscan::OutputContext::OutputContext(
ExecutionContext *execution_context,
Operator *op
)

Construct a new OutputContext object.

outputs for the OutputContext will be set to op->spec()->outputs()

Parameters

execution_context
ExecutionContext *

The pointer to the execution context.

op
Operator *

The pointer to the operator that this context is associated with.

Destructor

~OutputContext

virtual holoscan::OutputContext::~OutputContext() = default

Methods

execution_context

ExecutionContext * holoscan::OutputContext::execution_context() const

Get pointer to the execution context.

Returns: The pointer to the execution context.

op

Operator * holoscan::OutputContext::op() const

Return the operator that this context is associated with.

Returns: The pointer to the operator.

outputs

std::unordered_map<std::string, std::shared_ptr<IOSpec>> & holoscan::OutputContext::outputs() const

Return the reference to the map of the output specs.

Returns: The reference to the map of the output specs.

emit

inline
template <typename DataT,
typename = std::enable_if_t<holoscan::is_one_of_derived_v<DataT, nvidia::gxf::Entity>>>
void holoscan::OutputContext::emit(
DataT &data,
const char *name = nullptr,
const int64_t acq_timestamp = -1
)

Send message data (GXF Entity) to the output port with the given name.

This method is for interoperability with the GXF Codelet.

The object to be sent must be an object with holoscan::gxf::Entity type and the output port with the given name must exist.

If the operator has a single output port, the output port name can be omitted.

Example:

Template parameters

DataT
typename

The type of the data to send. It should be holoscan::gxf::Entity.

Parameters

data
DataT &

The entity object to send (holoscan::gxf::Entity).

name
const char *Defaults to nullptr

The name of the output port.

acq_timestamp
const int64_tDefaults to -1

The time when the message is acquired. For instance, this would generally be the timestamp of the camera when it captures an image.

Example

class PingTxOp : public holoscan::ops::GXFOperator {
public:
HOLOSCAN_OPERATOR_FORWARD_ARGS_SUPER(PingTxOp, holoscan::ops::GXFOperator)
PingTxOp() = default;
void setup(OperatorSpec& spec) override {
spec.input<holoscan::gxf::Entity>("in");
spec.output<holoscan::gxf::Entity>("out");
}
void compute(InputContext& op_input, OutputContext& op_output,
[[maybe_unused]] ExecutionContext& context) override
{
// The type of `in_message` is 'holoscan::gxf::Entity'.
auto in_message = op_input.receive<holoscan::gxf::Entity>("in");
// The type of `tensor` is 'std::shared_ptr<holoscan::Tensor>'.
auto tensor = in_message.get<Tensor>();
// Process with 'tensor' here.
// ...
// Create a new message (Entity)
auto out_message = holoscan::gxf::Entity::New(&context);
out_message.add(tensor, "tensor");
// Send the processed message.
op_output.emit(out_message, "out");
}
};

set_cuda_stream

virtual void holoscan::OutputContext::set_cuda_stream(
const cudaStream_t stream,
const char *output_port_name = nullptr
)

Set a CUDA stream to be emitted on a given output port.

When using receive_cuda_stream, output ports are automatically configured to emit the operator’s internal stream, so this method is typically not needed. Use this method when:

  • Using allocate_cuda_stream to allocate a stream for a root operator
  • Using receive_cuda_streams for manual stream handling

This method must be called before the corresponding emit() call for the port.

Parameters

stream
const cudaStream_t

The CUDA stream to emit. Must be a Holoscan-managed stream (one returned by receive_cuda_stream, receive_cuda_streams, or allocate_cuda_stream).

output_port_name
const char *Defaults to nullptr

The name of the output port. Can be omitted if the operator has only a single output port.

cuda_object_handler

std::shared_ptr<CudaObjectHandler> holoscan::OutputContext::cuda_object_handler()

Get the CUDA stream/event handler used by this input context.

This CudaObjectHandler class is designed primarily for internal use and is not guaranteed to have a stable API. Application authors should instead rely on the public set_cuda_stream method.

stream_to_emit

virtual std::optional<cudaStream_t> holoscan::OutputContext::stream_to_emit(
const char *output_port_name = nullptr
)

Get the CUDA stream to be emitted on the specified output port.

This method retrieves the CUDA stream that has been configured for emission on the given output port via set_cuda_stream. The base implementation returns std::nullopt. Derived classes should override this method to provide actual stream information.

Returns: Optional CUDA stream. std::nullopt if no stream is configured.

Parameters

output_port_name
const char *Defaults to nullptr

The name of the output port.

emit_impl

virtual void holoscan::OutputContext::emit_impl(
std::any data,
const char *name = nullptr,
OutputType out_type = OutputType::kAny,
const int64_t acq_timestamp = -1,
bool omit_data_logging = false,
bool skip_stream_propagation = false,
bool is_new_entity = false
)

The implementation of the emit method.

Depending on the type of the data, this method wraps the data with a message and sends it to the output port with the given name.

Parameters

data
std::any

The data to send.

name
const char *Defaults to nullptr

The name of the output port.

out_type
OutputTypeDefaults to OutputType::kAny

The type of the message data.

acq_timestamp
const int64_tDefaults to -1

The timestamp to publish in the output message. The default value of -1 does not publish a timestamp.

omit_data_logging
boolDefaults to false

If true, data will not be logged via the DataLogger interface.

skip_stream_propagation
boolDefaults to false

If true, skip propagating CUDA stream to entity memory buffers. Used when the caller has already set the stream on tensors.

is_new_entity
boolDefaults to false

If true, the entity was just created (not forwarded), allowing optimizations like skipping checks for existing components.

log_tensor

bool holoscan::OutputContext::log_tensor(
const std::shared_ptr<Tensor> &tensor,
const std::string &unique_id,
const char *port_name
)

log_tensormap

bool holoscan::OutputContext::log_tensormap(
const holoscan::TensorMap &tensor_map,
const std::string &unique_id,
const char *port_name
)

Types

OutputType

The output data type.

NameValueDescription
kGXFEntityThe message data to send is a GXF entity.
kAnyThe message data to send is a std::any.

Member variables

NameTypeDescription
execution_context_ExecutionContext *The execution context that is associated with.
op_Operator *The operator that this context is associated with.
outputs_std::unordered_map< std::string, std::shared_ptr< IOSpec > > &The outputs.
cuda_object_handler_std::shared_ptr< CudaObjectHandler >