OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
MsgBuffer.cpp
Go to the documentation of this file.
1//
2// Class MsgBuffer
3// MsgBuffer class to allow serializing message objects into plain buffers
4// to send directly with mpi calls or similar means
5//
6// Copyright (c) 2008 - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
7// All rights reserved
8//
9// This file is part of OPAL.
10//
11// OPAL is free software: you can redistribute it and/or modify
12// it under the terms of the GNU General Public License as published by
13// the Free Software Foundation, either version 3 of the License, or
14// (at your option) any later version.
15//
16// You should have received a copy of the GNU General Public License
17// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
18//
19#include "Message/MsgBuffer.h"
20
21#include "Message/Format.h"
22
23MsgBuffer::MsgBuffer(Format *f, int count, int offset)
24 : format(f), writepos(0), readpos(0)
25{
26 datasize = count*format->getSize();
27 data.resize(datasize+offset);
28}
29
30
31MsgBuffer::MsgBuffer(Format *f, char *buf, int size)
32 : format(f), writepos(0), readpos(0)
33{
34 datasize = size;
35 data.resize(datasize);
36 std::copy(buf, buf+size, data.begin());
37 delete[] buf;
38}
39
41{
42}
43
45{
46 unsigned int items = msg->size();
47
48 //check for full storage or message size mismatch
49 if (writepos == datasize || items != format->getItemCount())
50 return false;
51
52 int pos = writepos;
53 for (unsigned int i=0; i<items; ++i)
54 {
55 Message::MsgItem &msgitem = msg->item(i);
56
57 //check for format mismatch
58 if (format->getItemElems(i) != msgitem.numElems() ||
59 format->getItemBytes(i) != msgitem.numBytes())
60 return false;
61
62 //actually copy to buffer
63 std::memcpy(data.data()+pos, msgitem.data(), format->getItemBytes(i));
64 pos += format->getItemBytes(i);
65 }
66
67 writepos = pos;
68 return true;
69}
70
72{
73 if (readpos > datasize - format->getSize())
74 return 0;
75
76 unsigned int items = format->getItemCount();
77 Message *msg = new Message(items);
78
79 //get all the items according to format and add them to the message
80 for (unsigned int j = 0; j < items; j++)
81 {
82 unsigned int bytesize = format->getItemBytes(j);
83 unsigned int elements = format->getItemElems(j);
84
85 msg->setCopy(false);
86 msg->setDelete(false);
87 msg->putmsg(data.data()+readpos, bytesize/elements, elements);
88 readpos += bytesize;
89 }
90
91 return msg;
92}
elements
Definition: IndexMap.cpp:163
Definition: Format.h:27
unsigned int getItemBytes(int i)
Definition: Format.h:42
unsigned int getSize()
Definition: Format.h:33
unsigned int getItemCount()
Definition: Format.h:30
unsigned int getItemElems(int i)
Definition: Format.h:39
size_t size() const
Definition: Message.h:292
Message & putmsg(void *, int, int=0)
Message & setCopy(const bool c)
Definition: Message.h:319
MsgItem & item(size_t n)
Definition: Message.h:308
Message & setDelete(const bool c)
Definition: Message.h:331
unsigned int numBytes() const
Definition: Message.h:218
unsigned int numElems() const
Definition: Message.h:222
void * data()
Definition: Message.h:244
bool add(Message *)
Definition: MsgBuffer.cpp:44
MsgBuffer(Format *f, int count, int offset=0)
Definition: MsgBuffer.cpp:23
unsigned int readpos
Definition: MsgBuffer.h:67
unsigned int writepos
Definition: MsgBuffer.h:67
std::vector< char > data
Definition: MsgBuffer.h:68
Message * get()
Definition: MsgBuffer.cpp:71
Format * format
Definition: MsgBuffer.h:66
unsigned int datasize
Definition: MsgBuffer.h:67