• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MINDSPORE_CCSRC_PS_PS_CACHE_PS_DATA_PS_DATA_CHANNEL_H_
17 #define MINDSPORE_CCSRC_PS_PS_CACHE_PS_DATA_PS_DATA_CHANNEL_H_
18 
19 #include <memory>
20 #include <string>
21 #include <condition_variable>
22 
23 namespace mindspore {
24 namespace ps {
25 class PsDataChannel {
26  public:
PsDataChannel(const std::string & channel_name,size_t step_num)27   PsDataChannel(const std::string &channel_name, size_t step_num)
28       : channel_name_(channel_name),
29         step_num_(step_num),
30         current_data_step_(0),
31         current_graph_step_(0),
32         channel_open_(false),
33         data_(nullptr),
34         data_size_(0) {}
35   virtual ~PsDataChannel() = default;
36   void set_data(const void *data, const size_t data_size);
data()37   const void *data() const { return data_; }
data_size()38   size_t data_size() const { return data_size_; }
ResetData()39   void ResetData() { data_ = nullptr; }
set_step_num(size_t step_num)40   void set_step_num(size_t step_num) { step_num_ = step_num; }
41   void TryWakeChannel(bool force_wake = false);
42 
43  private:
44   void TryLockChannel();
45   std::string channel_name_;
46   // The step num of each epoch.
47   size_t step_num_;
48   size_t current_data_step_;
49   size_t current_graph_step_;
50   bool channel_open_;
51   std::mutex channel_mutex_;
52   std::condition_variable channel_;
53   void *data_;
54   size_t data_size_;
55 };
56 }  // namespace ps
57 }  // namespace mindspore
58 #endif  // MINDSPORE_CCSRC_PS_PS_CACHE_PS_DATA_PS_DATA_CHANNEL_H_
59