From b17e55afc11caff584573a9b3b2f86493b9642b8 Mon Sep 17 00:00:00 2001 From: "jan.reininghaus" Date: Wed, 30 Apr 2014 08:57:41 +0000 Subject: new vector_heap Representation git-svn-id: https://phat.googlecode.com/svn/trunk@164 8e3bb3c2-eed4-f18f-5264-0b6c94e6926d --- include/phat/representations/vector_heap.h | 157 +++++++++++++++++++++++++++++ src/benchmark.cpp | 10 +- src/phat.cpp | 7 +- src/self_test.cpp | 15 ++- 4 files changed, 183 insertions(+), 6 deletions(-) create mode 100644 include/phat/representations/vector_heap.h diff --git a/include/phat/representations/vector_heap.h b/include/phat/representations/vector_heap.h new file mode 100644 index 0000000..6706790 --- /dev/null +++ b/include/phat/representations/vector_heap.h @@ -0,0 +1,157 @@ +/* Copyright 2013 IST Austria +Contributed by: Jan Reininghaus + +This file is part of PHAT. + +PHAT is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +PHAT is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with PHAT. If not, see . */ + +#pragma once + +#include + +namespace phat { + class vector_heap { + + protected: + std::vector< dimension > dims; + std::vector< column > matrix; + + mutable thread_local_storage< column > temp_column_buffer; + +// std::vector< index > steps_since_last_prune; + + protected: + //void _prune( index idx ) + //{ + // temp_column_buffer( ).clear( ); + // _get_col( idx, temp_column_buffer( ) ); + //} + + index _pop_max_index( index idx ) + { + return _pop_max_index( matrix[ idx ] ); + } + + index _pop_max_index( column& col ) const + { + if( col.empty( ) ) + return -1; + else { + index max_element = col.front( ); + std::pop_heap( col.begin( ), col.end( ) ); + col.pop_back( ); + while( !col.empty( ) && col.front( ) == max_element ) { + std::pop_heap( col.begin( ), col.end( ) ); + col.pop_back( ); + if( col.empty( ) ) + return -1; + else { + max_element = col.front( ); + std::pop_heap( col.begin( ), col.end( ) ); + col.pop_back( ); + } + } + return max_element; + } + } + + public: + // overall number of cells in boundary_matrix + index _get_num_cols( ) const + { + return (index)matrix.size( ); + } + void _set_num_cols( index nr_of_columns ) + { + dims.resize( nr_of_columns ); + matrix.resize( nr_of_columns ); + // steps_since_last_prune.assign( nr_of_columns, 0 ); + } + + // dimension of given index + dimension _get_dim( index idx ) const + { + return dims[ idx ]; + } + void _set_dim( index idx, dimension dim ) + { + dims[ idx ] = dim; + } + + // replaces(!) content of 'col' with boundary of given index + void _get_col( index idx, column& col ) const + { + temp_column_buffer( ) = matrix[ idx ]; + + index max_index = _pop_max_index( temp_column_buffer() ); + while( max_index != -1 ) { + col.push_back( max_index ); + max_index = _pop_max_index( temp_column_buffer( ) ); + } + std::reverse( col.begin( ), col.end( ) ); + } + void _set_col( index idx, const column& col ) + { + matrix[ idx ] = col; + std::make_heap( matrix[ idx ].begin( ), matrix[ idx ].end( ) ); + } + + // true iff boundary of given idx is empty + bool _is_empty( index idx ) const + { + return _get_max_index( idx ) == -1; + } + + // largest row index of given column idx (new name for lowestOne()) + index _get_max_index( index idx ) const + { + column& col = const_cast< column& >( matrix[ idx ] ); + index max_element = _pop_max_index( col ); + col.push_back( max_element ); + std::push_heap( col.begin( ), col.end( ) ); + return max_element; + } + + // removes the maximal index of a column + void _remove_max( index idx ) + { + _pop_max_index( idx ); + } + + // clears given column + void _clear( index idx ) + { + matrix[ idx ].clear( ); + } + + // syncronizes all data structures (essential for openmp stuff) + void _sync( ) {} + + // adds column 'source' to column 'target' + void _add_to( index source, index target ) + { + for( index idx = 0; idx < (index)matrix[ source ].size( ); idx++ ) { + matrix[ target ].push_back( matrix[ source ][ idx ] ); + std::push_heap( matrix[ target ].begin( ), matrix[ target ].end( ) ); + } + // steps_since_last_prune[ target ] += matrix[ source ].size(); + //if( 2 * steps_since_last_prune[ target ] > matrix[ target ].size() ) { + // // std::cout << "Prune" << std::endl; + // _prune( target ); + // steps_since_last_prune[ target ] = 0; + //} + + } + }; +} diff --git a/src/benchmark.cpp b/src/benchmark.cpp index c1d897b..3abb4cd 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -38,7 +39,7 @@ #include -enum Representation_type {VECTOR_VECTOR, VECTOR_SET, SPARSE_PIVOT_COLUMN, HEAP_PIVOT_COLUMN, FULL_PIVOT_COLUMN, BIT_TREE_PIVOT_COLUMN, VECTOR_LIST}; +enum Representation_type { VECTOR_VECTOR, VECTOR_HEAP, VECTOR_SET, SPARSE_PIVOT_COLUMN, HEAP_PIVOT_COLUMN, FULL_PIVOT_COLUMN, BIT_TREE_PIVOT_COLUMN, VECTOR_LIST }; enum Algorithm_type {STANDARD, TWIST, ROW, CHUNK, CHUNK_SEQUENTIAL, SPECTRAL_SEQUENCE}; enum Ansatz_type {PRIMAL, DUAL}; @@ -53,7 +54,7 @@ void print_help() { std::cerr << "--help -- prints this screen" << std::endl; std::cerr << "--dualize -- use only dualization approach" << std::endl; std::cerr << "--primal -- use only primal approach" << std::endl; - std::cerr << "--vector_vector, --vector_set, --vector_list, --full_pivot_column, --sparse_pivot_column, --heap_pivot_column, --bit_tree_pivot_column -- use only a subset of representation data structures for boundary matrices" << std::endl; + std::cerr << "--vector_vector, --vector_heap, --vector_set, --vector_list, --full_pivot_column, --sparse_pivot_column, --heap_pivot_column, --bit_tree_pivot_column -- use only a subset of representation data structures for boundary matrices" << std::endl; std::cerr << "--standard, --twist, --chunk, --chunk_sequential, --spectral_sequence, --row -- use only a subset of reduction algorithms" << std::endl; } @@ -75,6 +76,7 @@ void parse_command_line( int argc, char** argv, bool& latex_tables_output, bool& else if( argument == "--latex" ) latex_tables_output = true; else if( argument == "--binary" ) use_binary = true; else if( argument == "--vector_vector" ) representations.push_back( VECTOR_VECTOR ); + else if( argument == "--vector_heap" ) representations.push_back( VECTOR_HEAP ); else if( argument == "--vector_set" ) representations.push_back( VECTOR_SET ); else if( argument == "--vector_list" ) representations.push_back( VECTOR_LIST ); else if( argument == "--full_pivot_column" ) representations.push_back( FULL_PIVOT_COLUMN ); @@ -98,6 +100,7 @@ void parse_command_line( int argc, char** argv, bool& latex_tables_output, bool& if( representations.empty() == true ) { representations.push_back( VECTOR_VECTOR ); + representations.push_back( VECTOR_HEAP ); representations.push_back( VECTOR_SET ); representations.push_back( VECTOR_LIST ); representations.push_back( FULL_PIVOT_COLUMN ); @@ -238,6 +241,7 @@ int main( int argc, char** argv ) std::cout << input_filename << ","; switch( representation ) { case VECTOR_VECTOR: COMPUTE(vector_vector) break; + case VECTOR_HEAP: COMPUTE( vector_heap ) break; case VECTOR_SET: COMPUTE(vector_set) break; case VECTOR_LIST: COMPUTE(vector_list) break; case FULL_PIVOT_COLUMN: COMPUTE(full_pivot_column) break; @@ -262,6 +266,7 @@ int main( int argc, char** argv ) Representation_type representation = representations[ idx_representation ]; switch( representation ) { case VECTOR_VECTOR: std::cout << "& V "; break; + case VECTOR_HEAP: std::cout << "& H "; break; case VECTOR_SET: std::cout << "& S "; break; case VECTOR_LIST: std::cout << "& L "; break; case FULL_PIVOT_COLUMN: std::cout << "& P-F "; break; @@ -293,6 +298,7 @@ int main( int argc, char** argv ) Representation_type representation = representations[ idx_representation ]; switch( representation ) { case VECTOR_VECTOR: COMPUTE_LATEX( vector_vector ) break; + case VECTOR_HEAP: COMPUTE_LATEX( vector_heap ) break; case VECTOR_SET: COMPUTE_LATEX( vector_set ) break; case VECTOR_LIST: COMPUTE_LATEX( vector_list ) break; case FULL_PIVOT_COLUMN: COMPUTE_LATEX( full_pivot_column ) break; diff --git a/src/phat.cpp b/src/phat.cpp index 8fcd42c..b099b59 100644 --- a/src/phat.cpp +++ b/src/phat.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -34,7 +35,7 @@ #include -enum Representation_type {VECTOR_VECTOR, VECTOR_SET, SPARSE_PIVOT_COLUMN, FULL_PIVOT_COLUMN, BIT_TREE_PIVOT_COLUMN, VECTOR_LIST, HEAP_PIVOT_COLUMN}; +enum Representation_type { VECTOR_VECTOR, VECTOR_HEAP, VECTOR_SET, SPARSE_PIVOT_COLUMN, FULL_PIVOT_COLUMN, BIT_TREE_PIVOT_COLUMN, VECTOR_LIST, HEAP_PIVOT_COLUMN }; enum Algorithm_type {STANDARD, TWIST, ROW, CHUNK, CHUNK_SEQUENTIAL, SPECTRAL_SEQUENCE }; void print_help() { @@ -47,7 +48,7 @@ void print_help() { std::cerr << "--help -- prints this screen" << std::endl; std::cerr << "--verbose -- verbose output" << std::endl; std::cerr << "--dualize -- use dualization approach" << std::endl; - std::cerr << "--vector_vector, --vector_set, --vector_list, --full_pivot_column, --sparse_pivot_column, --heap_pivot_column, --bit_tree_pivot_column -- selects a representation data structure for boundary matrices (default is '--bit_tree_pivot_column')" << std::endl; + std::cerr << "--vector_vector, --vector_heap, --vector_set, --vector_list, --full_pivot_column, --sparse_pivot_column, --heap_pivot_column, --bit_tree_pivot_column -- selects a representation data structure for boundary matrices (default is '--bit_tree_pivot_column')" << std::endl; std::cerr << "--standard, --twist, --chunk, --chunk_sequential, --spectral_sequence, --row -- selects a reduction algorithm (default is '--twist')" << std::endl; } @@ -71,6 +72,7 @@ void parse_command_line( int argc, char** argv, bool& use_binary, Representation else if( option == "--binary" ) use_binary = true; else if( option == "--dualize" ) dualize = true; else if( option == "--vector_vector" ) representation = VECTOR_VECTOR; + else if( option == "--vector_heap" ) representation = VECTOR_HEAP; else if( option == "--vector_set" ) representation = VECTOR_SET; else if( option == "--vector_list" ) representation = VECTOR_LIST; else if( option == "--full_pivot_column" ) representation = FULL_PIVOT_COLUMN; @@ -177,6 +179,7 @@ int main( int argc, char** argv ) switch( representation ) { case VECTOR_VECTOR: COMPUTE_PAIRING(vector_vector) break; + case VECTOR_HEAP: COMPUTE_PAIRING( vector_heap ) break; case VECTOR_SET: COMPUTE_PAIRING(vector_set) break; case VECTOR_LIST: COMPUTE_PAIRING(vector_list) break; case FULL_PIVOT_COLUMN: COMPUTE_PAIRING(full_pivot_column) break; diff --git a/src/self_test.cpp b/src/self_test.cpp index 02bca5e..249b9b2 100644 --- a/src/self_test.cpp +++ b/src/self_test.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -41,6 +42,7 @@ int main( int argc, char** argv ) typedef phat::full_pivot_column Full; typedef phat::bit_tree_pivot_column BitTree; typedef phat::vector_vector Vec_vec; + typedef phat::vector_heap Vec_heap; typedef phat::vector_set Vec_set; typedef phat::vector_list Vec_list; @@ -79,6 +81,11 @@ int main( int argc, char** argv ) phat::boundary_matrix< Vec_vec > vec_vec_boundary_matrix = boundary_matrix; phat::compute_persistence_pairs< phat::chunk_reduction >( vec_vec_pairs, vec_vec_boundary_matrix ); + std::cout << "Running Chunk - Vec_heap ..." << std::endl; + phat::persistence_pairs vec_heap_pairs; + phat::boundary_matrix< Vec_heap > vec_heap_boundary_matrix = boundary_matrix; + phat::compute_persistence_pairs< phat::chunk_reduction >( vec_heap_pairs, vec_heap_boundary_matrix ); + std::cout << "Running Chunk - Vec_set ..." << std::endl; phat::persistence_pairs vec_set_pairs; phat::boundary_matrix< Vec_set > vec_set_boundary_matrix = boundary_matrix; @@ -101,8 +108,12 @@ int main( int argc, char** argv ) std::cerr << "Error: full and vec_vec differ!" << std::endl; error = true; } - if( vec_vec_pairs != vec_set_pairs ) { - std::cerr << "Error: vec_vec and vec_set differ!" << std::endl; + if( vec_vec_pairs != vec_heap_pairs ) { + std::cerr << "Error: vec_vec and vec_heap differ!" << std::endl; + error = true; + } + if( vec_heap_pairs != vec_set_pairs ) { + std::cerr << "Error: vec_heap and vec_set differ!" << std::endl; error = true; } if( vec_set_pairs != bit_tree_pairs ) { -- cgit v1.2.3