TensorFlow Serving C++ API Documentation
thread_pool_factory.h
1 /* Copyright 2020 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_SERVING_SERVABLES_TENSORFLOW_THREAD_POOL_FACTORY_H_
17 #define TENSORFLOW_SERVING_SERVABLES_TENSORFLOW_THREAD_POOL_FACTORY_H_
18 
19 #include "tensorflow/core/platform/threadpool.h"
20 #include "tensorflow/core/platform/threadpool_options.h"
21 #include "tensorflow_serving/util/class_registration.h"
22 
23 namespace tensorflow {
24 namespace serving {
25 
26 // This class takes inter- and intra-op thread pools and returns
27 // tensorflow::thread::ThreadPoolOptions. The thread pools passed to an
28 // instance of this class will be kept alive for the lifetime of this instance.
30  public:
31  // The default constructor will set inter- and intra-op thread pools in the
32  // ThreadPoolOptions to nullptr, which will be ingored by Tensorflow runtime.
33  ScopedThreadPools() = default;
35  std::shared_ptr<thread::ThreadPoolInterface> inter_op_thread_pool,
36  std::shared_ptr<thread::ThreadPoolInterface> intra_op_thread_pool);
37  ~ScopedThreadPools() = default;
38 
39  tensorflow::thread::ThreadPoolOptions get();
40 
41  private:
42  std::shared_ptr<thread::ThreadPoolInterface> inter_op_thread_pool_;
43  std::shared_ptr<thread::ThreadPoolInterface> intra_op_thread_pool_;
44 };
45 
46 // Factory for returning intra- and inter-op thread pools to be used by
47 // Tensorflow.
49  public:
50  virtual ~ThreadPoolFactory() = default;
51 
52  virtual ScopedThreadPools GetThreadPools() = 0;
53 };
54 
55 DEFINE_CLASS_REGISTRY(ThreadPoolFactoryRegistry, ThreadPoolFactory);
56 #define REGISTER_THREAD_POOL_FACTORY(ClassCreator, ConfigProto) \
57  REGISTER_CLASS(ThreadPoolFactoryRegistry, ThreadPoolFactory, ClassCreator, \
58  ConfigProto);
59 
60 } // namespace serving
61 } // namespace tensorflow
62 
63 #endif // TENSORFLOW_SERVING_SERVABLES_TENSORFLOW_THREAD_POOL_FACTORY_H_