• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 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 #ifndef MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_COPY_ACTOR_H_
18 #define MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_COPY_ACTOR_H_
19 
20 #include <vector>
21 #include <string>
22 #include <memory>
23 #include <utility>
24 #include <unordered_map>
25 #include "runtime/framework/actor/actor_common.h"
26 #include "runtime/framework/actor/memory_aware_actor.h"
27 #include "runtime/hardware/device_context.h"
28 #include "runtime/framework/device_tensor_store.h"
29 
30 namespace mindspore {
31 namespace runtime {
32 using mindspore::device::DeviceContext;
33 
34 // The copy actor is used to receive the device tensors and control info to copy data between input device tensor and
35 // output device tensor. The processing flow is RunOpData/RunOpControl -> CheckRunningCondition -> SendMemoryAllocReq
36 // -> OnMemoryAllocFinish -> Copy -> SendMemoryFreeReq -> SendOutput.
37 class CopyActor : public MemoryAwareActor {
38  public:
CopyActor(const std::string & name,const AID & memory_manager_aid)39   CopyActor(const std::string &name, const AID &memory_manager_aid)
40       : MemoryAwareActor(name, KernelTransformType::kCopyActor, nullptr, memory_manager_aid), output_(nullptr) {}
41   ~CopyActor() override = default;
42 
43   void Init() override;
44 
45   // The copy actor run when receive the input data.
46   void RunOpData(OpData<DeviceTensor> *const input_data, OpContext<DeviceTensor> *const context) override;
47   // The copy actor run when receive the input control.
48   void RunOpControl(AID *const input_control, OpContext<DeviceTensor> *const context) override;
49 
50   // The memory related operation interface.
51   void SendMemoryAllocReq(OpContext<DeviceTensor> *const context) override;
52   void SendMemoryFreeReq(OpContext<DeviceTensor> *const context) override;
53   // The copy processing after memory alloc finished.
54   void OnMemoryAllocFinish(OpContext<DeviceTensor> *const context) override;
55 
56  private:
57   friend class GraphScheduler;
58 
59   // Fetch the device tensor for copy.
60   void FetchDeviceTensor(OpContext<DeviceTensor> *const context);
61 
62   // Send output data and output controls when finish copy.
63   void SendOutput(OpContext<DeviceTensor> *const context) const;
64 
65   // The input device tensor is saved from the input data or fetched by device_tensor_store_keys_.
66   std::vector<DeviceTensor *> input_device_tensor_;
67   // The output device tensor is saved from the output or fetched by device_tensor_store_keys_.
68   std::vector<DeviceTensor *> output_device_tensor_;
69 
70   //  The output_data_ corresponds to the output_data_arrows_ one by one.
71   std::vector<OpDataUniquePtr<DeviceTensor>> output_data_;
72 
73   // The output is created in the copy actor build, so can't be the raw pointer.
74   DeviceTensorPtr output_;
75 };
76 
77 using CopyActorPtr = std::shared_ptr<CopyActor>;
78 }  // namespace runtime
79 }  // namespace mindspore
80 
81 #endif  // MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_COPY_ACTOR_H_
82