31#ifndef DW_FRAMEWORK_SIMPLENODE_HPP_
32#define DW_FRAMEWORK_SIMPLENODE_HPP_
34#include <dw/core/base/Types.h>
46#include <dw/core/container/VectorFixed.hpp>
47#include <dw/core/container/BaseString.hpp>
64template <
typename NodeT>
87 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleNode::reset() not implemented");
98 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleNode::setInputChannel() not implemented");
115 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleNode::validate() not implemented");
122 dwStatus
validate(
const char* direction,
const PortCollectionDescriptor& collection,
const dw::core::HeapHashMap<
size_t, std::shared_ptr<PortBase>>& ports,
size_t indexOffset = 0);
129 dwStatus
getPasses(VectorFixed<
Pass*>& passList, dwProcessorType processorType, dwProcessType processType) override;
137 template <typename Func, typename PortList>
140 for (
auto elem : portList)
146 template <
typename Func>
156 const char* nodeName = nullptr;
157 this->getName(&nodeName);
158 throw ExceptionWithStatus(DW_BAD_CAST,
"SimpleNode: ports are wrong class, node ", nodeName);
163 template <
typename Func>
173 const char* nodeName = nullptr;
174 this->getName(&nodeName);
175 throw ExceptionWithStatus(DW_BAD_CAST,
"SimpleNode: ports are wrong class node ", nodeName);
180 template <
typename ModuleHandle_t>
183 dwModuleHandle_t moduleHandle;
185 if (DW_NULL_HANDLE == handle)
187 return DW_INVALID_ARGUMENT;
191 if (DW_SUCCESS != ret)
208 dwStatus
getModuleHandle(dwModuleHandle_t* moduleHandle,
void* handle, dwContextHandle_t context);
212 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleNode::createSetupPass() not implemented");
217 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleNode::createTeardownPass() not implemented");
242 template <
typename PassFunctionT,
typename... Args>
243 std::unique_ptr<PassImpl<PassFunctionT>>
make_pass(PassFunctionT func, Args&&... args)
245 return std::make_unique<PassImpl<PassFunctionT>>(*
this, func, std::forward<Args>(args)...);
256 typename NodeT,
size_t PassIndex,
typename PassFunctionT>
259 if (!isValidPass<NodeT>(PassIndex))
261 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"registerPass called with an invalid pass id: ", PassIndex);
263 if (m_passList.size() == 0 || m_passList.size() - 1 < PassIndex)
265 m_passList.resize(PassIndex + 1);
266 m_passOwnershipList.resize(PassIndex + 1);
268 if (m_passList[PassIndex] !=
nullptr)
270 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"registerPass called with a pass id which has been added before: ", PassIndex);
272 dwProcessorType processorType = passProcessorType<NodeT, PassIndex>();
273 dwProcessType processType = determineProcessType(processorType);
274 if (dlaEngine ==
nullptr)
276 m_passList[PassIndex] = std::make_unique<PassImpl<PassFunctionT>>(*
this, func, processorType, processType, -1, -1, -1);
278 else if (processorType == DW_PROCESSOR_TYPE_DLA_0)
280 m_passList[PassIndex] = std::make_unique<PassImpl<PassFunctionT>>(*
this, func, processorType, processType, -1, -1, -1, dlaEngine);
284 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"registerPass called with a pass which has dlaEngine but not a DLA pass: ", PassIndex);
286 m_passOwnershipList[PassIndex] =
true;
337 inline dwProcessType determineProcessType(dwProcessorType processorType)
339 if (processorType == DW_PROCESSOR_TYPE_GPU ||
340 processorType == DW_PROCESSOR_TYPE_DLA_0)
342 return DW_PROCESS_TYPE_ASYNC;
344 return DW_PROCESS_TYPE_SYNC;
347 dwStatus updateHealthSignalFromModule();
349 VectorFixed<std::unique_ptr<Pass>> m_passList;
351 VectorFixed<bool> m_passOwnershipList;
352 FixedString<MAX_NAME_LEN> m_name{};
353 bool m_setupTeardownCreated =
false;
359 dwModuleHandle_t m_object{};
360 uint32_t m_iterationCount{};
361 uint32_t m_nodePeriod;
365 template <
typename TPort,
typename... Args>
368 auto port = std::make_unique<TPort>(std::forward<Args>(args)...);
377 template <
typename NodeT,
size_t PortIndex,
typename... Args>
380 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
381 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
382 auto port = std::make_shared<ManagedPortInput<DataType>>(std::forward<Args>(args)...);
385 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Input port with the following id registered multiple times: ", PortIndex);
394 template <
typename NodeT,
size_t PortIndex,
typename... Args>
397 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
398 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
399 constexpr size_t descriptor_index = descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>();
400 constexpr size_t arraySize = descriptorPortSize<NodeT, PortDirection::INPUT, descriptor_index>();
401 for (
size_t i = 0; i < arraySize; ++i)
403 auto port = std::make_shared<ManagedPortInput<DataType>>(std::forward<Args>(args)...);
406 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Input port with the following id registered multiple times: ", PortIndex + i);
416 template <
typename NodeT,
size_t PortIndex,
typename... Args>
419 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
420 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
421 auto port = std::make_shared<ManagedPortOutput<DataType>>(std::forward<Args>(args)...);
424 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Output port with the following id registered multiple times: ", PortIndex);
433 template <
typename NodeT,
size_t PortIndex,
typename... Args>
436 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
437 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
438 constexpr size_t descriptor_index = descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>();
439 constexpr size_t arraySize = descriptorPortSize<NodeT, PortDirection::OUTPUT, descriptor_index>();
440 for (
size_t i = 0; i < arraySize; ++i)
442 auto port = std::make_shared<ManagedPortOutput<DataType>>(std::forward<Args>(args)...);
445 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Output port with the following id registered multiple times: ", PortIndex + i);
452 template <
typename NodeT,
size_t PortIndex>
455 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
458 static_assert(!isArray,
"Input port is an array, must pass an array index");
461 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Input port with the following id not registered: ", PortIndex);
463 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
465 using ReturnType = std::shared_ptr<PointerType>;
466 ReturnType port = std::dynamic_pointer_cast<PointerType>(
m_inputPorts[PortIndex]);
469 throw ExceptionWithStatus(DW_BAD_CAST,
"Failed to cast the following input port to its declared type: ", PortIndex);
475 template <
typename NodeT,
size_t PortIndex>
478 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
479 constexpr bool isArray = descriptorPortArray<NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>();
480 static_assert(isArray,
"Input port is not an array, must not pass an array index");
481 constexpr size_t arraySize = descriptorPortSize<NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>();
482 if (arrayIndex >= arraySize)
484 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"The array index is out of bound: ", arrayIndex);
488 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Input port with the following id not registered: ", PortIndex + arrayIndex);
490 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
492 using ReturnType = std::shared_ptr<PointerType>;
493 ReturnType port = std::dynamic_pointer_cast<PointerType>(
m_inputPorts[PortIndex + arrayIndex]);
496 throw ExceptionWithStatus(DW_BAD_CAST,
"Failed to cast the following input port to its declared type: ", PortIndex + arrayIndex);
502 template <
typename NodeT,
size_t PortIndex>
505 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
508 static_assert(!isArray,
"Output port is an array, must pass an array index");
511 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Output port with the following id not registered: ", PortIndex);
513 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
515 using ReturnType = std::shared_ptr<PointerType>;
516 ReturnType port = std::dynamic_pointer_cast<PointerType>(
m_outputPorts[PortIndex]);
519 throw ExceptionWithStatus(DW_BAD_CAST,
"Failed to cast the following output port to its declared type: ", PortIndex);
525 template <
typename NodeT,
size_t PortIndex>
528 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
529 constexpr bool isArray = descriptorPortArray<NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>();
530 static_assert(isArray,
"Output port is not an array, must not pass an array index");
531 constexpr size_t arraySize = descriptorPortSize<NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>();
532 if (arrayIndex >= arraySize)
534 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"The array index is out of bound: ", arrayIndex);
538 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Output port with the following id not registered: ", PortIndex + arrayIndex);
540 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
542 using ReturnType = std::shared_ptr<PointerType>;
543 ReturnType port = std::dynamic_pointer_cast<PointerType>(
m_outputPorts[PortIndex + arrayIndex]);
546 throw ExceptionWithStatus(DW_BAD_CAST,
"Failed to cast the following output port to its declared type: ", PortIndex + arrayIndex);
602 static_cast<void>(state);
616 static constexpr char LOG_TAG[] =
"SimpleSensorNode";
620 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleSensorNode::start() not implemented");
625 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleSensorNode::stop() not implemented");
630 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleSensorNode::isVirtual() not implemented");
635 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleSensorNode::setDataEventReadCallback() not implemented");
640 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleSensorNode::setDataEventWriteCallback() not implemented");
Basic error signal that gets reported only when there is an error.
Basic health signal that describes the health status of the graph.
dw::core::Function< bool(DataEvent &)> DataEventReadCallback
dw::core::Function< void(DataEvent)> DataEventWriteCallback
static constexpr uint32_t MAX_PORT_COUNT
static constexpr uint32_t MAX_PASS_COUNT
Pass is a runnable describes the metadata of a pass.
ManagedPortOutput< decltype(portType< NodeT, PortDirection::OUTPUT, PortIndex >())> & getOutputPort(size_t arrayIndex)
Get one specific ManagedPortOutput from a previously initialized output array port.
dwStatus getInputChannel(const uint8_t portID, ChannelObject *&channel) const override
Gets the input channel associated with the input port.
dwStatus reset() override
void iterateManagedInputPorts(Func func)
dwStatus validate(const char *direction, const PortCollectionDescriptor &collection, const dw::core::HeapHashMap< size_t, std::shared_ptr< PortBase > > &ports, size_t indexOffset=0)
Helper function used by dw::framework::SimpleNodeT::validate.
dwStatus setNodePeriod(uint32_t period) override
dwStatus validate() override
dwStatus getErrorSignal(dwGraphErrorSignal *&errorSignal) override
static constexpr const char * PASS_TEARDOWN_NAME
dwStatus setInputChannel(ChannelObject *, uint8_t, dwSerializationType) override
void initInputPort(Args &&... args)
Initialize a ManagedPortInput which will be owned by the base class and can be retrieved using getInp...
dwStatus updateHealthSignal(const dwGraphHealthSignal &signal)
Adds the provided Health Signal to the Health Signal Array. If the array is full, the new signal will...
dw::core::HeapHashMap< size_t, std::shared_ptr< PortBase > > m_inputPorts
const dw::core::HeapHashMap< size_t, std::shared_ptr< PortBase > > & getRegisteredInputPorts() const
dwStatus copyModuleHealthSignals(dwHealthSignal &outSignal)
Copy health signals from the module over to the node and stores in outSignal.
uint32_t getNodePeriod() const
void initInputArrayPort(Args &&... args)
Initialize an array of ManagedPortInput which will be owned by the base class and can be retrieved us...
void registerPass(PassFunctionT func, NvMediaDla *dlaEngine=nullptr)
Register a pass function with the node base class.
SimpleNode(NodeAllocationParams params)
Constructor which tailors the preallocated size of the internal collections for ports and passes to t...
dwStatus setInputChannel(ChannelObject *channel, uint8_t portID) override
Associate an input port with a channel instances.
size_t getPassCount() const noexcept override
dwStatus getPass(Pass **pass, uint8_t index) override
dwStatus setName(const char *name) final
dwStatus getOutputChannel(const uint8_t portID, ChannelObject *&channel) const override
Gets the output channel associated with the output port.
uint32_t getIterationCount() const
dwStatus reportCurrentErrorSignal(dwGraphErrorSignal &signal) override
A function that allows user override to update error signal It is automatically called by dwFramework...
dwStatus setModuleHandle(ModuleHandle_t handle, dwContextHandle_t context)
dw::core::HeapHashMap< size_t, std::shared_ptr< PortBase > > m_outputPorts
virtual std::unique_ptr< Pass > createTeardownPass()
const dw::core::HeapHashMap< size_t, std::shared_ptr< PortBase > > & getRegisteredOutputPorts() const
void iterateManagedOutputPorts(Func func)
void resetPorts() override
Default implementation to reset ports managed by the base class.
dwStatus setState(const char *state) override
dwStatus getName(const char **name) override
virtual std::unique_ptr< Pass > createSetupPass()
dwStatus runPass(size_t passIndex) override
std::unique_ptr< TPort > make_port(Args &&... args)
std::unique_ptr< PassImpl< PassFunctionT > > make_pass(PassFunctionT func, Args &&... args)
dwStatus getHealthSignal(dwGraphHealthSignal *&healthSignals, bool updateFromModule=false) override
dwStatus reportCurrentHealthSignal(dwGraphHealthSignal &signal) override
A function that allows user override to update health signal It is automatically called by dwFramewor...
dwStatus clearHealthSignal()
Clears the current Health Signals from the Health Signal Array.
dwStatus getPasses(VectorFixed< Pass * > &passList) override
dwStatus getModuleHandle(dwModuleHandle_t *moduleHandle, void *handle, dwContextHandle_t context)
dwStatus setup()
Default implementation of the setup pass.
ManagedPortInput< decltype(portType< NodeT, PortDirection::INPUT, PortIndex >())> & getInputPort()
Get a previously initialized non-array ManagedPortInput.
void initOutputPort(Args &&... args)
Initialize a ManagedPortOutput which will be owned by the base class and can be retrieved using getOu...
void iteratePorts(PortList &portList, Func func)
dwStatus getInputPort(const uint8_t portID, dw::framework::PortBase *&port) const override
Gets the input port associated with the input port Id.
dwStatus getOutputPort(const uint8_t portID, dw::framework::PortBase *&port) const override
Gets the output port associated with the output port Id.
virtual dwStatus setObjectHandle(dwModuleHandle_t handle)
ManagedPortInput< decltype(portType< NodeT, PortDirection::INPUT, PortIndex >())> & getInputPort(size_t arrayIndex)
Get one specific ManagedPortInput from a previously initialized input array port.
static constexpr const char * PASS_SETUP_NAME
dwStatus teardown()
Default implementation of the teardown pass.
dwStatus setIterationCount(uint32_t iterationCount) override
dwStatus setOutputChannel(ChannelObject *channel, uint8_t portID) override
Associate an output port with a channel instances.
std::atomic< bool > m_asyncResetFlag
void initOutputArrayPort(Args &&... args)
Initialize an array of ManagedPortOutput which will be owned by the base class and can be retrieved u...
ManagedPortOutput< decltype(portType< NodeT, PortDirection::OUTPUT, PortIndex >())> & getOutputPort()
Get a previously initialized non-array ManagedPortOutput.
SimpleProcessNode(NodeAllocationParams params)
dwStatus isVirtual(bool *) override
dwStatus setDataEventWriteCallback(DataEventWriteCallback) override
static constexpr char LOG_TAG[]
dwStatus start() override
dwStatus setDataEventReadCallback(DataEventReadCallback) override
constexpr bool descriptorPortArray()
constexpr size_t passIndex(dw::core::StringView identifier)
Get the the pass index for a pass identified by name.
NodeAllocationParams createAllocationParams()
size_t maxOutputPortCount