1 /* 2 * Copyright (c) 2020-2021.Huawei Technologies Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MDACLMANAGER_H 17 #define MDACLMANAGER_H 18 19 #include <climits> 20 #include <cstdio> 21 #include <map> 22 #include <iostream> 23 #include <memory> 24 #include <unistd.h> 25 #include <string> 26 #include <sys/stat.h> 27 #include <sys/types.h> 28 #include "acl/acl.h" 29 30 #include "minddata/dataset/core/tensor_shape.h" 31 #include "minddata/dataset/core/data_type.h" 32 #include "minddata/dataset/kernels/image/dvpp/utils/CommonDataType.h" 33 #include "minddata/dataset/kernels/image/dvpp/utils/DvppCommon.h" 34 #include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h" 35 #include "mindspore/ccsrc/minddata/dataset/core/device_tensor.h" 36 #include "mindspore/ccsrc/minddata/dataset/core/tensor.h" 37 #include "mindspore/core/utils/log_adapter.h" 38 #include "mindspore/ccsrc/minddata/dataset/util/status.h" 39 40 mode_t SetFileDefaultUmask(); 41 42 class MDAclProcess { 43 public: 44 MDAclProcess(uint32_t resizeWidth, uint32_t resizeHeight, uint32_t cropWidth, uint32_t cropHeight, 45 aclrtContext context, bool is_crop = true, aclrtStream stream = nullptr, 46 const std::shared_ptr<DvppCommon> &dvppCommon = nullptr); 47 48 MDAclProcess(uint32_t ParaWidth, uint32_t ParaHeight, aclrtContext context, bool is_crop = false, 49 aclrtStream stream = nullptr, const std::shared_ptr<DvppCommon> &dvppCommon = nullptr); 50 51 MDAclProcess(aclrtContext context, bool is_crop = false, aclrtStream stream = nullptr, 52 const std::shared_ptr<DvppCommon> &dvppCommon = nullptr); 53 ~MDAclProcess()54 ~MDAclProcess(){}; 55 56 // Release all the resource 57 APP_ERROR Release(); 58 // Create resource for this sample 59 APP_ERROR InitResource(); 60 // Get Ascend Resource core: context and stream which are created when InitResource() 61 aclrtContext GetContext(); 62 aclrtContext GetStream(); 63 64 // Process the result 65 APP_ERROR JPEG_DRC(const RawData &ImageInfo); 66 APP_ERROR JPEG_DRC(); 67 // Procss the image without crop 68 APP_ERROR JPEG_DR(const RawData &ImageInfo); 69 APP_ERROR JPEG_DR(); 70 // Process the JPEG image only with decode 71 APP_ERROR JPEG_D(const RawData &ImageInfo); 72 APP_ERROR JPEG_D(); 73 // Process the JPEG image only with resize 74 APP_ERROR JPEG_R(const DvppDataInfo &ImageInfo); 75 APP_ERROR JPEG_R(std::string &last_step); 76 // Process the JPEG image only with crop 77 APP_ERROR JPEG_C(const DvppDataInfo &ImageInfo); 78 APP_ERROR JPEG_C(std::string &last_step); 79 // Process the PNG image only with decode 80 APP_ERROR PNG_D(const RawData &ImageInfo); 81 APP_ERROR PNG_D(); 82 // API for access memory 83 std::shared_ptr<void> Get_Memory_Data(); 84 // API for access device memory of croped data 85 std::shared_ptr<DvppDataInfo> Get_Croped_DeviceData(); 86 // API for access device memory of resized data 87 std::shared_ptr<DvppDataInfo> Get_Resized_DeviceData(); 88 // API for access device memory of decode data 89 std::shared_ptr<DvppDataInfo> Get_Decode_DeviceData(); 90 91 APP_ERROR H2D_Sink(const std::shared_ptr<mindspore::dataset::Tensor> &input, 92 std::shared_ptr<mindspore::dataset::DeviceTensor> &device_input); 93 94 APP_ERROR D2H_Pop(const std::shared_ptr<mindspore::dataset::DeviceTensor> &device_output, 95 std::shared_ptr<mindspore::dataset::Tensor> &output); 96 97 // D-chip memory release 98 APP_ERROR device_memory_release(); 99 100 std::shared_ptr<DvppCommon> GetDeviceModule(); 101 102 std::vector<uint32_t> Get_Primary_Shape(); 103 104 // Set Dvpp parameters 105 APP_ERROR SetResizeParas(uint32_t width, uint32_t height); 106 APP_ERROR SetCropParas(uint32_t width, uint32_t height); 107 108 private: 109 // Crop definition 110 void CropConfigFilter(CropRoiConfig &cfg, DvppCropInputInfo &cropinfo, DvppDataInfo &resizeinfo); 111 // Resize definition 112 APP_ERROR ResizeConfigFilter(DvppDataInfo &resizeinfo, const uint32_t pri_w_, const uint32_t pri_h_) const; 113 // Initialize DVPP modules used by this sample 114 APP_ERROR InitModule(); 115 // Dvpp process with crop 116 APP_ERROR JPEG_DRC_(const RawData &ImageInfo); 117 // Dvpp process without crop 118 APP_ERROR JPEG_DR_(const RawData &ImageInfo); 119 // Impl of JPEG_D 120 APP_ERROR JPEG_D_(const RawData &ImageInfo); 121 APP_ERROR JPEG_D_(); 122 // Impl of JPEG_R 123 APP_ERROR JPEG_R_(const DvppDataInfo &ImageInfo); 124 APP_ERROR JPEG_R_(std::string &last_step); 125 // Impl of JPEG_C 126 APP_ERROR JPEG_C_(const DvppDataInfo &ImageInfo); 127 APP_ERROR JPEG_C_(std::string &last_step); 128 // Impl of PNG_D 129 APP_ERROR PNG_D_(const RawData &ImageInfo); 130 APP_ERROR PNG_D_(); 131 132 aclrtContext context_; 133 aclrtStream stream_; 134 std::shared_ptr<DvppCommon> dvppCommon_; // dvpp object 135 std::shared_ptr<void> processedInfo_; // processed data (On host) 136 uint32_t resizeWidth_; // dvpp resize width 137 uint32_t resizeHeight_; // dvpp resize height 138 uint32_t cropWidth_; // dvpp crop width 139 uint32_t cropHeight_; // dvpp crop height 140 bool contain_crop_; // Initialize with crop or not 141 }; 142 143 #endif 144