31#ifndef DW_FRAMEWORK_SIMPLENODE_HPP_
32#define DW_FRAMEWORK_SIMPLENODE_HPP_
34#include <dw/core/base/Types.h>
45#include <dwshared/dwfoundation/dw/core/container/BaseString.hpp>
46#include <dwshared/dwfoundation/dw/core/container/HashContainer.hpp>
47#include <dwshared/dwfoundation/dw/core/container/VectorFixed.hpp>
48#include <dwshared/dwfoundation/dw/core/language/Function.hpp>
62 size_t maxInputPortCount_,
63 size_t maxOutputPortCount_,
75template <
typename NodeT>
80 portSize<NodeT, PortDirection::INPUT>(),
81 portSize<NodeT, PortDirection::OUTPUT>(),
94 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleNode::reset() not implemented");
117 throw ExceptionWithStatus(DW_NOT_IMPLEMENTED,
"SimpleNode::validate() not implemented");
147 template <typename Func, typename PortList>
150 for (
auto& elem : portList)
156 template <
typename Func>
160 if (
nullptr == elem.second.get())
162 const char* nodeName{nullptr};
163 this->getName(&nodeName);
164 throw ExceptionWithStatus(DW_NOT_INITIALIZED,
"SimpleNode: input port not initialized, node ", nodeName,
", port id ", elem.first);
170 template <
typename Func>
173 iteratePorts(m_outputPorts, [&func,
this](
decltype(m_outputPorts)::TElement& elem) {
174 if (
nullptr == elem.second.get())
176 const char* nodeName{nullptr};
177 this->getName(&nodeName);
178 throw ExceptionWithStatus(DW_NOT_INITIALIZED,
"SimpleNode: output port not initialized, node ", nodeName,
", port id ", elem.first);
184 template <
typename ModuleHandle_t>
187 dwModuleHandle_t moduleHandle;
189 if (DW_NULL_HANDLE == handle)
191 return DW_INVALID_ARGUMENT;
194 dwStatus ret{getModuleHandle(&moduleHandle, handle, context)};
195 if (DW_SUCCESS != ret)
200 return setObjectHandle(moduleHandle);
212 dwStatus
getModuleHandle(dwModuleHandle_t* moduleHandle,
void* handle, dwContextHandle_t context);
222 typename NodeT,
size_t PassIndex,
typename PassFunctionT>
223 void registerPass(PassFunctionT func, std::initializer_list<std::pair<dwStatus, uint32_t>>
const& returnMapping = {})
229 if (0U == m_passList.size() || m_passList.size() - 1U < PassIndex)
232 m_passList.resize(PassIndex + 1U);
235 if (
nullptr != m_passList[PassIndex])
237 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"registerPass called with a pass id which has been added before: ", PassIndex);
239 dwProcessorType processorType{passProcessorType<NodeT, PassIndex>()};
241 m_passList[PassIndex] = std::make_unique<PassImpl<PassFunctionT>>(*
this, passName<NodeT, PassIndex>(), func, processorType, returnMapping);
249 typename NodeT,
size_t PassIndex,
typename PassFunctionT>
250 void registerPass(PassFunctionT func, cudaStream_t
const cudaStream, std::initializer_list<std::pair<dwStatus, uint32_t>>
const& returnMapping = {})
252 static_assert(DW_PROCESSOR_TYPE_GPU == passProcessorType<NodeT, PassIndex>(),
"The processor type of a pass with a cuda stream must be GPU");
253 registerPass<NodeT, PassIndex>(func, returnMapping);
254 m_passList[PassIndex]->m_cudaStream =
cudaStream;
288 return m_healthSignal;
292 VectorFixed<std::unique_ptr<Pass>> m_passList;
293 FixedString<MAX_NAME_LEN> m_name;
294 bool m_setupTeardownCreated;
300 dwModuleHandle_t m_object;
301 uint32_t m_iterationCount{};
302 uint32_t m_nodePeriod{};
309 template <
typename NodeT,
size_t PortIndex,
typename... Args>
312 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
313 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
314 std::shared_ptr<ManagedPortInput<DataType>> port{std::make_shared<ManagedPortInput<DataType>>(portName<NodeT, PortDirection::INPUT, PortIndex>(), std::forward<Args>(args)...)};
315 if (m_inputPorts.find(PortIndex) != m_inputPorts.end())
317 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Input port with the following id registered multiple times: ", PortIndex);
319 m_inputPorts[PortIndex] = port;
329 template <
typename NodeT,
size_t PortIndex,
typename... Args>
332 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
333 constexpr size_t arraySize = descriptorPortSize<NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>();
334 for (
size_t i = 0; i < arraySize; ++i)
336 initInputArrayPort<NodeT, PortIndex>(i, std::forward<Args>(args)...);
346 template <
typename NodeT,
size_t PortIndex,
typename... Args>
349 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
350 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
351 if (arrayIndex >=
descriptorPortSize<NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>())
353 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Invalid array index ", arrayIndex,
" for array input port ", PortIndex);
355 std::shared_ptr<ManagedPortInput<DataType>> port{std::make_shared<ManagedPortInput<DataType>>(portName<NodeT, PortDirection::INPUT, PortIndex>(arrayIndex), std::forward<Args>(args)...)};
356 if (m_inputPorts.find(PortIndex + arrayIndex) != m_inputPorts.end())
358 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Input port with the following id registered multiple times: ", PortIndex + arrayIndex);
360 m_inputPorts[PortIndex + arrayIndex] = port;
367 template <
typename NodeT,
size_t PortIndex,
typename... Args>
370 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
371 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
372 std::shared_ptr<ManagedPortOutput<DataType>> port{std::make_shared<ManagedPortOutput<DataType>>(portName<NodeT, PortDirection::OUTPUT, PortIndex>(), std::forward<Args>(args)...)};
373 if (m_outputPorts.find(PortIndex) != m_outputPorts.end())
375 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Output port with the following id registered multiple times: ", PortIndex);
377 m_outputPorts[PortIndex] = port;
387 template <
typename NodeT,
size_t PortIndex,
typename... Args>
390 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
391 constexpr size_t arraySize{descriptorPortSize<NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>()};
392 for (
size_t i{0U}; i < arraySize; ++i)
394 initOutputArrayPort<NodeT, PortIndex>(i, std::forward<Args>(args)...);
404 template <
typename NodeT,
size_t PortIndex,
typename... Args>
407 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
408 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
409 if (arrayIndex >=
descriptorPortSize<NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>())
411 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Invalid array index ", arrayIndex,
" for array output port ", PortIndex);
413 std::shared_ptr<ManagedPortOutput<DataType>> port{std::make_shared<ManagedPortOutput<DataType>>(portName<NodeT, PortDirection::OUTPUT, PortIndex>(arrayIndex), std::forward<Args>(args)...)};
414 if (m_outputPorts.find(PortIndex + arrayIndex) != m_outputPorts.end())
416 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Output port with the following id registered multiple times: ", PortIndex + arrayIndex);
418 m_outputPorts[PortIndex + arrayIndex] = port;
422 template <
typename NodeT,
size_t PortIndex>
425 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
427 NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>();
428 static_assert(!isArray,
"Input port is an array, must pass an array index");
429 if (m_inputPorts.find(PortIndex) == m_inputPorts.end())
431 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Input port with the following id not registered: ", PortIndex);
433 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
435 using ReturnType = std::shared_ptr<PointerType>;
436 ReturnType port = std::dynamic_pointer_cast<PointerType>(m_inputPorts[PortIndex]);
439 throw ExceptionWithStatus(DW_BAD_CAST,
"Failed to cast the following input port to its declared type: ", PortIndex);
445 template <
typename NodeT,
size_t PortIndex>
448 static_assert(PortIndex < portSize<NodeT, PortDirection::INPUT>(),
"Invalid port index");
449 constexpr bool isArray = descriptorPortArray<NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>();
450 static_assert(isArray,
"Input port is not an array, must not pass an array index");
451 constexpr size_t arraySize = descriptorPortSize<NodeT, PortDirection::INPUT, descriptorIndex<NodeT, PortDirection::INPUT, PortIndex>()>();
452 if (arrayIndex >= arraySize)
454 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"The array index is out of bound: ", arrayIndex);
456 if (m_inputPorts.find(PortIndex + arrayIndex) == m_inputPorts.end())
458 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Input port with the following id not registered: ", PortIndex + arrayIndex);
460 using DataType =
decltype(portType<NodeT, PortDirection::INPUT, PortIndex>());
462 using ReturnType = std::shared_ptr<PointerType>;
463 ReturnType port = std::dynamic_pointer_cast<PointerType>(m_inputPorts[PortIndex + arrayIndex]);
466 throw ExceptionWithStatus(DW_BAD_CAST,
"Failed to cast the following input port to its declared type: ", PortIndex + arrayIndex);
472 template <
typename NodeT,
size_t PortIndex>
475 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
477 NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>();
478 static_assert(!isArray,
"Output port is an array, must pass an array index");
479 if (m_outputPorts.find(PortIndex) == m_outputPorts.end())
481 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Output port with the following id not registered: ", PortIndex);
483 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
485 using ReturnType = std::shared_ptr<PointerType>;
486 ReturnType port = std::dynamic_pointer_cast<PointerType>(m_outputPorts[PortIndex]);
489 throw ExceptionWithStatus(DW_BAD_CAST,
"Failed to cast the following output port to its declared type: ", PortIndex);
495 template <
typename NodeT,
size_t PortIndex>
498 static_assert(PortIndex - portSize<NodeT, PortDirection::INPUT>() < portSize<NodeT, PortDirection::OUTPUT>(),
"Invalid port index");
499 constexpr bool isArray = descriptorPortArray<NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>();
500 static_assert(isArray,
"Output port is not an array, must not pass an array index");
501 constexpr size_t arraySize = descriptorPortSize<NodeT, PortDirection::OUTPUT, descriptorIndex<NodeT, PortDirection::OUTPUT, PortIndex>()>();
502 if (arrayIndex >= arraySize)
504 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"The array index is out of bound: ", arrayIndex);
506 if (m_outputPorts.find(PortIndex + arrayIndex) == m_outputPorts.end())
508 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Output port with the following id not registered: ", PortIndex + arrayIndex);
510 using DataType =
decltype(portType<NodeT, PortDirection::OUTPUT, PortIndex>());
512 using ReturnType = std::shared_ptr<PointerType>;
513 ReturnType port = std::dynamic_pointer_cast<PointerType>(m_outputPorts[PortIndex + arrayIndex]);
516 throw ExceptionWithStatus(DW_BAD_CAST,
"Failed to cast the following output port to its declared type: ", PortIndex + arrayIndex);
561 return m_outputPorts;
572 static_cast<void>(state);
577 dw::core::HeapHashMap<size_t, std::shared_ptr<ManagedPortInputBase>>
m_inputPorts;
578 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
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 getPass(Pass **pass, size_t index) override
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
dwStatus setName(const char *name) final
const dw::core::HeapHashMap< size_t, std::shared_ptr< ManagedPortOutputBase > > & getRegisteredOutputPorts() const
uint32_t getIterationCount() const
dwStatus getInputChannel(const size_t portID, ChannelObject *&channel) const override
Gets the input channel associated with the input port.
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
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
dwStatus collectHealthSignals(dwGraphHealthSignal *&healthSignal, bool updateFromModule=false) 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 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()
virtual dwStatus setObjectHandle(dwModuleHandle_t handle)
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
dwStatus collectErrorSignals(dwGraphErrorSignal *&errorSignal, bool updateFromModule=true) override
ManagedPortOutput< decltype(portType< NodeT, PortDirection::OUTPUT, PortIndex >())> & getOutputPort()
Get a previously initialized non-array ManagedPortOutput.
constexpr bool descriptorPortArray()
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_)