Apollo  6.0
Open source self driving car software
aligned_matrix.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  * Copyright 2019 The Apollo Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *****************************************************************************/
17 #pragma once
18 
19 #include <cstddef>
20 #include <iostream>
21 
22 namespace apollo {
23 namespace localization {
24 namespace msf {
25 namespace pyramid_map {
26 
27 template <typename Scalar, int aligned_len = alignof(max_align_t)>
29  public:
30  AlignedMatrix();
31  explicit AlignedMatrix(const AlignedMatrix<Scalar, aligned_len>& matrix);
33 
34  void Init(int rows, int cols);
35  void MakeEmpty();
36  void MakeEmpty(int start_id, int end_id);
37  int GetRow() const;
38  int GetCol() const;
39  Scalar* GetData();
40  void SetData(const Scalar* data, unsigned int data_size,
41  unsigned int start_id);
42 
44 
45  inline Scalar* operator[](int row) { return row_data_[row]; }
46 
47  inline const Scalar* operator[](int row) const { return row_data_[row]; }
48 
49  protected:
50  Scalar* data_ = nullptr;
51  Scalar** row_data_ = nullptr;
52  int rows_ = 0;
53  int cols_ = 0;
54 
55  private:
56  void* raw_ptr_ = nullptr;
57  int raw_size_ = 0;
58 };
59 
60 template <typename Scalar, int aligned_len>
62  : data_(nullptr),
63  row_data_(nullptr),
64  rows_(0),
65  cols_(0),
66  raw_ptr_(nullptr),
67  raw_size_(0) {}
68 
69 template <typename Scalar, int aligned_len>
71  const AlignedMatrix<Scalar, aligned_len>& matrix) {
72  rows_ = matrix.rows_;
73  cols_ = matrix.cols_;
74 
75  raw_size_ = matrix.raw_size_;
76  raw_ptr_ = malloc(raw_size_);
77 
78  row_data_ = reinterpret_cast<Scalar**>(malloc(sizeof(Scalar*) * rows_));
79 
80  unsigned char* ptr = reinterpret_cast<unsigned char*>(raw_ptr_);
81  int idx = 0;
82  while (idx < aligned_len) {
83  if ((uint64_t)(ptr) % aligned_len == 0) {
84  break;
85  }
86  ++ptr;
87  ++idx;
88  }
89  data_ = reinterpret_cast<Scalar*>(ptr);
90 
91  for (int k = 0; k < rows_; k++) {
92  row_data_[k] = data_ + k * cols_;
93  }
94 
95  memcpy(data_, matrix.data_, sizeof(Scalar) * rows_ * cols_);
96 }
97 
98 template <typename Scalar, int aligned_len>
100  if (raw_ptr_) {
101  free(raw_ptr_);
102  raw_size_ = 0;
103  raw_ptr_ = nullptr;
104  }
105 
106  if (row_data_) {
107  free(row_data_);
108  row_data_ = nullptr;
109  }
110 
111  data_ = nullptr;
112  raw_size_ = 0;
113 }
114 
115 template <typename Scalar, int aligned_len>
117  if (raw_ptr_) {
118  free(raw_ptr_);
119  raw_size_ = 0;
120  raw_ptr_ = nullptr;
121  }
122 
123  if (row_data_) {
124  free(row_data_);
125  row_data_ = nullptr;
126  }
127 
128  rows_ = rows;
129  cols_ = cols;
130 
131  raw_size_ = static_cast<int>(sizeof(Scalar)) * (rows * cols) + aligned_len;
132  raw_ptr_ = malloc(raw_size_);
133 
134  row_data_ = reinterpret_cast<Scalar**>(malloc(sizeof(Scalar*) * rows_));
135 
136  unsigned char* ptr = reinterpret_cast<unsigned char*>(raw_ptr_);
137  int idx = 0;
138  while (idx < aligned_len) {
139  if ((uint64_t)(ptr) % aligned_len == 0) {
140  break;
141  }
142  ++ptr;
143  ++idx;
144  }
145  data_ = reinterpret_cast<Scalar*>(ptr);
146 
147  for (int k = 0; k < rows_; k++) {
148  row_data_[k] = data_ + k * cols_;
149  }
150 
151  MakeEmpty();
152 
153  // printf("aligned addr: %p\n", (void*)data_);
154 }
155 
156 template <typename Scalar, int aligned_len>
158  memset(data_, 0, sizeof(Scalar) * rows_ * cols_);
159 }
160 
161 template <typename Scalar, int aligned_len>
162 void AlignedMatrix<Scalar, aligned_len>::MakeEmpty(int start_id, int end_id) {
163  memset(data_ + start_id, 0, sizeof(Scalar) * (end_id - start_id + 1));
164 }
165 
166 template <typename Scalar, int aligned_len>
168  return rows_;
169 }
170 
171 template <typename Scalar, int aligned_len>
173  return cols_;
174 }
175 
176 template <typename Scalar, int aligned_len>
178  return data_;
179 }
180 
181 template <typename Scalar, int aligned_len>
183  unsigned int data_size,
184  unsigned int start_id) {
185  memcpy(data_ + start_id, data, sizeof(Scalar) * data_size);
186 }
187 
188 template <typename Scalar, int aligned_len>
191  if (raw_ptr_) {
192  free(raw_ptr_);
193  raw_size_ = 0;
194  raw_ptr_ = nullptr;
195  }
196 
197  if (row_data_) {
198  free(row_data_);
199  row_data_ = nullptr;
200  }
201 
202  rows_ = matrix.rows_;
203  cols_ = matrix.cols_;
204 
205  raw_size_ = matrix.raw_size_;
206  raw_ptr_ = malloc(raw_size_);
207 
208  row_data_ = reinterpret_cast<Scalar**>(malloc(sizeof(Scalar*) * rows_));
209 
210  unsigned char* ptr = reinterpret_cast<unsigned char*>(raw_ptr_);
211  int idx = 0;
212  while (idx < aligned_len) {
213  if ((uint64_t)(ptr) % aligned_len == 0) {
214  break;
215  }
216  ++ptr;
217  ++idx;
218  }
219  data_ = reinterpret_cast<Scalar*>(ptr);
220 
221  for (int k = 0; k < rows_; k++) {
222  row_data_[k] = data_ + k * cols_;
223  }
224 
225  memcpy(data_, matrix.data_, sizeof(Scalar) * rows_ * cols_);
226  return *this;
227 }
228 
229 } // namespace pyramid_map
230 } // namespace msf
231 } // namespace localization
232 } // namespace apollo
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
AlignedMatrix()
Definition: aligned_matrix.h:61
const Scalar * operator[](int row) const
Definition: aligned_matrix.h:47
AlignedMatrix & operator=(const AlignedMatrix< Scalar, aligned_len > &matrix)
Definition: aligned_matrix.h:190
void MakeEmpty()
Definition: aligned_matrix.h:157
int GetRow() const
Definition: aligned_matrix.h:167
int rows_
Definition: aligned_matrix.h:52
int cols_
Definition: aligned_matrix.h:53
~AlignedMatrix()
Definition: aligned_matrix.h:99
void Init(int rows, int cols)
Definition: aligned_matrix.h:116
int GetCol() const
Definition: aligned_matrix.h:172
Scalar * operator[](int row)
Definition: aligned_matrix.h:45
Scalar ** row_data_
Definition: aligned_matrix.h:51
void SetData(const Scalar *data, unsigned int data_size, unsigned int start_id)
Definition: aligned_matrix.h:182
Scalar * data_
Definition: aligned_matrix.h:50
Scalar * GetData()
Definition: aligned_matrix.h:177