Search in sources :

Example 1 with ChannelMessage

use of edu.iu.dsc.tws.api.comms.messaging.ChannelMessage in project twister2 by DSC-SPIDAL.

the class TWSTCPChannel method postMessage.

/**
 * Send a message to the given rank.
 *
 * @param requests the message
 */
private void postMessage(TCPSendRequests requests) {
    ChannelMessage message = requests.message;
    for (int i = 0; i < message.getNormalBuffers().size(); i++) {
        sendCount++;
        DataBuffer buffer = message.getNormalBuffers().get(i);
        TCPMessage request = channel.iSend(buffer.getByteBuffer(), buffer.getSize(), requests.rank, message.getHeader().getEdge());
        // register to the loop to make communicationProgress on the send
        requests.pendingSends.add(new Request(request, buffer));
    }
}
Also used : TCPMessage(edu.iu.dsc.tws.common.net.tcp.TCPMessage) ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage) DataBuffer(edu.iu.dsc.tws.api.comms.packing.DataBuffer)

Example 2 with ChannelMessage

use of edu.iu.dsc.tws.api.comms.messaging.ChannelMessage in project twister2 by DSC-SPIDAL.

the class ControlledChannelOperation method sendProgress.

/**
 * Go through the out messages, create channel messages by using the serializer send them
 *
 * @param sendId send target
 */
