1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "ps/ps_cache/ps_data/ps_data_channel.h" 18 #include "utils/log_adapter.h" 19 20 namespace mindspore { 21 namespace ps { TryLockChannel()22void PsDataChannel::TryLockChannel() { 23 // The prefetch order of data needs to be consistent with the graph execution order. 24 // Example: if graph execution order is graph1 --> graph2 --> graph1 -->graph2, 25 // then the data prefetch order needs be channel1 --> channel 2 --> channel1 --> channel2. 26 if ((current_data_step_ != 0) && (current_data_step_ % step_num_ == 0)) { 27 MS_LOG(INFO) << "Lock channel:" << channel_name_; 28 std::unique_lock<std::mutex> locker(channel_mutex_); 29 channel_.wait(locker, [this] { return channel_open_; }); 30 channel_open_ = false; 31 } 32 current_data_step_++; 33 } 34 TryWakeChannel(bool force_wake)35void PsDataChannel::TryWakeChannel(bool force_wake) { 36 if (force_wake || ((current_graph_step_ != 0) && (current_graph_step_ % step_num_ == 0))) { 37 MS_LOG(INFO) << "Wake up channel:" << channel_name_; 38 std::lock_guard<std::mutex> locker(channel_mutex_); 39 channel_open_ = true; 40 channel_.notify_one(); 41 } 42 current_graph_step_++; 43 } 44 set_data(const void * data,const size_t data_size)45void PsDataChannel::set_data(const void *data, const size_t data_size) { 46 MS_EXCEPTION_IF_NULL(data); 47 TryLockChannel(); 48 data_ = const_cast<void *>(data); 49 data_size_ = data_size; 50 } 51 } // namespace ps 52 } // namespace mindspore 53