/* * Scheduler * * Author:Andrew Salamon * Date: Fri Jun 2 2006 * * Copyright (c) 2006 Machine Perception Laboratory * University of California San Diego. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include "CERT_Scheduler.h" #include #include #include #include #include "CERT_Arguments.h" #include "CERTPlugins.h" #include "numberOfProcessors.h" namespace CERT { const unsigned int AUcnt = 8; template< class T > class certWrap { public: certWrap( T *_obj, Scheduler *_del ) : obj(_obj), delegate(_del) { } void operator()() { delegate->run( obj ); } private: T *obj; Scheduler *delegate; }; class outputWrap { public: outputWrap( Scheduler *_del ) : delegate(_del) { } void operator()() { delegate->output(); } private: Scheduler *delegate; }; /** Scheduler constructor. * Create multiple CERT objects. */ Scheduler::Scheduler( const std::vector< std::string > &paths, int numThreads ) : loader(paths), nextFrame(0), certPool(numThreads), _interrupt(false), poolEmpty(false) { if( 0 == numThreads ) { int num = 1; int max = 1; if( numberOfProcessors( num, max ) && (num > 1) ) { if( !certPool.size_controller().resize(num) ) { throw exception( "Unable to resize CERT thread pool" ); } numThreads = num; mout() << "Creating " << numThreads << " CERT processors (threads)." << std::endl; } else { numThreads = 4; if( !certPool.size_controller().resize(numThreads) ) { throw exception( "Unable to resize CERT thread pool" ); } mout() << "Defaulting to " << numThreads << " CERT processors (threads)." << std::endl; //#ifdef DEBUG // std::cerr << "Unable to determine number of processors for Cert threadpool. Defaulting to " << numThreads << "." << std::endl; //#endif } } for( int i = 0; i < numThreads; ++i ) { MP_CERT *cert = new MP_CERT(false); processors.push_back(cert); available.push(cert); } } Scheduler::~Scheduler() { certPool.clear(); for( unsigned int i = 0; i < processors.size(); ++i ) { delete processors[i]; } } void Scheduler::start() { // start a thread to run the output method. boost::thread outputThread( outputWrap(this) ); loader.start(); poolEmpty = false; while( !loader.finished() ) { // Get next available cert processor // fill it's task item from the loader's next task // schedule it to run on the pool MP_CERT *cert = NULL; available.wait_and_pop( cert ); Task &task( cert->getTask() ); loader.getNextTask( task ); // if( loader.getOneTask( task ) ) { task.setNextFrameNum(); // run( *cert ); certWrap cw( cert, this ); // add it to the thread pool, which will call Scheduler::run certPool.schedule( cw ); } } certPool.wait(); poolEmpty = true; outputThread.join(); } void Scheduler::run( MP_CERT *cert ) { try { cert->processTask(); } catch( std::runtime_error &error ) { mout() << '\t' << error.what() << std::endl; return; } taskBuffer.push( cert->getTask() ); // make this cert available available.push( cert ); } bool Scheduler::finished() { return ( loader.finished() && poolEmpty && (0 == taskBuffer.size()) ); } void Scheduler::interrupt() { loader.interrupt(); _interrupt = true; } void Scheduler::output() { // Check the top element in taskBuffer. How? The concurrent_queue class doesn't support this // 1) maybe create a new method that takes a predicate. If the top item of the queue matches the pred, // then pop it, otherwise return false // 2) Or pop the next one off the queue. If it's order is greater than nextFrame, // then check the size of the taskBuffer, if it is too large, then post-process anyway and rest nextFrame // otherwise push it back while( !finished() ) { if( _interrupt ) return; Task task; taskBuffer.wait_and_pop( task ); // This will block (sleep) until there is something on the taskBuffer if( task.order == nextFrame ) { postProcessTask( task ); ++nextFrame; } else if( task.order < nextFrame ) { // Uh Oh, we missed one somehow. Silently drop it unless we are in debug mode. #ifdef DEBUG std::cerr << "Missed frame number " << task.order << std::endl; #endif } else { // task's frame number must be greater than nextFrame so just shove it back onto the queue taskBuffer.push( task ); // At this point we should try waiting until a new task is pushed onto the taskBuffer, but I'm not sure how to do that. } if( _interrupt ) return; } // If its order is less than or equal to nextFram, run post-proc and output // If ifs order is equal to nextFrame, increment nextFrame // If not, sleep until another task is added } void Scheduler::postProcessTask( Task &task ) { PluginController &pc = *PluginController::getPluginController(); // pc.display( std::cout ); std::vector< PluginBase *> postPlugins = pc.pluginsForStep( PluginSteps::PostProcess ); for( std::vector::iterator pluginIter = postPlugins.begin(), lastPlugin = postPlugins.end(); pluginIter != lastPlugin; ++pluginIter ) { AfterFacePlugin *plugin = dynamic_cast(*pluginIter); if( plugin ) plugin->processTask( task ); } postPlugins.clear(); postPlugins = pc.pluginsForStep( PluginSteps::Output ); for( std::vector::iterator pluginIter = postPlugins.begin(), lastPlugin = postPlugins.end(); pluginIter != lastPlugin; ++pluginIter ) { AfterFacePlugin *plugin = dynamic_cast(*pluginIter); if( plugin ) plugin->processTask( task ); } } std::ostream & Scheduler::mout() { return Arguments::outputStream(); } } // end namespace CERT