1 /* Copyright 2015 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_STREAM_EXECUTOR_EXECUTOR_CACHE_H_
17 #define TENSORFLOW_STREAM_EXECUTOR_EXECUTOR_CACHE_H_
18 
19 #include <functional>
20 #include <map>
21 
22 #include "absl/synchronization/mutex.h"
23 #include "tensorflow/stream_executor/lib/status.h"
24 #include "tensorflow/stream_executor/lib/statusor.h"
25 #include "tensorflow/stream_executor/stream_executor_pimpl.h"
26 
27 namespace stream_executor {
28 
29 // Utility class to allow Platform objects to manage cached StreamExecutors.
30 // Thread-safe.
31 class ExecutorCache {
32  public:
ExecutorCache()33   ExecutorCache() {}
34 
35   // Looks up 'config' in the cache. Returns a pointer to the existing executor,
36   // if already present, or creates it using 'factory', if it does not.
37   // Factories may be executed concurrently for different device ordinals.
38   typedef port::StatusOr<std::unique_ptr<StreamExecutor>> ExecutorFactory();
39   port::StatusOr<StreamExecutor*> GetOrCreate(
40       const StreamExecutorConfig& config,
41       const std::function<ExecutorFactory>& factory);
42 
43   // Returns a pointer to the described executor (if one with a matching config
44   // has been created), or a NOT_FOUND status.
45   port::StatusOr<StreamExecutor*> Get(const StreamExecutorConfig& config);
46 
47   // Destroys all Executors and clears the cache.
48   // Performs no synchronization with the executors - undefined behavior may
49   // occur if any executors are active!
50   void DestroyAllExecutors();
51 
52  private:
53   // Each Entry contains zero or more cached executors for a device ordinal.
54   struct Entry {
55     ~Entry();
56 
57     // Mutex that guards the contents of each entry. The 'mutex_' of the
58     // ExecutorCache class protects both the 'cache_' and the existence of each
59     // Entry, but not the Entry's contents. 'configurations_mutex' protects the
60     // contents of the entry after 'mutex_' has been dropped.
61     absl::Mutex configurations_mutex;
62 
63     // Vector of cached {config, executor} pairs.
64     std::vector<
65         std::pair<StreamExecutorConfig, std::unique_ptr<StreamExecutor>>>
66         configurations TF_GUARDED_BY(configurations_mutex);
67   };
68 
69   // Maps ordinal number to a list of cached executors for that ordinal.
70   // We key off of ordinal (instead of just looking up all fields in the
71   // StreamExecutorConfig) for a slight improvement in lookup time.
72   absl::Mutex mutex_;
73   std::map<int, Entry> cache_ TF_GUARDED_BY(mutex_);
74 
75   SE_DISALLOW_COPY_AND_ASSIGN(ExecutorCache);
76 };
77 
78 }  // namespace stream_executor
79 
80 #endif  // TENSORFLOW_STREAM_EXECUTOR_EXECUTOR_CACHE_H_
81