Grid input streams now implemented as thread local objects.

This commit is contained in:
DennisOSRM 2011-11-25 13:31:46 +01:00
parent 8d008f9dcc
commit 17a5b7a363

View File

@ -147,17 +147,19 @@ static void GetListOfIndexesForEdgeAndGridSize(_Coordinate& start, _Coordinate&
GetIndicesByBresenhamsAlgorithm(x1*32768, y1*32768, x2*32768, y2*32768, indexList); GetIndicesByBresenhamsAlgorithm(x1*32768, y1*32768, x2*32768, y2*32768, indexList);
} }
static boost::thread_specific_ptr<std::ifstream> localStream;
template<bool WriteAccess = false> template<bool WriteAccess = false>
class NNGrid { class NNGrid {
public: public:
NNGrid() : cellCache(500), fileCache(500) { NNGrid() /*: cellCache(500), fileCache(500)*/ {
ramIndexTable.resize((1024*1024), UINT_MAX); ramIndexTable.resize((1024*1024), UINT_MAX);
if( WriteAccess) { if( WriteAccess) {
entries = new stxxl::vector<GridEntry>(); entries = new stxxl::vector<GridEntry>();
} }
} }
NNGrid(const char* rif, const char* _i, unsigned numberOfThreads = omp_get_num_procs()): cellCache(500), fileCache(500) { NNGrid(const char* rif, const char* _i, unsigned numberOfThreads = omp_get_num_procs()) /*: cellCache(500), fileCache(500) */{
if(WriteAccess) { if(WriteAccess) {
ERR("Not available in Write mode"); ERR("Not available in Write mode");
} }
@ -173,6 +175,9 @@ public:
if (WriteAccess) { if (WriteAccess) {
delete entries; delete entries;
} }
if(localStream.get() && localStream->is_open()) {
localStream->close();
}
} }
void OpenIndexFiles() { void OpenIndexFiles() {
@ -331,22 +336,22 @@ public:
} }
} }
// INFO("startcoord: " << smallestEdge.startCoord << ", tgtcoord" << smallestEdge.targetCoord << "result: " << newEndpoint); // INFO("startcoord: " << smallestEdge.startCoord << ", tgtcoord" << smallestEdge.targetCoord << "result: " << newEndpoint);
// INFO("length of old edge: " << LengthOfVector(smallestEdge.startCoord, smallestEdge.targetCoord)); // INFO("length of old edge: " << LengthOfVector(smallestEdge.startCoord, smallestEdge.targetCoord));
// INFO("Length of new edge: " << LengthOfVector(smallestEdge.startCoord, newEndpoint)); // INFO("Length of new edge: " << LengthOfVector(smallestEdge.startCoord, newEndpoint));
// assert(!resultNode.isBidirected || (resultNode.weight1 == resultNode.weight2)); // assert(!resultNode.isBidirected || (resultNode.weight1 == resultNode.weight2));
// if(resultNode.weight1 != resultNode.weight2) { // if(resultNode.weight1 != resultNode.weight2) {
// INFO("-> Weight1: " << resultNode.weight1 << ", weight2: " << resultNode.weight2); // INFO("-> Weight1: " << resultNode.weight1 << ", weight2: " << resultNode.weight2);
// INFO("-> node: " << resultNode.edgeBasedNode << ", bidir: " << (resultNode.isBidirected ? "yes" : "no")); // INFO("-> node: " << resultNode.edgeBasedNode << ", bidir: " << (resultNode.isBidirected ? "yes" : "no"));
// } // }
double ratio = std::min(1., LengthOfVector(smallestEdge.startCoord, newEndpoint)/LengthOfVector(smallestEdge.startCoord, smallestEdge.targetCoord) ); double ratio = std::min(1., LengthOfVector(smallestEdge.startCoord, newEndpoint)/LengthOfVector(smallestEdge.startCoord, smallestEdge.targetCoord) );
assert(ratio >= 0 && ratio <=1); assert(ratio >= 0 && ratio <=1);
// INFO("Old weight1: " << resultNode.weight1 << ", old weight2: " << resultNode.weight2); // INFO("Old weight1: " << resultNode.weight1 << ", old weight2: " << resultNode.weight2);
resultNode.weight1 *= ratio; resultNode.weight1 *= ratio;
if(INT_MAX != resultNode.weight2) { if(INT_MAX != resultNode.weight2) {
resultNode.weight2 *= (1-ratio); resultNode.weight2 *= (1-ratio);
// INFO("New weight1: " << resultNode.weight1 << ", new weight2: " << resultNode.weight2); // INFO("New weight1: " << resultNode.weight1 << ", new weight2: " << resultNode.weight2);
} }
return foundNode; return foundNode;
} }
@ -518,6 +523,7 @@ private:
counter++; counter++;
} }
vectorWithSameFileIndex.clear(); vectorWithSameFileIndex.clear();
std::vector<GridEntry>().swap(vectorWithSameFileIndex);
return counter; return counter;
} }
@ -545,29 +551,25 @@ private:
cellMap.insert(std::make_pair(fileIndex, cellIndex)); cellMap.insert(std::make_pair(fileIndex, cellIndex));
} }
} }
{ if(!localStream.get() || !localStream->is_open()) {
std::ifstream localStream(iif.c_str(), std::ios::in | std::ios::binary); localStream.reset(new std::ifstream(iif.c_str(), std::ios::in | std::ios::binary));
localStream.seekg(startIndexInFile); }
localStream.read((char*) &cellIndex[0], 32*32*sizeof(unsigned)); localStream->seekg(startIndexInFile);
localStream.close(); localStream->read((char*) &cellIndex[0], 32*32*sizeof(unsigned));
assert(cellMap.find(fileIndex) != cellMap.end()); assert(cellMap.find(fileIndex) != cellMap.end());
if(cellIndex[cellMap.find(fileIndex)->second] == UINT_MAX) { if(cellIndex[cellMap.find(fileIndex)->second] == UINT_MAX) {
return; return;
}
} }
const unsigned position = cellIndex[cellMap.find(fileIndex)->second] + 32*32*sizeof(unsigned) ; const unsigned position = cellIndex[cellMap.find(fileIndex)->second] + 32*32*sizeof(unsigned) ;
std::ifstream localStream(iif.c_str(), std::ios::in | std::ios::binary); localStream->seekg(position);
localStream.seekg(position);
_GridEdge gridEdge; _GridEdge gridEdge;
do { do {
localStream.read((char *)&(gridEdge), sizeof(_GridEdge)); localStream->read((char *)&(gridEdge), sizeof(_GridEdge));
if(localStream.eof() || gridEdge.edgeBasedNode == UINT_MAX) if(localStream->eof() || gridEdge.edgeBasedNode == UINT_MAX)
break; break;
result.push_back(gridEdge); result.push_back(gridEdge);
} while(true); } while(true);
localStream.close();
} }
void AddEdge(_GridEdge edge) { void AddEdge(_GridEdge edge) {
@ -622,8 +624,8 @@ private:
stxxl::vector<GridEntry> * entries; stxxl::vector<GridEntry> * entries;
std::vector<unsigned> ramIndexTable; //4 MB for first level index in RAM std::vector<unsigned> ramIndexTable; //4 MB for first level index in RAM
std::string iif; std::string iif;
LRUCache<int,std::vector<unsigned> > cellCache; // LRUCache<int,std::vector<unsigned> > cellCache;
LRUCache<int,std::vector<_Edge> > fileCache; // LRUCache<int,std::vector<_Edge> > fileCache;
}; };
} }