Channels: Fixed-Size SPSC

In this example, we use the Channel frontend to exchange fixed-sized tokens between a single producer and a single consumer.

The size of the buffers is configurable per command line. For example:

  • mpirun -n 2 ./mpi 3 launches the examples with a consumer buffer of size 3.

In the example, the producer will send attempt to push three numeric tokens, 42, 43 and 44, into the consumer buffer.

Push tokens

Each of these are done via:

producer.push(sendSlot);

Receive tokens

The consumer will first receive, print, and pop a single token, as follows:

// Getting a single value from the channel
while (consumer.isEmpty()) consumer.updateDepth();

// Getting internal pointer of the token buffer slot
auto tokenBuffer = (ELEMENT_TYPE *)tokenBufferSlot->getPointer();

printf("Received Value: %u\n", tokenBuffer[consumer.peek()]);
consumer.pop();

It will wait until two more messages have arrived to print and pop.

Below is the expected result of the application for a buffer size >= 2:

Received Value: 42
Received Value: 43
Received Value: 44
Sent Value:     42
Sent Value:     43
Sent Value:     44

However, when running the application if a buffer size = 1, the consumer will lock waiting for two successive messages, when the producer can only send one at a time:

Sent Value:     42
Received Value: 42
Sent Value:     43