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);
}
static boost::thread_specific_ptr<std::ifstream> localStream;
template<bool WriteAccess = false>
class NNGrid {
public:
NNGrid() : cellCache(500), fileCache(500) {
NNGrid() /*: cellCache(500), fileCache(500)*/ {
ramIndexTable.resize((1024*1024), UINT_MAX);
if( WriteAccess) {
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) {
ERR("Not available in Write mode");
}
@ -173,6 +175,9 @@ public:
if (WriteAccess) {
delete entries;
}
if(localStream.get() && localStream->is_open()) {
localStream->close();
}
}
void OpenIndexFiles() {
@ -518,6 +523,7 @@ private:
counter++;
}
vectorWithSameFileIndex.clear();
std::vector<GridEntry>().swap(vectorWithSameFileIndex);
return counter;
}
@ -545,29 +551,25 @@ private:
cellMap.insert(std::make_pair(fileIndex, cellIndex));
}
}
{
std::ifstream localStream(iif.c_str(), std::ios::in | std::ios::binary);
localStream.seekg(startIndexInFile);
localStream.read((char*) &cellIndex[0], 32*32*sizeof(unsigned));
localStream.close();
if(!localStream.get() || !localStream->is_open()) {
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));
assert(cellMap.find(fileIndex) != cellMap.end());
if(cellIndex[cellMap.find(fileIndex)->second] == UINT_MAX) {
return;
}
}
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;
do {
localStream.read((char *)&(gridEdge), sizeof(_GridEdge));
if(localStream.eof() || gridEdge.edgeBasedNode == UINT_MAX)
localStream->read((char *)&(gridEdge), sizeof(_GridEdge));
if(localStream->eof() || gridEdge.edgeBasedNode == UINT_MAX)
break;
result.push_back(gridEdge);
} while(true);
localStream.close();
}
void AddEdge(_GridEdge edge) {
@ -622,8 +624,8 @@ private:
stxxl::vector<GridEntry> * entries;
std::vector<unsigned> ramIndexTable; //4 MB for first level index in RAM
std::string iif;
LRUCache<int,std::vector<unsigned> > cellCache;
LRUCache<int,std::vector<_Edge> > fileCache;
// LRUCache<int,std::vector<unsigned> > cellCache;
// LRUCache<int,std::vector<_Edge> > fileCache;
};
}