| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |  * @file    timeGaussianFactor.cpp | 
					
						
							|  |  |  |  * @brief   time GaussianFactor.eliminate | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @author  Alireza Fathi | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | #define GTSAM_MAGIC_KEY
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include <time.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*STL/C++*/ | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/tuple/tuple.hpp>
 | 
					
						
							| 
									
										
										
										
											2010-01-20 22:09:44 +08:00
										 |  |  | #include <boost/assign/std/list.hpp> // for operator += in Ordering
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/base/Matrix.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianConditional.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/Ordering.h>
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2010-01-20 22:09:44 +08:00
										 |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Alex's Machine | 
					
						
							|  |  |  |  * Results for Eliminate: | 
					
						
							|  |  |  |  * Initial (1891): 17.91 sec, 55834.7 calls/sec | 
					
						
							| 
									
										
										
										
											2010-01-27 13:08:54 +08:00
										 |  |  |  * NoiseQR       : 11.69 sec | 
					
						
							| 
									
										
										
										
											2010-01-20 22:09:44 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Results for matrix_augmented: | 
					
						
							|  |  |  |  * Initial (1891)       :  0.85 sec, 1.17647e+06 calls/sec | 
					
						
							|  |  |  |  * int->size_t Version  :  8.45 sec (for n1 reps) with memcpy version of collect() | 
					
						
							|  |  |  |  * w/ original collect():  8.73 sec (for n1 reps) | 
					
						
							|  |  |  |  * b memcpy Version     :  8.64 sec (for n1 reps) with original version of collect() | 
					
						
							|  |  |  |  * w/ memcpy collect()  :  8.40 sec (for n1 reps) | 
					
						
							| 
									
										
										
										
											2010-01-27 13:08:54 +08:00
										 |  |  |  * Rev 2100             :  8.15 sec | 
					
						
							| 
									
										
										
										
											2010-01-20 22:09:44 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int main() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   // create a linear factor
 | 
					
						
							|  |  |  |   Matrix Ax2 = Matrix_(8,2, | 
					
						
							|  |  |  | 		       // x2  
 | 
					
						
							|  |  |  | 		       -5., 0., | 
					
						
							|  |  |  | 		       +0.,-5., | 
					
						
							|  |  |  | 		       10., 0., | 
					
						
							|  |  |  | 		       +0.,10., | 
					
						
							|  |  |  | 		       -5., 0., | 
					
						
							|  |  |  | 		       +0.,-5., | 
					
						
							|  |  |  | 		       10., 0., | 
					
						
							|  |  |  | 		       +0.,10. | 
					
						
							|  |  |  | 		       ); | 
					
						
							|  |  |  |                       | 
					
						
							|  |  |  |   Matrix Al1 = Matrix_(8,10, | 
					
						
							|  |  |  | 		       // l1     
 | 
					
						
							|  |  |  | 		       5., 0.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       0., 5.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       0., 0.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       0., 0.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       5., 0.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       0., 5.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       0., 0.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       0., 0.,1.,2.,3.,4.,5.,6.,7.,8. | 
					
						
							|  |  |  | 		       ); | 
					
						
							|  |  |  |                       | 
					
						
							|  |  |  |   Matrix Ax1 = Matrix_(8,2, | 
					
						
							|  |  |  | 		       // x1
 | 
					
						
							|  |  |  | 		       0.00,  0.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       0.00,  0.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       -10.,  0.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       0.00,-10.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       0.00,  0.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       0.00,  0.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       -10.,  0.,1.,2.,3.,4.,5.,6.,7.,8., | 
					
						
							|  |  |  | 		       0.00,-10.,1.,2.,3.,4.,5.,6.,7.,8. | 
					
						
							|  |  |  | 		       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // and a RHS
 | 
					
						
							|  |  |  |   Vector b2(8); | 
					
						
							|  |  |  |   b2(0) = -1; | 
					
						
							|  |  |  |   b2(1) = 1.5; | 
					
						
							|  |  |  |   b2(2) = 2; | 
					
						
							|  |  |  |   b2(3) = -1; | 
					
						
							|  |  |  |   b2(4) = -1; | 
					
						
							|  |  |  |   b2(5) = 1.5; | 
					
						
							|  |  |  |   b2(6) = 2; | 
					
						
							|  |  |  |   b2(7) = -1; | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2010-01-20 22:09:44 +08:00
										 |  |  |   // time eliminate
 | 
					
						
							| 
									
										
										
										
											2010-01-21 02:32:48 +08:00
										 |  |  |   GaussianFactor combined("x2", Ax2,  "l1", Al1, "x1", Ax1, b2,sharedSigma(8,1)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   long timeLog = clock(); | 
					
						
							|  |  |  |   int n = 1000000; | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |   GaussianConditional::shared_ptr conditional; | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactor::shared_ptr factor; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   for(int i = 0; i < n; i++) | 
					
						
							|  |  |  |     boost::tie(conditional,factor) = combined.eliminate("x2"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   long timeLog2 = clock(); | 
					
						
							|  |  |  |   double seconds = (double)(timeLog2-timeLog)/CLOCKS_PER_SEC; | 
					
						
							| 
									
										
										
										
											2010-01-20 22:09:44 +08:00
										 |  |  |   cout << "Single Eliminate Timing:" << endl; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   cout << seconds << " seconds" << endl; | 
					
						
							|  |  |  |   cout << ((double)n/seconds) << " calls/second" << endl; | 
					
						
							| 
									
										
										
										
											2010-01-20 22:09:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // time matrix_augmented
 | 
					
						
							|  |  |  |   Ordering ordering; | 
					
						
							|  |  |  |   ordering += "x2", "l1", "x1"; | 
					
						
							|  |  |  |   size_t n1 = 10000000; | 
					
						
							|  |  |  |   timeLog = clock(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-11 05:01:06 +08:00
										 |  |  |   for(size_t i = 0; i < n1; i++) | 
					
						
							| 
									
										
										
										
											2010-01-20 22:09:44 +08:00
										 |  |  | 	  Matrix Ab = combined.matrix_augmented(ordering, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   timeLog2 = clock(); | 
					
						
							|  |  |  |   seconds = (double)(timeLog2-timeLog)/CLOCKS_PER_SEC; | 
					
						
							|  |  |  |   cout << "matrix_augmented Timing:" << endl; | 
					
						
							|  |  |  |   cout << seconds << " seconds" << endl; | 
					
						
							|  |  |  |   cout << ((double)n/seconds) << " calls/second" << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   return 0; | 
					
						
							|  |  |  | } |