Line data Source code
1 : /** 2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd. 3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of 4 : * CANN Open Software License Agreement Version 2.0 (the "License"). 5 : * Please refer to the License for details. You may not use this file except in compliance with the License. 6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. 8 : * See LICENSE in the root of the software repository for the full text of the License. 9 : */ 10 : 11 : #ifndef ACL_TENSOR_DATA_TRANSFER_H 12 : #define ACL_TENSOR_DATA_TRANSFER_H 13 : #include <string.h> 14 : #include <string> 15 : #include <vector> 16 : #include<memory> 17 : 18 : #include "acl/acl_base.h" 19 : #include "acl/acl_tdt.h" 20 : 21 : enum datasetMemType { 22 : MEM_UNKNOWN = 0, 23 : MEM_HOST, 24 : MEM_DEVICE 25 : }; 26 : 27 : struct acltdtDataItem { 28 30 : acltdtDataItem(acltdtTensorType tdtType, 29 : const int64_t *dims, size_t dimNum, const std::string &dimsStr, 30 : aclDataType type, const std::string &typeStr, 31 : std::shared_ptr<void> tensorData, size_t size) 32 30 : { 33 30 : this->tdtType = tdtType; 34 110 : for (size_t i = 0; i < dimNum; ++i) { 35 80 : this->dims.push_back(dims[i]); 36 : } 37 30 : this->dimsStr = dimsStr; 38 30 : this->dataType = type; 39 30 : this->dataTypeStr = typeStr; 40 30 : this->dataLen = size; 41 30 : this->dataPtr = tensorData; 42 30 : this->priorityData_ = nullptr; 43 30 : this->sliceNum = 0; 44 30 : this->sliceId = 0; 45 30 : } 46 2 : acltdtDataItem() = default; 47 32 : ~acltdtDataItem() = default; 48 : acltdtTensorType tdtType; 49 : std::vector<int64_t> dims; 50 : std::string dimsStr; 51 : aclDataType dataType; 52 : std::string dataTypeStr; 53 : size_t dataLen; 54 : std::shared_ptr<void> dataPtr; 55 : void *priorityData_; // this addr can not be free because it is passed by outside 56 : uint16_t sliceNum; 57 : uint16_t sliceId; 58 : }; 59 : 60 : struct acltdtDataset { 61 36 : acltdtDataset() : freeSelf(false) {}; 62 36 : ~acltdtDataset() 63 36 : { 64 36 : if (freeSelf) { 65 11 : for (auto iter = blobs.begin(); iter != blobs.end(); ++iter) { 66 3 : (void)acltdtDestroyDataItem(*iter); 67 : } 68 : } 69 36 : } 70 : std::string name; 71 : std::vector<acltdtDataItem *> blobs; 72 : datasetMemType memType = MEM_UNKNOWN; 73 : bool freeSelf; 74 : // mem reuse for performance optimization, used in acltdtReceiveTensor process 75 : size_t sharedMemSize_ = 0U; 76 : std::shared_ptr<void> sharedMem_; 77 : }; 78 : 79 : struct acltdtChannelHandle { 80 26 : acltdtChannelHandle(uint32_t deviceId, const char *channelName) 81 26 : { 82 26 : devId = deviceId; 83 26 : isTdtProcess = true; 84 26 : qid = 0; 85 26 : if (channelName != nullptr) { 86 26 : name = channelName; 87 26 : size_t prefixLen = sizeof("TF_RECEIVE_") - 1; 88 26 : if (strncmp(channelName, "TF_RECEIVE_", prefixLen) == 0) { 89 3 : recvName = channelName + prefixLen; 90 : } 91 : } 92 26 : } 93 1 : acltdtChannelHandle() = default; 94 27 : ~acltdtChannelHandle() = default; 95 : std::string name; 96 : std::string recvName; 97 : uint32_t devId; 98 : uint32_t qid; 99 : bool isTdtProcess; 100 : std::shared_ptr<void> ctx_; 101 : }; 102 : 103 : namespace acl { 104 : constexpr size_t RESERVED_SIZE = 24U; 105 : aclError acltdtSendTensorV2(const acltdtChannelHandle *handle, const acltdtDataset *dataset, int32_t timeout); 106 : 107 : aclError acltdtReceiveTensorV2(const acltdtChannelHandle *handle, acltdtDataset *dataset, int32_t timeout); 108 : 109 : aclError GetOrMallocHostMem(const acltdtChannelHandle *handle, acltdtDataset *dataset, 110 : size_t bufLen, void *&hostPtr); 111 : 112 : #pragma pack(push, 1) 113 : struct ItemInfo { 114 : int32_t version = 0; 115 : int32_t dataType = 0; 116 : uint32_t curCnt = 0U; 117 : uint32_t cnt = 0U; 118 : int32_t tensorType = 0; 119 : uint32_t dimNum = 0U; 120 : uint32_t dynamicBitSize = 0U; 121 : uint16_t sliceNum = 0; 122 : uint16_t sliceId = 0; 123 : char reserved[RESERVED_SIZE] = {0}; 124 : uint64_t dataLen = 0LU; 125 : }; 126 : #pragma pack(pop) 127 : 128 : struct aclTdtDataItemInfo { 129 : ItemInfo ctrlInfo; 130 : std::vector<int64_t> dims; 131 : std::shared_ptr<void> dataPtr; 132 : void *priorityDataPtr_ = nullptr; 133 : }; 134 : } 135 : #endif // ACL_TENSOR_DATA_TRANSFER_H