|
CIRCT 24.0.0git
|
A ChannelPort which sends data to the accelerator. More...
#include <Ports.h>


Classes | |
| struct | DefaultAsyncWriter |
Public Types | |
| using | DataFetchCallback |
| Callback mode data source. | |
| using | WriteErrorCallback = std::function<void(std::exception_ptr)> |
| Callback mode error handler. | |
Public Member Functions | |
| ~WriteChannelPort () override | |
| Stops the default callback mode implementation, if in use, dropping a fetched message which hasn't been completely written. | |
| virtual void | disconnect () override |
| Disconnect the port. | |
| virtual bool | isConnected () const override |
| virtual void | connect (const ConnectOptions &options={}) override |
| Connect in sync mode. Throws if connected in callback mode. | |
| void | write (const MessageData &data) |
| A very basic blocking write API. | |
| void | write (std::unique_ptr< SegmentedMessageData > msg) |
| Write a multi-segment message. | |
| bool | tryWrite (const MessageData &data) |
| A basic non-blocking write API. | |
| bool | tryWrite (std::unique_ptr< SegmentedMessageData > &msg) |
| Non-blocking write of a multi-segment message. | |
| bool | flush () |
| Flush any buffered data. | |
| virtual void | connect (DataFetchCallback fetch, WriteErrorCallback onError, const ConnectOptions &options={}) |
| Connect in callback mode. | |
| virtual void | notifyAsyncDataAvailable () |
| Notify the port that the DataFetchCallback has data again. | |
| ChannelPort (const Type *type) | |
| Public Member Functions inherited from esi::ChannelPort | |
| ChannelPort (const Type *type) | |
| virtual | ~ChannelPort () |
| bool | poll () |
| Poll for incoming data. | |
| size_t | getFrameSizeBytes () const |
| Get the size of each frame in bytes. | |
| const Type * | getType () const |
| const WindowType * | getWindowType () const |
| If this port carries a windowed type, return the original WindowType (whose intoType is what getType() returns). | |
Static Public Attributes | |
| static constexpr size_t | kMaxAsyncWritesPerCall = 64 |
| The most messages the default callback mode implementation writes per call to notifyAsyncDataAvailable() or poll(). | |
Protected Types | |
| enum | Mode { Disconnected , Sync , Callback } |
| Indicates the current mode of the channel. More... | |
Protected Member Functions | |
| bool | pollImpl () final |
| If the default callback mode implementation is in use, write fetched data (see connect()). | |
| virtual bool | pollWriteImpl () |
| Backend-specific polling. | |
| void | connectCallbackMode (const ConnectOptions &options) |
| For backends which override connect(DataFetchCallback, ...): check that the port is disconnected, set up message translation, call connectImpl(), and enter callback mode, in which the sync write APIs throw. | |
| const MessageData & | maybePadEmptyMessage (const MessageData &data) |
| Pad an empty payload with a single zero byte if the port type has a zero-width logical width (e.g. | |
| virtual void | writeImpl (const MessageData &)=0 |
| Implementation for write(). Subclasses must implement this. | |
| virtual bool | tryWriteImpl (const MessageData &data)=0 |
| Implementation for tryWrite(). Subclasses must implement this. | |
| virtual void | writeSegmentedImpl (std::unique_ptr< SegmentedMessageData > msg) |
| Implementation for the segmented write(). | |
| virtual bool | tryWriteSegmentedImpl (std::unique_ptr< SegmentedMessageData > &msg) |
| Implementation for the segmented tryWrite(), also used by the default callback mode implementation. | |
| std::vector< MessageData > | getMessageFrames (const MessageData &data) |
| Break a message into its frames. | |
| void | translateOutgoing (const MessageData &data) |
| Translate outgoing data if the port type is a window type. | |
| Protected Member Functions inherited from esi::ChannelPort | |
| virtual void | connectImpl (const ConnectOptions &options) |
| Called by all connect methods to let backends initiate the underlying connections. | |
Protected Attributes | |
| std::atomic< Mode > | mode = Mode::Disconnected |
| bool | translateMessages = false |
| Whether to translate outgoing data if the port type is a window type. | |
| std::vector< MessageData > | translationBuffer |
| If tryWrite cannot write all the messages of a windowed type at once, it stores them here and writes them out one by one on subsequent calls. | |
| size_t | translationBufferIdx = 0 |
| Index of the next message to write in translationBuffer. | |
| Protected Attributes inherited from esi::ChannelPort | |
| const Type * | type |
| std::unique_ptr< TranslationInfo > | translationInfo |
Static Protected Attributes | |
| static const MessageData | transportPad0Bytes = MessageData({0}) |
| Backing storage for maybePadEmptyMessage return so that the returned reference outlives the call. | |
Private Member Functions | |
| void | requireNotCallbackMode (const char *method) const |
| Throw if the port is connected in callback mode. | |
| void | connectCommon (const ConnectOptions &options) |
| Set up translation and call connectImpl(). Shared by both modes. | |
| void | writeInternal (const MessageData &data) |
| write(), tryWrite() and flush() without the mode check. | |
| bool | tryWriteInternal (const MessageData &data) |
| bool | flushInternal () |
| bool | hasPendingFrames () const |
| Whether a windowed message's frames are partially written. | |
| void | discardPendingFrames () |
| Drop the frames of a partially written (windowed) message. | |
| std::shared_ptr< DefaultAsyncWriter > | getAsyncWriter () |
| Return the default implementation's current connection, if any. | |
| void | stopAsyncWriter () |
| Detach and stop the default implementation's current connection, if any. | |
Private Attributes | |
| std::mutex | asyncWriterMutex |
| Protects asyncWriter. | |
| std::shared_ptr< DefaultAsyncWriter > | asyncWriter |
| The default implementation's current connection, if in use. | |
A ChannelPort which sends data to the accelerator.
It has two modes: Sync and Callback, which cannot be used at the same time. The mode is set at connect() time. To change the mode, disconnect() and then connect() again.
Callback mode data source.
Called whenever the port can accept a message: returns the next message to write (ownership is transferred), or nullptr if none is available yet. After returning nullptr (or throwing), it isn't called again until notifyAsyncDataAvailable() is. Must return immediately.
| using esi::WriteChannelPort::WriteErrorCallback = std::function<void(std::exception_ptr)> |
Callback mode error handler.
Called with the exception thrown while writing a message returned by the DataFetchCallback (which is then dropped), or by the DataFetchCallback itself.
|
protected |
|
override |
Stops the default callback mode implementation, if in use, dropping a fetched message which hasn't been completely written.
Definition at line 847 of file Ports.cpp.
References stopAsyncWriter().
| ChannelPort::ChannelPort | ( | const Type * | type | ) |
Definition at line 121 of file Ports.cpp.
References esi::ChannelPort::type.
|
overridevirtual |
Connect in sync mode. Throws if connected in callback mode.
Implements esi::ChannelPort.
Reimplemented in esi::UnknownWriteChannelPort.
Definition at line 867 of file Ports.cpp.
References Callback, connectCommon(), mode, and Sync.
Referenced by bandwidthWriteTest(), channelTest(), dmaWriteTest(), runLoopbackI8(), streamingAddTest(), streamingAddTranslatedTest(), and sv2cCosimserverEpRegister().
|
virtual |
Connect in callback mode.
Throws if already connected, if either callback is null, or if frames of a windowed message written in sync mode haven't all been sent yet (reconnect in sync mode and flush() them first). Must not be called concurrently with connect() or disconnect() on another thread. Callbacks may be invoked from any thread, but never concurrently with each other, and must return quickly. Exceptions thrown by fetch are reported to onError; those thrown by onError propagate out of whichever call invoked it (e.g. notifyAsyncDataAvailable() or poll(); the accelerator's service thread logs them). They may call notifyAsyncDataAvailable() and disconnect() (and then connect()), but must not wait on another thread which uses the port.
Default implementation: the port calls fetch and writes what it returns with tryWriteSegmentedImpl() (so backends which override that for scatter-gather support receive messages as-is), until fetch returns nullptr or the backend backpressures. It does so from notifyAsyncDataAvailable() and from poll() (e.g. called by the accelerator's service thread via AcceleratorServiceThread::addPoll()), which also retries the one fetched message the backend didn't accept. Each call writes at most kMaxAsyncWritesPerCall messages, so that it returns even if fetch always has data; poll() writes the rest. fetch is first called by the first of either after connecting. Its state is only allocated while it is in use.
Backends which override this must also override notifyAsyncDataAvailable(), uphold the callback contracts, and call connectCallbackMode().
Reimplemented in esi::UnknownWriteChannelPort.
Definition at line 886 of file Ports.cpp.
References asyncWriter, asyncWriterMutex, and connectCallbackMode().
|
protected |
For backends which override connect(DataFetchCallback, ...): check that the port is disconnected, set up message translation, call connectImpl(), and enter callback mode, in which the sync write APIs throw.
Definition at line 874 of file Ports.cpp.
References Callback, connectCommon(), Disconnected, hasPendingFrames(), and mode.
Referenced by connect().
|
private |
Set up translation and call connectImpl(). Shared by both modes.
Definition at line 857 of file Ports.cpp.
References esi::ChannelPort::connectImpl(), esi::ChannelPort::ConnectOptions::translateMessage, translateMessages, and esi::ChannelPort::translationInfo.
Referenced by connect(), and connectCallbackMode().
|
private |
Drop the frames of a partially written (windowed) message.
Definition at line 994 of file Ports.cpp.
References translationBuffer, and translationBufferIdx.
Referenced by disconnect(), and flushInternal().
|
overridevirtual |
Disconnect the port.
In callback mode, drops a fetched message which hasn't been completely written. With the default callback mode implementation, blocks until another thread writing fetched data (including the callbacks it invokes) has stopped. Must not be called concurrently with connect() or disconnect() on another thread. Subclasses which override this must call it.
Implements esi::ChannelPort.
Definition at line 902 of file Ports.cpp.
References Callback, discardPendingFrames(), Disconnected, mode, and stopAsyncWriter().
Referenced by bandwidthWriteTest(), channelTest(), dmaWriteTest(), streamingAddTest(), and streamingAddTranslatedTest().
|
inline |
Flush any buffered data.
Returns true if all data was flushed.
If translateMessages is false, calling flush() will immediately return true and perform no action, as there is no buffered data to flush.
Definition at line 401 of file Ports.h.
References flushInternal(), and requireNotCallbackMode().
|
private |
Definition at line 983 of file Ports.cpp.
References discardPendingFrames(), maybePadEmptyMessage(), translationBuffer, translationBufferIdx, and tryWriteImpl().
Referenced by flush(), and tryWriteInternal().
|
private |
Return the default implementation's current connection, if any.
Definition at line 913 of file Ports.cpp.
References asyncWriter, and asyncWriterMutex.
Referenced by notifyAsyncDataAvailable(), and pollImpl().
|
protected |
Break a message into its frames.
Definition at line 683 of file Ports.cpp.
References assert(), esi::ChannelPort::getFrameSizeBytes(), and esi::ChannelPort::type.
|
inlineprivate |
Whether a windowed message's frames are partially written.
Definition at line 542 of file Ports.h.
References translationBuffer, and translationBufferIdx.
Referenced by connectCallbackMode().
|
inlineoverridevirtual |
Implements esi::ChannelPort.
Definition at line 342 of file Ports.h.
References Disconnected, and mode.
|
inlineprotected |
Pad an empty payload with a single zero byte if the port type has a zero-width logical width (e.g.
VoidType). Transports (DMA engines, cosim, etc.) require every message to carry at least one byte.
Definition at line 477 of file Ports.h.
References transportPad0Bytes, and esi::ChannelPort::type.
Referenced by flushInternal(), tryWriteInternal(), and writeInternal().
|
virtual |
Notify the port that the DataFetchCallback has data again.
Throws if the port isn't connected in callback mode.
Default implementation: unless another thread is already writing fetched data (which then picks the new data up), writes it immediately on the calling thread.
Definition at line 938 of file Ports.cpp.
References Callback, getAsyncWriter(), and mode.
Referenced by channelTest().
|
finalprotectedvirtual |
If the default callback mode implementation is in use, write fetched data (see connect()).
Then, if the port is still connected (a callback may disconnect it), call pollWriteImpl() and return its result.
Reimplemented from esi::ChannelPort.
Definition at line 928 of file Ports.cpp.
References Callback, getAsyncWriter(), esi::ChannelPort::isConnected(), mode, and pollWriteImpl().
|
inlineprotectedvirtual |
Backend-specific polling.
Write port backends override this rather than pollImpl(). Returns true if any progress was made.
Definition at line 463 of file Ports.h.
References pollWriteImpl().
Referenced by pollImpl(), and pollWriteImpl().
|
private |
Throw if the port is connected in callback mode.
Definition at line 849 of file Ports.cpp.
References Callback, and mode.
Referenced by flush(), tryWrite(), tryWrite(), write(), and write().
|
private |
Detach and stop the default implementation's current connection, if any.
Definition at line 918 of file Ports.cpp.
References asyncWriter, and asyncWriterMutex.
Referenced by disconnect(), and ~WriteChannelPort().
|
protected |
Translate outgoing data if the port type is a window type.
Append the new message 'chunks' to translationBuffer.
Definition at line 589 of file Ports.cpp.
References assert(), translationBuffer, and esi::ChannelPort::translationInfo.
Referenced by tryWriteInternal(), and writeInternal().
|
inline |
A basic non-blocking write API.
Returns true if any of the data was sent; false means none of it was, and the write should be retried. If the data type is a window a 'true' return does not indicate that the message has been completely written. The 'flush' method can be used to check that the entire buffer has been written. It is invalid for backends to always return false (i.e. backends must eventually ensure that writes may succeed).
Definition at line 379 of file Ports.h.
References requireNotCallbackMode(), and tryWriteInternal().
Referenced by dmaWriteTest(), NB_MODULE(), and sv2cCosimserverEpTryPut().
|
inline |
Non-blocking write of a multi-segment message.
On success, takes ownership of msg (leaving it null) and returns true. On failure, returns false and leaves msg untouched so the caller can retry with the same message. Same completion semantics as tryWrite(const MessageData &). See tryWriteSegmentedImpl().
Definition at line 389 of file Ports.h.
References requireNotCallbackMode(), and tryWriteSegmentedImpl().
|
protectedpure virtual |
Implementation for tryWrite(). Subclasses must implement this.
Implemented in esi::UnknownWriteChannelPort.
Referenced by flushInternal(), and tryWriteInternal().
|
private |
Definition at line 961 of file Ports.cpp.
References assert(), flushInternal(), maybePadEmptyMessage(), translateMessages, translateOutgoing(), translationBuffer, translationBufferIdx, and tryWriteImpl().
Referenced by tryWrite(), and tryWriteSegmentedImpl().
|
inlineprotectedvirtual |
Implementation for the segmented tryWrite(), also used by the default callback mode implementation.
msg is non-null. On success, takes ownership of it (leaving it null) and returns true; otherwise leaves it untouched. The default flattens the message (without copying if it is already a MessageData) and writes it like tryWrite(const MessageData &). Backends override for scatter-gather / chunked-DMA support.
Definition at line 509 of file Ports.h.
References tryWriteInternal().
Referenced by tryWrite().
|
inline |
A very basic blocking write API.
Will likely change for performance reasons.
Definition at line 356 of file Ports.h.
References requireNotCallbackMode(), and writeInternal().
Referenced by bandwidthWriteTest(), NB_MODULE(), runLoopbackI8(), streamingAddTest(), and streamingAddTranslatedTest().
|
inline |
Write a multi-segment message.
Takes ownership so the backend can hold the message across partial writes / async completions. See writeSegmentedImpl().
Definition at line 364 of file Ports.h.
References requireNotCallbackMode(), and writeSegmentedImpl().
|
protectedpure virtual |
Implementation for write(). Subclasses must implement this.
Implemented in esi::UnknownWriteChannelPort.
Referenced by writeInternal().
|
private |
write(), tryWrite() and flush() without the mode check.
Definition at line 948 of file Ports.cpp.
References assert(), maybePadEmptyMessage(), translateMessages, translateOutgoing(), translationBuffer, and writeImpl().
Referenced by write(), and writeSegmentedImpl().
|
inlineprotectedvirtual |
Implementation for the segmented write().
msg is non-null. The default flattens the message (without copying if it is already a MessageData) and writes it like write(const MessageData &). Backends override for scatter-gather / chunked-DMA support.
Definition at line 497 of file Ports.h.
References writeInternal().
Referenced by write().
|
private |
The default implementation's current connection, if in use.
Definition at line 563 of file Ports.h.
Referenced by connect(), getAsyncWriter(), and stopAsyncWriter().
|
private |
Protects asyncWriter.
Definition at line 561 of file Ports.h.
Referenced by connect(), getAsyncWriter(), and stopAsyncWriter().
|
staticconstexpr |
The most messages the default callback mode implementation writes per call to notifyAsyncDataAvailable() or poll().
Definition at line 445 of file Ports.h.
Referenced by esi::WriteChannelPort::DefaultAsyncWriter::drain().
|
protected |
Definition at line 467 of file Ports.h.
Referenced by connect(), connectCallbackMode(), disconnect(), isConnected(), notifyAsyncDataAvailable(), pollImpl(), and requireNotCallbackMode().
|
protected |
Whether to translate outgoing data if the port type is a window type.
Set by the connect() method.
Definition at line 522 of file Ports.h.
Referenced by connectCommon(), tryWriteInternal(), and writeInternal().
|
protected |
If tryWrite cannot write all the messages of a windowed type at once, it stores them here and writes them out one by one on subsequent calls.
Definition at line 525 of file Ports.h.
Referenced by discardPendingFrames(), flushInternal(), hasPendingFrames(), translateOutgoing(), tryWriteInternal(), and writeInternal().
|
protected |
Index of the next message to write in translationBuffer.
Definition at line 527 of file Ports.h.
Referenced by discardPendingFrames(), flushInternal(), hasPendingFrames(), and tryWriteInternal().
|
inlinestaticprotected |
Backing storage for maybePadEmptyMessage return so that the returned reference outlives the call.
Definition at line 485 of file Ports.h.
Referenced by maybePadEmptyMessage().