public void sendProgress(int sendId) {
    boolean canProgress = true;
    Queue<OutMessage> pendingSendMessages = pendingSendMessagesPerSource.get(sendId);
    while (pendingSendMessages.size() > 0 && canProgress) {
        // take out pending messages
        OutMessage outMessage = pendingSendMessages.peek();
        Object data = outMessage.getData();
        // first lets send the message to internal destinations
        canProgress = sendInternally(outMessage, data);
        if (canProgress) {
            // we don't have an external executor to send this message
            if (outMessage.getExternalSends().size() == 0) {
                pendingSendMessages.poll();
                continue;
            }
            Queue<ChannelMessage> channelMessages = outMessage.getChannelMessages();
            // at this point lets build the message
            ChannelMessage serializeMessage = (ChannelMessage) messageSerializer.get(sendId).build(outMessage.getData(), outMessage);
            if (serializeMessage != null) {
                // we are incrementing the reference count here
                channelMessages.offer(serializeMessage);
            }
            ChannelMessage chMessage = channelMessages.peek();
            if (chMessage == null) {
                break;
            }
            List<Integer> externalRoutes = outMessage.getExternalSends();
            // okay we build the message, send it
            if (outMessage.getSendState() == OutMessage.SendState.SERIALIZED) {
                int startOfExternalRouts = chMessage.getAcceptedExternalSends();
                canProgress = sendExternally(outMessage, chMessage, externalRoutes, startOfExternalRouts);
                if (chMessage.getAcceptedExternalSends() == externalRoutes.size()) {
                    // we are done
                    pendingSendMessages.poll();
                    channelMessages.poll();
                    // the send is completed, we need to notify
                    receiver.sendCompleted(outMessage);
                }
            } else if (outMessage.getSendState() == OutMessage.SendState.PARTIALLY_SERIALIZED) {
                int startOfExternalRouts = chMessage.getAcceptedExternalSends();
                canProgress = sendExternally(outMessage, chMessage, externalRoutes, startOfExternalRouts);
                if (chMessage.getAcceptedExternalSends() == externalRoutes.size()) {
                    // we are done sending this channel message
                    channelMessages.poll();
                }
            } else {
                break;
            }
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage)

Example 3 with ChannelMessage

use of edu.iu.dsc.tws.api.comms.messaging.ChannelMessage in project twister2 by DSC-SPIDAL.

the class ControlledChannelOperation method receiveProgress.

/**
 * Progress the receive
 *
 * @param receiveId
 */
public void receiveProgress(int receiveId) {
    Queue<InMessage> pendingReceiveMessages = pendingReceiveMessagesPerSource.get(receiveId);
    boolean canProgress = true;
    while (pendingReceiveMessages.size() > 0 && canProgress) {
        InMessage currentMessage = pendingReceiveMessages.peek();
        lock.lock();
        try {
            // lets keep track that we have completed a receive from this executor
            int workerId = currentMessage.getOriginatingId();
            int count = currentReceives.get(workerId);
            int expected = expectedReceivePerWorker.get(workerId);
            InMessage.ReceivedState receivedState = currentMessage.getReceivedState();
            if (receivedState == InMessage.ReceivedState.BUILT) {
                // if this message is built, we need to check how many we are expecting
                count++;
                currentReceives.put(workerId, count);
            }
            if (receivedState == InMessage.ReceivedState.BUILDING || receivedState == InMessage.ReceivedState.BUILT) {
                while (currentMessage.getBuiltMessages().size() > 0) {
                    // get the first channel message
                    ChannelMessage msg = currentMessage.getBuiltMessages().peek();
                    if (msg != null) {
                        if (!receiver.handleReceivedChannelMessage(msg)) {
                            canProgress = false;
                            break;
                        }
                        ChannelMessage releaseMsg = currentMessage.getBuiltMessages().poll();
                        Objects.requireNonNull(releaseMsg).release();
                        if (receivedState == InMessage.ReceivedState.BUILDING) {
                            DataBuffer buffer = freeReceiveBuffers.poll();
                            Queue<DataBuffer> list = receiveBuffers.get(workerId);
                            if (buffer == null) {
                                throw new RuntimeException("Free buffers doesn't have any buffer");
                            }
                            // we get a free buffer and offer
                            list.offer(buffer);
                        } else {
                            if (expected > count) {
                                DataBuffer buffer = freeReceiveBuffers.poll();
                                Queue<DataBuffer> list = receiveBuffers.get(workerId);
                                if (buffer == null) {
                                    throw new RuntimeException("Free buffers doesn't have any buffer");
                                }
                                // we get a free buffer and offer
                                list.offer(buffer);
                            }
                        }
                    }
                }
                if (receivedState == InMessage.ReceivedState.BUILT && currentMessage.getBuiltMessages().size() == 0 && canProgress) {
                    currentMessage.setReceivedState(InMessage.ReceivedState.RECEIVE);
                }
            }
            if (receivedState == InMessage.ReceivedState.RECEIVE) {
                Object object = currentMessage.getDeserializedData();
                if (!receiver.receiveMessage(currentMessage.getHeader(), object)) {
                    break;
                }
                currentMessage.setReceivedState(InMessage.ReceivedState.DONE);
                pendingReceiveMessages.poll();
            } else {
                break;
            }
        } finally {
            lock.unlock();
        }
    }
}
Also used : ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage) DataBuffer(edu.iu.dsc.tws.api.comms.packing.DataBuffer)

Example 4 with ChannelMessage

use of edu.iu.dsc.tws.api.comms.messaging.ChannelMessage in project twister2 by DSC-SPIDAL.

the class FixedSchemaDataDeserializer method build.

/**
 * Builds the message from the data buffers in the partialObject. Since this method
 * supports multi-messages it iterates through the buffers and builds all the messages separately
 *
 * @param partialObject message object that needs to be built
 * @param edge the edge value associated with this message
 */
@Override
public void build(Object partialObject, int edge) {
    InMessage currentMessage = (InMessage) partialObject;
    DataPacker dataPacker = currentMessage.getDataType().getDataPacker();
    Queue<DataBuffer> buffers = currentMessage.getBuffers();
    MessageHeader header = currentMessage.getHeader();
    if (header == null) {
        throw new RuntimeException("Header must be built before the message");
    }
    List<DataBuffer> builtBuffers = new ArrayList<>();
    // get the number of objects deserialized
    DataBuffer buffer = buffers.peek();
    while (buffer != null) {
        int currentLocation = 0;
        int remaining = buffer.getSize();
        if (header.getNumberTuples() == 0) {
            builtBuffers.add(buffer);
            break;
        }
        // if we are at the beginning
        int currentObjectLength = currentMessage.getUnPkCurrentObjectLength();
        if (currentMessage.getUnPkBuffers() == 0) {
            currentLocation = DFWIOUtils.HEADER_SIZE;
            remaining = remaining - DFWIOUtils.HEADER_SIZE;
        } else {
            // source(int) + last_buffer_indicator(byte)
            currentLocation = DFWIOUtils.SHORT_HEADER_SIZE;
            remaining = remaining - DFWIOUtils.SHORT_HEADER_SIZE;
        }
        if (currentObjectLength == -1 || currentMessage.getUnPkBuffers() == 0) {
            currentObjectLength = this.messageSchema.getMessageSize();
            // starting to build a new object
            currentMessage.getDataBuilder().init(dataPacker, currentObjectLength);
        }
        while (remaining > 0) {
            // read the values from the buffer
            ObjectBuilderImpl dataBuilder = currentMessage.getDataBuilder();
            int bytesRead = dataPacker.readDataFromBuffer(dataBuilder, currentLocation, buffer);
            dataBuilder.incrementCompletedSizeBy(bytesRead);
            currentLocation += bytesRead;
            remaining = remaining - bytesRead;
            // okay we are done with this object
            if (dataBuilder.isBuilt()) {
                currentMessage.addCurrentObject();
                currentMessage.setUnPkCurrentObjectLength(-1);
            } else {
                // lets break the inner while loop
                break;
            }
        }
        // lets remove this buffer
        buffers.poll();
        builtBuffers.add(buffer);
        // increment the unpacked buffers
        currentMessage.incrementUnPkBuffers();
        // lets check weather we have read everythong
        int readObjectNumber = currentMessage.getUnPkNumberObjects();
        // we need to get number of tuples and get abs because we are using -1 for single messages
        if (readObjectNumber == Math.abs(currentMessage.getHeader().getNumberTuples())) {
            break;
        }
        // lets move to next
        buffer = buffers.peek();
    }
    if (builtBuffers.size() > 0) {
        ChannelMessage channelMessage = new ChannelMessage(currentMessage.getOriginatingId(), currentMessage.getDataType(), MessageDirection.IN, currentMessage.getReleaseListener());
        channelMessage.addBuffers(builtBuffers);
        channelMessage.setHeader(currentMessage.getHeader());
        channelMessage.incrementRefCount();
        currentMessage.addBuiltMessage(channelMessage);
    }
}
Also used : ArrayList(java.util.ArrayList) InMessage(edu.iu.dsc.tws.comms.dfw.InMessage) MessageHeader(edu.iu.dsc.tws.api.comms.messaging.MessageHeader) DataPacker(edu.iu.dsc.tws.api.comms.packing.DataPacker) DataBuffer(edu.iu.dsc.tws.api.comms.packing.DataBuffer) ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage)

