ortho_range_search_main.cc

Go to the documentation of this file.
00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00038 #include "data_aux.h"
00039 #include "naive_ortho_range_search.h"
00040 #include "ortho_range_search.h"
00041 
00042 #include "fastlib/mmanager/memory_manager.h"
00043 
00044 void OutputOrhogonalRangeSearchResults(const GenMatrix<bool> &search_results,
00045                                        const char *file_name) {
00046   
00047   FILE *file_pointer = fopen(file_name, "w+");
00048 
00049   for(index_t r = 0; r < search_results.n_rows(); r++) {
00050     for(index_t c = 0; c < search_results.n_cols(); c++) {
00051 
00052       if(search_results.get(r, c)) {
00053         fprintf(file_pointer, "1 ");
00054       }
00055       else {
00056         fprintf(file_pointer, "0 ");
00057       }
00058     }
00059     fprintf(file_pointer, "\n");
00060   }
00061 }
00062 
00112 int main(int argc, char *argv[]) {
00113 
00114   // Always initialize FASTexec with main's inputs at the beggining of
00115   // your program.  This reads the command line, among other things.
00116   fx_init(argc, argv, NULL);
00117 
00118   // Initialize Nick's memory manager...
00119   std::string pool_name("/scratch/temp_mem");
00120   mmapmm::MemoryManager<false>::allocator_ =
00121     new mmapmm::MemoryManager<false>();
00122   mmapmm::MemoryManager<false>::allocator_->set_pool_name(pool_name);
00123   mmapmm::MemoryManager<false>::allocator_->set_capacity(65536*1000);
00124   mmapmm::MemoryManager<false>::allocator_->Init();
00125 
00127 
00128   // The reference data file is a required parameter.
00129   const char* dataset_file_name = fx_param_str(NULL, "data", "data.csv");
00130 
00131   // column-oriented dataset matrix.
00132   GenMatrix<double> dataset;
00133 
00134   // data::Load inits a matrix with the contents of a .csv or .arff.
00135   data::Load(dataset_file_name, &dataset);
00136 
00137   // Generate the 200 uniformly distributed search ranges.
00138   GenMatrix<double> low_coord_limits, high_coord_limits;
00139   
00140   // The file containing the lower and the upper limits of each search
00141   // window.
00142   const char *lower_limit_file_name = fx_param_str(NULL, "lower", "lower.csv");
00143   const char *upper_limit_file_name = fx_param_str(NULL, "upper", "upper.csv");
00144 
00145   data::Load(lower_limit_file_name, &low_coord_limits);
00146   data::Load(upper_limit_file_name, &high_coord_limits);
00147 
00148   // flag for determining whether we need to do naive algorithm.
00149   bool do_naive = fx_param_exists(NULL, "do_naive");
00150 
00151   // File name containing the saved tree (if the user desires to do so)
00152   const char *load_tree_file_name;
00153 
00154   if(fx_param_exists(NULL, "load_tree_file")) {
00155     load_tree_file_name = fx_param_str(NULL, "load_tree_file", NULL);
00156   }
00157   else {
00158     load_tree_file_name = NULL;
00159   }
00160 
00161   // Declare fast tree-based orthogonal range search algorithm object.
00162   OrthoRangeSearch<double> fast_search;
00163   fast_search.Init(dataset, fx_param_exists(NULL, "do_naive"),
00164                    load_tree_file_name);
00165   GenMatrix<bool> fast_search_results;
00166   fast_search.Compute(low_coord_limits, high_coord_limits, 
00167                       &fast_search_results);
00168 
00169   if(fx_param_exists(NULL, "save_tree_file")) {
00170     const char *save_tree_file_name = fx_param_str(NULL, "save_tree_file",
00171                                                    NULL);
00172     fast_search.SaveTree(save_tree_file_name);
00173   }
00174 
00175   // Dump the result to the file.
00176   OutputOrhogonalRangeSearchResults(fast_search_results, "results.txt");
00177 
00178   // if naive option is specified, do naive algorithm
00179   if(do_naive) {
00180     NaiveOrthoRangeSearch<double> search;
00181     GenMatrix<bool> naive_search_results;
00182     search.Init(dataset);
00183     search.Compute(low_coord_limits, high_coord_limits, &naive_search_results);
00184     bool flag = true;
00185 
00186     for(index_t i = 0; i < fast_search_results.n_cols(); i++) {
00187       for(index_t j = 0; j < dataset.n_cols(); j++) {   
00188         if(fast_search_results.get(j, i) != naive_search_results.get(j, i)) {
00189           flag = false;
00190           printf("Differ on (%d, %d)\n", i, j);
00191           break;
00192         }
00193       }
00194     }
00195     if(flag) {
00196       printf("Both naive and tree-based method got the same results...\n");
00197     }
00198     else {
00199       printf("Both methods have different results...\n");
00200     }
00201   }
00202   fx_timer *tree_range_search_time = fx_get_timer(NULL, "tree_range_search");
00203   fx_timer *naive_search_time = fx_get_timer(NULL, "naive_search");
00204          
00205   fx_format_result(NULL, "speedup", "%g", 
00206                    ((double)(naive_search_time->total).micros) / 
00207                    ((double)(tree_range_search_time->total).micros));
00208 
00209   // Destroy Nick's memory manager...
00210   mmapmm::MemoryManager<false>::allocator_->Destruct();
00211   delete mmapmm::MemoryManager<false>::allocator_;
00212   fx_done(NULL);
00213   return 0;
00214 }
Generated on Mon Jan 24 12:04:38 2011 for FASTlib by  doxygen 1.6.3