cmtkThreadPool.txx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //  Copyright 2004-2009 SRI International
00005 //
00006 //  This file is part of the Computational Morphometry Toolkit.
00007 //
00008 //  http://www.nitrc.org/projects/cmtk/
00009 //
00010 //  The Computational Morphometry Toolkit is free software: you can
00011 //  redistribute it and/or modify it under the terms of the GNU General Public
00012 //  License as published by the Free Software Foundation, either version 3 of
00013 //  the License, or (at your option) any later version.
00014 //
00015 //  The Computational Morphometry Toolkit is distributed in the hope that it
00016 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00017 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //  GNU General Public License for more details.
00019 //
00020 //  You should have received a copy of the GNU General Public License along
00021 //  with the Computational Morphometry Toolkit.  If not, see
00022 //  <http://www.gnu.org/licenses/>.
00023 //
00024 //  $Revision: 2398 $
00025 //
00026 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00027 //
00028 //  $LastChangedBy: torstenrohlfing $
00029 //
00030 */
00031 
00032 #ifdef _OPENMP
00033 #  include <omp.h>
00034 #endif
00035 
00036 #include <System/cmtkThreads.h>
00037 #include <System/cmtkConsole.h>
00038 
00039 template<class TParam> 
00040 void
00041 cmtk::ThreadPool::Run
00042 ( const TaskFunction taskFunction, std::vector<TParam>& taskParameters, const size_t numberOfTasksOverride )
00043 {
00044   if ( ! this->m_ThreadsRunning )
00045     {
00046     this->StartThreads();
00047     }
00048 
00049   const size_t numberOfTasks = numberOfTasksOverride ? numberOfTasksOverride : taskParameters.size();
00050   if ( ! numberOfTasks )
00051     {
00052     StdErr << "ERROR: trying to run zero tasks on thread pool. Did you forget to resize the parameter vector?\n";
00053     exit( 1 );
00054     }
00055 
00056 #ifdef _OPENMP
00057   // if OpenMP is also used in CMTK, reduce the number of OMP threads by the number of threads/tasks that we're about to run in parallel.
00058   const int nThreadsOMP = std::max<int>( 1, 1+Threads::GetNumberOfThreads() - std::min<int>( numberOfTasks, this->m_NumberOfThreads ) );
00059   omp_set_num_threads( nThreadsOMP );
00060 #endif
00061 
00062 #ifdef CMTK_BUILD_SMP
00063   // set task function
00064   this->m_TaskFunction = taskFunction;
00065 
00066   // initialize task index and count
00067   this->m_NumberOfTasks = numberOfTasks;
00068   this->m_TaskParameters.resize( this->m_NumberOfTasks );
00069   this->m_NextTaskIndex = 0;
00070   
00071   // set parameter pointers and post semaphores for tasks waiting to supply all running threads.
00072   for ( size_t idx = 0; idx < numberOfTasks; ++idx )
00073     {
00074     this->m_TaskParameters[idx] = &(taskParameters[idx]);
00075     }
00076   this->m_TaskWaitingSemaphore.Post( numberOfTasks );
00077   
00078   // now wait for all tasks to complete, as signaled via the "thread waiting" semaphore.
00079   for ( size_t idx = 0; idx < numberOfTasks; ++idx )
00080     {
00081     this->m_ThreadWaitingSemaphore.Wait();
00082     }  
00083 #else
00084   // without SMP, just run everything sequentially.
00085   for ( size_t idx = 0; idx < numberOfTasks; ++idx )
00086     {
00087     taskFunction( &taskParameters[idx], idx, numberOfTasks, 0, 1 );
00088     }
00089 #endif
00090 
00091 #ifdef _OPENMP
00092   // restore OpenMP thread count to maximum
00093   omp_set_num_threads( Threads::GetNumberOfThreads() );
00094 #endif
00095 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines