CMS 3D CMS Logo

NoThreadPool.h
Go to the documentation of this file.
1 /*
2  * Custom TensorFlow thread pool implementation that does no threading at all,
3  * but schedules all tasks in the caller thread.
4  * Based on TensorFlow 2.1.
5  * For more info, see https://gitlab.cern.ch/mrieger/CMSSW-DNN.
6  *
7  * Author: Marcel Rieger
8  */
9 
10 #ifndef PHYSICSTOOLS_TENSORFLOW_NOTHREADPOOL_H
11 #define PHYSICSTOOLS_TENSORFLOW_NOTHREADPOOL_H
12 
14 
15 #include "tensorflow/core/lib/core/threadpool.h"
16 #include "tensorflow/core/lib/core/threadpool_options.h"
17 
18 namespace tensorflow {
19 
20  class NoThreadPool : public tensorflow::thread::ThreadPoolInterface {
21  public:
22  static NoThreadPool& instance() {
24  return pool;
25  }
26 
27  explicit NoThreadPool() : numScheduleCalled_(0) {}
28 
29  void Schedule(std::function<void()> fn) override {
30  numScheduleCalled_ += 1;
31  fn();
32  }
33 
34  void ScheduleWithHint(std::function<void()> fn, int start, int end) override { Schedule(fn); }
35 
36  void Cancel() override {}
37 
38  int NumThreads() const override { return 1; }
39 
40  int CurrentThreadId() const override { return -1; }
41 
43 
44  private:
45  std::atomic<int> numScheduleCalled_;
46  };
47 
48 } // namespace tensorflow
49 
50 #endif // PHYSICSTOOLS_TENSORFLOW_NOTHREADPOOL_H
Definition: start.py:1
int NumThreads() const override
Definition: NoThreadPool.h:38
void Schedule(std::function< void()> fn) override
Definition: NoThreadPool.h:29
void ScheduleWithHint(std::function< void()> fn, int start, int end) override
Definition: NoThreadPool.h:34
void Cancel() override
Definition: NoThreadPool.h:36
#define CMS_THREAD_SAFE
int CurrentThreadId() const override
Definition: NoThreadPool.h:40
static NoThreadPool & instance()
Definition: NoThreadPool.h:22
std::atomic< int > numScheduleCalled_
Definition: NoThreadPool.h:45