Search in sources :

Example 6 with MPIMessage

use of edu.iu.dsc.tws.comms.mpi.MPIMessage in project twister2 by DSC-SPIDAL.

the class MPIMultiMessageDeserializer method build.

@Override
public Object build(Object partialObject, int edge) {
    MPIMessage currentMessage = (MPIMessage) partialObject;
    int readLength = 0;
    int bufferIndex = 0;
    List<MPIBuffer> buffers = currentMessage.getBuffers();
    List<Object> returnList = new ArrayList<>();
    MessageHeader header = currentMessage.getHeader();
    if (header == null) {
        throw new RuntimeException("Header must be built before the message");
    }
    // LOG.info(String.format("%d deserilizing message", executor));
    while (readLength < header.getLength()) {
        List<MPIBuffer> messageBuffers = new ArrayList<>();
        MPIBuffer mpiBuffer = buffers.get(bufferIndex);
        ByteBuffer byteBuffer = mpiBuffer.getByteBuffer();
        // now read the length
        int length = byteBuffer.getInt();
        int tempLength = 0;
        int tempBufferIndex = bufferIndex;
        while (tempLength < length) {
            mpiBuffer = buffers.get(tempBufferIndex);
            messageBuffers.add(mpiBuffer);
            tempLength += mpiBuffer.getByteBuffer().remaining();
            tempBufferIndex++;
        // LOG.info(String.format("%d temp %d length %d readLength %d header %d buf_pos %d",
        // executor, tempLength, length, readLength, header.getLength(),
        // mpiBuffer.getByteBuffer().position()));
        }
        Object object = buildMessage(currentMessage, messageBuffers, length);
        readLength += length + 4;
        if (keyed && !MessageTypeUtils.isPrimitiveType(currentMessage.getKeyType())) {
            // adding 4 to the length since the key length is also kept
            readLength += 4;
            if (MessageTypeUtils.isMultiMessageType(currentMessage.getKeyType())) {
                readLength += 4;
            }
        }
        byteBuffer = mpiBuffer.getByteBuffer();
        if (byteBuffer.remaining() > 0) {
            bufferIndex = tempBufferIndex - 1;
        } else {
            bufferIndex = tempBufferIndex;
        }
        returnList.add(object);
    }
    return returnList;
}
Also used : MPIMessage(edu.iu.dsc.tws.comms.mpi.MPIMessage) ArrayList(java.util.ArrayList) MessageHeader(edu.iu.dsc.tws.comms.api.MessageHeader) MPIBuffer(edu.iu.dsc.tws.comms.mpi.MPIBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 7 with MPIMessage

use of edu.iu.dsc.tws.comms.mpi.MPIMessage in project twister2 by DSC-SPIDAL.

the class MPIMultiMessageSerializer method build.

@Override
public Object build(Object message, Object partialBuildObject) {
    int noOfMessages = 1;
    if (message instanceof List) {
        noOfMessages = ((List) message).size();
    }
    MPISendMessage sendMessage = (MPISendMessage) partialBuildObject;
    // we got an already serialized message, lets just return it
    MPIMessage mpiMessage = sendMessage.getMPIMessage();
    if (mpiMessage.isComplete()) {
        sendMessage.setSendState(MPISendMessage.SendState.SERIALIZED);
        return sendMessage;
    }
    // to keep track of the serialization progress of this message
    if (sendMessage.getSerializationState() == null) {
        sendMessage.setSerializationState(new SerializeState());
    }
    while (sendBuffers.size() > 0 && sendMessage.serializedState() != MPISendMessage.SendState.SERIALIZED) {
        MPIBuffer buffer = sendBuffers.poll();
        if (sendMessage.serializedState() == MPISendMessage.SendState.INIT || sendMessage.serializedState() == MPISendMessage.SendState.SENT_INTERNALLY) {
            // build the header
            buildHeader(buffer, sendMessage, noOfMessages);
            sendMessage.setSendState(MPISendMessage.SendState.HEADER_BUILT);
        }
        if (sendMessage.serializedState() == MPISendMessage.SendState.HEADER_BUILT || sendMessage.serializedState() == MPISendMessage.SendState.BODY_BUILT) {
            // first we need to serialize the body if needed
            serializeBody(message, sendMessage, buffer);
        }
        // okay we are adding this buffer
        mpiMessage.addBuffer(buffer);
        if (sendMessage.serializedState() == MPISendMessage.SendState.SERIALIZED) {
            SerializeState state = sendMessage.getSerializationState();
            int totalBytes = state.getTotalBytes();
            mpiMessage.getBuffers().get(0).getByteBuffer().putInt(12, totalBytes);
            MessageHeader.Builder builder = MessageHeader.newBuilder(sendMessage.getSource(), sendMessage.getEdge(), totalBytes);
            builder.destination(sendMessage.getDestintationIdentifier());
            sendMessage.getMPIMessage().setHeader(builder.build());
            state.setTotalBytes(0);
            // mark the original message as complete
            mpiMessage.setComplete(true);
        }
    }
    return sendMessage;
}
Also used : MPIMessage(edu.iu.dsc.tws.comms.mpi.MPIMessage) MPISendMessage(edu.iu.dsc.tws.comms.mpi.MPISendMessage) List(java.util.List) MessageHeader(edu.iu.dsc.tws.comms.api.MessageHeader) MPIBuffer(edu.iu.dsc.tws.comms.mpi.MPIBuffer)

Example 8 with MPIMessage

use of edu.iu.dsc.tws.comms.mpi.MPIMessage in project twister2 by DSC-SPIDAL.

the class Test method runTest2.

@SuppressWarnings("rawtypes")
public void runTest2() {
    IntData data = new IntData(128);
    List list = new ArrayList<>();
    list.add(new KeyedContent(new Short((short) 0), data));
    data = new IntData(128);
    list.add(new KeyedContent(new Short((short) 1), data));
    MPIMessage message = serializeObject(list, 1);
    System.out.println("Serialized first");
    deserialize(message);
// data = new IntData(128000);
// list = new ArrayList<>();
// list.add(new KeyedContent(new Short((short) 2), data));
// data = new IntData(128000);
// list.add(new KeyedContent(new Short((short) 3), data));
// MPIMessage message2 = serializeObject(list, 1);
// //    System.out.println("Serialized second");
// //
// list = new ArrayList<>();
// list.add(message);
// list.add(message2);
// //    data = new IntData(128000);
// //    list.add(new MultiObject(1, data));
// 
// MPIMessage second = serializeObject(list, 1);
// 
// deserialize(second);
}
Also used : MPIMessage(edu.iu.dsc.tws.comms.mpi.MPIMessage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 9 with MPIMessage

use of edu.iu.dsc.tws.comms.mpi.MPIMessage in project twister2 by DSC-SPIDAL.

the class Test method runTest.

@SuppressWarnings("rawtypes")
public void runTest() {
    IntData data = new IntData();
    List list = new ArrayList<>();
    list.add(data);
    MPIMessage message = serializeObject(list, 1);
    deserialize(message);
}
Also used : MPIMessage(edu.iu.dsc.tws.comms.mpi.MPIMessage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

MPIMessage (edu.iu.dsc.tws.comms.mpi.MPIMessage)9 ArrayList (java.util.ArrayList)5 List (java.util.List)5 MessageHeader (edu.iu.dsc.tws.comms.api.MessageHeader)4 MPIBuffer (edu.iu.dsc.tws.comms.mpi.MPIBuffer)4 MPISendMessage (edu.iu.dsc.tws.comms.mpi.MPISendMessage)3 ByteBuffer (java.nio.ByteBuffer)2 MessageType (edu.iu.dsc.tws.comms.api.MessageType)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1