Search in sources :

Example 6 with MessageHeader

use of edu.iu.dsc.tws.comms.api.MessageHeader 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 MessageHeader

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

the class Test method deserialize.

private void deserialize(MPIMessage message) {
    List<MPIBuffer> buffers = message.getBuffers();
    for (MPIBuffer mpiBuffer : buffers) {
        mpiBuffer.getByteBuffer().flip();
        mpiBuffer.getByteBuffer().rewind();
    }
    if (message.isComplete()) {
        System.out.printf("Complete message");
    }
    message.setKeyType(MessageType.SHORT);
    MessageHeader header = mpiMultiMessageDeserializer.buildHeader(message.getBuffers().get(0), 0);
    message.setHeader(header);
    System.out.println(String.format("%d %d %d", header.getLength(), header.getSourceId(), header.getEdge()));
    Object d = mpiMultiMessageDeserializer.build(message, 0);
    List list = (List) d;
    for (Object o : list) {
        if (o instanceof KeyedContent) {
            System.out.println(((KeyedContent) o).getSource());
            if (((KeyedContent) o).getObject() instanceof IntData) {
                System.out.println("Length: " + ((IntData) ((KeyedContent) o).getObject()).getData().length);
            }
        }
    }
    System.out.println("End");
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) MessageHeader(edu.iu.dsc.tws.comms.api.MessageHeader) MPIBuffer(edu.iu.dsc.tws.comms.mpi.MPIBuffer)

Aggregations

MessageHeader (edu.iu.dsc.tws.comms.api.MessageHeader)7 MPIBuffer (edu.iu.dsc.tws.comms.mpi.MPIBuffer)3 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 MPIMessage (edu.iu.dsc.tws.comms.mpi.MPIMessage)2 List (java.util.List)1