Example #1
0
    /// Destructor.
    ~StateContainer() {
        // Destroy the allocated states.
        for(StateMap::iterator it = _states.begin(); it != _states.end(); ++it) {
            destroyState(it->second);
            it->second = 0;
        }

        // Clear the map.
        _states.clear();
    }
Example #2
0
bool CCameraController::SetStateFloat(const StateMap& sm,
                                      const std::string& name, float& var)
{
	StateMap::const_iterator it = sm.find(name);
	if (it != sm.end()) {
		const float value = it->second;
		var = value;
		return true;
	}
	return false;
}
Example #3
0
void* MCJITHelper::identityGeneratorForOpenOSR(Function* F1, Instruction* OSRSrc, void* extra, void* profDataAddr) {
    MCJITHelper* TheHelper = (MCJITHelper*) extra;
    bool verbose = TheHelper->verbose;

    assert(OSRSrc->getParent()->getParent() == F1 && "MCJIT messed up the objects");

    if (verbose) {
        std::cerr << "Value for F1 is " << F1 << std::endl;
        std::cerr << "Value for OSRSrc is " << OSRSrc << std::endl;
        std::cerr << "Value for extra is " << extra << std::endl;
        std::cerr << "Value for profDataAddr is " << profDataAddr << std::endl;
    }

    std::pair<Function*, StateMap*> identityPair = StateMap::generateIdentityMapping(F1);

    Function* F2 = identityPair.first;
    StateMap* M = identityPair.second;

    Instruction* OSRSrcInF2 = cast<Instruction>(M->getCorrespondingOneToOneValue(OSRSrc));
    if (OSRLibrary::removeOSRPoint(*OSRSrcInF2) && verbose) {
        std::cerr << "OSR point removed after cloning F1" << std::endl;
    }

    Instruction* LPad = M->getLandingPad(OSRSrc);

    LivenessAnalysis LA(F1);
    std::vector<Value*>* valuesToPass = OSRLibrary::getLiveValsVecAtInstr(OSRSrc, LA);
    std::string OSRDestFunName = (F2->getName().str()).append("OSRCont");
    Function* OSRDestFun = OSRLibrary::genContinuationFunc(TheHelper->Context,
            *F1, *F2, *OSRSrc, *LPad, *valuesToPass, *M, &OSRDestFunName, verbose);
    delete valuesToPass;
    delete F2;
    delete M;

    // put the generated code into a module
    std::string modForJITName = "OpenOSRClone";
    modForJITName.append(OSRDestFunName);
    std::unique_ptr<Module> modForJIT = llvm::make_unique<Module>(modForJITName, TheHelper->Context);
    Module* modForJIT_ptr = modForJIT.get();
    modForJIT_ptr->getFunctionList().push_back(OSRDestFun);
    verifyFunction(*OSRDestFun, &outs());

    // remove dead code
    FunctionPassManager FPM(modForJIT_ptr);
    FPM.add(createCFGSimplificationPass());
    FPM.doInitialization();
    FPM.run(*OSRDestFun);

    // compile code
    TheHelper->addModule(std::move(modForJIT));
    return (void*)TheHelper->JIT->getFunctionAddress(OSRDestFunName);
}
Example #4
0
    /**
     * Returns the state with the given ID.
     * If the state doesn't exist, creates a new one and loads it. It will be
     * unloaded when this class is destroyed.
     **/
    inline State *getState(StateId id) {
        // Try to find the state. If it isn't found, create a new state with
        // it.
        StateMap::iterator it = _states.find(id);

        if(it == _states.end()) {
            // Create a new state.
            return createState(id);
        }
        else {
            // Return the existing state.
            return it->second;
        }
    }
