NVIDIA Holoscan SDK v0.5.1
Holoscan v0.5.1

Class InputContext

Derived Type

class InputContext

Class to hold the input context.

This class provides the interface to receive the input data from the operator.

Subclassed by holoscan::gxf::GXFInputContext

Public Functions

inline InputContext(Operator *op, std::unordered_map<std::string, std::unique_ptr<IOSpec>> &inputs)

Construct a new InputContext object.

Parameters
  • op – The pointer to the operator that this context is associated with.

  • inputs – The references to the map of the input specs.

inline explicit InputContext(Operator *op)

Construct a new InputContext object.

inputs for the InputContext will be set to op->spec()->inputs()

Parameters

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

inline Operator *op() const

Return the operator that this context is associated with.

Returns

The pointer to the operator.

inline std::unordered_map<std::string, std::unique_ptr<IOSpec>> &inputs() const

Return the reference to the map of the input specs.

Returns

The reference to the map of the input specs.

template<typename DataT, typename = std::enable_if_t<!holoscan::is_vector_v<DataT> && !holoscan::is_one_of_v<DataT, holoscan::gxf::Entity, std::any>>>
inline std::shared_ptr<DataT> receive(const char *name = nullptr)

Receive a shared pointer to the message data from the input port with the given name.

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

If the input port with the given name and type (DataT) is available, it will return a shared pointer to the data (std::shared_ptr<DataT>) from the input port. Otherwise, it will return a null shared pointer.

Example:

Copy
Copied!
            

