Optimized Belief Propagation (CPU and GPU)
BpLevel.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2024 Scott Grauer-Gray
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
27 #include <array>
28 #include <cmath>
31 
32 #ifndef BP_LEVEL_H_
33 #define BP_LEVEL_H_
34 
35 namespace beliefprop {
36 
43  unsigned int width_level_;
44  unsigned int height_level_;
45  unsigned int bytes_align_memory_;
48  unsigned int level_num_;
49  std::size_t offset_into_arrays_;
50 };
51 
52 };
53 
58 template <RunData_t T>
59 class BpLevel
60 {
61 public:
71  explicit BpLevel(
72  const std::array<unsigned int, 2>& width_height,
73  std::size_t offset_into_arrays,
74  unsigned int level_num,
75  run_environment::AccSetting acc_setting);
76 
86  explicit BpLevel(
87  const std::array<unsigned int, 2>& width_height,
88  std::size_t offset_into_arrays,
89  unsigned int level_num,
90  unsigned int bytes_align_memory);
91 
99  BpLevel NextBpLevel(unsigned int num_disparity_values) const;
100 
109  std::size_t NumDataInBpArrays(unsigned int num_disparity_values) const;
110 
118  unsigned int CheckerboardWidth(unsigned int width_level) const;
119 
130  unsigned int PaddedCheckerboardWidth(unsigned int checkerboard_width) const;
131 
139  std::size_t NumDataForAlignedMemoryAtLevel(
140  const std::array<unsigned int, 2>& width_height_level,
141  unsigned int num_possible_disparities) const;
142 
153  static std::size_t TotalDataForAlignedMemoryAllLevels(
154  const std::array<unsigned int, 2>& width_height_bottom_level,
155  unsigned int num_possible_disparities,
156  unsigned int num_levels,
157  run_environment::AccSetting acceleration);
158 
166  return level_properties_;
167  }
168 
169 private:
170  beliefprop::BpLevelProperties level_properties_;
171 };
172 
173 template <RunData_t T>
175  const std::array<unsigned int, 2>& width_height,
176  std::size_t offset_into_arrays,
177  unsigned int level_num,
178  run_environment::AccSetting acc_setting)
179 {
180  level_properties_.width_level_ = width_height[0];
181  level_properties_.height_level_ = width_height[1];
182  level_properties_.level_num_ = level_num;
183  level_properties_.offset_into_arrays_= offset_into_arrays;
184  level_properties_.bytes_align_memory_ =
186  level_properties_.width_checkerboard_level_ =
187  CheckerboardWidth(level_properties_.width_level_);
188  level_properties_.padded_width_checkerboard_level_ =
189  PaddedCheckerboardWidth(level_properties_.width_checkerboard_level_);
190 }
191 
192 
193 template <RunData_t T>
195  const std::array<unsigned int, 2>& width_height,
196  std::size_t offset_into_arrays,
197  unsigned int level_num,
198  unsigned int bytes_align_memory)
199 {
200  level_properties_.width_level_ = width_height[0];
201  level_properties_.height_level_ = width_height[1];
202  level_properties_.level_num_ = level_num;
203  level_properties_.offset_into_arrays_ = offset_into_arrays;
204  level_properties_.bytes_align_memory_ = bytes_align_memory;
205  level_properties_.width_checkerboard_level_ =
206  CheckerboardWidth(level_properties_.width_level_);
207  level_properties_.padded_width_checkerboard_level_ =
208  PaddedCheckerboardWidth(level_properties_.width_checkerboard_level_);
209 }
210 
211 //get bp level properties for next (higher) level in hierarchy that processes
212 //data with half width/height of current level
213 template <RunData_t T>
214 BpLevel<T> BpLevel<T>::NextBpLevel(unsigned int num_disparity_values) const
215 {
216  const std::size_t offset_next_level =
217  level_properties_.offset_into_arrays_ +
218  NumDataInBpArrays(num_disparity_values);
219  return BpLevel<T>(
220  {(unsigned int)std::ceil((float)level_properties_.width_level_ / 2.0f),
221  (unsigned int)std::ceil((float)level_properties_.height_level_ / 2.0f)},
222  offset_next_level,
223  (level_properties_.level_num_ + 1),
224  level_properties_.bytes_align_memory_);
225 }
226 
227 //get the amount of data in each BP array (data cost/messages for each
228 //checkerboard) at the current level with the specified number of possible
229 //disparity values
230 template <RunData_t T>
232  unsigned int num_disparity_values) const
233 {
234  return NumDataForAlignedMemoryAtLevel(
235  {level_properties_.width_level_, level_properties_.height_level_},
236  num_disparity_values);
237 }
238 
239 template <RunData_t T>
241  const std::array<unsigned int, 2>& width_height_level,
242  unsigned int num_possible_disparities) const
243 {
244  const std::size_t numDataAtLevel =
245  (std::size_t)PaddedCheckerboardWidth(CheckerboardWidth(width_height_level[0])) *
246  ((std::size_t)width_height_level[1]) *
247  (std::size_t)num_possible_disparities;
248  std::size_t numBytesAtLevel = numDataAtLevel * sizeof(T);
249 
250  if ((numBytesAtLevel % level_properties_.bytes_align_memory_) == 0) {
251  return numDataAtLevel;
252  }
253  else {
254  numBytesAtLevel +=
255  (level_properties_.bytes_align_memory_ -
256  (numBytesAtLevel % level_properties_.bytes_align_memory_));
257  return (numBytesAtLevel / sizeof(T));
258  }
259 }
260 
261 template <RunData_t T>
263  unsigned int width_level) const
264 {
265  return (unsigned int)std::ceil(((float)width_level) / 2.0f);
266 }
267 
268 template <RunData_t T>
270  unsigned int checkerboard_width) const
271 {
272  size_t num_data_align_width = level_properties_.bytes_align_memory_ / sizeof(T);
273  //add "padding" to checkerboard width if necessary for alignment
274  return ((checkerboard_width % num_data_align_width) == 0) ?
275  checkerboard_width :
276  (checkerboard_width +
277  (num_data_align_width - (checkerboard_width % num_data_align_width)));
278 }
279 
280 //static function
281 template <RunData_t T>
283  const std::array<unsigned int, 2>& width_height_bottom_level,
284  unsigned int num_possible_disparities,
285  unsigned int num_levels,
286  run_environment::AccSetting acceleration)
287 {
288  BpLevel<T> curr_level_properties(width_height_bottom_level, 0, 0, acceleration);
289  std::size_t total_data =
290  curr_level_properties.NumDataInBpArrays(num_possible_disparities);
291  for (unsigned int curr_level = 1; curr_level < num_levels; curr_level++)
292  {
293  curr_level_properties = curr_level_properties.NextBpLevel(num_possible_disparities);
294  total_data += curr_level_properties.NumDataInBpArrays(num_possible_disparities);
295  }
296 
297  return total_data;
298 }
299 
300 #endif //BP_LEVEL_H_
Declares and defines structure that stores settings for current implementation run as well as functio...
Define constraints for data type in processing.
Class to store and retrieve properties of a bp processing level including a data type specified as a ...
Definition: BpLevel.h:60
BpLevel(const std::array< unsigned int, 2 > &width_height, std::size_t offset_into_arrays, unsigned int level_num, run_environment::AccSetting acc_setting)
Construct a new BpLevel object with specified height/wdith, offset into data/message arrays,...
Definition: BpLevel.h:174
std::size_t NumDataInBpArrays(unsigned int num_disparity_values) const
Get the amount of data in each BP array (data cost/messages for each checkerboard) at the current lev...
Definition: BpLevel.h:231
const beliefprop::BpLevelProperties & LevelProperties() const
Return level properties as const reference to avoid copying and not allow it to be modified.
Definition: BpLevel.h:165
unsigned int CheckerboardWidth(unsigned int width_level) const
Get width of checkerboard to use in bp processing at level. Checkerboard width is half of the width ...
Definition: BpLevel.h:262
unsigned int PaddedCheckerboardWidth(unsigned int checkerboard_width) const
Get width of padded checkerboard to use in bp processing at level. Padding can add additional width ...
Definition: BpLevel.h:269
std::size_t NumDataForAlignedMemoryAtLevel(const std::array< unsigned int, 2 > &width_height_level, unsigned int num_possible_disparities) const
Get count of total data needed for bp processing at level.
Definition: BpLevel.h:240
BpLevel NextBpLevel(unsigned int num_disparity_values) const
Get bp level properties for next (higher) level in hierarchy that processes data with half width/heig...
Definition: BpLevel.h:214
static std::size_t TotalDataForAlignedMemoryAllLevels(const std::array< unsigned int, 2 > &width_height_bottom_level, unsigned int num_possible_disparities, unsigned int num_levels, run_environment::AccSetting acceleration)
Static function to get count of total data needed for bp processing at all levels.
Definition: BpLevel.h:282
Namespace for enums, constants, structures, and functions specific to belief propagation processing.
AccSetting
Enum for acceleration setting.
unsigned int GetBytesAlignMemory(AccSetting accel_setting)
Definition: RunSettings.h:43
POD struct to store bp level data. Struct can be passed to global CUDAs kernel so needs to take restr...
Definition: BpLevel.h:42
unsigned int height_level_
Definition: BpLevel.h:44
unsigned int width_checkerboard_level_
Definition: BpLevel.h:46
unsigned int bytes_align_memory_
Definition: BpLevel.h:45
unsigned int width_level_
Definition: BpLevel.h:43
std::size_t offset_into_arrays_
Definition: BpLevel.h:49
unsigned int padded_width_checkerboard_level_
Definition: BpLevel.h:47