31#ifndef DW_FRAMEWORK_SIMPLENODE_HPP_
32#define DW_FRAMEWORK_SIMPLENODE_HPP_
34#include <dw/core/base/Types.h>
36#include <dwcgf/Types.hpp>
46#include <dw/core/container/VectorFixed.hpp>
47#include <dw/core/container/BaseString.hpp>
64template <
typename NodeT>
87 throw Exception(DW_NOT_IMPLEMENTED,
"SimpleNode::reset() not implemented");
98 throw Exception(DW_NOT_IMPLEMENTED,
"SimpleNode::setInputChannel() not implemented");
109 throw Exception(DW_NOT_IMPLEMENTED,
"SimpleNode::validate() not implemented");
116 dwStatus
validate(
const char* direction,
const PortCollectionDescriptor& collection,
const dw::core::HeapHashMap<
size_t, std::shared_ptr<PortBase>>& ports,
size_t indexOffset = 0);
123 dwStatus
getPasses(VectorFixed<
Pass*>& passList, dwProcessorType processorType, dwProcessType processType) override;
131 template <typename Func, typename PortList>
134 for (
auto elem : portList)
140 template <
typename Func>
150 const char* nodeName = nullptr;
151 this->getName(&nodeName);
152 throw Exception(DW_BAD_CAST,
"SimpleNode: ports are wrong class, node ", nodeName);
157 template <
typename Func>
167 const char* nodeName = nullptr;
168 this->getName(&nodeName);
169 throw Exception(DW_BAD_CAST,
"SimpleNode: ports are wrong class node ", nodeName);
174 template <
typename ModuleHandle_t>
177 dwModuleHandle_t moduleHandle;
179 if (DW_NULL_HANDLE == handle)
181 return DW_INVALID_ARGUMENT;
185 if (DW_SUCCESS != ret)
196 dwStatus
getModuleHandle(dwModuleHandle_t* moduleHandle,
void* handle, dwContextHandle_t context);
200 throw Exception(DW_NOT_IMPLEMENTED,
"SimpleNode::createSetupPass() not implemented");
205 throw Exception(DW_NOT_IMPLEMENTED,
"SimpleNode::createTeardownPass() not implemented");
230 template <
typename PassFunctionT,
typename... Args>
231 std::unique_ptr<PassImpl<PassFunctionT>>
make_pass(PassFunctionT func, Args&&... args)
233 return std::make_unique<PassImpl<PassFunctionT>>(*
this, func, std::forward<Args>(args)...);
244 typename NodeT,
size_t PassIndex,
typename PassFunctionT>
247 if (!isValidPass<NodeT>(PassIndex))
249 throw Exception(DW_INVALID_ARGUMENT,
"registerPass called with an invalid pass id: ", PassIndex);
251 if (m_passList.size() == 0 || m_passList.size() - 1 < PassIndex)
253 m_passList.resize(PassIndex + 1);
254 m_passOwnershipList.resize(PassIndex + 1);
256 if (m_passList[PassIndex] !=
nullptr)
258 throw Exception(DW_INVALID_ARGUMENT,
"registerPass called with a pass id which has been added before: ", PassIndex);
260 dwProcessorType processorType = passProcessorType<NodeT, PassIndex>();
261 dwProcessType processType = determineProcessType(processorType);
262 if (dlaEngine ==
nullptr)
264 m_passList[PassIndex] = std::make_unique<PassImpl<PassFunctionT>>(*
this, func, processorType, processType, -1, -1, -1);
266 else if (processorType == DW_PROCESSOR_TYPE_DLA_0)
268 m_passList[PassIndex] = std::make_unique<PassImpl<PassFunctionT>>(*
this, func, processorType, processType, -1, -1, -1, dlaEngine);
272 throw Exception(DW_INVALID_ARGUMENT,
"registerPass called with a pass which has dlaEngine but not a DLA pass: ", PassIndex);
274 m_passOwnershipList[PassIndex] =
true;
325 inline dwProcessType determineProcessType(dwProcessorType processorType)
327 if (processorType == DW_PROCESSOR_TYPE_GPU ||
328 processorType == DW_PROCESSOR_TYPE_DLA_0)
330 return DW_PROCESS_TYPE_ASYNC;
332 return DW_PROCESS_TYPE_SYNC;
335 dwStatus updateHealthSignalFromModule();
337 uint16_t getInstanceIdFromName()
const;
339 VectorFixed<std::unique_ptr<Pass>> m_passList;
341 VectorFixed<bool> m_passOwnershipList;
342 FixedString<MAX_NAME_LEN> m_name{};
343 bool m_setupTeardownCreated =
false;
349 dwModuleHandle_t m_object{};
350 uint32_t m_iterationCount{};
354 template <
typename TPort,
typename... Args>
357 auto port = std::make_unique<TPort>(std::forward<Args>(args)...);
366 template <
typename NodeT,
size_t PortIndex,
typename... Args>
369 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
370 auto port = std::make_shared<ManagedPortInput<DataType>>(std::forward<Args>(args)...);
373 throw Exception(DW_INVALID_ARGUMENT,
"Input port with the following id registered multiple times: ", PortIndex);
382 template <
typename NodeT,
size_t PortIndex,
typename... Args>
385 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
386 constexpr size_t descriptor_index = descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>();
387 constexpr size_t arraySize = descriptorPortSize<NodeT, PortDirection::INPUT, descriptor_index>();
388 for (
size_t i = 0; i < arraySize; ++i)
390 auto port = std::make_shared<ManagedPortInput<DataType>>(std::forward<Args>(args)...);
393 throw Exception(DW_INVALID_ARGUMENT,
"Input port with the following id registered multiple times: ", PortIndex + i);
403 template <
typename NodeT,
size_t PortIndex,
typename... Args>
406 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
407 auto port = std::make_shared<ManagedPortOutput<DataType>>(std::forward<Args>(args)...);
410 throw Exception(DW_INVALID_ARGUMENT,
"Output port with the following id registered multiple times: ", PortIndex);
419 template <
typename NodeT,
size_t PortIndex,
typename... Args>
422 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
423 constexpr size_t descriptor_index = descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>();
424 constexpr size_t arraySize = descriptorPortSize<NodeT, PortDirection::OUTPUT, descriptor_index>();
425 for (
size_t i = 0; i < arraySize; ++i)
427 auto port = std::make_shared<ManagedPortOutput<DataType>>(std::forward<Args>(args)...);
430 throw Exception(DW_INVALID_ARGUMENT,
"Output port with the following id registered multiple times: ", PortIndex + i);
437 template <
typename NodeT,
size_t PortIndex>
442 static_assert(!isArray,
"Input port is an array, must pass an array index");
445 throw Exception(DW_INVALID_ARGUMENT,
"Input port with the following id not registered: ", PortIndex);
447 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
449 using ReturnType = std::shared_ptr<PointerType>;
450 ReturnType port = std::dynamic_pointer_cast<PointerType>(
m_inputPorts[PortIndex]);
453 throw Exception(DW_BAD_CAST,
"Failed to cast the following input port to its declared type: ", PortIndex);
459 template <
typename NodeT,
size_t PortIndex>
462 constexpr bool isArray = descriptorPortArray<NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>();
463 static_assert(isArray,
"Input port is not an array, must not pass an array index");
464 constexpr size_t arraySize = descriptorPortSize<NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>();
465 if (arrayIndex >= arraySize)
467 throw Exception(DW_INVALID_ARGUMENT,
"The array index is out of bound: ", arrayIndex);
471 throw Exception(DW_INVALID_ARGUMENT,
"Input port with the following id not registered: ", PortIndex + arrayIndex);
473 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
475 using ReturnType = std::shared_ptr<PointerType>;
476 ReturnType port = std::dynamic_pointer_cast<PointerType>(
m_inputPorts[PortIndex + arrayIndex]);
479 throw Exception(DW_BAD_CAST,
"Failed to cast the following input port to its declared type: ", PortIndex + arrayIndex);
485 template <
typename NodeT,
size_t PortIndex>
490 static_assert(!isArray,
"Output port is an array, must pass an array index");
493 throw Exception(DW_INVALID_ARGUMENT,
"Output port with the following id not registered: ", PortIndex);
495 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
497 using ReturnType = std::shared_ptr<PointerType>;
498 ReturnType port = std::dynamic_pointer_cast<PointerType>(
m_outputPorts[PortIndex]);
501 throw Exception(DW_BAD_CAST,
"Failed to cast the following output port to its declared type: ", PortIndex);
507 template <
typename NodeT,
size_t PortIndex>
510 constexpr bool isArray = descriptorPortArray<NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>();
511 static_assert(isArray,
"Output port is not an array, must not pass an array index");
512 constexpr size_t arraySize = descriptorPortSize<NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>();
513 if (arrayIndex >= arraySize)
515 throw Exception(DW_INVALID_ARGUMENT,
"The array index is out of bound: ", arrayIndex);
519 throw Exception(DW_INVALID_ARGUMENT,
"Output port with the following id not registered: ", PortIndex + arrayIndex);
521 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
523 using ReturnType = std::shared_ptr<PointerType>;
524 ReturnType port = std::dynamic_pointer_cast<PointerType>(
m_outputPorts[PortIndex + arrayIndex]);
527 throw Exception(DW_BAD_CAST,
"Failed to cast the following output port to its declared type: ", PortIndex + arrayIndex);
577 static_cast<void>(state);
591 static constexpr char LOG_TAG[] =
"SimpleSensorNode";
595 throw Exception(DW_NOT_IMPLEMENTED,
"SimpleSensorNode::start() not implemented");
600 throw Exception(DW_NOT_IMPLEMENTED,
"SimpleSensorNode::stop() not implemented");
605 throw Exception(DW_NOT_IMPLEMENTED,
"SimpleSensorNode::isVirtual() not implemented");
610 throw Exception(DW_NOT_IMPLEMENTED,
"SimpleSensorNode::setDataEventReadCallback() not implemented");
615 throw Exception(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.
void resetPorts()
Default implementation to reset ports managed by the base class.
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 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.
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 setName(const char *name) final
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)
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 runPassByID(uint8_t passID) override
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