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_CORE_TPU_KERNELS_TPU_ORDINAL_SELECTOR_H_ 17 #define TENSORFLOW_CORE_TPU_KERNELS_TPU_ORDINAL_SELECTOR_H_ 18 19 #include "tensorflow/core/tpu/kernels/tpu_ordinal_selector_interface.h" 20 #include "tensorflow/core/tpu/tpu_api.h" 21 #include "tensorflow/core/tpu/tpu_ops_c_api.h" 22 23 namespace tensorflow { 24 namespace tpu { 25 26 // A reserved ID for deferred core selection. Intentionally set at a number 27 // that is more than the number of cores available in a future system. 28 constexpr int32_t kDeferredCoreSelectionReserved = -8193; 29 30 class TPUOrdinalSelector : TPUOrdinalSelectorInterface { 31 public: 32 explicit TPUOrdinalSelector(int num_cores_per_replica = 1) { 33 OpsApiFn()->TfTpuOrdinalSelector_CreateFn(&ordinal_selector_, 34 num_cores_per_replica); 35 } ~TPUOrdinalSelector()36 ~TPUOrdinalSelector() override { 37 OpsApiFn()->TfTpuOrdinalSelector_DestroyFn(ordinal_selector_); 38 } GetOrdinal(absl::optional<uint64> key,int64_t * req_id)39 int64_t GetOrdinal(absl::optional<uint64> key, int64_t* req_id) override { 40 int64_t ordinal; 41 OpsApiFn()->TfTpuOrdinalSelector_GetOrdinalFn(ordinal_selector_, key, 42 req_id, &ordinal); 43 return ordinal; 44 } DequeueFromCoreSelector(int32_t device_ordinal,int64_t req_id)45 void DequeueFromCoreSelector(int32_t device_ordinal, 46 int64_t req_id) override { 47 OpsApiFn()->TfTpuOrdinalSelector_DequeueFromCoreSelectorFn( 48 ordinal_selector_, device_ordinal, req_id); 49 } 50 51 private: 52 TfTpuOrdinalSelector* ordinal_selector_; 53 }; 54 55 } // namespace tpu 56 } // namespace tensorflow 57 58 #endif // TENSORFLOW_CORE_TPU_KERNELS_TPU_ORDINAL_SELECTOR_H_ 59