class PingRxOp : public holoscan::ops::GXFOperator { public: HOLOSCAN_OPERATOR_FORWARD_ARGS_SUPER(PingRxOp, holoscan::ops::GXFOperator) PingRxOp() = default; void setup(OperatorSpec& spec) override { spec.input<ValueData>("in"); } void compute(InputContext& op_input, OutputContext&, ExecutionContext&) override { // The type of `value` is `std::shared_ptr ` auto value = op_input.receive<ValueData>("in"); if (value) { HOLOSCAN_LOG_INFO("Message received (value: {})", value->data()); } } };

Template Parameters

DataT – The type of the data to receive.

Parameters

name – The name of the input port to receive the data from.

Returns

The shared pointer to the data.

template<typename DataT, typename = std::enable_if_t<holoscan::is_one_of_v<DataT, holoscan::gxf::Entity>>>
inline DataT receive(const char *name)

Receive message data (GXF Entity) from the input port with the given name.

This method is for interoperability with the GXF Codelet.

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

If the input port with the given name and the type (nvidia::gxf::Entity) is available, it will return the message data (holoscan::gxf::Entity) from the input port. Otherwise, it will return an empty Entity or throw an exception if it fails to parse the message data.

Example:

Copy
Copied!
            

class PingRxOp : public holoscan::ops::GXFOperator { public: HOLOSCAN_OPERATOR_FORWARD_ARGS_SUPER(PingRxOp, holoscan::ops::GXFOperator) PingRxOp() = default; void setup(OperatorSpec& spec) override { spec.input<holoscan::gxf::Entity>("in"); } void compute(InputContext& op_input, OutputContext&, ExecutionContext&) override { // The type of `in_entity` is 'holoscan::gxf::Entity'. auto in_entity = op_input.receive<holoscan::gxf::Entity>("in"); if (in_entity) { // Process with `in_entity`. // ... } } };

Template Parameters

DataT – The type of the data to receive. It should be holoscan::gxf::Entity.

Parameters

name – The name of the input port to receive the data from.

Throws

std::runtime_error – if it fails to parse the message data from the input port with the given type (DataT).

Returns

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

template<typename DataT, typename = std::enable_if_t<holoscan::is_one_of_v<DataT, std::any>>>
inline std::any receive(const char *name)

Receive message data (std::any) from the input port with the given name.

This method is for interoperability with arbitrary data types.

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

If the input port with the given name is available, it will return the message data (either holoscan::gxf::Entity or std::shared_ptr<T>) from the input port. Otherwise, it will return a std::any object with the std::nullptr.

Example:

Copy
Copied!
            

class PingRxOp : public holoscan::ops::GXFOperator { public: HOLOSCAN_OPERATOR_FORWARD_ARGS_SUPER(PingRxOp, holoscan::ops::GXFOperator) PingRxOp() = default; void setup(OperatorSpec& spec) override { spec.input<std::any>("in"); } void compute(InputContext& op_input, OutputContext&, ExecutionContext&) override { // The type of `in_any` is 'std::any'. auto in_any = op_input.receive<std::any>("in"); auto& in_any_type = in_any.type(); if (in_any_type == typeid(holoscan::gxf::Entity)) { auto in_entity = std::any_cast<holoscan::gxf::Entity>(in_any); // Process with `in_entity`. // ... } else if (in_any_type == typeid(std::shared_ptr<ValueData>)) { auto in_message = std::any_cast<std::shared_ptr<ValueData>>(in_any); // Process with `in_message`. // ... } else if (in_any_type == typeid(nullptr_t)) { // No message is available. } else { HOLOSCAN_LOG_ERROR("Invalid message type: {}", in_any_type.name()); return; } } };

Template Parameters

DataT – The type of the data to receive. It should be holoscan::gxf::Entity.

Parameters

name – The name of the input port to receive the data from.

Returns

The std::any object (holoscan::gxf::Entity, std::shared_ptr<T>, or std::nullptr).

template<typename DataT, typename = std::enable_if_t<holoscan::is_vector_v<DataT> && !holoscan::is_one_of_v<typename holoscan::type_info<DataT>::element_type, holoscan::gxf::Entity, std::any>>>
inline std::vector<std::shared_ptr<typename holoscan::type_info<DataT>::element_type>> receive(const char *name)

Receive a vector of the shared pointers to the message data from the receivers with the given name.

If the parameter (of type std::vector<IOSpec*>) with the given name is available, it will return a vector of the shared pointers to the data (std::vector<std::shared_ptr<DataT>>) from the input port. Otherwise, it will return an empty vector. The vector can have empty shared pointers if the data of the corresponding input port is not available.

Example:

Copy
Copied!
            

class ProcessTensorOp : public holoscan::ops::GXFOperator { public: HOLOSCAN_OPERATOR_FORWARD_ARGS_SUPER(ProcessTensorOp, holoscan::ops::GXFOperator) ProcessTensorOp() = default; void setup(OperatorSpec& spec) override { spec.param(receivers_, "receivers", "Input Receivers", "List of input receivers.", {}); } void compute(InputContext& op_input, OutputContext& op_output, ExecutionContext&) override { auto in_messages = op_input.receive<std::vector<ValueData>("receivers"); HOLOSCAN_LOG_INFO("Received {} messages", in_messages.size()); // The type of `in_message` is 'std::vector >'. auto& in_message = in_messages[0]; if (in_message) { // Process with `in_message`. // ... } } private: Parameter<std::vector<IOSpec*>> receivers_; };

Template Parameters

DataT – The type of the data to receive.

Parameters

name – The name of the receivers whose parameter type is ‘std::vector<IOSpec*>’.

Returns

The vector of the shared pointers to the data.

template<typename DataT, typename = std::enable_if_t<holoscan::is_vector_v<DataT> && holoscan::is_one_of_v<typename holoscan::type_info<DataT>::element_type, holoscan::gxf::Entity>>>
inline std::vector<holoscan::gxf::Entity> receive(const char *name)

Receive a vector of entities from the receivers with the given name.

This method is for interoperability with the GXF Codelet and is only available when the DataT is std::vector<holoscan::gxf::Entity>.

If the parameter (of type std::vector<IOSpec*>) with the given name is available, it will return a vector of entities (std::vector<holoscan::gxf::Entity>). Otherwise, it will return an empty vector. The vector can have empty entities if the data of the corresponding input port is not available.

Example:

Copy
Copied!
            

class ProcessTensorOp : public holoscan::ops::GXFOperator { public: HOLOSCAN_OPERATOR_FORWARD_ARGS_SUPER(ProcessTensorOp, holoscan::ops::GXFOperator) ProcessTensorOp() = default; void setup(OperatorSpec& spec) override { spec.param(receivers_, "receivers", "Input Receivers", "List of input receivers.", {}); } void compute(InputContext& op_input, OutputContext& op_output, ExecutionContext&) override { auto in_messages = op_input.receive<std::vector<holoscan::gxf::Entity>>("receivers"); HOLOSCAN_LOG_INFO("Received {} messages", in_messages.size()); // in_message's type is 'holoscan::gxf::Entity'. auto& in_message = in_messages[0]; if (in_message) { // Process with 'in_message' here. } } private: Parameter<std::vector<IOSpec*>> receivers_; };

Template Parameters

DataT – The type of the data to receive.

Parameters

name – The name of the receivers whose parameter type is ‘std::vector<IOSpec*>’.

Returns

The vector of entities (std::vector<holoscan::gxf::Entity>).

template<typename DataT, typename = std::enable_if_t<holoscan::is_vector_v<DataT> && holoscan::is_one_of_v<typename holoscan::type_info<DataT>::element_type, std::any>>>
inline std::vector<std::any> receive(const char *name)

Receive a vector of entities from the receivers with the given name.

If the parameter (of type std::vector<IOSpec*>) with the given name is available, it will return a vector of entities (std::vector<std::any>) from the receivers. Otherwise, it will return an empty vector.

Example:

Copy
Copied!
            

class ProcessTensorOp : public holoscan::ops::GXFOperator { public: HOLOSCAN_OPERATOR_FORWARD_ARGS_SUPER(ProcessTensorOp, holoscan::ops::GXFOperator) ProcessTensorOp() = default; void setup(OperatorSpec& spec) override { spec.param(receivers_, "receivers", "Input Receivers", "List of input receivers.", {}); } void compute(InputContext& op_input, OutputContext& op_output, ExecutionContext&) override { auto in_any_vector = op_input.receive<std::vector<std::any>>("receivers"); HOLOSCAN_LOG_INFO("Received {} messages", in_any_vector.size()); if (in_any_vector.empty()) { HOLOSCAN_LOG_ERROR("No input messages received."); return; } // in_any's type is 'std::any'. auto& in_any = in_any_vector[0]; auto& in_any_type = in_any.type(); if (in_any_type == typeid(holoscan::gxf::Entity)) { auto in_entity = std::any_cast<holoscan::gxf::Entity>(in_any); // Process with `in_entity`. // ... } else if (in_any_type == typeid(std::shared_ptr<ValueData>)) { auto in_message = std::any_cast<std::shared_ptr<ValueData>>(in_any); // Process with `in_message`. // ... } else if (in_any_type == typeid(nullptr_t)) { // No message is available. } else { HOLOSCAN_LOG_ERROR("Invalid message type: {}", in_any_type.name()); return; } } private: Parameter<std::vector<IOSpec*>> receivers_; };

Template Parameters

DataT – The type of the data to receive.

Parameters

name – The name of the receivers whose parameter type is ‘std::vector<IOSpec*>’.

Returns

The vector of entities (std::vector<std::any>).

Protected Functions

inline virtual std::any receive_impl(const char *name = nullptr, bool no_error_message = false)

The implementation of the receive method.

Depending on the type of the data, this method receives a message from the input port with the given name.

Parameters
  • name – The name of the input port.

  • no_error_message – Whether to print an error message when the input port is not found.

Returns

The data received from the input port.

Protected Attributes

Operator *op_ = nullptr

The operator that this context is associated with.

std::unordered_map<std::string, std::unique_ptr<IOSpec>> &inputs_

The inputs.

© Copyright 2022-2023, NVIDIA. Last updated on Jul 28, 2023.