stateMaskVec_t const mkStateMasks(string const & obsStr, StateMaskMap const & stateMaskMap, StateMap const & metaNucMap)
{
  vector<string> obsVec = stringToVectorOfStrings(obsStr);
  stateMaskVec_t stateMasks(obsStr.size(), NULL);
  for (unsigned i = 0; i < obsStr.size(); i++)
    stateMasks[i] = & stateMaskMap.metaState2StateMask( metaNucMap.symbol2State(obsVec[i]) );
  return stateMasks;
}
Example #6
0
bool processStateMachineNode(AnimNode::Pointer node, const QJsonObject& jsonObj, const QString& nodeId, const QUrl& jsonUrl) {
    auto smNode = std::static_pointer_cast<AnimStateMachine>(node);
    assert(smNode);

    READ_STRING(currentState, jsonObj, nodeId, jsonUrl, false);

    auto statesValue = jsonObj.value("states");
    if (!statesValue.isArray()) {
        qCCritical(animation) << "AnimNodeLoader, bad array \"states\" in stateMachine node, id =" << nodeId;
        return false;
    }

    // build a map for all children by name.
    std::map<QString, int> childMap;
    buildChildMap(childMap, node);

    // first pass parse all the states and build up the state and transition map.
    using StringPair = std::pair<QString, QString>;
    using TransitionMap = std::multimap<AnimStateMachine::State::Pointer, StringPair>;
    TransitionMap transitionMap;

    using StateMap = std::map<QString, AnimStateMachine::State::Pointer>;
    StateMap stateMap;

    auto statesArray = statesValue.toArray();
    for (const auto& stateValue : statesArray) {
        if (!stateValue.isObject()) {
            qCCritical(animation) << "AnimNodeLoader, bad state object in \"states\", id =" << nodeId;
            return false;
        }
        auto stateObj = stateValue.toObject();

        READ_STRING(id, stateObj, nodeId, jsonUrl, false);
        READ_FLOAT(interpTarget, stateObj, nodeId, jsonUrl, false);
        READ_FLOAT(interpDuration, stateObj, nodeId, jsonUrl, false);
        READ_OPTIONAL_STRING(interpType, stateObj);

        READ_OPTIONAL_STRING(interpTargetVar, stateObj);
        READ_OPTIONAL_STRING(interpDurationVar, stateObj);
        READ_OPTIONAL_STRING(interpTypeVar, stateObj);

        auto iter = childMap.find(id);
        if (iter == childMap.end()) {
            qCCritical(animation) << "AnimNodeLoader, could not find stateMachine child (state) with nodeId =" << nodeId << "stateId =" << id;
            return false;
        }

        AnimStateMachine::InterpType interpTypeEnum = AnimStateMachine::InterpType::SnapshotPrev;  // default value
        if (!interpType.isEmpty()) {
            interpTypeEnum = stringToInterpType(interpType);
            if (interpTypeEnum == AnimStateMachine::InterpType::NumTypes) {
                qCCritical(animation) << "AnimNodeLoader, bad interpType on stateMachine state, nodeId = " << nodeId << "stateId =" << id;
                return false;
            }
        }

        auto statePtr = std::make_shared<AnimStateMachine::State>(id, iter->second, interpTarget, interpDuration, interpTypeEnum);
        assert(statePtr);

        if (!interpTargetVar.isEmpty()) {
            statePtr->setInterpTargetVar(interpTargetVar);
        }
        if (!interpDurationVar.isEmpty()) {
            statePtr->setInterpDurationVar(interpDurationVar);
        }
        if (!interpTypeVar.isEmpty()) {
            statePtr->setInterpTypeVar(interpTypeVar);
        }

        smNode->addState(statePtr);
        stateMap.insert(StateMap::value_type(statePtr->getID(), statePtr));

        auto transitionsValue = stateObj.value("transitions");
        if (!transitionsValue.isArray()) {
            qCritical(animation) << "AnimNodeLoader, bad array \"transitions\" in stateMachine node, stateId =" << id << "nodeId =" << nodeId;
            return false;
        }

        auto transitionsArray = transitionsValue.toArray();
        for (const auto& transitionValue : transitionsArray) {
            if (!transitionValue.isObject()) {
                qCritical(animation) << "AnimNodeLoader, bad transition object in \"transtions\", stateId =" << id << "nodeId =" << nodeId;
                return false;
            }
            auto transitionObj = transitionValue.toObject();

            READ_STRING(var, transitionObj, nodeId, jsonUrl, false);
            READ_STRING(state, transitionObj, nodeId, jsonUrl, false);

            transitionMap.insert(TransitionMap::value_type(statePtr, StringPair(var, state)));
        }
    }

    // second pass: now iterate thru all transitions and add them to the appropriate states.
    for (auto& transition : transitionMap) {
        AnimStateMachine::State::Pointer srcState = transition.first;
        auto iter = stateMap.find(transition.second.second);
        if (iter != stateMap.end()) {
            srcState->addTransition(AnimStateMachine::State::Transition(transition.second.first, iter->second));
        } else {
            qCCritical(animation) << "AnimNodeLoader, bad state machine transtion from srcState =" << srcState->_id << "dstState =" << transition.second.second << "nodeId =" << nodeId;
            return false;
        }
    }

    auto iter = stateMap.find(currentState);
    if (iter == stateMap.end()) {
        qCCritical(animation) << "AnimNodeLoader, bad currentState =" << currentState << "could not find child node" << "id =" << nodeId;
    }
    smNode->setCurrentState(iter->second);

    return true;
}
Example #7
0
        int runMany() {
            StateMap threads;

            {
                string orig = getParam( "host" );
                bool showPorts = false;
                if ( orig == "" )
                    orig = "localhost";

                if ( orig.find( ":" ) != string::npos || hasParam( "port" ) )
                    showPorts = true;

                StringSplitter ss( orig.c_str() , "," );
                while ( ss.more() ) {
                    string host = ss.next();
                    if ( showPorts && host.find( ":" ) == string::npos) {
                        // port supplied, but not for this host.  use default.
                        if ( hasParam( "port" ) )
                            host += ":" + _params["port"].as<string>();
                        else
                            host += ":27017";
                    }
                    _add( threads , host );
                }
            }

            sleepsecs(1);

            int row = 0;
            bool discover = hasParam( "discover" );
            int maxLockedDbWidth = 0;

            while ( 1 ) {
                sleepsecs( (int)ceil(_statUtil.getSeconds()) );

                // collect data
                vector<Row> rows;
                for ( map<string,shared_ptr<ServerState> >::iterator i=threads.begin(); i!=threads.end(); ++i ) {
                    scoped_lock lk( i->second->lock );

                    if ( i->second->error.size() ) {
                        rows.push_back( Row( i->first , i->second->error ) );
                    }
                    else if ( i->second->prev.isEmpty() || i->second->now.isEmpty() ) {
                        rows.push_back( Row( i->first ) );
                    }
                    else {
                        BSONObj out = _statUtil.doRow( i->second->prev , i->second->now );
                        rows.push_back( Row( i->first , out ) );
                    }

                    if ( discover && ! i->second->now.isEmpty() ) {
                        if ( _discover( threads , i->first , i->second ) )
                            break;
                    }
                }

                // compute some stats
                unsigned longestHost = 0;
                BSONObj biggest;
                for ( unsigned i=0; i<rows.size(); i++ ) {
                    if ( rows[i].host.size() > longestHost )
                        longestHost = rows[i].host.size();
                    if ( rows[i].data.nFields() > biggest.nFields() )
                        biggest = rows[i].data;

                    // adjust width up as longer 'locked db' values appear
                    setMaxLockedDbWidth( &rows[i].data, &maxLockedDbWidth ); 
                }

                {
                    // check for any headers not in biggest

                    // TODO: we put any new headers at end,
                    //       ideally we would interleave

                    set<string> seen;

                    BSONObjBuilder b;

                    {
                        // iterate biggest
                        BSONObjIterator i( biggest );
                        while ( i.more() ) {
                            BSONElement e = i.next();
                            seen.insert( e.fieldName() );
                            b.append( e );
                        }
                    }

                    // now do the rest
                    for ( unsigned j=0; j<rows.size(); j++ ) {
                        BSONObjIterator i( rows[j].data );
                        while ( i.more() ) {
                            BSONElement e = i.next();
                            if ( seen.count( e.fieldName() ) )
                                continue;
                            seen.insert( e.fieldName() );
                            b.append( e );
                        }

                    }

                    biggest = b.obj();

                }

                // display data

                cout << endl;

                //    header
                if ( row++ % 5 == 0 && ! biggest.isEmpty() ) {
                    cout << setw( longestHost ) << "" << "\t";
                    printHeaders( biggest );
                }

                //    rows
                for ( unsigned i=0; i<rows.size(); i++ ) {
                    cout << setw( longestHost ) << rows[i].host << "\t";
                    if ( rows[i].err.size() )
                        cout << rows[i].err << endl;
                    else if ( rows[i].data.isEmpty() )
                        cout << "no data" << endl;
                    else
                        printData( rows[i].data , biggest );
                }

            }

            return 0;
        }
Example #8
0
 GTRRateMatrix::GTRRateMatrix(StateMap const & staMap, vector_t const & rateParameters, vector_t const & equiFreqs) 
   : BaseTRRateMatrix(rateParameters, equiFreqs, "GTR"), staMap_(staMap) 
   { 
     assert( (staMap.stateCount() * ( staMap.stateCount() - 1) ) / 2 == rateParameterCount_);
     assert(staMap.stateCount() == equiFreqCount_);
   }
Example #9
0
 GTRRateMatrix::GTRRateMatrix(StateMap const & staMap) 
   : BaseTRRateMatrix( (staMap.stateCount() * ( staMap.stateCount() - 1) ) / 2, staMap.stateCount(), "GTR"), staMap_(staMap) 
 {}
Example #10
0
 void normalizeRM(matrix_t & Q, StateMap const & staMap)
 {
   normalizeRM(Q, staMap, staMap.symbolSize() );
 }
Example #11
0
void PrintFibres(P &out,const TopCompress &compress,const BottomCompress &bottom,const StateMap &map)
 {
  Printf(out,"#;\n",Title("Fibres"));

  bottom.applyForStates( [&] (State state)
                             {
                              auto range=map.getFibre(state);

                              Printf(out,"\n#;:#;) <-",state.getIndex(),state.getPropIndex());

                              if( !range )
                                {
                                 Putobj(out," empty\n");
                                }
                              else
                                {
                                 PrintFibre(out,compress,range);
                                }
                             }
                       );

  {
   auto range=map.getNullFibre();

   if( +range )
     {
      Printf(out,"\nNULL <-");

      PrintFibre(out,compress,range);
     }
  }

  Printf(out,"\n#;\n",TextDivider());

  ulen count=0;

  bottom.applyForStates( [&] (State state)
                             {
                              auto range=map.getFibre(state);

                              if( range.len>1 && HasMultipleProps(range) )
                                {
                                 Printf(out,"\n#;) MP fibre size #;\n",state.getIndex(),range.len);

                                 count++;
                                }
                             }
                       );

  {
   auto range=map.getNullFibre();

   if( range.len>1 && HasMultipleProps(range) )
     {
      Printf(out,"\nNULL MP fibre size #;\n",range.len);

      count++;
     }
  }

  Printf(out,"\nMP fibres = #;\n",count);

  Printf(out,"\n#;\n",TextDivider());
 }
Example #12
0
void* MCJITHelper::dynamicInlinerForOpenOSR(Function* F1, Instruction* OSRSrc, void* extra, void* profDataAddr) {
    DynamicInlinerInfo* inlineInfo = (DynamicInlinerInfo*) extra;
    MCJITHelper* TheHelper = inlineInfo->TheHelper;
    bool verbose = TheHelper->verbose;

    assert(OSRSrc->getParent()->getParent() == F1 && "MCJIT messed up the objects");

    if (verbose) {
        std::cerr << "Value for F1 is " << F1 << std::endl;
        std::cerr << "Value for OSRSrc is " << OSRSrc << std::endl;
        std::cerr << "Value for extra is " << extra << std::endl;
        std::cerr << "Value for profDataAddr is " << profDataAddr << std::endl;
    }

    std::pair<Function*, StateMap*> identityPair = StateMap::generateIdentityMapping(F1);

    Function* F2 = identityPair.first;
    StateMap* M = identityPair.second;

    Value* valToInlineInF2 = M->getCorrespondingOneToOneValue(inlineInfo->valToInline);

    Instruction* OSRSrcInF2 = cast<Instruction>(M->getCorrespondingOneToOneValue(OSRSrc));
    assert (OSRSrcInF2 != nullptr && "TODO cannot find corresponding OSRSrc in temporary F2");
    if (OSRLibrary::removeOSRPoint(*OSRSrcInF2) && verbose) {
        std::cerr << "OSR point removed after cloning F1" << std::endl;
    }

    Instruction* LPad = M->getLandingPad(OSRSrc);

    StateMap* M_F2toOSRContFun;
    LivenessAnalysis LA(F1);
    std::vector<Value*>* valuesToPass = OSRLibrary::getLiveValsVecAtInstr(OSRSrc, LA);
    std::string OSRDestFunName = (F2->getName().str()).append("OSRCont");
    Function* OSRContFun = OSRLibrary::genContinuationFunc(TheHelper->Context,
            *F1, *F2, *OSRSrc, *LPad, *valuesToPass, *M, &OSRDestFunName, verbose, &M_F2toOSRContFun);

    Value* valToInline = M_F2toOSRContFun->getCorrespondingOneToOneValue(valToInlineInF2);
    assert (valToInline != nullptr && "broken state map for continuation function");

    delete valuesToPass;
    delete F2;
    delete M;
    delete M_F2toOSRContFun;

    // create a module for generated code
    std::string modForJITName = "OpenOSRDynInline";
    modForJITName.append(OSRDestFunName);
    std::unique_ptr<Module> modForJIT = llvm::make_unique<Module>(modForJITName, TheHelper->Context);
    Module* modForJIT_ptr = modForJIT.get();

    // determine which function is called
    uint64_t calledFun = (uint64_t)profDataAddr;

    std::cerr << "Address of invoked function: " << calledFun << std::endl;
    Function* funToInline = nullptr;
    for (AddrSymPair &pair: TheHelper->CompiledFunAddrTable) {
        if (pair.first == calledFun) {
            std::string &FunName = pair.second;
            funToInline = TheHelper->getFunction(FunName);
            break;
        }
    }

    Function* myFunToInline = nullptr;
    if (funToInline == nullptr) {
        std::cerr << "Sorry, I could not determine which function was called!" << std::endl;
    } else {
        std::cerr << "Function being inlined: " << funToInline->getName().str() << std::endl;
        ValueToValueMapTy VMap;
        myFunToInline = CloneFunction(funToInline, VMap, false, nullptr);
        myFunToInline->addFnAttr(Attribute::AlwaysInline);
        myFunToInline->setLinkage(Function::LinkageTypes::PrivateLinkage);
        modForJIT_ptr->getFunctionList().push_back(myFunToInline);
        OSRLibrary::fixUsesOfFunctionsAndGlobals(funToInline, myFunToInline);
        for (Value::use_iterator UI = valToInline->use_begin(),
                UE = valToInline->use_end(); UI != UE; ) {
            Use &U = *(UI++);
            if (CallInst* CI = dyn_cast<CallInst>(U.getUser())) {
                if (CI->getParent()->getParent() != OSRContFun) continue;
                if (verbose) {
                    raw_os_ostream errStream(std::cerr);
                    std::cerr << "Updating instruction ";
                    CI->print(errStream);
                    std::cerr << std::endl;
                }
                U.set(myFunToInline);
            }
        }
    }

    modForJIT_ptr->getFunctionList().push_back(OSRContFun);
    verifyFunction(*OSRContFun, &outs());

    // remove dead code & inline when possible
    FunctionPassManager FPM(modForJIT_ptr);
    FPM.add(createCFGSimplificationPass());
    FPM.doInitialization();
    FPM.run(*OSRContFun);

    if (funToInline != nullptr) {
        PassManager PM;
        PM.add(llvm::createAlwaysInlinerPass());
        PM.run(*modForJIT_ptr);
    }

    // compile code
    TheHelper->addModule(std::move(modForJIT));
    return (void*)TheHelper->JIT->getFunctionAddress(OSRDestFunName);
}
string const varVecToObsStr(vector<unsigned> const & varVec, StateMap const & metaNucMap)
{
  string obsStr;
  BOOST_FOREACH(unsigned state, varVec) {
    obsStr += metaNucMap.state2Symbol(state);
  }
Example #14
0
 /**
  * Creates a new state. Make sure the state doesn't exist when calling this
  * function!
  * This function also inserts the state into the _states map.
  * @param id The id of the new state.
  **/
 State *createState(StateId id) {
     State *state = stateFactory(id);
     state->load();
     _states.insert(StateMapPair(id, state));
     return state;
 }