00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef WTHREADEDJOBS_H
00026 #define WTHREADEDJOBS_H
00027
00028 #include <iostream>
00029 #include <string>
00030
00031 #include <boost/shared_ptr.hpp>
00032
00033 #include "WException.h"
00034 #include "WFlag.h"
00035 #include "WLogger.h"
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 template< class Input_T, class Job_T >
00052 class WThreadedJobs
00053 {
00054 public:
00055
00056 typedef Input_T InputType;
00057
00058
00059 typedef Job_T JobType;
00060
00061
00062
00063
00064
00065
00066 WThreadedJobs( boost::shared_ptr< InputType const > input );
00067
00068
00069
00070
00071 virtual ~WThreadedJobs();
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 void operator() ( std::size_t id, std::size_t numThreads, WBoolFlag const& shutdown );
00082
00083
00084
00085
00086
00087
00088
00089 virtual bool getJob( JobType& job ) = 0;
00090
00091
00092
00093
00094
00095
00096
00097 virtual void compute( boost::shared_ptr< InputType const > input, JobType const& job ) = 0;
00098
00099 protected:
00100
00101
00102 boost::shared_ptr< InputType const > m_input;
00103 private:
00104 };
00105
00106 template< class Input_T, class Job_T >
00107 WThreadedJobs< Input_T, Job_T >::WThreadedJobs( boost::shared_ptr< InputType const > input )
00108 : m_input( input )
00109 {
00110 if( !m_input )
00111 {
00112 throw WException( std::string( "Invalid input." ) );
00113 }
00114 }
00115
00116 template< class Input_T, class Job_T >
00117 WThreadedJobs< Input_T, Job_T >::~WThreadedJobs()
00118 {
00119 }
00120
00121 template< class Input_T, class Job_T >
00122 void WThreadedJobs< Input_T, Job_T >::operator() ( std::size_t , std::size_t , WBoolFlag const& shutdown )
00123 {
00124 JobType job;
00125 while( getJob( job ) && !shutdown() )
00126 {
00127 compute( m_input, job );
00128 }
00129 }
00130
00131
00132
00133
00134
00135 template< class Input_T, class Job_T >
00136 class WThreadedStripingJobs
00137 {
00138 public:
00139
00140 typedef Input_T InputType;
00141
00142
00143 typedef Job_T JobType;
00144
00145
00146
00147
00148
00149
00150 WThreadedStripingJobs( boost::shared_ptr< InputType const > input );
00151
00152
00153
00154
00155 virtual ~WThreadedStripingJobs();
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 void operator() ( std::size_t id, std::size_t numThreads, WBoolFlag const& shutdown );
00166
00167
00168
00169
00170
00171
00172
00173 virtual void compute( boost::shared_ptr< InputType const > input, std::size_t voxelNum ) = 0;
00174
00175 protected:
00176
00177
00178 boost::shared_ptr< InputType const > m_input;
00179 private:
00180 };
00181
00182 template< class Input_T, class Job_T >
00183 WThreadedStripingJobs< Input_T, Job_T >::WThreadedStripingJobs( boost::shared_ptr< InputType const > input )
00184 : m_input( input )
00185 {
00186 if( !m_input )
00187 {
00188 throw WException( std::string( "Invalid input." ) );
00189 }
00190 }
00191
00192 template< class Input_T, class Job_T >
00193 WThreadedStripingJobs< Input_T, Job_T >::~WThreadedStripingJobs()
00194 {
00195 }
00196
00197 template< class Input_T, class Job_T >
00198 void WThreadedStripingJobs< Input_T, Job_T >::operator() ( std::size_t id, std::size_t numThreads, WBoolFlag const& shutdown )
00199 {
00200 WAssert( m_input, "Bug: operations of an invalid input requested." );
00201 size_t numElements = m_input->size();
00202
00203
00204 size_t start = numElements / numThreads * id;
00205 size_t end = ( id + 1 ) * ( numElements / numThreads );
00206 if( id == numThreads - 1 )
00207 {
00208 end = numElements;
00209 }
00210
00211 for( size_t voxelNum = start; ( voxelNum < end ) && !shutdown(); ++voxelNum )
00212 {
00213 compute( m_input, voxelNum );
00214 }
00215 }
00216
00217 #endif // WTHREADEDJOBS_H