/* * ===================================================================================== * * Filename: binary_tree_unit.cc * * Description: * * Version: 1.0 * Created: 04/27/2007 10:20:40 AM EDT * Revision: none * Compiler: gcc * * Author: Nikolaos Vasiloglou (NV), nvasil@ieee.org * Company: Georgia Tech Fastlab-ESP Lab * * ===================================================================================== */ #include #include #include #include "fastlib/fastlib.h" #include "u/nvasil/loki/NullType.h" #include "u/nvasil/mmanager/memory_manager.h" #include "u/nvasil/mmanager_with_tpie/memory_manager.h" #include "u/nvasil/test/test.h" #include "u/nvasil/dataset/binary_dataset.h" #include "tree_parameters_macro.h" #include "euclidean_metric.h" #include "null_statistics.h" #include "hyper_rectangle.h" #include "point_identity_discriminator.h" #include "kd_pivoter1.h" #include "kd_pivoter2.h" #include "binary_tree.h" using namespace std; template class BinaryTreeTest { public: typedef typename TYPELIST::Node_t Node_t; typedef typename Node_t::Precision_t Precision_t; typedef typename Node_t::Allocator_t Allocator_t; typedef typename Node_t::Metric_t Metric_t; typedef typename Node_t::BoundingBox_t BoundingBox_t; typedef typename Node_t::NodeCachedStatistics_t NodeCachedStatistics_t; typedef typename Node_t::PointIdDiscriminator_t PointIdDiscriminator_t; typedef typename TYPELIST::Pivot_t Pivot_t; typedef Point Point_t; typedef BinaryTree BinaryTree_t; BinaryTreeTest() { } void Init() { Allocator_t::allocator_ = new Allocator_t(); Allocator_t::allocator_->Init(); dimension_=2; num_of_points_=1000; data_file_="data"; knns_=40; range_=0.2; result_file_="allnn"; data_.Init(data_file_, num_of_points_, dimension_); for(index_t i=0; iDestruct(); delete Allocator_t::allocator_; unlink("temp_mem"); } void BuildDepthFirst(){ printf("Testing BuildDepthFirst...\n"); tree_.BuildDepthFirst(); //tree_.Print(); printf("%s\n", tree_.Statistics().c_str()); } void BuildBreadthFirst() { printf("Testing BuildBreadthFirst...\n"); tree_.BuildBreadthFirst(); //tree_.Print(); printf("%s\n", tree_.Statistics().c_str()); } void kNearestNeighbor() { printf("Testing kNearestNeighbor...\n"); tree_.BuildDepthFirst(); //tree_.Print(); vector > nearest_tree; pair nearest_naive[num_of_points_]; for(index_t i=0; i::epsilon()); TEST_ASSERT(nearest_tree[j].second.get_id()== nearest_naive[j+1].second) ; } } } void RangeNearestNeighbor() { printf("Testing RangeNearestNeighbor...\n"); tree_.BuildBreadthFirst(); //tree_.Print(); vector > nearest_tree; pair nearest_naive[num_of_points_]; for(index_t i=0; i::epsilon()); TEST_ASSERT(nearest_tree[j].second.get_id()== nearest_naive[j+1].second) ; } } } void AllKNearestNeighbors() { printf("Testing AllKNearestNeighbors...\n"); tree_.BuildDepthFirst(); // tree_.Print(); tree_.InitAllKNearestNeighborOutput(result_file_, knns_); tree_.AllNearestNeighbors(tree_.parent_, knns_); tree_.CloseAllKNearestNeighborOutput(knns_); struct stat info; if (stat(result_file_.c_str(), &info)!=0) { FATAL("Error %s file %s\n", strerror(errno), data_file_.c_str()); } uint64 map_size = info.st_size; int fp=open(result_file_.c_str(), O_RDWR); typename Node_t::NNResult *res; res=(typename Node_t::NNResult *)mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fp, 0); TEST_ASSERT(res!=MAP_FAILED); close(fp); std::sort(res, res+num_of_points_*knns_); pair nearest_naive[num_of_points_]; for(index_t i=0; i::epsilon()); TEST_ASSERT(res[data_.get_id(i)*knns_+j].nearest_.get_id()== nearest_naive[j+1].second); } } munmap(res, map_size); } void AllRangeNearestNeighbors() { printf("Testing AllRangeNearestNeighbors...\n"); tree_.BuildBreadthFirst(); //tree_.Print(); tree_.InitAllRangeNearestNeighborOutput(result_file_); tree_.AllNearestNeighbors(tree_.parent_, range_); tree_.CloseAllRangeNearestNeighborOutput(); struct stat info; if (stat(result_file_.c_str(), &info)!=0) { FATAL( "Error %s file %s\n", strerror(errno), data_file_.c_str()); } uint64 map_size = info.st_size; int fp=open(result_file_.c_str(), O_RDWR); typename Node_t::NNResult *res; res=(typename Node_t::NNResult *)mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fp, 0); close(fp); TEST_ASSERT(res!=MAP_FAILED); std::sort(res, res+map_size/sizeof(typename Node_t::NNResult)); pair nearest_naive[num_of_points_]; index_t i=0; while (i::epsilon()); j++; k++; } i++; } munmap(res, map_size); } void TestAll() { Init(); BuildDepthFirst(); Destruct(); Init(); BuildBreadthFirst(); Destruct(); Init(); kNearestNeighbor(); Destruct(); Init(); RangeNearestNeighbor(); Destruct(); Init(); AllKNearestNeighbors(); Destruct(); Init(); AllRangeNearestNeighbors(); Destruct(); } private: BinaryTree_t tree_; BinaryDataset data_; string data_file_; string result_file_; int32 dimension_; index_t num_of_points_; index_t knns_; Precision_t range_; void Naive(index_t query, pair *result) { for(index_t i=0; i *result) { for(index_t i=0; i, EuclideanMetric, HyperRectangle, NullStatistics, SimpleDiscriminator, KdPivoter1, false) */ struct BasicTypes1 { typedef float32 Precision_t; typedef mmapmm::MemoryManager Allocator_t; typedef EuclideanMetric Metric_t; }; struct NodeParameters1 { typedef float32 Precision_t; typedef mmapmm::MemoryManager Allocator_t; typedef EuclideanMetric Metric_t; typedef HyperRectangle BoundingBox_t; typedef NullStatistics NodeCachedStatistics_t; typedef SimpleDiscriminator PointIdDiscriminator_t; }; struct TreeParameters1 { typedef Node Node_t; typedef KdPivoter1 Pivot_t; }; struct BasicTypes2 { typedef float32 Precision_t; typedef tpiemm::MemoryManager Allocator_t; typedef EuclideanMetric Metric_t; }; struct NodeParameters2 { typedef float32 Precision_t; typedef tpiemm::MemoryManager Allocator_t; typedef EuclideanMetric Metric_t; typedef HyperRectangle BoundingBox_t; typedef NullStatistics NodeCachedStatistics_t; typedef SimpleDiscriminator PointIdDiscriminator_t; }; struct TreeParameters2 { typedef Node Node_t; typedef KdPivoter1 Pivot_t; }; struct TreeParameters3 { typedef Node Node_t; typedef KdPivoter2 Pivot_t; }; struct TreeParameters4 { typedef Node Node_t; typedef KdPivoter2 Pivot_t; }; typedef BinaryTreeTest BinaryTreeTest1_t; typedef BinaryTreeTest BinaryTreeTest2_t; typedef BinaryTreeTest BinaryTreeTest3_t; typedef BinaryTreeTest BinaryTreeTest4_t; int main(int argc, char *argv[]) { BinaryTreeTest1_t test1; test1.TestAll(); BinaryTreeTest2_t test2; test2.TestAll(); BinaryTreeTest3_t test3; test3.TestAll(); BinaryTreeTest4_t test4; test4.TestAll(); }