昇腾社区首页
中文
注册
开发者
下载

Csrot

  • 安全声明:

    该样例旨在提供快速上手、开发和调试算子的最小化实现,其核心目标是使用最精简的代码展示算子的核心功能,而非提供生产级的安全保障。

    不推荐用户直接将样例作为业务代码,若用户将示例代码应用在自身的真实业务场景中且发生了安全问题,则需用户自行承担。

  • asdBlasCsrot算子调用示例:
    #include <iostream>
    #include <vector>
    #include <complex>
    #include "asdsip.h"
    #include "acl/acl.h"
    #include "acl_meta.h"
    #include "utils/mem_base.h"
    
    using namespace AsdSip;
    
    #define ASD_STATUS_CHECK(err)                                                \
        do {                                                                     \
            AsdSip::AspbStatus err_ = (err);                                     \
            if (err_ != AsdSip::ErrorType::ACL_SUCCESS) {                                      \
                std::cout << "Execute failed." << std::endl; \
                exit(-1);                                                        \
            } else {                                                             \
                std::cout << "Execute successfully." << std::endl;               \
            }                                                                    \
        } while (0)
    
    #define CHECK_RET(cond, return_expr) \
        do {                             \
            if (!(cond)) {               \
                return_expr;             \
            }                            \
        } while (0)
    
    #define LOG_PRINT(message, ...)         \
        do {                                \
            printf(message, ##__VA_ARGS__); \
        } while (0)
    
    int64_t GetShapeSize(const std::vector<int64_t> &shape)
    {
        int64_t shapeSize = 1;
        for (auto i : shape) {
            shapeSize *= i;
        }
        return shapeSize;
    }
    
    int Init(int32_t deviceId, aclrtStream *stream)
    {
        // 固定写法,acl初始化
        auto ret = aclInit(nullptr);
        CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
        ret = aclrtSetDevice(deviceId);
        CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
        ret = aclrtCreateStream(stream);
        CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
        return 0;
    }
    
    template <typename T>
    int CreateAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> &shape, void **deviceAddr,
        aclDataType dataType, aclTensor **tensor)
    {
        auto size = GetShapeSize(shape) * sizeof(T);
        // 调用aclrtMalloc申请device侧内存
        auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
        CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
        // 调用aclrtMemcpy将host侧数据复制到device侧内存上
        ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
        CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
    
        // 计算连续tensor的strides
        std::vector<int64_t> strides(shape.size(), 1);
        for (int64_t i = shape.size() - 2; i >= 0; i--) {
            strides[i] = shape[i + 1] * strides[i + 1];
        }
    
        // 调用aclCreateTensor接口创建aclTensor
        *tensor = aclCreateTensor(shape.data(),
            shape.size(),
            dataType,
            strides.data(),
            0,
            aclFormat::ACL_FORMAT_ND,
            shape.data(),
            shape.size(),
            *deviceAddr);
        return 0;
    }
    
    void printTensor(const std::complex<float> *tensorData, int64_t tensorSize)
    {
        for (int64_t i = 0; i < tensorSize; i++) {
            std::cout << tensorData[i] << " ";
        }
        std::cout << std::endl;
    }
    
    int main(int argc, char **argv)
    {
        int deviceId = 0;
    
        aclrtStream stream;
        auto ret = Init(deviceId, &stream);
        CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
    
        int64_t n = 4;
        int incx = 1;
        int incy = 1;
        const float cosValue = sqrt(3) / 2;
        const float sinValue = 0.5;
    
        int64_t xSize = n;
        int64_t ySize = n;
    
        std::vector<std::complex<float>> tensorInXData;
        tensorInXData.reserve(xSize);
        for (int64_t i = 0; i < n; i++) {
            tensorInXData[i] = (std::complex<float>){2.0, 3.0};
        }
    
        std::vector<std::complex<float>> tensorInYData;
        tensorInYData.reserve(ySize);
        for (int64_t i = 0; i < n; i++) {
            tensorInYData[i] = (std::complex<float>){5.0, 6.0};
        }
    
        std::cout << "cosValue = " << cosValue << std::endl;
        std::cout << "sinValue = " << sinValue << std::endl;
        std::cout << "------- input TensorInX -------" << std::endl;
        printTensor(tensorInXData.data(), xSize);
        std::cout << "------- input TensorInY -------" << std::endl;
        printTensor(tensorInYData.data(), ySize);
    
        std::vector<int64_t> xShape = {xSize};
        std::vector<int64_t> yShape = {ySize};
        aclTensor *inputX = nullptr;
        aclTensor *inputY = nullptr;
        void *inputXDeviceAddr = nullptr;
        void *inputYDeviceAddr = nullptr;
        ret = CreateAclTensor(tensorInXData, xShape, &inputXDeviceAddr, aclDataType::ACL_COMPLEX64, &inputX);
        CHECK_RET(ret == ::ACL_SUCCESS, return ret);
        ret = CreateAclTensor(tensorInYData, yShape, &inputYDeviceAddr, aclDataType::ACL_COMPLEX64, &inputY);
        CHECK_RET(ret == ::ACL_SUCCESS, return ret);
    
        asdBlasHandle handle;
        asdBlasCreate(handle);
    
        size_t lwork = 0;
        void *buffer = nullptr;
        asdBlasMakeRotPlan(handle);
        asdBlasGetWorkspaceSize(handle, lwork);
        std::cout << "lwork = " << lwork << std::endl;
        if (lwork > 0) {
            ret = aclrtMalloc(&buffer, static_cast<int64_t>(lwork), ACL_MEM_MALLOC_HUGE_FIRST);
            CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
        }
        asdBlasSetWorkspace(handle, buffer);
        asdBlasSetStream(handle, stream);
    
        ASD_STATUS_CHECK(asdBlasCsrot(handle, n, inputX, incx, inputY, incy, cosValue, sinValue));
    
        asdBlasSynchronize(handle);
        asdBlasDestroy(handle);
    
        ret = aclrtMemcpy(tensorInXData.data(),
            xSize * sizeof(std::complex<float>),
            inputXDeviceAddr,
            xSize * sizeof(std::complex<float>),
            ACL_MEMCPY_DEVICE_TO_HOST);
        CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("copy tensor x from device to host failed. ERROR: %d\n", ret); return ret);
    
        ret = aclrtMemcpy(tensorInYData.data(),
            ySize * sizeof(std::complex<float>),
            inputYDeviceAddr,
            ySize * sizeof(std::complex<float>),
            ACL_MEMCPY_DEVICE_TO_HOST);
        CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("copy tensor y from device to host failed. ERROR: %d\n", ret); return ret);
    
        std::cout << "------- output TensorInX -------" << std::endl;
        printTensor(tensorInXData.data(), xSize);
        std::cout << "------- output TensorInY -------" << std::endl;
        printTensor(tensorInYData.data(), ySize);
    
        aclDestroyTensor(inputX);
        aclDestroyTensor(inputY);
        aclrtFree(inputXDeviceAddr);
        aclrtFree(inputYDeviceAddr);
    
        aclrtDestroyStream(stream);
        aclrtResetDevice(deviceId);
        aclFinalize();
        return 0;
    }