Agora  1.2.0
Agora project
memory_manage.h
Go to the documentation of this file.
1 
6 #ifndef MEMORY_MANAGE_H_
7 #define MEMORY_MANAGE_H_
8 
9 #include <array>
10 #include <cassert>
11 #include <complex>
12 #include <cstddef>
13 #include <cstdlib>
14 #include <cstring>
15 #include <random>
16 
17 namespace Agora_memory {
18 enum class Alignment_t : size_t {
19  kAlign32 = 32,
20  kAlign64 = 64,
21  kAlign4096 = 4096
22 };
23 
24 void* PaddedAlignedAlloc(Alignment_t alignment, size_t size);
25 } // namespace Agora_memory
26 
27 template <typename T>
28 class Table {
29  private:
30  size_t dim2_{0};
31  size_t dim1_{0};
32  T* data_;
33 
34  public:
35  Table() : data_(nullptr) {}
36 
37  void Malloc(size_t dim1, size_t dim2, Agora_memory::Alignment_t alignment) {
38  this->dim2_ = dim2;
39  this->dim1_ = dim1;
40  // RtAssert(((dim1 > 0) && (dim2 == 0)), "Table: Malloc one dimension = 0");
41  size_t alloc_size = (this->dim1_ * this->dim2_ * sizeof(T));
42  this->data_ = static_cast<T*>(
43  Agora_memory::PaddedAlignedAlloc(alignment, alloc_size));
44  }
45  void Calloc(size_t dim1, size_t dim2, Agora_memory::Alignment_t alignment) {
46  // RtAssert(((dim1 > 0) && (dim2 == 0)), "Table: Calloc one dimension = 0");
47  this->Malloc(dim1, dim2, alignment);
48  std::memset(static_cast<void*>(this->data_), 0,
49  (this->dim1_ * this->dim2_ * sizeof(T)));
50  }
51 
52  // Allocate the table and fill it with random floating point values between
53  // -1.0 and 1.0
54  void RandAllocFloat(size_t dim1, size_t dim2,
55  Agora_memory::Alignment_t alignment) {
56  std::default_random_engine generator;
57  std::uniform_real_distribution<float> distribution(-1.0, 1.0);
58 
59  assert(sizeof(T) >= sizeof(float));
60  this->Malloc(dim1, dim2, alignment);
61  auto* base = reinterpret_cast<float*>(this->data_);
62  for (size_t i = 0; i < (dim1 * dim2); i++) {
63  base[i] = distribution(generator);
64  }
65  }
66 
67  // Allocate the table and fill it with random complex floating point values
68  // between -1.0 and 1.0
69  void RandAllocCxFloat(size_t dim1, size_t dim2,
70  Agora_memory::Alignment_t alignment) {
71  std::default_random_engine generator;
72  std::uniform_real_distribution<float> distribution(-1.0, 1.0);
73 
74  assert(sizeof(T) >= sizeof(std::complex<float>));
75  this->Malloc(dim1, dim2, alignment);
76  auto* base = reinterpret_cast<std::complex<float>*>(this->data_);
77  for (size_t i = 0; i < (dim1 * dim2); i++) {
78  base[i] = {distribution(generator), distribution(generator)};
79  }
80  }
81 
82  bool IsAllocated() { return (this->data_ != nullptr); }
83 
84  void Free() {
85  if (this->data_ != nullptr) {
86  std::free(this->data_);
87  }
88  this->dim2_ = 0u;
89  this->dim1_ = 0u;
90  this->data_ = nullptr;
91  }
92 
93  const T* At(size_t dim1) const { return (data_ + (dim1 * dim2_)); }
94 
95  T* operator[](size_t dim1) {
96  assert(this->dim1_ > dim1);
97  return (this->data_ + (dim1 * this->dim2_));
98  }
99 
100  size_t Dim1() { return (this->dim1_); }
101  size_t Dim2() { return (this->dim2_); }
102 };
103 
104 template <typename T, typename U>
105 static void AllocBuffer1d(T** buffer, U dim,
106  Agora_memory::Alignment_t alignment, int init_zero) {
107  size_t size = dim * sizeof(T);
108  // RtAssert(((dim > 0)), "AllocBuffer1d: size = 0");
109  *buffer = static_cast<T*>(Agora_memory::PaddedAlignedAlloc(alignment, size));
110  if (init_zero) {
111  std::memset(static_cast<void*>(*buffer), 0u, size);
112  }
113 };
114 
115 template <typename T>
116 static void FreeBuffer1d(T** buffer) {
117  std::free(*buffer);
118 };
119 
120 // PtrGrid is a 2D grid of pointers with [ROWS] rows and [COLS] columns. Each
121 // entry of the grid is a pointer to an array of [T].
122 template <size_t ROWS, size_t COLS, class T>
123 class PtrGrid {
124  public:
125  PtrGrid() : backing_buf_(nullptr) {}
126 
129  explicit PtrGrid(size_t num_entries) { this->Alloc(ROWS, COLS, num_entries); }
130 
135  PtrGrid(size_t n_rows, size_t n_cols, size_t n_entries) {
136  assert(n_rows <= ROWS && n_cols <= COLS);
137  this->Alloc(n_rows, n_cols, n_entries);
138  }
139 
141  if (this->backing_buf_ != nullptr) {
142  std::free(this->backing_buf_);
143  this->backing_buf_ = nullptr;
144  }
145  }
146 
148  void Alloc(size_t n_rows, size_t n_cols, size_t n_entries) {
149  const size_t alloc_sz = n_rows * n_cols * n_entries * sizeof(T);
150  this->backing_buf_ = static_cast<T*>(Agora_memory::PaddedAlignedAlloc(
152  std::memset(static_cast<void*>(this->backing_buf_), 0, alloc_sz);
153 
154  // Fill-in the grid with pointers into backing_buf
155  size_t offset = 0;
156  for (size_t i = 0; i < n_rows; i++) {
157  for (size_t j = 0; j < n_cols; j++) {
158  mat_[i][j] = &this->backing_buf_[offset];
159  offset += n_entries;
160  }
161  }
162  }
163 
166  void RandAllocCxFloat(size_t n_entries) {
167  static_assert(sizeof(T) == 2 * sizeof(float), "T must be complex_float");
168  Alloc(ROWS, COLS, n_entries);
169 
170  std::default_random_engine generator;
171  std::uniform_real_distribution<float> distribution(-1.0, 1.0);
172 
173  for (auto& row : mat_) {
174  for (auto& entry : row) {
175  auto* base = reinterpret_cast<float*>(entry);
176  for (size_t i = 0; i < n_entries * 2; i++) {
177  base[i] = distribution(generator);
178  }
179  }
180  }
181  }
182 
183  std::array<T*, COLS>& operator[](size_t row_idx) {
184  return this->mat_[row_idx];
185  }
186 
187  // Delete copy constructor and copy assignment
188  PtrGrid(PtrGrid const&) = delete;
189  PtrGrid& operator=(PtrGrid const&) = delete;
190 
191  private:
192  std::array<std::array<T*, COLS>, ROWS> mat_;
193 
197 };
198 
199 // PtrCube is a 3D cube of pointers. Each entry of the cube is a pointer to an
200 // array of [T].
201 template <size_t DIM1, size_t DIM2, size_t DIM3, class T>
202 class PtrCube {
203  public:
204  PtrCube() : backing_buf_(nullptr) {}
205 
208  explicit PtrCube(size_t num_entries) {
209  this->Alloc(DIM1, DIM2, DIM3, num_entries);
210  }
211 
216  PtrCube(size_t dim_1, size_t dim_2, size_t dim_3, size_t n_entries) {
217  assert(dim_1 <= DIM1 && dim_2 <= DIM2 && dim_3 <= DIM3);
218  this->Alloc(dim_1, dim_2, dim_3, n_entries);
219  }
220 
222  if (this->backing_buf_ != nullptr) {
223  std::free(this->backing_buf_);
224  this->backing_buf_ = nullptr;
225  }
226  }
227 
229  void Alloc(size_t dim_1, size_t dim_2, size_t dim_3, size_t n_entries) {
230  const size_t alloc_sz = dim_1 * dim_2 * dim_3 * n_entries * sizeof(T);
231  this->backing_buf_ = static_cast<T*>(Agora_memory::PaddedAlignedAlloc(
233  std::memset(static_cast<void*>(this->backing_buf_), 0, alloc_sz);
234 
235  // Fill-in the grid with pointers into backing_buf
236  for (auto& mat : this->cube_) {
237  for (auto& row : mat) {
238  for (auto& entry : row) {
239  entry = nullptr;
240  }
241  }
242  }
243 
244  size_t offset = 0;
245  for (size_t i = 0; i < dim_1; i++) {
246  for (size_t j = 0; j < dim_2; j++) {
247  for (size_t k = 0; k < dim_3; k++) {
248  this->cube_[i][j][k] = &this->backing_buf_[offset];
249  offset += n_entries;
250  }
251  }
252  }
253  }
254 
255  std::array<std::array<T*, DIM3>, DIM2>& operator[](size_t row_idx) {
256  return this->cube_[row_idx];
257  }
258 
259  // Delete copy constructor and copy assignment
260  PtrCube(PtrCube const&) = delete;
261  PtrCube& operator=(PtrCube const&) = delete;
262 
263  private:
265  std::array<std::array<std::array<T*, DIM3>, DIM2>, DIM1> cube_;
266 
270 };
271 
272 #endif // MEMORY_MANAGE_H_
PtrGrid::Alloc
void Alloc(size_t n_rows, size_t n_cols, size_t n_entries)
Allocate [n_entries] entries per pointer cell.
Definition: memory_manage.h:148
Table::Calloc
void Calloc(size_t dim1, size_t dim2, Agora_memory::Alignment_t alignment)
Definition: memory_manage.h:45
Table::IsAllocated
bool IsAllocated()
Definition: memory_manage.h:82
size
end IFFT Reshape the symbol vector into two different spatial streams size
Definition: generate_data.m:73
FreeBuffer1d
static void FreeBuffer1d(T **buffer)
Definition: memory_manage.h:116
Table::At
const T * At(size_t dim1) const
Definition: memory_manage.h:93
Agora_memory::PaddedAlignedAlloc
void * PaddedAlignedAlloc(Alignment_t alignment, size_t size)
Definition: memory_manage.cc:15
Table::Table
Table()
Definition: memory_manage.h:35
memory_manage.h
PtrGrid::PtrGrid
PtrGrid(size_t num_entries)
Definition: memory_manage.h:129
PtrGrid::operator[]
std::array< T *, COLS > & operator[](size_t row_idx)
Definition: memory_manage.h:183
Agora_memory::Alignment_t
Alignment_t
Definition: memory_manage.h:18
AllocBuffer1d
static void AllocBuffer1d(T **buffer, U dim, Agora_memory::Alignment_t alignment, int init_zero)
Definition: memory_manage.h:105
PtrCube::PtrCube
PtrCube(size_t num_entries)
Definition: memory_manage.h:208
Table
Definition: memory_manage.h:28
T
T
Definition: simulate_performance.m:4
Table::dim1_
size_t dim1_
Definition: memory_manage.h:31
Table::operator[]
T * operator[](size_t dim1)
Definition: memory_manage.h:95
PtrGrid
Definition: memory_manage.h:123
PtrCube::operator[]
std::array< std::array< T *, DIM3 >, DIM2 > & operator[](size_t row_idx)
Definition: memory_manage.h:255
PtrGrid::operator=
PtrGrid & operator=(PtrGrid const &)=delete
plot_csv.row
list row
Definition: plot_csv.py:11
Table::Dim1
size_t Dim1()
Definition: memory_manage.h:100
PtrGrid::PtrGrid
PtrGrid()
Definition: memory_manage.h:125
Table::Free
void Free()
Definition: memory_manage.h:84
PtrCube::PtrCube
PtrCube(size_t dim_1, size_t dim_2, size_t dim_3, size_t n_entries)
Definition: memory_manage.h:216
i
for i
Definition: generate_data.m:107
u
Plot Rx waveform for u
Definition: inspect_single_frame.m:108
Agora_memory::Alignment_t::kAlign64
@ kAlign64
plot_csv.n_rows
n_rows
Definition: plot_csv.py:18
PtrGrid::~PtrGrid
~PtrGrid()
Definition: memory_manage.h:140
PtrGrid::backing_buf_
T * backing_buf_
The pointer cells.
Definition: memory_manage.h:196
plot_csv.n_cols
n_cols
Definition: plot_csv.py:18
PtrGrid::mat_
std::array< std::array< T *, COLS >, ROWS > mat_
Definition: memory_manage.h:192
PtrCube::operator=
PtrCube & operator=(PtrCube const &)=delete
Agora_memory
Definition: memory_manage.cc:3
PtrCube::PtrCube
PtrCube()
Definition: memory_manage.h:204
Agora_memory::Alignment_t::kAlign32
@ kAlign32
PtrCube::backing_buf_
T * backing_buf_
Definition: memory_manage.h:269
PtrCube::~PtrCube
~PtrCube()
Definition: memory_manage.h:221
Table::Dim2
size_t Dim2()
Definition: memory_manage.h:101
Table::Malloc
void Malloc(size_t dim1, size_t dim2, Agora_memory::Alignment_t alignment)
Definition: memory_manage.h:37
PtrGrid::PtrGrid
PtrGrid(size_t n_rows, size_t n_cols, size_t n_entries)
Definition: memory_manage.h:135
Agora_memory::Alignment_t::kAlign4096
@ kAlign4096
Table::dim2_
size_t dim2_
Definition: memory_manage.h:30
PtrCube::Alloc
void Alloc(size_t dim_1, size_t dim_2, size_t dim_3, size_t n_entries)
Allocate [n_entries] entries per pointer cell.
Definition: memory_manage.h:229
PtrCube
Definition: memory_manage.h:202
Agora_memory::PaddedAllocSize
size_t PaddedAllocSize(Alignment_t alignment, size_t size)
Definition: memory_manage.cc:4
Table::data_
T * data_
Definition: memory_manage.h:32
Table::RandAllocCxFloat
void RandAllocCxFloat(size_t dim1, size_t dim2, Agora_memory::Alignment_t alignment)
Definition: memory_manage.h:69
PtrCube::cube_
std::array< std::array< std::array< T *, DIM3 >, DIM2 >, DIM1 > cube_
The pointer cells.
Definition: memory_manage.h:265
Table::RandAllocFloat
void RandAllocFloat(size_t dim1, size_t dim2, Agora_memory::Alignment_t alignment)
Definition: memory_manage.h:54
PtrGrid::RandAllocCxFloat
void RandAllocCxFloat(size_t n_entries)
Definition: memory_manage.h:166