CIRCT 24.0.0git
Loading...
Searching...
No Matches
esi::WriteChannelPort Class Referenceabstract

A ChannelPort which sends data to the accelerator. More...

#include <Ports.h>

Inheritance diagram for esi::WriteChannelPort:
Collaboration diagram for esi::WriteChannelPort:

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.

Detailed Description

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.

Definition at line 315 of file Ports.h.

Member Typedef Documentation

◆ DataFetchCallback

Initial value:
std::function<std::unique_ptr<SegmentedMessageData>()>

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.

Definition at line 327 of file Ports.h.

◆ WriteErrorCallback

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.

Definition at line 333 of file Ports.h.

Member Enumeration Documentation

◆ Mode

Indicates the current mode of the channel.

Enumerator
Disconnected 
Sync 
Callback 

Definition at line 466 of file Ports.h.

Constructor & Destructor Documentation

◆ ~WriteChannelPort()

WriteChannelPort::~WriteChannelPort ( )
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().

Member Function Documentation

◆ ChannelPort()

ChannelPort::ChannelPort ( const Type * type)

Definition at line 121 of file Ports.cpp.

References esi::ChannelPort::type.

◆ connect() [1/2]

void WriteChannelPort::connect ( const ConnectOptions & options = {})
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().

◆ connect() [2/2]

void WriteChannelPort::connect ( DataFetchCallback fetch,
WriteErrorCallback onError,
const ConnectOptions & options = {} )
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().

◆ connectCallbackMode()

void WriteChannelPort::connectCallbackMode ( const ConnectOptions & options)
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().

◆ connectCommon()

void WriteChannelPort::connectCommon ( const ConnectOptions & options)
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().

◆ discardPendingFrames()

void WriteChannelPort::discardPendingFrames ( )
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().

◆ disconnect()

void WriteChannelPort::disconnect ( )
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().

◆ flush()

bool esi::WriteChannelPort::flush ( )
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().

◆ flushInternal()

bool WriteChannelPort::flushInternal ( )
private

◆ getAsyncWriter()

std::shared_ptr< WriteChannelPort::DefaultAsyncWriter > WriteChannelPort::getAsyncWriter ( )
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().

◆ getMessageFrames()

std::vector< MessageData > WriteChannelPort::getMessageFrames ( const MessageData & data)
protected

Break a message into its frames.

Definition at line 683 of file Ports.cpp.

References assert(), esi::ChannelPort::getFrameSizeBytes(), and esi::ChannelPort::type.

◆ hasPendingFrames()

bool esi::WriteChannelPort::hasPendingFrames ( ) const
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().

◆ isConnected()

virtual bool esi::WriteChannelPort::isConnected ( ) const
inlineoverridevirtual

Implements esi::ChannelPort.

Definition at line 342 of file Ports.h.

References Disconnected, and mode.

◆ maybePadEmptyMessage()

const MessageData & esi::WriteChannelPort::maybePadEmptyMessage ( const MessageData & data)
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().

◆ notifyAsyncDataAvailable()

void WriteChannelPort::notifyAsyncDataAvailable ( )
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().

◆ pollImpl()

bool WriteChannelPort::pollImpl ( )
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().

◆ pollWriteImpl()

virtual bool esi::WriteChannelPort::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().

◆ requireNotCallbackMode()

void WriteChannelPort::requireNotCallbackMode ( const char * method) const
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().

◆ stopAsyncWriter()

void WriteChannelPort::stopAsyncWriter ( )
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().

◆ translateOutgoing()

void WriteChannelPort::translateOutgoing ( const MessageData & data)
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().

◆ tryWrite() [1/2]

bool esi::WriteChannelPort::tryWrite ( const MessageData & data)
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().

◆ tryWrite() [2/2]

bool esi::WriteChannelPort::tryWrite ( std::unique_ptr< SegmentedMessageData > & msg)
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().

◆ tryWriteImpl()

virtual bool esi::WriteChannelPort::tryWriteImpl ( const MessageData & data)
protectedpure virtual

Implementation for tryWrite(). Subclasses must implement this.

Implemented in esi::UnknownWriteChannelPort.

Referenced by flushInternal(), and tryWriteInternal().

◆ tryWriteInternal()

bool WriteChannelPort::tryWriteInternal ( const MessageData & data)
private

◆ tryWriteSegmentedImpl()

virtual bool esi::WriteChannelPort::tryWriteSegmentedImpl ( std::unique_ptr< SegmentedMessageData > & msg)
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().

◆ write() [1/2]

void esi::WriteChannelPort::write ( const MessageData & data)
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().

◆ write() [2/2]

void esi::WriteChannelPort::write ( std::unique_ptr< SegmentedMessageData > msg)
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().

◆ writeImpl()

virtual void esi::WriteChannelPort::writeImpl ( const MessageData & )
protectedpure virtual

Implementation for write(). Subclasses must implement this.

Implemented in esi::UnknownWriteChannelPort.

Referenced by writeInternal().

◆ writeInternal()

void WriteChannelPort::writeInternal ( const MessageData & data)
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().

◆ writeSegmentedImpl()

virtual void esi::WriteChannelPort::writeSegmentedImpl ( std::unique_ptr< SegmentedMessageData > msg)
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().

Member Data Documentation

◆ asyncWriter

std::shared_ptr<DefaultAsyncWriter> esi::WriteChannelPort::asyncWriter
private

The default implementation's current connection, if in use.

Definition at line 563 of file Ports.h.

Referenced by connect(), getAsyncWriter(), and stopAsyncWriter().

◆ asyncWriterMutex

std::mutex esi::WriteChannelPort::asyncWriterMutex
private

Protects asyncWriter.

Definition at line 561 of file Ports.h.

Referenced by connect(), getAsyncWriter(), and stopAsyncWriter().

◆ kMaxAsyncWritesPerCall

size_t esi::WriteChannelPort::kMaxAsyncWritesPerCall = 64
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().

◆ mode

std::atomic<Mode> esi::WriteChannelPort::mode = Mode::Disconnected
protected

◆ translateMessages

bool esi::WriteChannelPort::translateMessages = false
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().

◆ translationBuffer

std::vector<MessageData> esi::WriteChannelPort::translationBuffer
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().

◆ translationBufferIdx

size_t esi::WriteChannelPort::translationBufferIdx = 0
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().

◆ transportPad0Bytes

const MessageData esi::WriteChannelPort::transportPad0Bytes = MessageData({0})
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().


The documentation for this class was generated from the following files: