Exemplo n.º 1
0
/**
 * \brief Executes the callback of a timer.
 *
 * Then, if the callback returns \c true, the timer is rescheduled,
 * otherwise it is discarded.
 *
 * Does nothing if the timer is already finished.
 *
 * \param timer The timer to execute.
 */
void LuaContext::do_timer_callback(const TimerPtr& timer) {

  Debug::check_assertion(timer->is_finished(), "This timer is still running");

  auto it = timers.find(timer);
  if (it != timers.end() &&
      !it->second.callback_ref.is_empty()) {
    ScopedLuaRef& callback_ref = it->second.callback_ref;
    push_ref(l, callback_ref);
    const bool success = call_function(0, 1, "timer callback");

    bool repeat = false;
    if (success) {
      repeat = lua_isboolean(l, -1) && lua_toboolean(l, -1);
      lua_pop(l, 1);
    }

    if (repeat) {
      // The callback returned true: reschedule the timer.
      timer->set_expiration_date(timer->get_expiration_date() + timer->get_initial_duration());
      if (timer->is_finished()) {
        // Already finished: this is possible if the duration is smaller than
        // the main loop stepsize.
        do_timer_callback(timer);
      }
    }
    else {
      callback_ref.clear();
      timers_to_remove.push_back(timer);
    }
  }
}
Exemplo n.º 2
0
	void Bagger::buildTree(){
		int treeId;
		
		// Choose tree id
		m_chooseTree->lock();
		treeId = m_treeId++;
		m_chooseTree->unlock();

		// sorting is performed inside this constructor
		DataCachePtr myData = DataCachePtr(new DataCache(m_data,m_evaluation,m_seed));

		// create the in-bag dataset (and be sure to remember what's in bag)
		// for computing the out-of-bag error later
		DataCachePtr bagData = myData->resample(m_bagSize);
		/*bagData.reusableRandomGenerator = bagData.getRandomNumberGenerator(
				random.nextInt());*/
		m_inBag[treeId] = bagData->inBag;

		// build the classifier
		RandomTreePtr aTree = m_trees[treeId];
		aTree->data = bagData;
		TimerPtr timer = TimerPtr(new boost::timer);
		aTree->build();
		double time = timer->elapsed();

		m_chooseTree->lock();
		m_buildTime += time;
		m_totalNumNodes += aTree->getNumNodes();
		m_chooseTree->unlock();
	}
Exemplo n.º 3
0
long EventLoop::earliest_expire_time()
{
    TimerPtr timer = timer_heap_.top();
    if (!timer)
        return -1;
    long delay = timer->expire_msec() - current_msec_;
    return (delay > 0) ? delay : 0;
}
Exemplo n.º 4
0
	bool Time::ResumeTimer(const UInt& _uTimerID)
	{
		TimerPtr pTimer = GetTimer(_uTimerID);
		bool bResult = (NULL != pTimer);

		if (false != bResult)
		{
			bResult = pTimer->Resume();
		}

		return bResult;
	}
Exemplo n.º 5
0
	bool Time::ResetTimer(const UInt& _uTimerID, float& _fElapsedMilliseconds)
	{
		TimerPtr pTimer = GetTimer(_uTimerID);
		bool bResult = (NULL != pTimer);

		if (false != bResult)
		{
			bResult = pTimer->GetElapsedTime(_fElapsedMilliseconds) && pTimer->Reset();
		}

		return bResult;
	}
Exemplo n.º 6
0
 TimerPtr
     Dashboard::getTimer(const std::string & theName) {
         std::map<std::string,TimerPtr>::iterator found = _myTimers.find(theName);
         if (found == _myTimers.end()) {
             TimerPtr myParent   = findParent();
             TimerPtr myNewTimer = TimerPtr(new Timer(myParent));
             _myTimers[theName]  = myNewTimer;
             myNewTimer->setName(theName);
             _mySortedTimers.push_back(std::make_pair(theName, myNewTimer));
             return myNewTimer;
         }
         return found->second;
 }
Exemplo n.º 7
0
void Timer::run(TaskContextPtr taskContextPtr, const boost::system::error_code& e){
	if(e == boost::asio::error::operation_aborted){
		return ;
	}
	taskContextPtr->func_();
	if(taskContextPtr->mode == 1){
		TimerPtr  timerPtr = taskContextPtr->timerPtr_;
		timerPtr->expires_at(timerPtr->expires_at() + boost::posix_time::millisec(taskContextPtr->deadline_time));
		timerPtr->async_wait(taskContextPtr->strand_.wrap(bind(&Timer::run, this, taskContextPtr, placeholders::error)));
	}else{

	}
}
Exemplo n.º 8
0
void GraphIO::loadNodes(const YAML::Node& doc, SemanticVersion version)
{
    TimerPtr timer = getProfiler()->getTimer("load graph");

    YAML::Node nodes = doc["nodes"];
    if (nodes.IsDefined()) {
        for (std::size_t i = 0, total = nodes.size(); i < total; ++i) {
            const YAML::Node& n = nodes[i];

            auto interlude = timer->step(n["uuid"].as<std::string>());
            loadNode(n, version);
        }
    }
}
Exemplo n.º 9
0
void RunLoop::ProcessTimer(TimerPtr timer)
{
    if ( timer->IsCancelled() )
    {
        RemoveTimer(timer);
        return;
    }

    timer->_fn(*timer);     // DO CALLOUT

    if ( !timer->Repeats() || timer->IsCancelled() )
    {
        RemoveTimer(timer);
    }
}
Exemplo n.º 10
0
int EventLoop::handle_timer_events()
{
    if (expired_timers_.empty())
        return 0;

    // handle single timer
    TimerPtr timer = expired_timers_.front();
    expired_timers_.pop_front();
    thread_pool_->promote_new_leader();
    if (timer->repeat()) {
        timer->reset(current_msec_);
        timer_heap_.schedule(timer);
    }
    timer->handle_timeout(current_msec_);
    return 1;
}
    //-------------------------------------------------------------------------
    void RTPReceiverChannelAudio::onTimer(TimerPtr timer)
    {
      ZS_LOG_DEBUG(log("timer") + ZS_PARAM("timer id", timer->getID()))

      AutoRecursiveLock lock(*this);
#define TODO 1
#define TODO 2
    }
Exemplo n.º 12
0
void GraphIO::saveGraphTo(YAML::Node& yaml)
{
    TimerPtr timer = getProfiler()->getTimer("save graph");
    timer->restart();

    yaml["version"] = csapex::info::CSAPEX_VERSION;

    saveNodes(yaml);
    saveConnections(yaml);

    {
        auto interlude = timer->step("save view");
        saveViewRequest(graph_, yaml);
    }

    timer->finish();
}
Exemplo n.º 13
0
    void TimerMonitor::monitorBegin(TimerPtr timer)
    {
      AutoRecursiveLock lock(mLock);

      if (!mThread) {
        mThread = ThreadPtr(new boost::thread(boost::ref(*getTimerMonitor().get())));
      }

      PUID timerID = timer->getID();
      mMonitoredTimers[timerID] = TimerWeakPtr(timer);
      wakeUp();
    }
Exemplo n.º 14
0
	UInt Time::CreateTimer(const bool _bStart)
	{
		bool bResult = true;

		TimerPtrVec::iterator iTimer = m_vpTimers.begin();
		TimerPtrVec::iterator iEnd = m_vpTimers.end();
		TimerPtr pTimer = NULL;
		UInt uResult = 0;

		while (iEnd != iTimer)
		{
			if (false == (*iTimer)->m_bIsActive)
			{
				pTimer = *iTimer;
				break;
			}
			++iTimer;
			++uResult;
		}

		if (NULL == pTimer)
		{
			pTimer = new Timer(m_lTicksPerSeconds);
			m_vpTimers.push_back(pTimer);
		}

		if (false == _bStart)
		{
			float fTemp;
			bResult = ResetTimer(uResult, fTemp) && PauseTimer(uResult);
		}

		if (false == bResult)
		{
			pTimer->Release();
			uResult = 0xffffffff;
		}

		return uResult;
	}
Exemplo n.º 15
0
void GraphIO::loadGraphFrom(const YAML::Node& doc)
{
    TimerPtr timer = getProfiler()->getTimer("load graph");
    timer->restart();

    SemanticVersion version;
    if(doc["version"].IsDefined()) {
        version = doc["version"].as<SemanticVersion>();
    } else {
        // with 0.9.7 versioning was introduced, so assume that the version was 0.9.6
        version = SemanticVersion(0, 9, 6);
    }

    graph_.getLocalGraph()->beginTransaction();
    {
        auto interlude = timer->step("load nodes");
        loadNodes(doc, version);
    }

    {
        auto interlude = timer->step("load connections");
        loadConnections(doc, version);
    }
    graph_.getLocalGraph()->finalizeTransaction();

    {
        auto interlude = timer->step("load view");
        loadViewRequest(graph_, doc);
    }

    timer->finish();
}
Exemplo n.º 16
0
    void TimerMonitor::operator()()
    {
      bool shouldShutdown = false;

#ifndef _LINUX
      pthread_setname_np("com.zslib.timer");
#endif

      do
      {
        Duration duration;
        // wait completed, do notifications from select
        {
          AutoRecursiveLock lock(mLock);
          shouldShutdown = mShouldShutdown;

          duration = fireTimers();
        }

        boost::unique_lock<boost::mutex> flagLock(mFlagLock);
        mFlagNotify.timed_wait<Duration>(flagLock, duration);

        // notify all those timers needing to be notified
      } while (!shouldShutdown);

      {
        AutoRecursiveLock lock(mLock);

        // go through timers and cancel them completely
        for (TimerMap::iterator monIter = mMonitoredTimers.begin(); monIter != mMonitoredTimers.end(); ) {
          TimerMap::iterator current = monIter;
          ++monIter;

          TimerPtr timer = current->second.lock();
          if (timer)
            timer->background(false);
        }
        mMonitoredTimers.clear();
      }
    }
Exemplo n.º 17
0
int EventLoop::get_ready_events()
{
    int nevents = expired_timers_.size() + fired_events_.size();
    if (nevents == 0) {
        long delta = current_msec_;
        long earliest = earliest_expire_time();
        nevents = poller_->dispatch(fired_events_, earliest);

        update_time();
        delta = current_msec_ - delta;
        if (delta) {
            TimerPtr timer = timer_heap_.top();
            while (timer && timer->expire_msec() <= current_msec_) {
                expired_timers_.push_back(timer);
                timer_heap_.pop();
                timer = timer_heap_.top();
                ++nevents;
            }
        }
    }
    return nevents;
}
Exemplo n.º 18
0
    void
        Dashboard::printTimers(Table & theTable, TimerPtr theParent, const std::string & theIndent) {

            for (unsigned i = 0; i < _mySortedTimers.size(); ++i) {
                std::string & myTimerName = _mySortedTimers[i].first;
                TimerPtr myTimerPtr       = _mySortedTimers[i].second;

                if (myTimerPtr->getParent() == theParent) {
                    theTable.addRow();
                    theTable.setField("timername", theIndent + myTimerName);
                    unsigned long myCycleCount = _myGroupCounters[myTimerPtr->getGroup()].getCount();
                    if (myCycleCount) {
                        const asl::Timer & myTimer = _myCompleteCycleTimers[myTimerName];
                        theTable.setField("elapsed",as_string((double)(myTimer.getElapsed().micros())/1000.0/myCycleCount));
                        const asl::Counter & myCounter = myTimer.getCounter();
                        if (myCounter.getCount()>1) {
                            theTable.setField("intervals",as_string((myCounter.getCount()+1.0)/myCycleCount));
                            theTable.setField("persec",as_string(myCounter.getCount()/myTimer.getElapsed().seconds()));
                            theTable.setField("average",as_string(myTimer.getElapsed().micros()/1000.0/myCounter.getCount()));
                            theTable.setField("minimum",as_string(myTimer.getMin().micros()/1000.0));
                            theTable.setField("maximum",as_string(myTimer.getMax().micros()/1000.0));
                            theTable.setField("cycles",as_string(myCycleCount));
                        }
                    } else {
                        const asl::Timer & myTimer = *myTimerPtr;
                        const asl::Counter & myCounter = myTimer.getCounter();
                        theTable.setField("elapsed",as_string((double)(myTimer.getElapsed().micros())/1000.0));
                        if (myCounter.getCount()>1) {
                            theTable.setField("intervals",as_string(myCounter.getCount()));
                            theTable.setField("persec",as_string(myCounter.getCount()/myTimer.getElapsed().seconds()));
                            theTable.setField("average",as_string(myTimer.getElapsed().micros()/1000.0));
                        }
                        theTable.setField("cycles","incomplete");
                    }
                    printTimers(theTable, myTimerPtr, theIndent+"  ");
                }
            }
    }
Exemplo n.º 19
0
void TimeManager::Register(TimerPtr pTimer,TimerStop bStop )
{
	uint64 nTime;
	mpTime->Get(nTime);

	TimerDataPtr ptd = new TimerData;
	ptd->pTimer = pTimer;
	ptd->nLastTime = nTime + pTimer->GetInterval();
	ptd->bStop = bStop;

	mTimeDatas.push_back(ptd);
	
	
}
Exemplo n.º 20
0
    Duration TimerMonitor::fireTimers()
    {
      AutoRecursiveLock lock(mLock);

      Time time = boost::posix_time::microsec_clock::universal_time();

      Duration duration = boost::posix_time::seconds(1);

      for (TimerMap::iterator monIter = mMonitoredTimers.begin(); monIter != mMonitoredTimers.end(); )
      {
        TimerMap::iterator current = monIter;
        ++monIter;

        TimerPtr timer = (current->second).lock();
        bool done = true;

        if (timer)
          done = timer->tick(time, duration);

        if (done)
          mMonitoredTimers.erase(current);
      }
      return duration;
    }
Exemplo n.º 21
0
void TaskManager::scheduleRepeated(const TimerPtr& timer) {
	_scheduler->scheduleRepeated(timer, IceUtil::Time::milliSeconds(timer->delay()));
}
Exemplo n.º 22
0
	void GPUERT::deviceHandler(){
		m_devId = m_gfxMgr->createDeviceContext(m_devId);
		calculateMaxNodes(m_devId);
		initResources();

		m_constantsBagging.cb_baggingActivated = false;
		TimerPtr timerKernel = TimerPtr(new boost::timer);

		TimerPtr timer = TimerPtr(new boost::timer);
		TimerPtr timerTotal = TimerPtr(new boost::timer);

		m_internalNodes = 0, m_leafNodes = 0;
		int treesLeft = m_numTrees;
		int treesToLaunch = m_maxTreesPerIteration;
		int lastLaunch = 0;
		int checkSum = m_maxTreesPerIteration;
		int newNodes = m_maxTreesPerIteration;
		std::vector<int> checkVars(4,0);

		m_buildTime = 0;
		m_baggingTime = 0;
		m_classificationTime = 0;
		m_depth = 0;

		timerTotal->restart();
		int numIter = ceil(float(m_numTrees)/float(m_maxTreesPerIteration));
		for(unsigned int j=0; j<numIter; ++j){
			checkSum = treesToLaunch;
			newNodes = treesToLaunch;
			m_constants.cb_nodeBufferStart = 0;
			m_constants.cb_nodeIdFlip = 0;

			initResourceBatch(lastLaunch == treesToLaunch);
			lastLaunch = treesToLaunch;

			treesLeft -= treesToLaunch;
			timer->restart();
			for(unsigned int i=0; i<m_MaxDepth; ++i){
				if(i > m_depth)
					m_depth = i;

				assert(newNodes < m_maxNodesPerIteration);
				int nodeLimit = 10000;
				int innerNodes = newNodes;
				int numInnerIter = ceil(float(innerNodes)/float(nodeLimit));
				int launchCount = 0;

				m_constants.cb_availableNodes = newNodes;
				m_constants.cb_numFeatures = 0;
				timerKernel->restart();
				for(unsigned int k=0; k<m_numFeatures; ++k){
					innerNodes = newNodes;
					numInnerIter = ceil(float(innerNodes)/float(nodeLimit));
					launchCount = 0;

					// Best split kernel
					m_gfxMgr->setGPUBuffer(m_devId,m_setBufferIdsExFindSplit,m_setResourceTypesExFindSplit);
					m_gfxMgr->setGPUProgram(m_devId,m_gpuFunctionIds["RFP_ExFindSplit"]);

					innerNodes = newNodes;
					launchCount = 0;
					m_constants.cb_currentDepth = 0;
					for(unsigned int l=0; l<numInnerIter; ++l){
						launchCount = innerNodes > nodeLimit ? nodeLimit : innerNodes;
						
						m_gfxMgr->copyToGPU(m_devId,m_bufferIds["RFB_constants"],&ConstantUpdate(&m_constants,KID_ExtremeFindSplit),sizeof(m_constants));
						m_gfxMgr->launchComputation(m_devId,launchCount*thread_group_size,1,1);

						m_constants.cb_currentDepth += launchCount;
						innerNodes -= launchCount;
						if(innerNodes <= 0)
							break;
					}
					++m_constants.cb_numFeatures;
				}
				if(m_kernelSpecificTimings){
					m_gfxMgr->syncDevice(m_devId);
					m_kernelTimes[L"RFP_ExFindSplit"] += timerKernel->elapsed();
				}

				innerNodes = newNodes;
				launchCount = 0;
				m_constants.cb_currentDepth = 0;
				m_gfxMgr->setGPUBuffer(m_devId,m_setBufferIdsExMakeSplit,m_setResourceTypesExMakeSplit);
				m_gfxMgr->setGPUProgram(m_devId,m_gpuFunctionIds["RFP_ExMakeSplit"]);

				// Split data
				timerKernel->restart();
				for(unsigned int k=0; k<numInnerIter; ++k){
					launchCount = innerNodes > nodeLimit ? nodeLimit : innerNodes;
						
					m_gfxMgr->copyToGPU(m_devId,m_bufferIds["RFB_constants"],&ConstantUpdate(&m_constants,KID_ExtremeMakeSplit),sizeof(m_constants));
					m_gfxMgr->launchComputation(m_devId,launchCount*thread_group_size,1,1);

					m_constants.cb_currentDepth += launchCount;
					innerNodes -= launchCount;
					if(innerNodes <= 0)
						break;
				}
				if(m_kernelSpecificTimings){
					m_gfxMgr->syncDevice(m_devId);
					m_kernelTimes[L"RFP_ExMakeSplit"] += timerKernel->elapsed();
				}

				// Swap buffer to avoid unecessary copying
				int tmpBuffId = m_bufferIds["RFB_nodeIndices"];
				m_bufferIds["RFB_nodeIndices"] = m_bufferIds["RFB_nodeIndicesMirror"];
				m_bufferIds["RFB_nodeIndicesMirror"] = tmpBuffId;
				setBufferSettings();

				m_constants.cb_currentDepth = i;

				// Evaluate splits
				timerKernel->restart();
				m_gfxMgr->setGPUBuffer(m_devId,m_setBufferIdsExCreateNodes,m_setResourceTypesExCreateNodes);
				m_gfxMgr->copyToGPU(m_devId,m_bufferIds["RFB_constants"],&ConstantUpdate(&m_constants,KID_ExtremeCreateNodes),sizeof(m_constants));
				m_gfxMgr->setGPUProgram(m_devId,m_gpuFunctionIds["RFP_ExCreateNodes"]);
				m_gfxMgr->launchComputation(m_devId,newNodes,1,1);

				// Get continuation variables
				m_gfxMgr->copyFromGPU(m_devId,m_bufferIds["RFB_checkVariables"],&checkVars[0],checkVars.size()*sizeof(int));
				if(m_kernelSpecificTimings){
					m_gfxMgr->syncDevice(m_devId);
					m_kernelTimes[L"RFP_ExCreateNd"] += timerKernel->elapsed();
				}
			
				m_constants.cb_nodeBufferStart = checkSum;
				checkSum = checkVars[2];
				m_leafNodes += checkVars[3];
				newNodes = checkSum;
				m_internalNodes += checkSum;

				checkVars[1] = 0;
				checkVars[2] = 0;
				checkVars[3] = 0;
				m_gfxMgr->copyToGPU(m_devId,m_bufferIds["RFB_checkVariables"],&checkVars[0],checkVars.size()*sizeof(int));
				m_constants.cb_nodeIdFlip = (m_constants.cb_nodeIdFlip == 0) ? 1 : 0;

				if(newNodes <= 0)
					break;
			}
			m_buildTime += timer->elapsed();

			// Vote on test instances
			timer->restart();
			runClassificationProcess(treesToLaunch);
			m_classificationTime += timer->elapsed();

			if(m_saveModel)
				getResultsFromGPU();

			m_data->m_gui->setProgressBar(IDC_PROGRESSBAR_PROGRESS,100,(float(m_numTrees-treesLeft)/float(m_numTrees))*100);
			if(treesLeft != 0 && treesLeft < m_maxTreesPerIteration){
				treesToLaunch = treesLeft;
				m_maxTreesPerIteration = treesToLaunch;
			}
		}

		getVotesFromGPU();
		m_totalTime = timerTotal->elapsed();
		m_bar->wait();
	}
Exemplo n.º 23
0
void Timer::destroy(int id)
{
  TimerPtr timer = city::Timers::instance().find( id );
  if( timer.isValid() )
    timer->destroy();
}
Exemplo n.º 24
0
void
TimerPriorityTest::run()
{
#ifdef _WIN32
    //
    // Test to create a timer with a given priority
    //
    try
    {
        TimerPtr t = new Timer(THREAD_PRIORITY_IDLE);
        t->destroy();

        t = new Timer(THREAD_PRIORITY_LOWEST);
        t->destroy();

        t = new Timer(THREAD_PRIORITY_BELOW_NORMAL);
        t->destroy();

        t = new Timer(THREAD_PRIORITY_NORMAL);
        t->destroy();

        t = new Timer(THREAD_PRIORITY_ABOVE_NORMAL);
        t->destroy();

        t = new Timer(THREAD_PRIORITY_HIGHEST);
        t->destroy();

        t = new Timer(THREAD_PRIORITY_TIME_CRITICAL);
        t->destroy();
    }
    catch(...)
    {
        test(false);
    }

    //
    // Test to create a timer with priorities too high
    //
    try
    {
        TimerPtr t = new Timer(THREAD_PRIORITY_TIME_CRITICAL + 10);
        test(false);
    }
    catch(const ThreadSyscallException&)
    {
        //Expected
    }
    catch(...)
    {
        test(false);
    }

    //
    // Test to create a timer with priorities too low
    //
    try
    {
        TimerPtr t = new Timer(THREAD_PRIORITY_IDLE - 10);
        test(false);
    }
    catch(const ThreadSyscallException&)
    {
        //Expected
    }
    catch(...)
    {
        test(false);
    }

#else

    //
    // Test to create a timer with a given priority
    //
    ThreadControl c;
    try
    {
        for(int cont = 1; cont < 10; ++cont)
        {
            TimerPtr t = new Timer(cont);
        }
    }
    catch(...)
    {
        test(false);
    }

    //
    // Test to create a timer with priorities too high
    //
    for(int cont = 1; cont < 10; ++cont)
    {
        try
        {
            TimerPtr t = new Timer(300 * cont);
            test(false);
        }
        catch(const ThreadSyscallException& e)
        {
            //Expected
        }
        catch(...)
        {
            test(false);
        }
    }

    //
    // Test to create a timer with priorities too low
    //
    for(int cont = 1; cont < 10; ++cont)
    {
        try
        {
            TimerPtr t = new Timer(-10 * cont);
            test(false);
        }
        catch(const ThreadSyscallException& e)
        {
            //Expected
        }
        catch(...)
        {
            test(false);
        }
    }
#endif
}
Exemplo n.º 25
0
	bool Time::GetElapsedTime(const UInt& _uTimerID, float& _fElapsedMilliseconds)
	{
		TimerPtr pTimer = GetTimer(_uTimerID);
		bool bResult = (NULL != pTimer) && (false != pTimer->GetElapsedTime(_fElapsedMilliseconds));
		return bResult;
	}
void RunLoop::AddTimer(TimerPtr timer)
{
    StackLock lock(_listLock);
    if ( ContainsTimer(timer) )
        return;
    
    _timers.push_back(timer);
    _timers.sort();
    
    if ( _waiting && _waitingUntilTimer != nullptr && _waitingUntilTimer->GetNextFireDate() < timer->GetNextFireDate() )
    {
        // signal a Run() invocation that it needs to adjust its timeout to the fire
        // date of this new timer
        WakeUp();
    }
}
Exemplo n.º 27
0
/**
 * \brief Registers a timer into a context (table or a userdata).
 * \param timer A timer.
 * \param context_index Index of the table or userdata in the stack.
 * \param callback_ref Lua ref to the function to call when the timer finishes.
 */
void LuaContext::add_timer(
    const TimerPtr& timer,
    int context_index,
    const ScopedLuaRef& callback_ref
) {
  const void* context;
  if (lua_type(l, context_index) == LUA_TUSERDATA) {
    ExportableToLuaPtr* userdata = static_cast<ExportableToLuaPtr*>(
        lua_touserdata(l, context_index)
    );
    context = userdata->get();
  }
  else {
    context = lua_topointer(l, context_index);
  }

  callback_ref.push();

#ifndef NDEBUG
  // Sanity check: check the uniqueness of the ref.
  for (const auto& kvp: timers) {
    if (kvp.second.callback_ref.get() == callback_ref.get()) {
      std::ostringstream oss;
      oss << "Callback ref " << callback_ref.get()
          << " is already used by a timer (duplicate luaL_unref?)";
      Debug::die(oss.str());
    }
  }
#endif

  Debug::check_assertion(timers.find(timer) == timers.end(),
      "Duplicate timer in the system");

  timers[timer].callback_ref = callback_ref;
  timers[timer].context = context;

  Game* game = main_loop.get_game();
  if (game != nullptr) {
    // We are during a game: depending on the timer's context,
    // suspend the timer or not.
    if (is_map(l, context_index)
        || is_entity(l, context_index)
        || is_item(l, context_index)) {

      bool initially_suspended = false;

      // By default, we want the timer to be automatically suspended when a
      // camera movement, a dialog or the pause menu starts.
      if (!is_entity(l, context_index)) {
        // The timer normally gets suspended/resumed with the map.
        timer->set_suspended_with_map(true);

        // But in the initial state, we override that rule.
        // We initially suspend the timer only during a dialog.
        // In particular, we don't want to suspend timers created during a
        // camera movement.
        // This would be very painful for users.
        initially_suspended = game->is_dialog_enabled();
      }
      else {
        // Entities are more complex: they also get suspended when disabled
        // and when far from the camera. Therefore, they don't simply follow
        // the map suspended state.
        EntityPtr entity = check_entity(l, context_index);
        initially_suspended = entity->is_suspended() || !entity->is_enabled();
      }

      timer->set_suspended(initially_suspended);
    }
  }
}
	void ExRandomTrees::evaluateTestSet(){
		m_votes.assign(m_evaluation->getNumTestingInstances()*2,0);
		m_workLeft = m_trees.size();
		m_choiceId = 0;

		TM_runFunctionPtr runFunc = TM_runFunctionPtr(new boost::function<void(void)>(std::bind(std::mem_fun(&ExRandomTrees::collectSingleVote),this)));
		TM_callbackFunctionPtr callbackFunc = TM_callbackFunctionPtr(new boost::function<void(int)>(std::bind1st(std::mem_fun(&ExRandomTrees::buildTreeCallback),this)));
		for(size_t treeIdx = 0; treeIdx < m_trees.size(); ++treeIdx) {
			ThreadManager::queueWorkPackage(runFunc,callbackFunc);
		}

		// Start tree building threads
		TimerPtr timer = TimerPtr(new boost::timer);
		ThreadManager::executeWorkQueue();
		m_barrier->wait();
		m_testTimer = timer->elapsed();

		unsigned int correct = 0,wrong = 0;

		std::map<double,std::vector<bool>,std::greater<double>> rankingcl1,rankingcl2;
		bool truePositive;
		std::vector<int> clCorrect(m_document->getNumClassValues(),0),clWrong(m_document->getNumClassValues(),0);
		std::vector<double> probs(2,0);
		for(size_t i = 0; i < m_evaluation->getNumTestingInstances(); i++) {
			unsigned int vote = m_votes[2*i] > m_votes[2*i+1] ? 0 : 1;
			probs[0] = m_votes[2*i];
			probs[1] = m_votes[2*i+1];

			if(vote == m_classSetTest[i]){
				++clCorrect[m_classSetTest[i]];
				truePositive;
				++correct;
			}
			else{
				++clWrong[m_classSetTest[i]];
				++wrong;
			}

			rankingcl1[double(probs[0])/double(probs[0]+probs[1])].push_back((0 == m_classSetTest[i]));
			rankingcl2[double(probs[1])/double(probs[0]+probs[1])].push_back((1 == m_classSetTest[i]));
		}

		double auc = (m_evaluation->calculateAUC(rankingcl1)+m_evaluation->calculateAUC(rankingcl2))/2.0;

		m_outputStream <<
			"	Time used for building trees: " << m_buildTimer << "s\r\n" <<
			"	Time used for evaluation of trees: " << m_testTimer << "s\r\n";

		m_outputStream	<< 
			"\r\n	"; 

		std::vector<std::vector<unsigned int>> confusMatrix(m_document->getNumClassValues(),std::vector<unsigned int>(m_document->getNumClassValues(),0));
		for(unsigned int i=0; i<m_document->getNumClassValues(); ++i){
			for(unsigned int j=0; j<m_document->getNumClassValues(); ++j){
				if(i == j)
					confusMatrix[i][j] = clCorrect[i];
				else
					confusMatrix[i][j] = clWrong[i];
			}
		}

		for(unsigned int i=0; i<m_document->getNumClassValues(); ++i){
			m_outputStream << i << "	";
		}
		for(unsigned int i=0; i<m_document->getNumClassValues(); ++i){
			m_outputStream << "\r\n	";
			for(unsigned int j=0; j<m_document->getNumClassValues(); ++j){
				m_outputStream  << confusMatrix[i][j] << "	";
			}
			m_outputStream << i;
		}
		m_outputStream << "\r\n";

		double accuracy = double(double(correct)/double(wrong+correct))*100;

		m_outputStream << 
			"\r\n	" << "Accuracy: " << accuracy << "%\r\n";

		PROCESS_MEMORY_COUNTERS pmc;
		GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
		size_t memUsageCPU = pmc.WorkingSetSize;

		m_resultWriter->addKeyValue("accuracy",accuracy);
		m_resultWriter->addKeyValue("cl1Correct",clCorrect[0]);
		m_resultWriter->addKeyValue("cl1Wrong",clWrong[0]);
		m_resultWriter->addKeyValue("cl2Correct",clCorrect[1]);
		m_resultWriter->addKeyValue("cl2Wrong",clWrong[1]);
		m_resultWriter->addKeyValue("trainingTime",m_buildTimer);
		m_resultWriter->addKeyValue("testingTime",m_testTimer);
		m_resultWriter->addKeyValue("totalTime",m_testTimer+m_buildTimer);
		m_resultWriter->addKeyValue("testingInstances",m_evaluation->getNumTestingInstances());
		m_resultWriter->addKeyValue("trainingInstances",m_evaluation->getNumTrainingInstances());
		m_resultWriter->addKeyValue("auc",auc);
		m_resultWriter->addKeyValue("memUsageCPU",double(memUsageCPU)/1024.0/1024.0);
	}
Exemplo n.º 29
0
 bool operator()(const TimerPtr & a, const TimerPtr & b) const { return a->getMax() < b->getMax(); }
	void ExRandomTrees::run(){
		std::string setting = m_data->m_gui->getEditText(IDC_RANDFOREST_NUMFEATURES);
		
		try{
			m_numFeatures = boost::lexical_cast<int,std::string>(setting);
		}
		catch(...){
			m_numFeatures = (int)log((float)m_document->getNumAttributes())+1;
		}

		if(m_numFeatures == 0)
			m_numFeatures = (int)log((float)m_document->getNumAttributes())+1;

		setting = m_data->m_gui->getEditText(IDC_RANDFOREST_NUMTREES);
		try{
			m_numTrees = boost::lexical_cast<int,std::string>(setting);
		}
		catch(...){
			m_numTrees = 10;
		}

		setting = m_data->m_gui->getEditText(IDC_RANDFOREST_TREEDEPTH);
		try{
			m_maxDepth = boost::lexical_cast<int,std::string>(setting);
		}
		catch(...){
			m_maxDepth = 100;
		}

		setting = m_data->m_gui->getEditText(IDC_RANDFOREST_SEED);
		try{
			m_randomSeed = boost::lexical_cast<int,std::string>(setting);
		}
		catch(...){
			m_randomSeed = 100;
		}

		setting = m_data->m_gui->getEditText(IDC_RANDFOREST_MAXINST);
		try{
			m_minNumInst = boost::lexical_cast<int,std::string>(setting);
		}
		catch(...){
			m_minNumInst = 10;
		}

		m_dataSetTrain.assign(m_document->getNumAttributes(),std::vector<float>(m_evaluation->getNumTrainingInstances(),0));
		m_classSetTrain.assign(m_document->getNumInstances(),0);
		m_dataSetTest.assign(m_document->getNumAttributes(),std::vector<float>(m_evaluation->getNumTestingInstances(),0));
		m_classSetTest.assign(m_evaluation->getNumTestingInstances(),0);
		for(unsigned int a=0; a<m_document->getNumAttributes(); ++a){
			for(unsigned int i=0; i<m_evaluation->getNumTrainingInstances(); ++i){
				m_dataSetTrain[a][i] = m_evaluation->getTrainingInstance(i)->getValue(a);

				if(a == 0)
					m_classSetTrain[i] = m_evaluation->getTrainingInstance(i)->classValue();
			}

			for(unsigned int i=0; i<m_evaluation->getNumTestingInstances(); ++i){
				m_dataSetTest[a][i] = m_evaluation->getTestingInstance(i)->getValue(a);

				if(a == 0)
					m_classSetTest[i] = m_evaluation->getTestingInstance(i)->classValue();
			}
		}

		// Create trees
		ExTree tree = ExTree(m_evaluation->getNumTrainingInstances());
		m_trees.assign(m_numTrees,tree);
		
		// Seed treees
		boost::random::uniform_int_distribution<> indRand(0,INT_MAX);
		boost::random::mt19937 rng;
		rng.seed(m_randomSeed);
		for(unsigned int i=0; i<m_trees.size(); ++i){
			m_trees[i].seed(indRand(rng));
		}

		m_choiceId = 0;
		m_workLeft = m_numTrees;
		m_chooseIdLock = MutexPtr(new boost::mutex);
		m_barrier = BarrierPtr(new boost::barrier(2));

		TM_runFunctionPtr runFunc = TM_runFunctionPtr(new boost::function<void(void)>(std::bind(std::mem_fun(&ExRandomTrees::buildTree),this)));
		TM_callbackFunctionPtr callbackFunc = TM_callbackFunctionPtr(new boost::function<void(int)>(std::bind1st(std::mem_fun(&ExRandomTrees::buildTreeCallback),this)));
		for(size_t treeIdx = 0; treeIdx < m_trees.size(); ++treeIdx) {
			ThreadManager::queueWorkPackage(runFunc,callbackFunc);
		}

		// Start tree building threads
		TimerPtr timer = TimerPtr(new boost::timer);
		ThreadManager::executeWorkQueue();
		// Make sure all trees have been trained before proceeding
		m_barrier->wait();
		m_buildTimer = timer->elapsed();

		evaluateTestSet();
	}