Skip to content

Socket

Sockets are used to exchange data between tasks. There are 3 different types of sockets:

  • Input socket (socket_t::SIN): read only data,
  • Output socket (socket_t::SOUT): write only data,
  • Forward socket (socket_t::SFWD): read and write data.

A task can have multiple sockets of different types (input, output and forward). This is illustrated in the following figure:

Task with sockets!

Tasks with different socket types.

A socket is a C++ object of the spu::runtime::Socket class. The following sections try to give an overview of the most important attributes and methods to facilitate the code understanding.

Main Attributes

socket_t type;
Define the socket type IN, OUT or FWD.

std::string name;
Custom name for the socket.

std::type_index datatype;
The type of data exchanged.

void* dataptr;
Pointer to the data of the socket (memory space).

std::vector<Socket*> bound_sockets;
The input or forward sockets bound to the current socket. Only relevant for output or forward sockets.

Socket* bound_socket;
The unique output or forward socket bound to the current socket. Only relevant for input or forward sockets.

Main Methods

The most important methods of the socket class are bind and unbind.

void bind(Socket &s_out, const int priority = -1);
This function is used to connect sockets with each other, it can be called by an input or forward socket and takes as parameter an output or forward socket. The function gets the caller's dataptr and redirects it to s_out dataptr.

Below some examples of valid and invalids socket bindings :

Valid bindings Valid bindings
Examples of valid socket bindings.

Invalid bindings Invalid bindings
Examples of invalid socket bindings.

For invalid socket bindings, StreamPU will throw an exception at runtime.

void unbind(Socket &s_out, const int priority = -1);
This function is used to disconnect sockets from each other.

Note

s_out must be bound to the caller socket otherwise StreamPU will throw an exception.

Standard SIN/SOUT Sockets versus SFWD Socket

Using a couple of SIN/SOUT sockets or a single SFWD socket can have an impact on the code behavior and on the performance of the application. The most important point is the impact on the socket dataptr attribute.

  • In the case of SIN/SOUT sockets, the input and the output sockets have their own dataptr. The input socket receives the pointer from its bound socket and the output socket has its own allocated memory space, the data received and computed by the task are written to the output memory space. The initial data are not modified in this case, there are no side effects.
  • In the case of a single SFWD socket, the socket receives its dataptr from the bound socket like an input. But unlike in the SIN/SOUT case, the computed data are written directly on the provided memory space, thus overwriting it (and potentially losing important information), there are side effects.