• 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 
17 #ifndef MINDSPORE_CORE_IR_DEVICE_SYNC_H_
18 #define MINDSPORE_CORE_IR_DEVICE_SYNC_H_
19 
20 #include <vector>
21 #include <memory>
22 #include <string>
23 
24 #include "ir/dtype/type.h"
25 #include "utils/shape_utils.h"
26 
27 using std::string;
28 
29 namespace mindspore {
30 // Interface for data synchornize between device and host.
31 class DeviceSync {
32  public:
33   // Used to sync data between different device addresses, only need the data size and data ptr. The CPU device doesn't
34   // need use the interfaces, so need the default implementation.
SyncDeviceToHost(size_t size,void * host_ptr)35   virtual bool SyncDeviceToHost(size_t size, void *host_ptr) const { return true; }
SyncHostToDevice(size_t size,const void * host_ptr)36   virtual bool SyncHostToDevice(size_t size, const void *host_ptr) const { return true; }
37 
38   // Used to sync data between host tensor and device address, additional need the data shape and data type.
39   virtual bool SyncDeviceToHost(const ShapeVector &shape, size_t size, TypeId type, void *host_ptr) const = 0;
40   virtual bool SyncHostToDevice(const ShapeVector &shape, size_t size, TypeId type, const void *host_ptr,
41                                 const std::string &format = "DefaultFormat") const = 0;
SyncDeviceToDevice(const ShapeVector & shape,size_t size,TypeId type,const void * src_ptr,const std::string & format)42   virtual bool SyncDeviceToDevice(const ShapeVector &shape, size_t size, TypeId type, const void *src_ptr,
43                                   const std::string &format) const {
44     return true;
45   }
46   virtual void *GetMutablePtr() const = 0;
47   virtual void ClearDeviceMemory() = 0;
48 
49   // The related interface of reference count operation.
set_original_ref_count(size_t original_ref_count)50   void set_original_ref_count(size_t original_ref_count) { original_ref_count_ = original_ref_count; }
original_ref_count()51   size_t original_ref_count() const { return original_ref_count_; }
set_ref_count(size_t ref_count)52   void set_ref_count(size_t ref_count) { ref_count_ = ref_count; }
ref_count()53   size_t ref_count() const { return ref_count_; }
IncreaseOriginalRefCount()54   void IncreaseOriginalRefCount() {
55     if (original_ref_count_ < SIZE_MAX) {
56       original_ref_count_++;
57     }
58   }
DecreaseRefCount()59   void DecreaseRefCount() { ref_count_--; }
ResetRefCount()60   void ResetRefCount() { ref_count_ = original_ref_count_; }
61 
62  protected:
63   mutable size_t original_ref_count_{1};
64   // It will be decreased in the running, and reset by original_ref_count_ when it is zero.
65   mutable size_t ref_count_{1};
66 };
67 using DeviceSyncPtr = std::shared_ptr<DeviceSync>;
68 }  // namespace mindspore
69 #endif  // MINDSPORE_CORE_IR_DEVICE_SYNC_H_
70