• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. 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_CORE_DISTRIBUTED_RUNTIME_RPC_GRPC_WORKER_CACHE_H_
17 #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RPC_GRPC_WORKER_CACHE_H_
18 
19 #include "tensorflow/core/distributed_runtime/rpc/grpc_channel.h"
20 #include "tensorflow/core/distributed_runtime/rpc/grpc_client_cq_tag.h"
21 #include "tensorflow/core/distributed_runtime/worker_cache.h"
22 #include "tensorflow/core/platform/env.h"
23 #include "tensorflow/core/platform/threadpool.h"
24 
25 namespace tensorflow {
26 
27 class GrpcWorkerEnv {
28  public:
29   GrpcWorkerEnv(size_t num_completion_queues, size_t num_threads);
30 
31   ~GrpcWorkerEnv();
32 
GetThreadPool()33   thread::ThreadPool* GetThreadPool() const { return threadpool_.get(); }
34 
CompletionQueueSize()35   size_t CompletionQueueSize() const { return threads_.size(); }
36 
GetCompletionQueue(size_t index)37   ::grpc::CompletionQueue* GetCompletionQueue(size_t index) const {
38     return threads_.at(index).completion_queue();
39   }
40 
41  private:
42   // Thread wrapping class that drives work over a single gRPC
43   // CompletionQueue.
44   class GrpcWorkerCacheThread {
45    public:
46     GrpcWorkerCacheThread();
47 
48     ~GrpcWorkerCacheThread();
49 
completion_queue()50     ::grpc::CompletionQueue* completion_queue() const {
51       return &completion_queue_;
52     }
53 
54    private:
55     mutable ::grpc::CompletionQueue completion_queue_;
56     std::unique_ptr<Thread> thread_;
57   };
58 
59   std::unique_ptr<thread::ThreadPool> threadpool_;
60   std::vector<GrpcWorkerCacheThread> threads_;
61 };
62 
63 // Create a GrpcWorkerEnv instance that can be used as argument to create
64 // gRPC worker cache. Caller should take the ownership of the returned instance.
65 GrpcWorkerEnv* CreateGrpcWorkerEnv();
66 
67 // The returned WorkerCacheInterface object takes the ownership of "cc".
68 WorkerCacheInterface* NewGrpcWorkerCache(std::shared_ptr<GrpcChannelCache> cc,
69                                          GrpcWorkerEnv* worker_env);
70 
71 WorkerCacheInterface* NewGrpcWorkerCacheWithLocalWorker(
72     std::shared_ptr<GrpcChannelCache> cc, GrpcWorkerEnv* worker_env,
73     WorkerInterface* local_worker, const string& local_target);
74 
75 }  // namespace tensorflow
76 #endif  // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RPC_GRPC_WORKER_CACHE_H_
77