Example 5 with ChannelMessage

use of edu.iu.dsc.tws.api.comms.messaging.ChannelMessage in project twister2 by DSC-SPIDAL.

the class ChannelDataFlowOperation method receiveProgress.

private void receiveProgress(Queue<InMessage> pendingReceiveMessages) {
    boolean canProgress = true;
    while (pendingReceiveMessages.size() > 0 && canProgress) {
        InMessage currentMessage = pendingReceiveMessages.peek();
        lock.lock();
        try {
            if (currentMessage.getReceivedState() == InMessage.ReceivedState.BUILDING || currentMessage.getReceivedState() == InMessage.ReceivedState.BUILT) {
                while (currentMessage.getBuiltMessages().size() > 0) {
                    // get the first channel message
                    ChannelMessage msg = currentMessage.getBuiltMessages().peek();
                    if (msg != null) {
                        if (!receiver.handleReceivedChannelMessage(msg)) {
                            canProgress = false;
                            break;
                        }
                        ChannelMessage releaseMsg = currentMessage.getBuiltMessages().poll();
                        Objects.requireNonNull(releaseMsg).release();
                    }
                }
                if (currentMessage.getReceivedState() == InMessage.ReceivedState.BUILT && currentMessage.getBuiltMessages().size() == 0 && canProgress) {
                    currentMessage.setReceivedState(InMessage.ReceivedState.RECEIVE);
                }
            }
            if (currentMessage.getReceivedState() == InMessage.ReceivedState.RECEIVE) {
                Object object = currentMessage.getDeserializedData();
                if (!receiver.receiveMessage(currentMessage.getHeader(), object)) {
                    break;
                }
                currentMessage.setReceivedState(InMessage.ReceivedState.DONE);
                pendingReceiveMessages.poll();
            } else {
                break;
            }
        } finally {
            lock.unlock();
        }
    }
}
Also used : ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage)

Aggregations

ChannelMessage (edu.iu.dsc.tws.api.comms.messaging.ChannelMessage)17 DataBuffer (edu.iu.dsc.tws.api.comms.packing.DataBuffer)14 MessageHeader (edu.iu.dsc.tws.api.comms.messaging.MessageHeader)10 InMessage (edu.iu.dsc.tws.comms.dfw.InMessage)10 ArrayList (java.util.ArrayList)9 OutMessage (edu.iu.dsc.tws.comms.dfw.OutMessage)7 DataPacker (edu.iu.dsc.tws.api.comms.packing.DataPacker)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 MessageType (edu.iu.dsc.tws.api.comms.messaging.types.MessageType)2 ChannelListener (edu.iu.dsc.tws.api.comms.channel.ChannelListener)1 TCPMessage (edu.iu.dsc.tws.common.net.tcp.TCPMessage)1 DataDeserializer (edu.iu.dsc.tws.comms.dfw.io.DataDeserializer)1 DataSerializer (edu.iu.dsc.tws.comms.dfw.io.DataSerializer)1 Queue (java.util.Queue)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MPIException (mpi.MPIException)1 Request (mpi.Request)1 Test (org.junit.Test)1