| 
									
										
										
										
											2010-10-14 12:54:38 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							| 
									
										
										
										
											2010-10-14 12:54:38 +08:00
										 |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    timeFactorOverhead.cpp | 
					
						
							|  |  |  |  * @brief   Compares times of solving large single-factor graphs with their multi-factor equivalents. | 
					
						
							|  |  |  |  * @author  Richard Roberts | 
					
						
							| 
									
										
										
										
											2011-10-23 03:56:26 +08:00
										 |  |  |  * @date    Aug 20, 2010 | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  | #include <gtsam/base/timing.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianBayesNet.h>
 | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  | #include <gtsam/linear/GaussianFactorGraph.h>
 | 
					
						
							| 
									
										
										
										
											2012-10-03 02:36:39 +08:00
										 |  |  | #include <gtsam/linear/NoiseModel.h>
 | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  | #include <gtsam/linear/VectorValues.h>
 | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 02:05:23 +08:00
										 |  |  | #include <random>
 | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 02:05:23 +08:00
										 |  |  | static std::mt19937 rng; | 
					
						
							| 
									
										
										
										
											2020-07-24 02:54:21 +08:00
										 |  |  | static std::uniform_real_distribution<> uniform(0.0, 1.0); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int main(int argc, char *argv[]) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |   Key key = 0; | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   size_t vardim = 2; | 
					
						
							|  |  |  |   size_t blockdim = 1; | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |   size_t nBlocks = 4000; | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |   size_t nTrials = 20; | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   double blockbuild, blocksolve, combbuild, combsolve; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cout << "\n1 variable of dimension " << vardim << ", " << | 
					
						
							|  |  |  |       nBlocks << " blocks of dimension " << blockdim << "\n"; | 
					
						
							|  |  |  |   cout << nTrials << " trials\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  |   // Timing test with blockwise Gaussian factor graphs
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     // Build GFG's
 | 
					
						
							|  |  |  |     cout << "Building blockwise Gaussian factor graphs... "; | 
					
						
							|  |  |  |     cout.flush(); | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |     gttic_(blockbuild); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |     vector<GaussianFactorGraph> blockGfgs; | 
					
						
							|  |  |  |     blockGfgs.reserve(nTrials); | 
					
						
							|  |  |  |     for(size_t trial=0; trial<nTrials; ++trial) { | 
					
						
							|  |  |  |       blockGfgs.push_back(GaussianFactorGraph()); | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  |       SharedDiagonal noise = noiseModel::Isotropic::Sigma(blockdim, 1.0); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |       for(size_t i=0; i<nBlocks; ++i) { | 
					
						
							|  |  |  |         // Generate a random Gaussian factor
 | 
					
						
							|  |  |  |         Matrix A(blockdim, vardim); | 
					
						
							|  |  |  |         for(size_t j=0; j<blockdim; ++j) | 
					
						
							|  |  |  |           for(size_t k=0; k<vardim; ++k) | 
					
						
							| 
									
										
										
										
											2020-07-24 02:54:21 +08:00
										 |  |  |             A(j,k) = uniform(rng); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |         Vector b(blockdim); | 
					
						
							|  |  |  |         for(size_t j=0; j<blockdim; ++j) | 
					
						
							| 
									
										
										
										
											2020-07-24 02:54:21 +08:00
										 |  |  |           b(j) = uniform(rng); | 
					
						
							| 
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 |  |  |         blockGfgs[trial].push_back(std::make_shared<JacobianFactor>(key, A, b, noise)); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |     gttoc_(blockbuild); | 
					
						
							|  |  |  |     tictoc_getNode(blockbuildNode, blockbuild); | 
					
						
							|  |  |  |     blockbuild = blockbuildNode->secs(); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |     cout << blockbuild << " s" << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Solve GFG's
 | 
					
						
							|  |  |  |     cout << "Solving blockwise Gaussian factor graphs... "; | 
					
						
							|  |  |  |     cout.flush(); | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |     gttic_(blocksolve); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |     for(size_t trial=0; trial<nTrials; ++trial) { | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //      cout << "Trial " << trial << endl;
 | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |       GaussianBayesNet::shared_ptr gbn = blockGfgs[trial].eliminateSequential(); | 
					
						
							|  |  |  |       VectorValues soln = gbn->optimize(); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |     gttoc_(blocksolve); | 
					
						
							|  |  |  |     tictoc_getNode(blocksolveNode, blocksolve); | 
					
						
							|  |  |  |     blocksolve = blocksolveNode->secs(); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |     cout << blocksolve << " s" << endl; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  |   // Timing test with combined-factor Gaussian factor graphs
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     // Build GFG's
 | 
					
						
							|  |  |  |     cout << "Building combined-factor Gaussian factor graphs... "; | 
					
						
							|  |  |  |     cout.flush(); | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |     gttic_(combbuild); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |     vector<GaussianFactorGraph> combGfgs; | 
					
						
							|  |  |  |     for(size_t trial=0; trial<nTrials; ++trial) { | 
					
						
							|  |  |  |       combGfgs.push_back(GaussianFactorGraph()); | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  |       SharedDiagonal noise = noiseModel::Isotropic::Sigma(blockdim, 1.0); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       Matrix Acomb(blockdim*nBlocks, vardim); | 
					
						
							|  |  |  |       Vector bcomb(blockdim*nBlocks); | 
					
						
							|  |  |  |       for(size_t i=0; i<nBlocks; ++i) { | 
					
						
							|  |  |  |         // Generate a random Gaussian factor
 | 
					
						
							|  |  |  |         for(size_t j=0; j<blockdim; ++j) | 
					
						
							|  |  |  |           for(size_t k=0; k<vardim; ++k) | 
					
						
							| 
									
										
										
										
											2020-07-24 02:54:21 +08:00
										 |  |  |             Acomb(blockdim*i+j, k) = uniform(rng); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |         Vector b(blockdim); | 
					
						
							|  |  |  |         for(size_t j=0; j<blockdim; ++j) | 
					
						
							| 
									
										
										
										
											2020-07-24 02:54:21 +08:00
										 |  |  |           bcomb(blockdim*i+j) = uniform(rng); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 |  |  |       combGfgs[trial].push_back(std::make_shared<JacobianFactor>(key, Acomb, bcomb, | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |           noiseModel::Isotropic::Sigma(blockdim*nBlocks, 1.0))); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |     gttoc(combbuild); | 
					
						
							|  |  |  |     tictoc_getNode(combbuildNode, combbuild); | 
					
						
							|  |  |  |     combbuild = combbuildNode->secs(); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |     cout << combbuild << " s" << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Solve GFG's
 | 
					
						
							|  |  |  |     cout << "Solving combined-factor Gaussian factor graphs... "; | 
					
						
							|  |  |  |     cout.flush(); | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |     gttic_(combsolve); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |     for(size_t trial=0; trial<nTrials; ++trial) { | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |       GaussianBayesNet::shared_ptr gbn = combGfgs[trial].eliminateSequential(); | 
					
						
							|  |  |  |       VectorValues soln = gbn->optimize(); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-06-22 07:26:48 +08:00
										 |  |  |     gttoc_(combsolve); | 
					
						
							|  |  |  |     tictoc_getNode(combsolveNode, combsolve); | 
					
						
							|  |  |  |     combsolve = combsolveNode->secs(); | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |     cout << combsolve << " s" << endl; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  |   // Print per-graph times
 | 
					
						
							|  |  |  |   cout << "\nPer-factor-graph times for building and solving\n"; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   cout << "Blockwise:  total " << (1000.0*(blockbuild+blocksolve)/double(nTrials)) << | 
					
						
							|  |  |  |       "  build " << (1000.0*blockbuild/double(nTrials)) << | 
					
						
							|  |  |  |       "  solve " << (1000.0*blocksolve/double(nTrials)) << " ms/graph\n"; | 
					
						
							|  |  |  |   cout << "Combined:   total " << (1000.0*(combbuild+combsolve)/double(nTrials)) << | 
					
						
							|  |  |  |       "  build " << (1000.0*combbuild/double(nTrials)) << | 
					
						
							|  |  |  |       "  solve " << (1000.0*combsolve/double(nTrials)) << " ms/graph\n"; | 
					
						
							| 
									
										
										
										
											2010-08-22 01:54:59 +08:00
										 |  |  |   cout << "Fraction of time spent in overhead\n" << | 
					
						
							|  |  |  |       "  total " << (((blockbuild+blocksolve)-(combbuild+combsolve)) / (blockbuild+blocksolve)) << "\n" << | 
					
						
							|  |  |  |       "  build " << ((blockbuild-combbuild) / blockbuild) << "\n" << | 
					
						
							|  |  |  |       "  solve " << ((blocksolve-combsolve) / blocksolve) << "\n"; | 
					
						
							|  |  |  |   cout << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |