1 /* Copyright 2020 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_PJRT_NCCL_ID_STORE_H_ 17 #define TENSORFLOW_COMPILER_XLA_PJRT_NCCL_ID_STORE_H_ 18 19 #include <memory> 20 #include <utility> 21 #include <string> 22 23 #include "absl/base/attributes.h" 24 #include "absl/container/flat_hash_map.h" 25 #include "absl/synchronization/mutex.h" 26 #include "tensorflow/compiler/xla/pjrt/distributed/client.h" 27 #include "tensorflow/compiler/xla/service/global_device_id.h" 28 #include "tensorflow/compiler/xla/service/gpu/gpu_executable_run_options.h" 29 #include "tensorflow/compiler/xla/statusor.h" 30 31 namespace xla { 32 33 // A table mapping NcclCliqueKeys to ncclUniqueId values encoded as strings. 34 // In a distributed setup the table of NCCL IDs is kept on the master node 35 // (node 0). The node of the first participating device will create the unique 36 // id. 37 class NcclIdStore { 38 public: NcclIdStore(int node_id,std::shared_ptr<DistributedRuntimeClient> client,absl::flat_hash_map<GlobalDeviceId,int> device_to_node)39 NcclIdStore(int node_id, std::shared_ptr<DistributedRuntimeClient> client, 40 absl::flat_hash_map<GlobalDeviceId, int> device_to_node) 41 : node_id_(node_id), 42 client_(std::move(client)), 43 device_to_node_(std::move(device_to_node)) {} 44 45 StatusOr<std::string> GetNcclUniqueId(const gpu::NcclCliqueKey& key); 46 47 private: 48 const int node_id_; 49 const std::shared_ptr<DistributedRuntimeClient> client_; 50 const absl::flat_hash_map<GlobalDeviceId, int> device_to_node_; 51 52 absl::Mutex mu_; 53 absl::flat_hash_map<gpu::NcclCliqueKey, std::string> cache_ 54 ABSL_GUARDED_BY(mu_); 55 }; 56 57 } // namespace xla 58 59 #endif // TENSORFLOW_COMPILER_XLA_PJRT_NCCL_ID_STORE_H_ 60