31#ifndef DW_FRAMEWORK_SIMPLENODE_HPP_
32#define DW_FRAMEWORK_SIMPLENODE_HPP_
34#include <dw/core/base/Types.h>
44#include <dwshared/dwfoundation/dw/core/container/BaseString.hpp>
45#include <dwshared/dwfoundation/dw/core/container/HashContainer.hpp>
46#include <dwshared/dwfoundation/dw/core/container/VectorFixed.hpp>
47#include <dwshared/dwfoundation/dw/core/language/Function.hpp>
61 size_t maxInputPortCount_,
62 size_t maxOutputPortCount_,
74template <
typename NodeT>
79 portSize<NodeT, PortDirection::INPUT>(),
80 portSize<NodeT, PortDirection::OUTPUT>(),
93 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleNode::reset() not implemented");
116 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleNode::validate() not implemented");
146 template <typename Func, typename PortList>
149 for (
auto& elem : portList)
155 template <
typename Func>
159 if (
nullptr == elem.second.get())
161 const char* nodeName{nullptr};
162 static_cast<void>(this->getName(&nodeName));
163 throw ExceptionWithStatus(DW_NOT_INITIALIZED,
"SimpleNode: input port not initialized, node ", nodeName,
", port id ", elem.first);
169 template <
typename Func>
172 iteratePorts(m_outputPorts, [&func,
this](
decltype(m_outputPorts)::TElement& elem) {
173 if (
nullptr == elem.second.get())
175 const char* nodeName{nullptr};
176 static_cast<void>(this->getName(&nodeName));
177 throw ExceptionWithStatus(DW_NOT_INITIALIZED,
"SimpleNode: output port not initialized, node ", nodeName,
", port id ", elem.first);
183 template <
typename ModuleHandle_t>
186 dwModuleHandle_t moduleHandle;
188 if (DW_NULL_HANDLE == handle)
190 return DW_INVALID_ARGUMENT;
193 dwStatus ret{getModuleHandle(&moduleHandle, handle, context)};
194 if (DW_SUCCESS != ret)
199 return setObjectHandle(moduleHandle);
213 dwStatus
getModuleHandle(dwModuleHandle_t* moduleHandle,
void* handle, dwContextHandle_t context);
223 typename NodeT,
size_t PassIndex,
typename PassFunctionT>
224 void registerPass(PassFunctionT func, std::initializer_list<std::pair<dwStatus, uint32_t>>
const& returnMapping = {})
230 if (0U == m_passList.size() || m_passList.size() - 1U < PassIndex)
233 m_passList.resize(PassIndex + 1U);
236 if (
nullptr != m_passList[PassIndex])
238 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"registerPass called with a pass id which has been added before: ", PassIndex);
240 dwProcessorType processorType{passProcessorType<NodeT, PassIndex>()};
242 m_passList[PassIndex] = std::make_unique<PassImpl<PassFunctionT>>(*
this, passName<NodeT, PassIndex>(), func, processorType, returnMapping);
250 typename NodeT,
size_t PassIndex,
typename PassFunctionT>
251 void registerPass(PassFunctionT func, cudaStream_t
const cudaStream, std::initializer_list<std::pair<dwStatus, uint32_t>>
const& returnMapping = {})
253 static_assert(DW_PROCESSOR_TYPE_GPU == passProcessorType<NodeT, PassIndex>(),
"The processor type of a pass with a cuda stream must be GPU");
254 registerPass<NodeT, PassIndex>(func, returnMapping);
256 m_passList[PassIndex]->m_cudaStream =
cudaStream;
290 return m_healthSignal;
294 VectorFixed<std::unique_ptr<Pass>> m_passList;
295 FixedString<MAX_NAME_LEN> m_name;
296 bool m_setupTeardownCreated;
302 dwModuleHandle_t m_object;
303 uint32_t m_iterationCount{};
304 uint32_t m_nodePeriod{};
311 template <
typename NodeT,
size_t PortIndex,
typename... Args>
314 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
315 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
316 std::shared_ptr<ManagedPortInput<DataType>> port{std::make_shared<ManagedPortInput<DataType>>(portName<NodeT, PortDirection::INPUT, PortIndex>(), std::forward<Args>(args)...)};
317 if (m_inputPorts.find(PortIndex) != m_inputPorts.end())
319 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Input port with the following id registered multiple times: ", PortIndex);
321 m_inputPorts[PortIndex] = port;
331 template <
typename NodeT,
size_t PortIndex,
typename... Args>
334 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
335 constexpr size_t arraySize{descriptorPortSize<NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>()};
336 for (
size_t i{0U}; i < arraySize; ++i)
338 initInputArrayPort<NodeT, PortIndex>(i, std::forward<Args>(args)...);
348 template <
typename NodeT,
size_t PortIndex,
typename... Args>
351 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
352 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
353 if (arrayIndex >=
descriptorPortSize<NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>())
355 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Invalid array index ", arrayIndex,
" for array input port ", PortIndex);
357 std::shared_ptr<ManagedPortInput<DataType>> port{std::make_shared<ManagedPortInput<DataType>>(portName<NodeT, PortDirection::INPUT, PortIndex>(arrayIndex), std::forward<Args>(args)...)};
358 if (m_inputPorts.find(PortIndex + arrayIndex) != m_inputPorts.end())
360 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Input port with the following id registered multiple times: ", PortIndex + arrayIndex);
362 m_inputPorts[PortIndex + arrayIndex] = port;
369 template <
typename NodeT,
size_t PortIndex,
typename... Args>
372 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
373 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
374 std::shared_ptr<ManagedPortOutput<DataType>> port{std::make_shared<ManagedPortOutput<DataType>>(portName<NodeT, PortDirection::OUTPUT, PortIndex>(), std::forward<Args>(args)...)};
375 if (m_outputPorts.find(PortIndex) != m_outputPorts.end())
377 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Output port with the following id registered multiple times: ", PortIndex);
379 m_outputPorts[PortIndex] = port;
389 template <
typename NodeT,
size_t PortIndex,
typename... Args>
392 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
393 constexpr size_t arraySize{descriptorPortSize<NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>()};
394 for (
size_t i{0U}; i < arraySize; ++i)
396 initOutputArrayPort<NodeT, PortIndex>(i, std::forward<Args>(args)...);
406 template <
typename NodeT,
size_t PortIndex,
typename... Args>
409 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
410 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
411 if (arrayIndex >=
descriptorPortSize<NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>())
413 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Invalid array index ", arrayIndex,
" for array output port ", PortIndex);
415 std::shared_ptr<ManagedPortOutput<DataType>> port{std::make_shared<ManagedPortOutput<DataType>>(portName<NodeT, PortDirection::OUTPUT, PortIndex>(arrayIndex), std::forward<Args>(args)...)};
416 if (m_outputPorts.find(PortIndex + arrayIndex) != m_outputPorts.end())
418 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Output port with the following id registered multiple times: ", PortIndex + arrayIndex);
420 m_outputPorts[PortIndex + arrayIndex] = port;
424 template <
typename NodeT,
size_t PortIndex>
427 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
428 constexpr bool isArray{descriptorPortArray<NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>()};
429 static_assert(!isArray,
"Input port is an array, must pass an array index");
430 if (m_inputPorts.find(PortIndex) == m_inputPorts.end())
432 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Input port with the following id not registered: ", PortIndex);
435 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
439 PointerType port{
dynamic_cast<PointerType
>(portBase)};
442 throw ExceptionWithStatus(DW_BAD_CAST,
"Failed to cast the following input port to its declared type: ", PortIndex);
449 template <
typename NodeT,
size_t PortIndex>
452 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
453 constexpr bool isArray{descriptorPortArray<NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>()};
454 static_assert(isArray,
"Input port is not an array, must not pass an array index");
455 constexpr size_t arraySize{descriptorPortSize<NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>()};
456 if (arrayIndex >= arraySize)
458 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"The array index is out of bound: ", arrayIndex);
460 if (m_inputPorts.find(PortIndex + arrayIndex) == m_inputPorts.end())
462 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Input port with the following id not registered: ", PortIndex + arrayIndex);
465 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
469 PointerType port{
dynamic_cast<PointerType
>(portBase)};
472 throw ExceptionWithStatus(DW_BAD_CAST,
"Failed to cast the following input port to its declared type: ", PortIndex + arrayIndex);
479 template <
typename NodeT,
size_t PortIndex>
482 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
483 constexpr bool isArray{descriptorPortArray<NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>()};
484 static_assert(!isArray,
"Output port is an array, must pass an array index");
485 if (m_outputPorts.find(PortIndex) == m_outputPorts.end())
487 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Output port with the following id not registered: ", PortIndex);
490 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
494 PointerType port{
dynamic_cast<PointerType
>(portBase)};
497 throw ExceptionWithStatus(DW_BAD_CAST,
"Failed to cast the following output port to its declared type: ", PortIndex);
504 template <
typename NodeT,
size_t PortIndex>
507 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
508 constexpr bool isArray{descriptorPortArray<NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>()};
509 static_assert(isArray,
"Output port is not an array, must not pass an array index");
510 constexpr size_t arraySize{descriptorPortSize<NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>()};
511 if (arrayIndex >= arraySize)
513 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"The array index is out of bound: ", arrayIndex);
515 if (m_outputPorts.find(PortIndex + arrayIndex) == m_outputPorts.end())
517 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Output port with the following id not registered: ", PortIndex + arrayIndex);
520 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
524 PointerType port{
dynamic_cast<PointerType
>(portBase)};
527 throw ExceptionWithStatus(DW_BAD_CAST,
"Failed to cast the following output port to its declared type: ", PortIndex + arrayIndex);
573 return m_outputPorts;
584 static_cast<void>(state);
589 dw::core::HeapHashMap<size_t, std::shared_ptr<ManagedPortInputBase>>
m_inputPorts;
590 dw::core::HeapHashMap<size_t, std::shared_ptr<ManagedPortOutputBase>>
m_outputPorts;
Basic error signal that gets reported only when there is an error.
Basic health signal that describes the health status of the graph.
Pass is a runnable describes the metadata of a pass.
dw::core::HeapHashMap< size_t, std::shared_ptr< ManagedPortInputBase > > m_inputPorts
ManagedPortOutput< decltype(portType< NodeT, PortDirection::OUTPUT, PortIndex >())> & getOutputPort(size_t arrayIndex)
Get one specific ManagedPortOutput from a previously initialized output array port.
dwStatus getOutputChannel(const size_t portID, ChannelObject *&channel) const override
Gets the output channel associated with the output port.
dwStatus getInputPort(const size_t portID, dw::framework::PortBase *&port) const override
Gets the input port associated with the input port Id.
void initOutputArrayPorts(Args &&... args)
Initialize an array of ManagedPortOutput which will be owned by the base class and can be retrieved u...
dwStatus reset() override
void iterateManagedInputPorts(Func func)
dwStatus setNodePeriod(uint32_t period) override
dwStatus validate() override
dwStatus collectErrorSignals(dwGraphErrorSignal *&errorSignal) 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...
dwStatus addToHealthSignal(uint32_t error, dwTime_t timestamp=0L) override
uint32_t getNodePeriod() const
dwStatus setInputChannel(ChannelObject *channel, size_t portID) override
Associate an input port with a channel instances.
SimpleNode(NodeAllocationParams params)
Constructor which tailors the preallocated size of the internal collections for ports and passes to t...
void initOutputArrayPort(size_t arrayIndex, Args &&... args)
Initialize one ManagedPortOutput of an array which will be owned by the base class and can be retriev...
dwStatus setOutputChannel(ChannelObject *channel, size_t portID) override
Associate an output port with a channel instances.
size_t getPassCount() const noexcept override
dwStatus clearHealthSignal() override
const dw::core::HeapHashMap< size_t, std::shared_ptr< ManagedPortOutputBase > > & getRegisteredOutputPorts() const
uint32_t getIterationCount() const
dwStatus collectHealthSignals(dwGraphHealthSignal *&healthSignal) override
dwStatus getInputChannel(const size_t portID, ChannelObject *&channel) const override
Gets the input channel associated with the input port.
dwStatus setObjectHandle(dwModuleHandle_t handle)
dwStatus setModuleHandle(ModuleHandle_t handle, dwContextHandle_t context)
dwStatus updateCurrentErrorSignal(dwGraphErrorSignal &signal) override
A function that allows user override to update error signal It is automatically called by dwFramework...
const dw::core::HeapHashMap< size_t, std::shared_ptr< ManagedPortInputBase > > & getRegisteredInputPorts() const
void iterateManagedOutputPorts(Func func)
dwStatus getNodeErrorSignal(dwGraphErrorSignal &errorSignal) override
dwStatus setName(const char *name) override
dwModuleHandle_t getObjectHandle() const
void resetPorts() override
Default implementation to reset ports managed by the base class.
dwStatus getNodeHealthSignal(dwGraphHealthSignal &healthSignal) override
dwStatus setState(const char *state) override
dwStatus getOutputPort(const size_t portID, dw::framework::PortBase *&port) const override
Gets the output port associated with the output port Id.
dwStatus getName(const char **name) override
dwStatus runPass(size_t passIndex) override
void registerPass(PassFunctionT func, cudaStream_t const cudaStream, std::initializer_list< std::pair< dwStatus, uint32_t > > const &returnMapping={})
Register a GPU pass function and a cuda stream with the node base class.
void registerPass(PassFunctionT func, std::initializer_list< std::pair< dwStatus, uint32_t > > const &returnMapping={})
Register a pass function with the node base class.
dwStatus getPasses(VectorFixed< Pass * > &passList) override
dwStatus getModuleHandle(dwModuleHandle_t *moduleHandle, void *handle, dwContextHandle_t context)
dwStatus getPass(Pass *&pass, size_t index) override
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)
void initInputArrayPort(size_t arrayIndex, Args &&... args)
Initialize one ManagedPortInput of an array which will be owned by the base class and can be retrieve...
void initInputArrayPorts(Args &&... args)
Initialize an array of ManagedPortInput which will be owned by the base class and can be retrieved us...
dwGraphHealthSignal & getHealthSignal()
dwStatus updateCurrentHealthSignal(dwGraphHealthSignal &signal) override
A function that allows user override to update health signal It is automatically called by dwFramewor...
ManagedPortInput< decltype(portType< NodeT, PortDirection::INPUT, PortIndex >())> & getInputPort(size_t arrayIndex)
Get one specific ManagedPortInput from a previously initialized input array port.
dwStatus teardown()
Default implementation of the teardown pass.
dwStatus validate(const char *direction, const PortCollectionDescriptor &collection, dw::core::Function< bool(size_t)> isPortBound)
Helper function used by dw::framework::SimpleNodeT::validate.
dwStatus clearErrorSignal() override
dwStatus setIterationCount(uint32_t iterationCount) override
dwStatus getModuleErrorSignal(dwErrorSignal &errorSignal) override
dwStatus getModuleHealthSignal(dwHealthSignal &healthSignal) override
std::atomic< bool > m_asyncResetFlag
dwStatus addToErrorSignal(uint32_t error, dwTime_t timestamp=0L) final
dw::core::HeapHashMap< size_t, std::shared_ptr< ManagedPortOutputBase > > m_outputPorts
ManagedPortOutput< decltype(portType< NodeT, PortDirection::OUTPUT, PortIndex >())> & getOutputPort()
Get a previously initialized non-array ManagedPortOutput.
constexpr size_t descriptorPortSize()
constexpr size_t passIndex(dw::core::StringView identifier)
Get the the pass index for a pass identified by name.
NodeAllocationParams createAllocationParams()
NodeAllocationParams()=delete
size_t maxOutputPortCount
NodeAllocationParams(size_t maxInputPortCount_, size_t maxOutputPortCount_, size_t maxPassCount_)