Cambridge SMT System
multithreading.helpers.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
15 #ifndef MULTITHREADING_HELPERS_HPP
16 #define MULTITHREADING_HELPERS_HPP
17 
18 #include <multithreading.hpp>
19 
20 namespace HifstConstants {
21 const std::string kNThreads = "nthreads";
22 const std::string kServerEnable = "server.enable";
23 }
24 
25 namespace ucam {
26 namespace util {
27 
37 template<class SingleThreadedFunctorT, class MultiThreadedFunctorT>
38 class Runner {
39  private:
40  //Use multithreading or not
41  bool multithread_;
42  //How many threads is the user requesting
43  unsigned threadcount_;
44  //Command-line/config file options
45  const RegistryPO& rg_;
46 
47  SingleThreadedFunctorT sf_;
48  MultiThreadedFunctorT mtf_;
49 
50  public:
55  Runner ( const RegistryPO& rg ) :
56  rg_ ( rg ),
57  multithread_ ( rg.exists ( HifstConstants::kNThreads ) ),
58  threadcount_ ( multithread_ ? rg.get<unsigned> ( HifstConstants::kNThreads ) :
59  0 ) {
60  if ( multithread_ ) {
61  LINFO ( "Multithreading:" << threadcount_ << " threads" );
62  } else {
63  LINFO ( "Multithreading: no" );
64  }
65  };
66 
68  inline void operator() () {
69  if ( multithread_ ) mtf_ ( rg_, threadcount_ );
70  else sf_ ( rg_ );
71  };
72 
73 };
74 
83 template<class SingleThreadedFunctorT, class MultiThreadedFunctorT >
84 class Runner2 {
85  private:
86  //Use multithreading or not
87  bool multithread_;
88  //Command-line/config file options
89  const RegistryPO& rg_;
90 
91  public:
96  Runner2 ( const RegistryPO& rg ) :
97  rg_ ( rg ),
98  multithread_ ( rg.exists ( HifstConstants::kNThreads ) )
99 
100  {
101  if ( multithread_ ) {
102  LINFO ( "Multithreading:yes" );
103  } else {
104  LINFO ( "Multithreading:no" );
105  }
106  };
107 
109  inline void operator() () {
110  if ( multithread_ ) {
111  MultiThreadedFunctorT mtf ( rg_ );
112  mtf();
113  return;
114  }
115  SingleThreadedFunctorT sf ( rg_ );
116  sf();
117  };
118 
119 };
120 
129 template<class SingleThreadedFunctorT, class MultiThreadedFunctorT, class ServerFunctorT >
130 class Runner3 {
131  private:
132  //Use multithreading or not
133  bool multithread_;
134  //Command-line/config file options
135  const RegistryPO& rg_;
136 
137  bool server_;
138 
139  public:
144  Runner3 ( const RegistryPO& rg ) :
145  rg_ ( rg ),
146  multithread_ ( rg.exists ( HifstConstants::kNThreads ) ),
147  server_ ( rg.getBool ( HifstConstants::kServerEnable ) )
148 
149  {
150  if ( server_ ) {
151  LINFO ( "Initializing server..." );
152  } else if ( multithread_ ) {
153  LINFO ( "Multithreading:yes" );
154  } else {
155  LINFO ( "Multithreading:no" );
156  }
157  };
158 
160  inline void operator() () {
161  if ( server_ ) {
162  ServerFunctorT serv ( rg_ );
163  serv();
164  return;
165  }
166  if ( multithread_ ) {
167  MultiThreadedFunctorT mtf ( rg_ );
168  mtf();
169  return;
170  }
171  SingleThreadedFunctorT sf ( rg_ );
172  sf();
173  };
174 
175 };
176 
177 }
178 } // end namespaces
179 #endif
const std::string kServerEnable
Convenience wrapper class that can kick off three type of executions: singlethreaded, multithreaded, or server, triggered by program options. Possibly multithreading with 1 thread would do, but I keep both implementations as any plain bug that might arise will be easier to trace down with a single thread execution. The class is templated three functors, one for each type of execution Note that the details are up to the each of these functors.
#define LINFO(msg)
Runner3(const RegistryPO &rg)
Constructor.
Convenience wrapper class that can kick off two type of executions: singlethreaded or multithreaded...
bool exists(const std::string &source, const std::string &needle)
Convenience function to find out whether a needle exists in a text.
const std::string kNThreads
Runner(const RegistryPO &rg)
Constructor.
Implements trivial threadpool using boost::asio library.
Runner2(const RegistryPO &rg)
Constructor.
Convenience wrapper class that can kick off two type of executions: single or multithreaded, triggered by program options. Possibly multithreading with 1 thread would do, but I keep both implementations as any plain bug that might arise will be easier to trace down with a serialized execution (threadpool uses two, actually). The class is templated with two classes, one for single threading and another for multithreading. Note that the multithreading details are up to the second templated class.
Definition: bleu.hpp:14