osrm-backend/createHierarchy.cpp

196 lines
6.7 KiB
C++
Raw Normal View History

2010-07-09 05:05:40 -04:00
/*
open source routing machine
Copyright (C) Dennis Luxen, 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.
*/
2010-07-09 05:05:40 -04:00
//g++ createHierarchy.cpp -fopenmp -Wno-deprecated -o createHierarchy -O3 -march=native -DNDEBUG
#define VERBOSE(x) x
#define VERBOSE2(x)
#ifdef NDEBUG
#undef VERBOSE
#undef VERBOSE2
#endif
2010-07-09 05:05:40 -04:00
#include <climits>
#include <fstream>
#include <istream>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
2011-11-25 12:42:27 -05:00
#include "openmp.h"
2010-07-09 05:05:40 -04:00
#include "typedefs.h"
#include "Contractor/Contractor.h"
#include "Contractor/ContractionCleanup.h"
#include "DataStructures/BinaryHeap.h"
#include "DataStructures/LevelInformation.h"
#include "DataStructures/NNGrid.h"
#include "Util/BaseConfiguration.h"
#include "Util/InputFileUtil.h"
#include "Util/GraphLoader.h"
2010-07-09 05:05:40 -04:00
using namespace std;
typedef DynamicGraph<EdgeData>::InputEdge InputEdge;
typedef StaticGraph<EdgeData>::InputEdge StaticEdge;
typedef BaseConfiguration ContractorConfiguration;
2010-07-09 05:05:40 -04:00
vector<NodeInfo> * int2ExtNodeMap = new vector<NodeInfo>();
int main (int argc, char *argv[]) {
if(argc <= 1) {
cerr << "usage: " << endl << argv[0] << " <osrm-data>" << endl;
2010-07-09 05:05:40 -04:00
exit(-1);
}
unsigned numberOfThreads = omp_get_num_procs();
if(testDataFile("contractor.ini")) {
ContractorConfiguration contractorConfig("contractor.ini");
if(atoi(contractorConfig.GetParameter("Threads").c_str()) != 0 && (unsigned)atoi(contractorConfig.GetParameter("Threads").c_str()) <= numberOfThreads)
numberOfThreads = (unsigned)atoi( contractorConfig.GetParameter("Threads").c_str() );
}
omp_set_num_threads(numberOfThreads);
cout << "preprocessing data from input file " << argv[1];
#ifdef _GLIBCXX_PARALLEL
cout << " using STL parallel mode" << std::endl;
#else
cout << " using STL serial mode" << std::endl;
#endif
2010-07-09 05:05:40 -04:00
ifstream in;
2011-09-28 11:22:03 -04:00
in.open (argv[1], ifstream::in | ifstream::binary);
2010-07-09 05:05:40 -04:00
if (!in.is_open()) {
cerr << "Cannot open " << argv[1] << endl; exit(-1);
}
vector<ImportEdge> edgeList;
const NodeID n = readBinaryOSRMGraphFromStream(in, edgeList, int2ExtNodeMap);
2010-07-09 05:05:40 -04:00
in.close();
char nodeOut[1024];
char edgeOut[1024];
char ramIndexOut[1024];
char fileIndexOut[1024];
char levelInfoOut[1024];
2010-07-09 05:05:40 -04:00
strcpy(nodeOut, argv[1]);
strcpy(edgeOut, argv[1]);
strcpy(ramIndexOut, argv[1]);
strcpy(fileIndexOut, argv[1]);
strcpy(levelInfoOut, argv[1]);
2010-07-09 05:05:40 -04:00
strcat(nodeOut, ".nodes");
strcat(edgeOut, ".hsgr");
strcat(ramIndexOut, ".ramIndex");
strcat(fileIndexOut, ".fileIndex");
strcat(levelInfoOut, ".levels");
2010-07-09 05:05:40 -04:00
2010-09-13 11:31:29 -04:00
cout << "initializing contractor ..." << flush;
2010-07-09 05:05:40 -04:00
Contractor* contractor = new Contractor( n, edgeList );
contractor->Run();
LevelInformation * levelInfo = contractor->GetLevelInformation();
std::cout << "sorting level info" << std::endl;
for(unsigned currentLevel = levelInfo->GetNumberOfLevels(); currentLevel>0; currentLevel--) {
std::vector<unsigned> & level = levelInfo->GetLevel(currentLevel-1);
std::sort(level.begin(), level.end());
}
std::cout << "writing level info" << std::endl;
ofstream levelOutFile(levelInfoOut, ios::binary);
unsigned numberOfLevels = levelInfo->GetNumberOfLevels();
levelOutFile.write((char *)&numberOfLevels, sizeof(unsigned));
for(unsigned currentLevel = 0; currentLevel < levelInfo->GetNumberOfLevels(); currentLevel++ ) {
std::vector<unsigned> & level = levelInfo->GetLevel(currentLevel);
unsigned sizeOfLevel = level.size();
levelOutFile.write((char *)&sizeOfLevel, sizeof(unsigned));
for(unsigned currentLevelEntry = 0; currentLevelEntry < sizeOfLevel; currentLevelEntry++) {
unsigned node = level[currentLevelEntry];
levelOutFile.write((char *)&node, sizeof(unsigned));
assert(node < n);
}
}
levelOutFile.close();
2010-07-09 05:05:40 -04:00
std::vector< ContractionCleanup::Edge > contractedEdges;
contractor->GetEdges( contractedEdges );
delete contractor;
2010-07-09 05:05:40 -04:00
ContractionCleanup * cleanup = new ContractionCleanup(n, contractedEdges);
contractedEdges.clear();
2010-07-09 05:05:40 -04:00
cleanup->Run();
std::vector< InputEdge> cleanedEdgeList;
2010-07-09 05:05:40 -04:00
cleanup->GetData(cleanedEdgeList);
delete cleanup;
2010-07-09 05:05:40 -04:00
2010-09-13 11:31:29 -04:00
cout << "Serializing edges " << flush;
ofstream edgeOutFile(edgeOut, ios::binary);
Percent p(cleanedEdgeList.size());
for(std::vector< InputEdge>::iterator it = cleanedEdgeList.begin(); it != cleanedEdgeList.end(); it++) {
2010-09-13 11:31:29 -04:00
p.printIncrement();
2010-07-09 05:05:40 -04:00
int distance= it->data.distance;
assert(distance > 0);
bool shortcut= it->data.shortcut;
bool forward= it->data.forward;
bool backward= it->data.backward;
NodeID middle;
if(shortcut)
middle = it->data.middleName.middle;
else {
middle = it->data.middleName.nameID;
}
2010-07-09 05:05:40 -04:00
NodeID source = it->source;
NodeID target = it->target;
short type = it->data.type;
2010-07-09 05:05:40 -04:00
edgeOutFile.write((char *)&(distance), sizeof(int));
edgeOutFile.write((char *)&(shortcut), sizeof(bool));
edgeOutFile.write((char *)&(forward), sizeof(bool));
edgeOutFile.write((char *)&(backward), sizeof(bool));
edgeOutFile.write((char *)&(middle), sizeof(NodeID));
edgeOutFile.write((char *)&(type), sizeof(short));
2010-07-09 05:05:40 -04:00
edgeOutFile.write((char *)&(source), sizeof(NodeID));
edgeOutFile.write((char *)&(target), sizeof(NodeID));
}
edgeOutFile.close();
cleanedEdgeList.clear();
std::cout << "writing node map ..." << std::flush;
ofstream mapOutFile(nodeOut, ios::binary);
for(NodeID i = 0; i < int2ExtNodeMap->size(); i++) {
mapOutFile.write((char *)&(int2ExtNodeMap->at(i)), sizeof(NodeInfo));
}
mapOutFile.close();
std::cout << "ok" << std::endl;
WritableGrid * writeableGrid = new WritableGrid();
cout << "building grid ..." << flush;
writeableGrid->ConstructGrid(edgeList, int2ExtNodeMap, ramIndexOut, fileIndexOut);
delete writeableGrid;
int2ExtNodeMap->clear();
delete int2ExtNodeMap;
cout << "finished" << endl;
2011-07-21 10:30:36 -04:00
return 0;
2010-07-09 05:05:40 -04:00
}