27 #ifndef GALOIS_RUNTIME_SERIALIZE_H
28 #define GALOIS_RUNTIME_SERIALIZE_H
30 #include <type_traits>
38 #include <boost/mpl/has_xxx.hpp>
51 class DeSerializeBuffer;
80 void insert(
const uint8_t* c,
size_t bytes) {
81 bufdata.
insert(bufdata.
end(), c, c + bytes);
86 void insertAt(
const uint8_t* c,
size_t bytes,
size_t offset) {
87 std::copy_n(c, bytes, bufdata.
begin() + offset);
98 size_t retval = bufdata.
size();
99 bufdata.
resize(retval + bytes);
130 o <<
"<{" << std::hex;
131 for (
auto& i : bufdata)
132 o << (
unsigned int)i <<
" ";
133 o << std::dec <<
"}>";
166 : bufdata(std::move(v)), offset(start) {}
185 template <
typename Iter>
192 bufdata.
swap(buf.bufdata);
213 assert(off <=
size());
225 unsigned char pop() {
return bufdata.
at(offset++); }
238 memcpy(dst, &bufdata[offset], num);
263 o <<
"<{(" << offset <<
") " << std::hex;
264 for (
auto ii = bufdata.
begin(), ee = bufdata.
end(); ii != ee; ++ii)
265 o << (
unsigned int)*ii <<
" ";
266 o << std::dec <<
"}>";
285 template <
typename T>
286 __attribute__((always_inline)) constexpr
size_t
288 typename std::enable_if<is_memory_copyable<T>::value>::type* = 0) {
300 template <
typename T>
301 __attribute__((always_inline)) constexpr
size_t
303 typename std::enable_if<!is_memory_copyable<T>::value>::type* = 0,
304 typename std::enable_if<has_serialize<T>::value>::type* = 0) {
305 return sizeof(uintptr_t);
314 template <
typename T1,
typename T2>
315 inline size_t gSizedObj(
const std::pair<T1, T2>& data) {
316 return gSizedObj(data.first) + gSizedObj(data.second);
323 template <
typename Seq>
324 size_t gSizedSeq(
const Seq& seq) {
325 typename Seq::size_type size = seq.size();
327 size_t tsize = std::conditional<
328 is_memory_copyable<T>::value, std::integral_constant<size_t, sizeof(T)>,
329 std::integral_constant<size_t, sizeof(uintptr_t)>>::type::value;
330 return sizeof(size) + tsize * size;
339 template <
typename T,
typename Alloc>
340 inline size_t gSizedObj(
const std::vector<T, Alloc>& data) {
341 return gSizedSeq(data);
350 template <
typename T>
352 return gSizedSeq(data);
361 template <
typename T,
typename Alloc>
362 inline size_t gSerializeObj(
const std::deque<T, Alloc>& data) {
363 return gSizedSeq(data);
372 template <
typename T,
unsigned CS>
374 return gSizedSeq(data);
383 template <
typename A>
385 gSizedObj(
const std::basic_string<
char, std::char_traits<char>, A>& data) {
386 return data.length() + 1;
394 inline size_t gSizedObj(
const SerializeBuffer& data) {
return data.size(); }
401 inline size_t gSizedObj(
const DeSerializeBuffer& rbuf) {
return rbuf.r_size(); }
408 template <
typename T>
417 inline size_t adder() {
return 0; }
423 inline size_t adder(
size_t a) {
return a; }
428 template <
typename... Args>
429 inline size_t adder(
size_t a,
size_t b, Args&&... args) {
430 return a + b + adder(args...);
441 template <
typename... Args>
442 static inline size_t gSized(Args&&... args) {
443 return internal::adder(internal::gSizedObj(args)...);
458 template <
typename T>
459 inline void gSerializeObj(
460 SerializeBuffer& buf,
const T& data,
461 typename std::enable_if<is_memory_copyable<T>::value>::type* = 0) {
462 uint8_t* pdata = (uint8_t*)&data;
463 buf.insert(pdata,
sizeof(T));
473 template <
typename T>
475 gSerializeObj(SerializeBuffer& buf,
const T& data,
476 typename std::enable_if<!is_memory_copyable<T>::value>::type* = 0,
477 typename std::enable_if<has_serialize<T>::value>::type* = 0) {
487 template <
typename T1,
typename T2>
488 inline void gSerializeObj(SerializeBuffer& buf,
const std::pair<T1, T2>& data) {
489 gSerialize(buf, data.first, data.second);
499 template <
typename T1,
typename T2>
500 inline void gSerializeObj(SerializeBuffer& buf,
502 if (is_memory_copyable<T1>::value && is_memory_copyable<T2>::value) {
504 buf.insert((uint8_t*)&data,
sizeof(data));
519 template <
typename T1,
typename T2,
typename T3>
520 inline void gSerializeObj(SerializeBuffer& buf,
522 if (is_memory_copyable<T1>::value && is_memory_copyable<T2>::value &&
523 is_memory_copyable<T3>::value) {
525 buf.insert((uint8_t*)&data,
sizeof(data));
539 template <
typename T>
540 inline void gSerializeObj(SerializeBuffer& buf,
542 T temp = data.load();
543 buf.insert((uint8_t*)(&temp),
sizeof(T));
552 template <
typename A>
554 gSerializeObj(SerializeBuffer& buf,
555 const std::basic_string<
char, std::char_traits<char>, A>& data) {
556 buf.insert((uint8_t*)data.data(), data.length() + 1);
560 template <
typename T,
typename Alloc>
561 inline void gSerializeObj(SerializeBuffer& buf,
562 const std::vector<T, Alloc>& data);
571 template <
typename Seq>
572 void gSerializeSeq(SerializeBuffer& buf,
const Seq& seq) {
573 typename Seq::size_type size = seq.size();
574 gSerializeObj(buf, size);
576 gSerializeObj(buf, o);
585 template <
typename Seq>
586 void gSerializeLinearSeq(SerializeBuffer& buf,
const Seq& seq) {
587 typename Seq::size_type size = seq.size();
589 size_t tsize =
sizeof(T);
591 gSerializeObj(buf, size);
592 buf.insert((uint8_t*)seq.data(), size * tsize);
602 template <
typename T,
typename Alloc>
603 inline void gSerializeObj(SerializeBuffer& buf,
604 const std::vector<T, Alloc>& data) {
605 if (is_memory_copyable<T>::value)
606 gSerializeLinearSeq(buf, data);
608 gSerializeSeq(buf, data);
618 template <
typename T>
619 inline void gSerializeObj(SerializeBuffer& buf,
621 gSerializeLinearSeq(buf, data);
630 template <
typename T,
typename Alloc>
631 inline void gSerializeObj(SerializeBuffer& buf,
632 const std::deque<T, Alloc>& data) {
633 gSerializeSeq(buf, data);
642 template <
typename T,
unsigned CS>
643 inline void gSerializeObj(SerializeBuffer& buf,
645 gSerializeSeq(buf, data);
654 inline void gSerializeObj(SerializeBuffer& buf,
const SerializeBuffer& data) {
655 buf.insert(data.linearData(), data.size());
664 inline void gSerializeObj(SerializeBuffer& buf,
const DeSerializeBuffer& rbuf) {
666 buf.insert(rbuf.r_linearData(), rbuf.r_size());
675 inline void gSerializeObj(SerializeBuffer& buf,
677 gSerializeObj(buf, data.
size());
678 gSerializeObj(buf, data.
get_vec());
694 gSerializeObj(buf, bag.size());
695 auto headerVec = bag.getHeads();
696 size_t totalSize = 0;
697 for(
auto h : headerVec){
698 size_t localSize = (h->dend - h->dbegin);
699 buf.insert((uint8_t*)h->dbegin, localSize*
sizeof(T));
700 totalSize += (h->dend - h->dbegin);
703 assert(totalSize == bag.size());
713 template <
typename T>
723 template <
typename Seq>
728 typename Seq::size_type size = num;
729 internal::gSerializeObj(buf, size);
744 template <
typename Ty>
745 static inline void gSerializeLazy(SerializeBuffer& buf, LazyRef<Ty> r,
746 unsigned item, Ty&& data) {
747 size_t off = r.off +
sizeof(Ty) * item;
748 uint8_t* pdata = (uint8_t*)&data;
749 buf.insertAt(pdata,
sizeof(Ty), off);
755 template <
typename T1,
typename... Args>
756 static inline void gSerialize(SerializeBuffer& buf, T1&& t1, Args&&... args) {
757 buf.reserve(gSized(t1, args...));
758 internal::gSerializeObj(buf, std::forward<T1>(t1));
759 gSerialize(buf, std::forward<Args>(args)...);
765 static inline void gSerialize(SerializeBuffer&) {}
779 template <
typename T>
780 void gDeserializeObj(
781 DeSerializeBuffer& buf, T& data,
782 typename std::enable_if<is_memory_copyable<T>::value>::type* = 0) {
783 uint8_t* pdata = (uint8_t*)&data;
784 buf.extract(pdata,
sizeof(T));
793 template <
typename T>
794 void gDeserializeObj(
795 DeSerializeBuffer& buf, T& data,
796 typename std::enable_if<!is_memory_copyable<T>::value>::type* = 0,
797 typename std::enable_if<has_serialize<T>::value>::type* = 0) {
798 data.deserialize(buf);
807 template <
typename T1,
typename T2>
808 void gDeserializeObj(DeSerializeBuffer& buf, std::pair<T1, T2>& data) {
819 template <
typename T1,
typename T2>
820 inline void gDeserializeObj(DeSerializeBuffer& buf,
822 if (is_memory_copyable<T1>::value && is_memory_copyable<T2>::value) {
824 buf.extract((uint8_t*)&data,
sizeof(data));
839 template <
typename T1,
typename T2,
typename T3>
840 inline void gDeserializeObj(DeSerializeBuffer& buf,
842 if (is_memory_copyable<T1>::value && is_memory_copyable<T2>::value &&
843 is_memory_copyable<T3>::value) {
845 buf.extract((uint8_t*)&data,
sizeof(data));
859 template <
typename T>
862 uint8_t* pointerToTemp = (uint8_t*)&tempData;
863 buf.extract(pointerToTemp,
sizeof(T));
864 data.store(tempData);
870 template <
int N,
int... S>
871 struct gens : gens<N - 1, N - 1, S...> {};
873 struct gens<0, S...> {
874 typedef seq<S...> type;
884 template <
typename... T,
int... S>
885 void gDeserializeTuple(DeSerializeBuffer& buf, std::tuple<T...>& data,
896 template <
typename... T>
897 void gDeserializeObj(DeSerializeBuffer& buf, std::tuple<T...>& data) {
898 return gDeserializeTuple(buf, data,
typename gens<
sizeof...(T)>::type());
907 template <
typename A>
909 gDeserializeObj(DeSerializeBuffer& buf,
910 std::basic_string<
char, std::char_traits<char>, A>& data) {
919 template <
typename T,
typename Alloc>
920 void gDeserializeObj(DeSerializeBuffer& buf, std::vector<T, Alloc>& data);
928 template <
typename Seq>
929 void gDeserializeSeq(DeSerializeBuffer& buf, Seq& seq) {
931 typename Seq::size_type size;
932 gDeserializeObj(buf, size);
935 gDeserializeObj(buf, v);
946 template <
typename Seq>
947 void gDeserializeLinearSeq(DeSerializeBuffer& buf, Seq& seq) {
950 typename Seq::size_type size;
951 gDeserializeObj(buf, size);
953 if (buf.atAlignment(
alignof(T))) {
954 T* src = (T*)buf.r_linearData();
955 seq.assign(src, &src[size]);
956 buf.setOffset(buf.getOffset() + size *
sizeof(T));
959 buf.extract((uint8_t*)seq.data(), size *
sizeof(T));
969 template <
typename T,
typename Alloc>
970 void gDeserializeObj(DeSerializeBuffer& buf, std::deque<T, Alloc>& data) {
971 gDeserializeSeq(buf, data);
981 template <
typename T,
typename Alloc>
982 void gDeserializeObj(DeSerializeBuffer& buf, std::vector<T, Alloc>& data) {
983 if (is_memory_copyable<T>::value)
984 gDeserializeLinearSeq(buf, data);
986 gDeserializeSeq(buf, data);
995 template <
typename T>
996 void gDeserializeObj(DeSerializeBuffer& buf,
998 gDeserializeLinearSeq(buf, data);
1007 template <
typename T,
unsigned CS>
1009 gDeserializeSeq(buf, data);
1018 inline void gDeserializeObj(DeSerializeBuffer& buf,
1021 gDeserializeObj(buf, size);
1023 gDeserializeObj(buf, data.
get_vec());
1031 template <
typename T1,
typename... Args>
1033 internal::gDeserializeObj(buf, std::forward<T1>(t1));
1051 template <
typename Iter,
typename T>
1055 unsigned char* pdata = (
unsigned char*)&data;
1056 for (
size_t i = 0; i <
sizeof(T); ++i)
1064 #endif // SERIALIZE DEF end
iterator end()
Definition: PODResizeableArray.h:107
reference at(size_type __n)
Definition: PODResizeableArray.h:152
friend std::ostream & operator<<(std::ostream &os, const DeSerializeBuffer &buf)
Operator for printing deserialize buffer.
Definition: Serialize.h:270
void * linearData()
Get a pointer to the underlying data of the deserialize buffer.
Definition: Serialize.h:247
void push_back(const _Tp &value)
Definition: PODResizeableArray.h:177
size_t size_type
Definition: PODResizeableArray.h:47
void push(const char c)
Push a character onto the serialize buffer.
Definition: Serialize.h:77
DeSerializeBuffer(vTy &data)
Constructor that takes an existing vector to use as the deserialize buffer.
Definition: Serialize.h:170
const_iterator cbegin() const
Definition: PODResizeableArray.h:119
void resize(size_t n)
Definition: PODResizeableArray.h:142
Concurrent dynamically allocated bitset.
Definition: libgalois/include/galois/DynamicBitset.h:47
bool empty() const
Definition: PODResizeableArray.h:127
Buffer for serialization of data.
Definition: Serialize.h:56
void reserve(size_t s)
Reserve more space in the serialize buffer.
Definition: Serialize.h:110
size_t r_size() const
Get the remaining size of the deserialize buffer (as determined by offset)
Definition: Serialize.h:254
void insert(iterator GALOIS_USED_ONLY_IN_DEBUG(position), InputIterator first, InputIterator last)
Definition: PODResizeableArray.h:183
T1 first
first element
Definition: CopyableTuple.h:66
const_pointer const_iterator
Definition: PODResizeableArray.h:54
SerializeBuffer()=default
default constructor
Contains the DynamicBitSet class and most of its implementation.
friend std::ostream & operator<<(std::ostream &os, const SerializeBuffer &b)
Operator that calls the print function of the serialize buffer.
Definition: Serialize.h:137
DeSerializeBuffer & operator=(DeSerializeBuffer &&buf)=default
Disable copy constructor.
void gDeserialize(DeSerializeBuffer &buf, T1 &&t1, Args &&...args)
Deserialize data in a buffer into a series of objects.
Definition: Serialize.h:1032
void setOffset(unsigned off)
Sets the offset into the deserialize buffer.
Definition: Serialize.h:212
void print(std::ostream &o) const
Utility print function for the serialize buffer.
Definition: Serialize.h:129
vTy & getVec()
Returns vector of data stored in this serialize buffer.
Definition: Serialize.h:115
Class that inherits from std::atomic to make it copyable by defining a copy constructor.
Definition: AtomicWrapper.h:40
void reserve(size_t n)
Definition: PODResizeableArray.h:129
void insertAt(const uint8_t *c, size_t bytes, size_t offset)
Insert characters from a buffer into the serialize buffer at a particular offset. ...
Definition: Serialize.h:86
vTy::const_iterator begin() const
Returns an iterator to the beginning of the data in this serialize buffer.
Definition: Serialize.h:118
size_t off
Definition: Serialize.h:715
vTy::const_iterator end() const
Returns an iterator to the end of the data in this serialize buffer.
Definition: Serialize.h:120
void pop_back(unsigned x)
Clears the last x bytes of the deserialize buffer, resizing it as well.
Definition: Serialize.h:229
T1 first
first element
Definition: CopyableTuple.h:41
void swap(PODResizeableArray &v)
Definition: PODResizeableArray.h:192
void insert(const uint8_t *c, size_t bytes)
Insert characters from a buffer into the serialize buffer.
Definition: Serialize.h:80
void resize(uint64_t n)
Resizes the bitset.
Definition: libgalois/include/galois/DynamicBitset.h:78
DeSerializeBuffer(Iter b, Iter e)
Initializes the deserialize buffer using vector initialization from 2 iterators.
Definition: Serialize.h:186
DeSerializeBuffer(SerializeBuffer &&buf)
Initialize a deserialize buffer from a serialize buffer.
Definition: Serialize.h:191
Unordered collection of elements.
Definition: Bag.h:42
const_iterator cend() const
Definition: PODResizeableArray.h:120
size_type size() const
Returns the size of the serialize buffer.
Definition: Serialize.h:125
void reset(int count)
Reset deserialize buffer.
Definition: Serialize.h:204
Struct that contains 3 elements.
Definition: CopyableTuple.h:64
size_t size() const
Gets the size of the bitset.
Definition: libgalois/include/galois/DynamicBitset.h:99
T2 second
second element
Definition: CopyableTuple.h:68
void print(std::ostream &o) const
Utility print of deserialize buffer.
Definition: Serialize.h:262
bool empty() const
Returns true if the deserialize buffer is empty.
Definition: Serialize.h:222
unsigned char pop()
Get the next character in the deserialize buffer.
Definition: Serialize.h:225
size_type size() const
Definition: PODResizeableArray.h:125
DeSerializeBuffer(int count)
Initializes the deserialize buffer with a certain size.
Definition: Serialize.h:179
SerializeBuffer(const char *d, unsigned len)
Creates a buffer from another buffer.
Definition: Serialize.h:74
vTy::size_type size_type
Definition: Serialize.h:122
size_t encomber(size_t bytes)
Reserve space at the end for inserting new data into the serialize buffer.
Definition: Serialize.h:97
bool atAlignment(size_t a)
Checks if the current location in the deserialize buffer is aligned to some size a.
Definition: Serialize.h:258
DeSerializeBuffer(vTy &&v, uint32_t start=0)
Move constructor.
Definition: Serialize.h:165
iterator begin()
Definition: PODResizeableArray.h:105
unsigned getOffset() const
Gets the current offset into the deserialize buffer.
Definition: Serialize.h:210
const uint8_t * r_linearData() const
Get a pointer to the remaining data of the deserialize buffer (as determined by offset) ...
Definition: Serialize.h:251
void resize(size_t bytes)
Definition: Serialize.h:103
auto gDeserializeRaw(Iter iter, T &data) -> decltype(std::declval< typename std::enable_if< is_memory_copyable< T >::value >::type >(), Iter())
"Deserialize" data in an iterator type into a data object.
Definition: Serialize.h:1052
T2 second
second element
Definition: CopyableTuple.h:43
Contains a copyable atomics class.
Struct that contains 2 elements.
Definition: CopyableTuple.h:39
void clear()
Definition: Bag.h:220
void extract(uint8_t *dst, size_t num)
Extracts a certain amount of data from the deserialize buffer.
Definition: Serialize.h:237
Buffer for deserialization of data.
Definition: Serialize.h:147
DeSerializeBuffer()
Constructor initializes offset into buffer to 0.
Definition: Serialize.h:159
pointer data()
Definition: PODResizeableArray.h:174
LazyRef structure; used to store both a type and an offset to begin saving data into.
Definition: Serialize.h:714
unsigned size() const
Gets the size of the deserialize buffer.
Definition: Serialize.h:218
T3 third
third element
Definition: CopyableTuple.h:70
Like std::deque but use Galois memory management functionality.
Definition: gdeque.h:43
T value_type
Definition: Executor_ParaMeter.h:111
const auto & get_vec() const
Returns the underlying bitset representation to the user.
Definition: libgalois/include/galois/DynamicBitset.h:63
const uint8_t * linearData() const
Returns a pointer to the data stored in this serialize buffer.
Definition: Serialize.h:113
Indicates if T is memory copyable.
Definition: ExtraTraits.h:64
vTy & getVec()
Get the underlying vector storing the data of the deserialize buffer.
Definition: Serialize.h:244
Contains copyable tuple classes whose elements are contiguous in memory.