1 /** 2 * Copyright 2023 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_LITE_SRC_EXTENDRT_KERNEL_ASCEND_ASCEND_ALLOCATOR_PLUGIN_H_ 18 #define MINDSPORE_LITE_SRC_EXTENDRT_KERNEL_ASCEND_ASCEND_ALLOCATOR_PLUGIN_H_ 19 #include <string> 20 #include <memory> 21 #include "include/api/status.h" 22 namespace mindspore::kernel { 23 class AscendAllocatorPluginImpl { 24 public: 25 AscendAllocatorPluginImpl() = default; 26 virtual ~AscendAllocatorPluginImpl() = default; 27 28 virtual int GetCurrentDeviceId() = 0; 29 virtual void *Malloc(size_t size, int device_id = -1) = 0; 30 virtual void Free(void *device_data, int device_id) = 0; 31 virtual void *MallocHost(size_t size) = 0; 32 virtual void FreeHost(void *host_data) = 0; 33 virtual Status CopyDeviceDataToHost(void *device_data, void *host_data, size_t data_size, int device_id) = 0; 34 virtual Status CopyHostDataToDevice(void *host_data, void *device_data, size_t data_size) = 0; 35 virtual Status CopyDeviceDataToDevice(void *src_device, void *dst_device, size_t src_data_size, size_t dst_data_size, 36 int src_device_id, int dst_device_id) = 0; 37 }; 38 39 class MS_API AscendAllocatorPlugin { 40 public: 41 static AscendAllocatorPlugin &GetInstance(); 42 bool Register(); 43 44 int GetCurrentDeviceId(); 45 void *Malloc(size_t size, int device_id = -1); 46 void Free(void *device_data, int device_id); 47 void *MallocHost(size_t size); 48 void FreeHost(void *host_data); 49 void Clear(); 50 Status CopyDeviceDataToHost(void *device_data, void *host_data, size_t data_size, int device_id); 51 Status CopyHostDataToDevice(void *host_data, void *device_data, size_t data_size); 52 Status CopyDeviceDataToDevice(void *src_device, void *dst_device, size_t src_data_size, size_t dst_data_size, 53 int src_device_id, int dst_device_id); 54 55 private: 56 AscendAllocatorPlugin(); 57 ~AscendAllocatorPlugin(); 58 59 std::string plugin_path_; 60 void *handle_ = nullptr; 61 bool is_registered_ = false; 62 std::shared_ptr<AscendAllocatorPluginImpl> ascend_allocator_plugin_impl_ = nullptr; 63 }; 64 } // namespace mindspore::kernel 65 #endif 66