Class InputContext
Defined in File io_context.hpp
Derived Type
public holoscan::gxf::GXFInputContext
(Class GXFInputContext)
-
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
Construct a new InputContext object.
- Parameters
execution_context – The pointer to the execution context.
op – The pointer to the operator that this context is associated with.
inputs – The references to the map of the input specs.
-
inline InputContext(ExecutionContext *execution_context, Operator *op)
Construct a new InputContext object.
inputs for the InputContext will be set to op->spec()->inputs()
- Parameters
execution_context – The pointer to GXF execution runtime
op – The pointer to the operator that this context is associated with.
-
virtual ~InputContext() = default
-
inline ExecutionContext *execution_context() const
Get pointer to the execution context.
- Returns
The pointer to the execution context.
-
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::shared_ptr<IOSpec>> &inputs() const
Return the reference to the map of the input specs.
- Returns
The reference to the map of the input specs.
-
inline bool empty(const char *name = nullptr)
Return whether the input port has any data.
For parameters with std::vector<IOSpec*> type, if all the inputs are empty, it will return true. Otherwise, it will return false.
- Parameters
name – The name of the input port to check.
- Returns
True, if it has no data, otherwise false.
-
template<typename DataT>
inline holoscan::expected<DataT, holoscan::RuntimeError> receive(const char *name = nullptr) Receive a message 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 the data from the input port. Otherwise, it will return an object of the holoscan::unexpected class which will contain the error message. The error message can be access by calling thewhat()
method of the holoscan::unexpected object.It throws an invalid argument exception if the operator attempts to receive non-vector data (
op_input.receive<T>()
) from an input port with a queue size ofIOSpec::kAnySize
.Example:
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::shared_ptr<ValueData>>("in"); } void compute(InputContext& op_input, [[maybe_unused]] OutputContext& op_output, [[maybe_unused]] ExecutionContext& context) override { auto value = op_input.receive<std::shared_ptr<ValueData>>("in"); if (value.has_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 received data.
-
inline std::shared_ptr<gxf::CudaObjectHandler> 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 publicreceive_cuda_stream
andreceive_cuda_streams
methods.
Set the CUDA stream/event handler used by this input context.
-
virtual cudaStream_t receive_cuda_stream(const char *input_port_name = nullptr, bool allocate = true, bool sync_to_default = false) = 0
Synchronize any streams found on this port to the operator’s internal CUDA stream.
The
receive
method must have been called forinput_port_name
prior to calling this method in order for any received streams to be found. This method will callcudaSetDevice
to make the device corresponding to the operator’s internal stream current.If no
CudaStreamPool
resource was available on the operator, the operator will not have an internal stream. In that case, the first stream received on the input port will be returned and any additional streams on the input will have been synchronized to it. If no streams were found on the input and noCudaStreamPool
resource was available,cudaStreamDefault
is returned.- Parameters
input_port_name – The name of the input port. Can be omitted if the operator only has a single input port.
allocate – Whether to allocate a new stream if no stream is found. If false or the operator does not have a
cuda_stream_pool
parameter set, returns cudaStreamDefault.sync_to_default – Whether to also synchronize any received streams to the default stream.
- Returns
The operator’s internal CUDA stream, when possible. Returns
cudaStreamDefault
instead if no CudaStreamPool resource was available and no stream was found on the input port.
-
virtual std::vector<std::optional<cudaStream_t>> receive_cuda_streams(const char *input_port_name = nullptr) = 0
Retrieve the CUDA streams found an input port.
This method is intended for advanced use cases where it is the users responsibility to manage any necessary stream synchronization. In most cases, it is recommended to use
receive_cuda_stream
instead.- Parameters
input_port_name – The name of the input port. Can be omitted if the operator only has a single input port.
- Returns
Vector of (optional) cudaStream_t. The length of the vector will match the number of messages on the input port. Any messages that do not contain a stream will have value of std::nullopt.
Protected Types
-
enum class InputType
The input data type.
Values:
-
enumerator kGXFEntity
The message data to receive is a GXF entity.
-
enumerator kAny
The message data to receive is a std::any.
-
enumerator kGXFEntity
Protected Functions
-
inline virtual bool empty_impl(const char *name = nullptr)
The implementation of the
empty
method.- Parameters
name – The name of the input port
- Returns
True if the input port is empty or by default. Otherwise, false.
-
inline virtual std::any receive_impl(const char *name = nullptr, InputType in_type = InputType::kAny, 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.
-
inline bool is_valid_param_type(const ArgType &arg_type)
-
template<typename DataT>
inline bool fill_input_vector_from_params(ParameterWrapper ¶m_wrapper, const char *name, DataT &input_vector, InputType in_type, std::string &error_message)
-
template<typename DataT>
inline bool fill_input_vector_from_inputs(const char *name, DataT &input_vector, InputType in_type, std::string &error_message)
-
inline bool populate_tensor_map(const holoscan::gxf::Entity &gxf_entity, holoscan::TensorMap &tensor_map)
-
template<typename DataT>
inline bool process_received_value(std::any &value, const std::type_info &value_type, const char *name, int index, DataT &input_vector, std::string &error_message)
-
template<typename DataT>
inline void handle_null_value(DataT &input_vector)
-
template<typename DataT>
inline bool handle_bad_any_cast(std::any &value, const char *name, int index, DataT &input_vector, std::string &error_message)
-
template<typename DataT>
inline holoscan::expected<DataT, holoscan::RuntimeError> receive_single_value(const char *name, InputType in_type)
-
inline holoscan::RuntimeError create_receive_error(const char *name, const char *message)
-
template<typename DataT>
inline holoscan::expected<DataT, holoscan::RuntimeError> handle_null_value()
Protected Attributes
-
ExecutionContext *execution_context_ = nullptr
The execution context that is associated with.
-
Operator *op_ = nullptr
The operator that this context is associated with.
-
std::unordered_map<std::string, std::shared_ptr<IOSpec>> &inputs_
The inputs.