1 /** 2 * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/). 3 * 4 * Copyright 2023 Huawei Technologies Co., Ltd 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #ifndef MINDSPORE_MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_TENSOR_FUTURE_H_ 20 #define MINDSPORE_MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_TENSOR_FUTURE_H_ 21 22 #include <utility> 23 #include <memory> 24 #include "ir/tensor.h" 25 #include "include/common/visible.h" 26 27 namespace mindspore { 28 namespace pynative { 29 using DeviceAddressFutureData = tensor::FutureData<DeviceSync>; 30 using DeviceAddressFutureDataPtr = std::shared_ptr<DeviceAddressFutureData>; 31 32 class COMMON_EXPORT DeviceAddressFuture : public tensor::FutureBase<DeviceSync> { 33 public: DeviceAddressFuture(std::future<DeviceAddressFutureDataPtr> future)34 explicit DeviceAddressFuture(std::future<DeviceAddressFutureDataPtr> future) 35 : tensor::FutureBase<DeviceSync>(std::move(future)) {} 36 ~DeviceAddressFuture() override; 37 std::shared_ptr<DeviceSync> Get() override; 38 39 private: 40 std::once_flag once_flag_; 41 }; 42 43 class COMMON_EXPORT DeviceAddressPromise { 44 public: DeviceAddressPromise(std::promise<DeviceAddressFutureDataPtr> promise)45 explicit DeviceAddressPromise(std::promise<DeviceAddressFutureDataPtr> promise) : promise_(std::move(promise)) {} 46 ~DeviceAddressPromise() = default; 47 48 void SetValue(const DeviceAddressFutureDataPtr &data); 49 std::future<DeviceAddressFutureDataPtr> GetFuture(); 50 51 private: 52 std::promise<DeviceAddressFutureDataPtr> promise_; 53 std::once_flag once_flag_; 54 }; 55 using DeviceAddressPromisePtr = std::shared_ptr<DeviceAddressPromise>; 56 } // namespace pynative 57 } // namespace mindspore 58 #endif // MINDSPORE_MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_TENSOR_FUTURE_H_ 59