Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
StorageView.cpp
Go to the documentation of this file.
1 //===-- serialbox/core/StorageView.cpp ----------------------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
14 //===------------------------------------------------------------------------------------------===//
15 
18 #include "serialbox/core/Logging.h"
20 #include <algorithm>
21 #include <cassert>
22 
23 namespace serialbox {
24 
25 StorageView::StorageView(void* originPtr, TypeID type, const std::vector<int>& dims,
26  const std::vector<int>& strides)
27  : originPtr_(reinterpret_cast<Byte*>(originPtr)), type_(type), dims_(dims), strides_(strides),
28  slice_((Slice::Empty())) {
29  assert(!dims_.empty() && "empty dimension");
30  assert(dims_.size() == strides_.size() && "dimension mismatch");
31  assert(slice_.empty());
32 }
33 
34 StorageView::StorageView(void* originPtr, TypeID type, std::vector<int>&& dims,
35  std::vector<int>&& strides)
36  : originPtr_(reinterpret_cast<Byte*>(originPtr)), type_(type), dims_(dims), strides_(strides),
37  slice_((Slice::Empty())) {
38  assert(!dims_.empty() && "empty dimension");
39  assert(dims_.size() == strides_.size() && "dimension mismatch");
40  assert(slice_.empty());
41 }
42 
43 void StorageView::swap(StorageView& other) noexcept {
44  std::swap(originPtr_, other.originPtr_);
45  std::swap(type_, other.type_);
46  dims_.swap(other.dims_);
47  strides_.swap(other.strides_);
48 }
49 
50 bool StorageView::operator==(const StorageView& right) const noexcept {
51  return (originPtr_ == right.originPtr_ && type_ == right.type_ && dims_ == right.dims_);
52 }
53 
54 std::ostream& operator<<(std::ostream& stream, const StorageView& s) {
55  stream << "StorageView = {\n";
56  stream << " originPtr: " << static_cast<void*>(s.originPtr_) << "\n";
57  stream << " type: " << s.type_ << "\n";
58 
59  stream << " dims: [";
60  for(auto i : s.dims_)
61  stream << " " << i;
62 
63  stream << " ]\n strides: [";
64  for(auto i : s.strides_)
65  stream << " " << i;
66 
67  stream << " ]\n}\n";
68  return stream;
69 }
70 
72  if(slice.sliceTriples().size() > dims_.size())
73  throw Exception("number of slices (%i) exceeds number of dimensions (%i)",
74  slice.sliceTriples().size(), dims_.size());
75 
76  // Append un-sliced dimensions
77  while(slice.sliceTriples().size() != dims_.size())
78  slice();
79 
80  // Expand negative Slice.stop
81  for(std::size_t i = 0; i < slice.sliceTriples().size(); ++i) {
82 
83  slice.sliceTriples()[i].stop =
84  (slice.sliceTriples()[i].stop < 0 ? dims_[i] + 1 + slice.sliceTriples()[i].stop
85  : slice.sliceTriples()[i].stop);
86  }
87  assert(slice.sliceTriples().size() == dims_.size());
88  slice_ = std::move(slice);
89 }
90 
91 bool StorageView::isMemCopyable() const noexcept {
92  // Check if data is sliced
93  if(!slice_.empty())
94  return false;
95 
96  // Check if data is col-major
97  int stride = 1;
98  if(strides_[0] != 1)
99  return false;
100 
101  // Check that there is no padding
102  for(std::size_t i = 1; i < dims_.size(); ++i) {
103  stride *= dims_[i - 1];
104  if(strides_[i] != stride)
105  return false;
106  }
107  return true;
108 }
109 
110 std::size_t StorageView::size() const noexcept {
111  std::size_t size = 1;
112  for(std::size_t i = 0; i < dims_.size(); ++i)
113  size *= (dims_[i] == 0 ? 1 : dims_[i]);
114  return size;
115 }
116 
117 std::size_t StorageView::sizeInBytes() const noexcept { return size() * bytesPerElement(); }
118 
119 void swap(StorageView& a, StorageView& b) noexcept { a.swap(b); }
120 
121 } // namespace serialbox
Byte * originPtr() noexcept
Get raw data pointer.
Definition: StorageView.h:86
friend std::ostream & operator<<(std::ostream &stream, const StorageView &s)
Convert to stream.
Definition: StorageView.cpp:54
std::size_t sizeInBytes() const noexcept
Size of the allocated, sliced data (without padding) in Bytes.
Represent a mutable view to a multi-dimensional storage.
Definition: StorageView.h:33
void setSlice(Slice slice)
Set the slice of the StorageView
Definition: StorageView.cpp:71
TypeID type() const noexcept
Get type.
Definition: StorageView.h:90
Namespace of the serialbox library.
Definition: Archive.h:25
TypeID
Type-id of types recognized by serialbox.
Definition: Type.h:55
char Byte
Represent a byte i.e sizeof(Byte) == 1.
Definition: Type.h:35
const std::vector< int > & dims() const noexcept
Get dimensions.
Definition: StorageView.h:96
std::vector< SliceTriple > & sliceTriples() noexcept
Get slice triples.
Definition: Slice.h:106
bool isMemCopyable() const noexcept
Return true if the storage is contiguous in memory (i.e no padding) and is column-major ordered...
Definition: StorageView.cpp:91
std::size_t size() const noexcept
Size of the allocated, sliced data (without padding)
bool empty() const noexcept
Check if slice is empty.
Definition: Slice.h:100
const std::vector< int > & strides() const noexcept
Get strides.
Definition: StorageView.h:99
StorageView(void *originPtr, TypeID type, const std::vector< int > &dims, const std::vector< int > &strides)
Construct StorageView.
Definition: StorageView.cpp:25
serialbox::Slice slice
Specification of the slice indices which is used for partial loading of serialized data...
Definition: Slice.h:43
bool operator==(const StorageView &right) const noexcept
Test for equality.
Definition: StorageView.cpp:50
Specification of the slice indices which is used for partial loading of serialized data...
Definition: Slice.h:53
void swap(StorageView &other) noexcept
Swap with other.
Definition: StorageView.cpp:43
Exception class which stores a human-readable error description.
Definition: Exception.h:30
int bytesPerElement() const noexcept
Get bytes per element.
Definition: StorageView.h:93