snark
bursty_pipeline.h
1 // This file is part of snark, a generic and flexible library
2 // for robotics research.
3 //
4 // Copyright (C) 2011 The University of Sydney
5 //
6 // snark is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // snark is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 // for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with snark. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef SNARK_IMAGING_BURSTY_PIPELINE_H_
20 #define SNARK_IMAGING_BURSTY_PIPELINE_H_
21 
22 #include <tbb/task_scheduler_init.h>
23 #include <tbb/pipeline.h>
24 #include <snark/tbb/bursty_reader.h>
25 
26 namespace snark { namespace tbb {
27 
29 template< typename T >
31 {
32 public:
33  bursty_pipeline( unsigned int numThread = 0 );
34  void run_once( bursty_reader< T >& reader, const ::tbb::filter_t< T, void >& filter );
35  void run( bursty_reader< T >& reader, const ::tbb::filter_t< T, void >& filter );
36 
37 private:
38  unsigned int m_threads;
39  ::tbb::task_scheduler_init m_init;
40 };
41 
44 template< typename T >
45 bursty_pipeline< T >::bursty_pipeline( unsigned int numThread ):
46  m_threads( numThread )
47 {
48  if( numThread == 0 )
49  {
50  m_threads = m_init.default_num_threads();
51  }
52 }
53 
55 template< typename T >
56 void bursty_pipeline< T >::run_once( bursty_reader< T >& reader, const ::tbb::filter_t< T, void >& filter )
57 {
58  ::tbb::parallel_pipeline( m_threads, reader.filter() & filter );
59 }
60 
62 template< typename T >
63 void bursty_pipeline< T >::run( bursty_reader< T >& reader, const ::tbb::filter_t< T, void >& filter )
64 {
65  const ::tbb::filter_t< void, void > f = reader.filter() & filter;
66  while( reader.wait() )
67  {
68  ::tbb::parallel_pipeline( m_threads, f );
69  }
70 }
71 
72 
73 
74 } }
75 
76 #endif // SNARK_IMAGING_BURSTY_PIPELINE_H_