Optimized Belief Propagation (CPU and GPU)
DriverBpStereoCPU_customRun.cpp
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 
29 #include <iostream>
38 
51 int main(int argc, char** argv)
52 {
53  //get input stereo set, disparity count, and output disparity map file path
54  //from input parameters
55  const std::array<std::string, 2> refTestImPath{argv[1], argv[2]};
56  beliefprop::BpSettings alg_settings;
57  alg_settings.num_disp_vals = std::stoi(argv[3]);
58  const std::string out_disp_map_path{argv[4]};
59 
60  //generate bp algorithm and parallel parameter settings to use in run
61  alg_settings.disc_k_bp = (float)alg_settings.num_disp_vals / 7.5f;
62  const unsigned int dispMapScale = 256 / alg_settings.num_disp_vals;
63  ParallelParamsBp parallel_params{
65  alg_settings.num_levels,
67 
68  //generate optimized CPU implementation object to run belief propagation on
69  //input stereo set
70  //implementation is optimized using OpenMP and vectorization, with specific
71  //vectorization dependent on what's available on the target architecture
72 #if (CPU_VECTORIZATION_DEFINE == AVX_512_DEFINE)
73  std::unique_ptr<RunBpOnStereoSet<float, 0, run_environment::AccSetting::kAVX512>>
74  runOptBpNumItersNoTemplate =
75  std::make_unique<RunBpOnStereoSetOptimizedCPU<float, 0, run_environment::AccSetting::kAVX512>>();
76 #elif (CPU_VECTORIZATION_DEFINE == AVX_512_F16_DEFINE)
77  std::unique_ptr<RunBpOnStereoSet<float, 0, run_environment::AccSetting::kAVX512_F16>>
78  runOptBpNumItersNoTemplate =
79  std::make_unique<RunBpOnStereoSetOptimizedCPU<float, 0, run_environment::AccSetting::kAVX512_F16>>();
80 #elif (CPU_VECTORIZATION_DEFINE == AVX_256_DEFINE)
81  std::unique_ptr<RunBpOnStereoSet<float, 0, run_environment::AccSetting::kAVX256>>
82  runOptBpNumItersNoTemplate =
83  std::make_unique<RunBpOnStereoSetOptimizedCPU<float, 0, run_environment::AccSetting::kAVX256>>();
84 #elif (CPU_VECTORIZATION_DEFINE == AVX_256_F16_DEFINE)
85  std::unique_ptr<RunBpOnStereoSet<float, 0, run_environment::AccSetting::kAVX256_F16>>
86  runOptBpNumItersNoTemplate =
87  std::make_unique<RunBpOnStereoSetOptimizedCPU<float, 0, run_environment::AccSetting::kAVX256_F16>>();
88 #elif (CPU_VECTORIZATION_DEFINE == NEON_DEFINE)
89  std::unique_ptr<RunBpOnStereoSet<float, 0, run_environment::AccSetting::kNEON>>
90  runOptBpNumItersNoTemplate =
91  std::make_unique<RunBpOnStereoSetOptimizedCPU<float, 0, run_environment::AccSetting::kNEON>>();
92 #endif //CPU_VECTORIZATION_DEFINE
93  //run optimized belief propagation implementation on CPU on specified input stereo set
94  //using specified algorithm settings and parallel parameters
95  //resulting run output includes runtime and computed disparity map
96  const auto run_output = runOptBpNumItersNoTemplate->operator()(
97  {refTestImPath[0], refTestImPath[1]}, alg_settings, parallel_params);
98 
99  //display implementation run time and save disparity map to specified file
100  //path
101  std::cout << "BP processing runtime (optimized w/ OpenMP + SIMD on CPU): "
102  << run_output->run_time.count() << std::endl;
103  run_output->out_disparity_map.SaveDisparityMap(out_disp_map_path, dispMapScale);
104  std::cout << "Output disparity map saved to " << out_disp_map_path << std::endl;
105 
106  //commented out code to compare result to single-thread CPU run...currently
107  //only works if disparity count known at compile time since the single-thread
108  //CPU implementation only works with disparity count given as template value
109  /*if ((argc > 5) && (std::string(argv[5]) == "comp")) {
110  std::unique_ptr<RunBpOnStereoSet<float, 64, run_environment::AccSetting::kNone>> runBpStereoSingleThread =
111  std::make_unique<RunBpOnStereoSetSingleThreadCPU<float, 64, run_environment::AccSetting::kNone>>();
112  auto run_output_single_thread = runBpStereoSingleThread->operator()({refTestImPath[0], refTestImPath[1]}, alg_settings, parallel_params);
113  std::cout << "BP processing runtime (single threaded imp): " << run_output_single_thread->run_time.count() << std::endl;
114  const auto outComp = run_output_single_thread->out_disparity_map.OutputComparison(run_output->out_disparity_map, beliefprop::DisparityMapEvaluationParams());
115  std::cout << "Difference between resulting disparity maps (no difference expected)" << std::endl;
116  std::cout << outComp.AsRunData();
117  }*/
118 
119  return 0;
120 }
Belief propagation implementation constants related to file processing.
int main(int argc, char **argv)
main() function to run optimized CPU belief propagation implementation on CPU on an input stereo set ...
Declares abstract class to set up and run belief propagation on target device using specified acceler...
Declares child class of RunBpOnStereoSet to run optimized CPU implementation of belief propagation on...
Contains namespace with CPU run defaults and constants.
Declares child class of RunImpMultInputs to run specified belief propagation implementation on a numb...
Declares class to run and evaluate implementation(s) of an algorithm using multiple settings includin...
Child class of ParallelParams to store and process parallelization parameters to use in each BP kerne...
const std::array< unsigned int, 2 > kParallelParamsDefault
Default parallel parameters setting on CPU.
Structure to store the belief propagation settings including the number of levels and iterations.
Definition: BpSettings.h:87
unsigned int num_disp_vals
Number of disparity values must be set for each stereo set.
Definition: BpSettings.h:101
unsigned int num_levels
Definition: BpSettings.h:89
float disc_k_bp
Discontinuity cost cap set to high value by default but is expected to be dependent on number of disp...
Definition: BpSettings.h:98