1 /* Copyright 2021 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_COMPILER_XLA_SERVICE_GPU_XLA_EXECUTOR_STATE_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_XLA_EXECUTOR_STATE_H_ 18 19 #include <memory> 20 21 #include "tensorflow/compiler/xla/service/gpu/infeed_manager.h" 22 #include "tensorflow/compiler/xla/service/gpu/outfeed_manager.h" 23 24 // Defines XLA:GPU specific state that will be attached to the GpuExecutor. 25 26 namespace xla { 27 namespace gpu { 28 29 class GpuExecutorXLAState { 30 public: GpuExecutorXLAState(stream_executor::StreamExecutor *)31 explicit GpuExecutorXLAState(stream_executor::StreamExecutor *) {} 32 getOrCreateInfeedManager(stream_executor::StreamExecutor * se)33 InfeedManager *getOrCreateInfeedManager(stream_executor::StreamExecutor *se) { 34 absl::MutexLock l(&this->mu_); 35 if (!infeed_manager_) infeed_manager_ = std::make_unique<InfeedManager>(se); 36 return infeed_manager_.get(); 37 } 38 getOrCreateOutfeedManager(stream_executor::StreamExecutor * se)39 OutfeedManager *getOrCreateOutfeedManager( 40 stream_executor::StreamExecutor *se) { 41 absl::MutexLock l(&this->mu_); 42 if (!outfeed_manager_) 43 outfeed_manager_ = std::make_unique<OutfeedManager>(); 44 return outfeed_manager_.get(); 45 } 46 47 private: 48 template <typename T> getOrCreate(stream_executor::StreamExecutor * se,std::unique_ptr<T> & ptr)49 T *getOrCreate(stream_executor::StreamExecutor *se, std::unique_ptr<T> &ptr) { 50 if (!ptr) { 51 ptr = std::make_unique<T>(se); 52 } 53 return ptr.get(); 54 } 55 56 absl::Mutex mu_; 57 std::unique_ptr<InfeedManager> infeed_manager_ ABSL_GUARDED_BY(mu_); 58 std::unique_ptr<OutfeedManager> outfeed_manager_ ABSL_GUARDED_BY(mu_); 59 }; 60 61 } // namespace gpu 62 } // namespace xla 63 64 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_XLA_EXECUTOR_STATE_H_ 65