From f60f676563fbdb7a8c755ddea6e46cb8c587dd83 Mon Sep 17 00:00:00 2001 From: DennisOSRM Date: Tue, 10 Jul 2012 11:50:41 +0200 Subject: [PATCH] Cache-Aware preprocessing with less space requirements --- Contractor/Contractor.h | 6 ++- DataStructures/XORFastHash.h | 44 +++++++++++++--- DataStructures/XORFastHashStorage.h | 80 +++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 DataStructures/XORFastHashStorage.h diff --git a/Contractor/Contractor.h b/Contractor/Contractor.h index a51da1fc1..da5adfa83 100644 --- a/Contractor/Contractor.h +++ b/Contractor/Contractor.h @@ -38,6 +38,7 @@ or see http://www.gnu.org/licenses/agpl.txt. #include "../DataStructures/DynamicGraph.h" #include "../DataStructures/Percent.h" #include "../DataStructures/XORFastHash.h" +#include "../DataStructures/XORFastHashStorage.h" #include "../Util/OpenMPReplacement.h" #include "../Util/StringUtil.h" @@ -66,7 +67,8 @@ private: }; typedef DynamicGraph< _ContractorEdgeData > _DynamicGraph; - typedef BinaryHeap< NodeID, NodeID, int, _HeapData > _Heap; +// typedef BinaryHeap< NodeID, NodeID, int, _HeapData, ArrayStorage > _Heap; + typedef BinaryHeap< NodeID, NodeID, int, _HeapData, XORFastHashStorage > _Heap; typedef _DynamicGraph::InputEdge _ContractorEdge; struct _ThreadData { @@ -91,7 +93,7 @@ private: }; struct _NodePartitionor { - bool operator()( std::pair< NodeID, bool > & nodeData ) const { + inline bool operator()( std::pair< NodeID, bool > & nodeData ) const { return !nodeData.second; } }; diff --git a/DataStructures/XORFastHash.h b/DataStructures/XORFastHash.h index 8aca51912..e90de23df 100644 --- a/DataStructures/XORFastHash.h +++ b/DataStructures/XORFastHash.h @@ -41,27 +41,55 @@ or see http://www.gnu.org/licenses/agpl.txt. 10: ret */ -class XORFastHash { - std::vector table1; - std::vector table2; +class XORFastHash { //65k entries + std::vector table1; + std::vector table2; public: XORFastHash() { - table1.resize(1 << 16); - table2.resize(1 << 16); - for(unsigned i = 0; i < (1 << 16); ++i) { + table1.resize(2 << 16); + table2.resize(2 << 16); + for(unsigned i = 0; i < (2 << 16); ++i) { table1[i] = i; table2[i]; } std::random_shuffle(table1.begin(), table1.end()); std::random_shuffle(table2.begin(), table2.end()); } - unsigned short operator()(const unsigned originalValue) const { + + inline unsigned short operator()(const unsigned originalValue) const { unsigned short lsb = ((originalValue) & 0xffff); unsigned short msb = (((originalValue) >> 16) & 0xffff); return table1[lsb] ^ table2[msb]; } }; +class XORMiniHash { //256 entries + std::vector table1; + std::vector table2; + std::vector table3; + std::vector table4; - +public: + XORMiniHash() { + table1.resize(1 << 8); + table2.resize(1 << 8); + table3.resize(1 << 8); + table4.resize(1 << 8); + for(unsigned i = 0; i < (1 << 8); ++i) { + table1[i] = i; table2[i]; + table3[i] = i; table4[i]; + } + std::random_shuffle(table1.begin(), table1.end()); + std::random_shuffle(table2.begin(), table2.end()); + std::random_shuffle(table3.begin(), table3.end()); + std::random_shuffle(table4.begin(), table4.end()); + } + unsigned char operator()(const unsigned originalValue) const { + unsigned char byte1 = ((originalValue) & 0xff); + unsigned char byte2 = ((originalValue >> 8) & 0xff); + unsigned char byte3 = ((originalValue >> 16) & 0xff); + unsigned char byte4 = ((originalValue >> 24) & 0xff); + return table1[byte1] ^ table2[byte2] ^ table3[byte3] ^ table4[byte4]; + } +}; #endif /* FASTXORHASH_H_ */ diff --git a/DataStructures/XORFastHashStorage.h b/DataStructures/XORFastHashStorage.h new file mode 100644 index 000000000..f59284419 --- /dev/null +++ b/DataStructures/XORFastHashStorage.h @@ -0,0 +1,80 @@ +/* + open source routing machine + Copyright (C) Dennis Luxen, others 2010 + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU AFFERO General Public License as published by +the Free Software Foundation; either version 3 of the License, or +any later version. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +or see http://www.gnu.org/licenses/agpl.txt. + */ + +#ifndef XORFASTHASHSTORAGE_H_ +#define XORFASTHASHSTORAGE_H_ + +#include +#include +#include + +#include "XORFastHash.h" + +template< typename NodeID, typename Key > +class XORFastHashStorage { +public: + struct HashCell{ + Key key; + NodeID id; + unsigned time; + HashCell() : key(UINT_MAX), id(UINT_MAX), time(UINT_MAX) {} + + HashCell(const HashCell & other) : key(other.key), id(other.id), time(other.time) { } + + inline operator Key() const { + return key; + } + + inline void operator=(const Key & keyToInsert) { + key = keyToInsert; + } + }; + + XORFastHashStorage( size_t size ) : positions(2<<16), currentTimestamp(0) { } + + inline HashCell& operator[]( const NodeID node ) { + unsigned short position = fastHash(node); + while((positions[position].time == currentTimestamp) && (positions[position].id != node)){ + ++position %= (2<<16); + } + + positions[position].id = node; + positions[position].time = currentTimestamp; + return positions[position]; + } + + inline void Clear() { + ++currentTimestamp; + if(UINT_MAX == currentTimestamp) { + positions.clear(); + positions.resize((2<<16)); + } + } + +private: + XORFastHashStorage() : positions(2<<16), currentTimestamp(0) {} + std::vector positions; + XORFastHash fastHash; + unsigned currentTimestamp; +}; + + +#endif /* XORFASTHASHSTORAGE_H_ */ +