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