QString Enum::toString() const {
    foreach (const QString &n , nameMap().keys()) {
        if (nameMap()[n] == m_Val)
            return n;
    }
    return QString("Enum::Unknown");
}
QStringList Enum::names() const {
    QMap<int, QString> map;
    foreach (const QString &n, nameMap().keys()) {
        int v = nameMap()[n];
        map[v]=n;
    }
    return map.values();
}
bool Enum::operator=(int other)  {
    if ((other >= 0) && (other <= nameMap().size())) {
        m_Val = other;
        return true;
    }
    qDebug() << "Enum::operator=() Undefined other: " << other;
    return false;
}
bool Enum::fromString(QString val) {
    const QRegExp numbers("\\d+");
    if (numbers.exactMatch(val)) {
        m_Val = val.toInt();
        return true;
    }

    const NameMap& nm = nameMap();
    if (nm.contains(val)) {
        m_Val = nm[val];
//        qDebug() << " fromString(" << val << ") = " << m_Val;
        return true;
    } else {
        foreach (const QString &name, nm.keys()) {
           if (name.toLower()==val.toLower()) {
                m_Val = nm[name];
//                qDebug() << " fromString(" << val << ") = " << m_Val;
                return true;
            }
        }
    }
//    qDebug() << "fromString: undefined " << val;
    return false;
}
int doWork(MPI_Datatype* ResultMpiType,const int k){

    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    int MAX_RESULT_SIZE = k;

    char msg[MAX_MSG_SIZE];
    float cmpData[SEARCH_VECTOR_SIZE];
    double times[2];
    MPI_Status status;
    Timing parserTime;
    Timing searchTime;
    Timing workWallTime;

    workWallTime.start();
    std::chrono::duration<double> read_time_elapse;

    MPI_Recv(cmpData,
        SEARCH_VECTOR_SIZE,
        MPI_FLOAT,
        0,
        MPI_ANY_TAG,
        MPI_COMM_WORLD,
        &status);

    if(status.MPI_TAG != SEARCH_VECTOR){
        std::cout<< rank << " recieved terminate signal" << std::endl;
            return 0;
    }


    while (1) {
        // Receive a message from the master
        MPI_Recv(msg,           /* message buffer */
            MAX_MSG_SIZE,     /* buffer size */
            MPI_CHAR,       /* data item is an integer */
            0,              /* Receive from master */
            MPI_ANY_TAG,
                MPI_COMM_WORLD,     /* default communicator */
                &status);


        // Check if we have been terminated by the master
        // exit from the worker loop
        if (status.MPI_TAG == TERMINATE) {
            std::cout<< rank << " recieved terminate signal" << std::endl;
            return 0;
        }


        std::shared_ptr<MapString_t> nameMap(new MapString_t);
        std::vector<result_t> results;
        std::shared_ptr<std::vector<float>> dataVector(new std::vector<float>);
        Parser p(nameMap,dataVector);

        parserTime.start();
        if(!p.parse_file(msg,&read_time_elapse)){
            std::cerr<<"could not parse file: "<<msg<<std::endl;
            return 0;
        }
        parserTime.end();
        searchTime.start();
        int lineLength = p.get_line_length();
        int index = 0;
        for(auto& file : *nameMap){
            results.push_back(result_t());
            results.at(index).distance = findDist(lineLength,
                dataVector,file.second,cmpData);
            strcpy(results.at(index).fileName,file.first.c_str());
            index++;
        }


        std::sort(results.begin(),results.end(),resultPairSort);


        results.resize(MAX_RESULT_SIZE);

        searchTime.end();

        MPI_Send(results.data(),           /* message buffer */
             MAX_RESULT_SIZE,         /* buffer size */
             *ResultMpiType,              /* data item is an integer */
             0,                     /* destination process rank, the master */
             RESULTS,             /* user chosen message tag */
             MPI_COMM_WORLD);

        workWallTime.end();

        //add the times to an array to be sent to the master
        times[0] = parserTime.get_elapse();
        times[1] = searchTime.get_elapse();
        times[2] = workWallTime.get_elapse();

        //send out the timing results to be compiled
        MPI_Send(times,
            NUM_TIME_RESULTS,
            MPI_DOUBLE,
            0,
            TIMING,
            MPI_COMM_WORLD);

    }







    return 0;

}
bool Enum::operator==(const QString& other) const {
    int v = nameMap()[other];
    return m_Val == v;
}
Ejemplo n.º 7
0
bool ContractionHierarchies::Preprocess( IImporter* importer, QString dir )
{
	QString filename = fileInDirectory( dir, "Contraction Hierarchies" );

	std::vector< IImporter::RoutingNode > inputNodes;
	std::vector< IImporter::RoutingEdge > inputEdges;

	if ( !importer->GetRoutingNodes( &inputNodes ) )
		return false;
	if ( !importer->GetRoutingEdges( &inputEdges ) )
		return false;

	unsigned numEdges = inputEdges.size();
	unsigned numNodes = inputNodes.size();

	Contractor* contractor = new Contractor( numNodes, inputEdges );
	std::vector< IImporter::RoutingEdge >().swap( inputEdges );
	contractor->Run();

	std::vector< Contractor::Witness > witnessList;
	contractor->GetWitnessList( witnessList );

	std::vector< ContractionCleanup::Edge > contractedEdges;
	std::vector< ContractionCleanup::Edge > contractedLoops;
	contractor->GetEdges( &contractedEdges );
	contractor->GetLoops( &contractedLoops );
	delete contractor;

	ContractionCleanup* cleanup = new ContractionCleanup( inputNodes.size(), contractedEdges, contractedLoops, witnessList );
	std::vector< ContractionCleanup::Edge >().swap( contractedEdges );
	std::vector< ContractionCleanup::Edge >().swap( contractedLoops );
	std::vector< Contractor::Witness >().swap( witnessList );
	cleanup->Run();

	std::vector< CompressedGraph::Edge > edges;
	std::vector< NodeID > map;
	cleanup->GetData( &edges, &map );
	delete cleanup;

	{
		std::vector< unsigned > edgeIDs( numEdges );
		for ( unsigned edge = 0; edge < edges.size(); edge++ ) {
			if ( edges[edge].data.shortcut )
				continue;
			unsigned id = 0;
			unsigned otherEdge = edge;
			while ( true ) {
				if ( otherEdge == 0 )
					break;
				otherEdge--;
				if ( edges[otherEdge].source != edges[edge].source )
					break;
				if ( edges[otherEdge].target != edges[edge].target )
					continue;
				if ( edges[otherEdge].data.shortcut )
					continue;
				id++;
			}
			edgeIDs[edges[edge].data.id] = id;
		}
		importer->SetEdgeIDMap( edgeIDs );
	}

	std::vector< IRouter::Node > nodes( numNodes );
	for ( std::vector< IImporter::RoutingNode >::const_iterator i = inputNodes.begin(), iend = inputNodes.end(); i != iend; i++ )
		nodes[map[i - inputNodes.begin()]].coordinate = i->coordinate;
	std::vector< IImporter::RoutingNode >().swap( inputNodes );

	std::vector< IRouter::Node > pathNodes;
	{
		std::vector< IImporter::RoutingNode > edgePaths;
		if ( !importer->GetRoutingEdgePaths( &edgePaths ) )
			return false;
		pathNodes.resize( edgePaths.size() );
		for ( unsigned i = 0; i < edgePaths.size(); i++ )
			pathNodes[i].coordinate = edgePaths[i].coordinate;
	}

	if ( !importer->GetRoutingEdges( &inputEdges ) )
		return false;

	{
		std::vector< QString > inputNames;
		if ( !importer->GetRoutingWayNames( &inputNames ) )
			return false;

		QFile nameFile( filename + "_names" );
		if ( !openQFile( &nameFile, QIODevice::WriteOnly ) )
			return false;

		std::vector< unsigned > nameMap( inputNames.size() );
		for ( unsigned name = 0; name < inputNames.size(); name++ ) {
			nameMap[name] = nameFile.pos();
			QByteArray buffer = inputNames[name].toUtf8();
			buffer.push_back( ( char ) 0 );
			nameFile.write( buffer );
		}

		nameFile.close();
		nameFile.open( QIODevice::ReadOnly );
		const char* test = ( const char* ) nameFile.map( 0, nameFile.size() );
		for ( unsigned name = 0; name < inputNames.size(); name++ ) {
			QString testName = QString::fromUtf8( test + nameMap[name] );
			assert( testName == inputNames[name] );
		}

		for ( unsigned edge = 0; edge < numEdges; edge++ )
			inputEdges[edge].nameID = nameMap[inputEdges[edge].nameID];
	}

	{
		std::vector< QString > inputTypes;
		if ( !importer->GetRoutingWayTypes( &inputTypes ) )
			return false;

		QFile typeFile( filename + "_types" );
		if ( !openQFile( &typeFile, QIODevice::WriteOnly ) )
			return false;

		QStringList typeList;
		for ( unsigned type = 0; type < inputTypes.size(); type++ )
			typeList.push_back( inputTypes[type] );

		typeFile.write( typeList.join( ";" ).toUtf8() );
	}

	for ( std::vector< IImporter::RoutingEdge >::iterator i = inputEdges.begin(), iend = inputEdges.end(); i != iend; i++ ) {
		i->source = map[i->source];
		i->target = map[i->target];
	}

	CompressedGraphBuilder* builder = new CompressedGraphBuilder( 1u << m_settings.blockSize, nodes, edges, inputEdges, pathNodes );
	if ( !builder->run( filename, &map ) )
		return false;
	delete builder;

	importer->SetIDMap( map );

	return true;
}