/**
* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/


/* !
 * \file power.asc
 * \brief
 */

#include "acl/acl.h"
#include "data_utils.h"
#include "kernel_operator.h"

template <typename T>
class KernelPower {
public:
    __aicore__ inline KernelPower() {}
    __aicore__ inline void Init(GM_ADDR srcGmBase, GM_ADDR srcGmExp, GM_ADDR dstGm, uint32_t srcSize, uint32_t mode,
                                AscendC::TPipe* pipeIn)
    {
        pipe = pipeIn;
        srcGlobalBase.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(srcGmBase), srcSize);
        srcGlobalExp.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(srcGmExp), srcSize);
        dstGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(dstGm), srcSize);

        pipe->InitBuffer(inQueueX1, 1, srcSize * sizeof(T));
        pipe->InitBuffer(inQueueX2, 1, srcSize * sizeof(T));
        pipe->InitBuffer(outQueue, 1, srcSize * sizeof(T));
        bufferSize = srcSize;
        this->mode = mode;
    }
    __aicore__ inline void Process()
    {
        AscendC::AscendCUtils::SetOverflow(1);
        CopyIn();
        Compute();
        CopyOut();
        AscendC::AscendCUtils::SetOverflow(0);
    }

    __aicore__ inline void CopyIn()
    {
        AscendC::LocalTensor<T> srcLocalBase = inQueueX1.AllocTensor<T>();
        AscendC::LocalTensor<T> srcLocalExp = inQueueX2.AllocTensor<T>();
        AscendC::DataCopy(srcLocalBase, srcGlobalBase, bufferSize);
        AscendC::DataCopy(srcLocalExp, srcGlobalExp, bufferSize);
        inQueueX1.EnQue(srcLocalBase);
        inQueueX2.EnQue(srcLocalExp);
    }
    __aicore__ inline void Compute()
    {
        AscendC::LocalTensor<T> dstLocal = outQueue.AllocTensor<T>();
        AscendC::LocalTensor<T> srcLocalBase = inQueueX1.DeQue<T>();
        AscendC::LocalTensor<T> srcLocalExp = inQueueX2.DeQue<T>();

#if __NPU_ARCH__ == 3101
        static constexpr AscendC::PowerAlgo valueLowPrecision = AscendC::PowerAlgo::INTRINSIC;
        static constexpr AscendC::PowerAlgo valueHighPrecision = AscendC::PowerAlgo::DOUBLE_FLOAT_TECH;
        static constexpr AscendC::PowerConfig configLowPrecision = {valueLowPrecision};
        static constexpr AscendC::PowerConfig configHighPrecision = {valueHighPrecision};
        if (mode == 0) {
            AscendC::Power<T, false, configHighPrecision>(dstLocal, srcLocalBase, srcLocalExp);
        } else if (mode == 1) {
            T scalarValue = srcLocalExp.GetValue(0);
            AscendC::PipeBarrier<PIPE_V>();
            AscendC::Power<T, false, configHighPrecision>(dstLocal, srcLocalBase, scalarValue);
        } else if (mode == 2) {
            T scalarValue = srcLocalBase.GetValue(0);
            AscendC::PipeBarrier<PIPE_V>();
            AscendC::Power<T, false, configHighPrecision>(dstLocal, scalarValue, srcLocalExp);
        }
#elif __NPU_ARCH__ == 2201
        if (mode == 0) {
            AscendC::Power<T, false>(dstLocal, srcLocalBase, srcLocalExp);
        } else if (mode == 1) {
            T scalarValue = srcLocalExp.GetValue(0);
            AscendC::PipeBarrier<PIPE_V>();
            AscendC::Power<T, false>(dstLocal, srcLocalBase, scalarValue);
        } else if (mode == 2) {
            T scalarValue = srcLocalBase.GetValue(0);
            AscendC::PipeBarrier<PIPE_V>();
            AscendC::Power<T, false>(dstLocal, scalarValue, srcLocalExp);
        }
#endif
        AscendC::PipeBarrier<PIPE_V>();
        outQueue.EnQue<T>(dstLocal);
        inQueueX1.FreeTensor(srcLocalBase);
        inQueueX2.FreeTensor(srcLocalExp);
    }
    __aicore__ inline void CopyOut()
    {
        AscendC::LocalTensor<T> dstLocal = outQueue.DeQue<T>();
        AscendC::DataCopy(dstGlobal, dstLocal, bufferSize);
        outQueue.FreeTensor(dstLocal);
    }

private:
    AscendC::TPipe* pipe;
    AscendC::GlobalTensor<T> srcGlobalBase;
    AscendC::GlobalTensor<T> srcGlobalExp;
    AscendC::GlobalTensor<T> dstGlobal;

    AscendC::TQue<AscendC::QuePosition::VECIN, 1> inQueueX1;
    AscendC::TQue<AscendC::QuePosition::VECIN, 1> inQueueX2;
    AscendC::TQue<AscendC::QuePosition::VECOUT, 1> outQueue;

    uint32_t bufferSize = 0;
    uint32_t mode = 0;
};

__vector__ __global__ void power_custom(GM_ADDR srcGmBase, GM_ADDR srcGmExp, GM_ADDR dstGm, uint32_t srcSize,
                                        uint32_t mode)
{
    AscendC::TPipe pipe;
    KernelPower<float> op;
    op.Init(srcGmBase, srcGmExp, dstGm, srcSize, mode, &pipe);
    op.Process();
}

static bool CompareResult(const void* outputData, uint32_t outSize)
{
    void* goldenData;
    aclrtMallocHost((void**)(&goldenData), outSize);
    size_t goldenSize = outSize;
    bool ret = ReadFile("./output/golden.bin", goldenSize, goldenData, goldenSize);
    if (ret) {
        printf("ReadFile golden.bin success!\n");
    } else {
        printf("test failed!\n");
        return false;
    }
    constexpr float EPS = 1e-4;
    int64_t wrongNum = 0;

    for (size_t i = 0; i < outSize / sizeof(float); i++) {
        float a = (reinterpret_cast<const float*>(outputData))[i];
        float b = (reinterpret_cast<const float*>(goldenData))[i];
        float ae = std::abs(a - b);
        float re = ae / std::abs(b);
        if (ae > EPS && re > EPS) {
            printf("CompareResult golden.bin failed output is %lf, golden is %lf\n", a, b);
            wrongNum++;
        }
    }
    aclrtFreeHost(goldenData);
    if (wrongNum != 0) {
        return false;
    } else {
        printf("CompareResult golden.bin success!\n");
        return true;
    }
}

int32_t main(int32_t argc, char* argv[])
{
    size_t param1FileSize = 16 * sizeof(float);
    size_t param2FileSize = 16 * sizeof(float);
    size_t param3FileSize = 16 * sizeof(float);
    size_t srcSize = param1FileSize / sizeof(float);
    constexpr uint32_t mode = 0; // 默认模式为mode=0,即指数和底数都为张量
    uint32_t numBlocks = 1;

    aclInit(nullptr);
    aclrtContext context;
    int32_t deviceId = 0;
    aclrtSetDevice(deviceId);
    aclrtCreateContext(&context, deviceId);
    aclrtStream stream = nullptr;
    aclrtCreateStream(&stream);

    uint8_t* param1Host;
    uint8_t* param1Device;
    aclrtMallocHost((void**)(&param1Host), param1FileSize);
    aclrtMalloc((void**)&param1Device, param1FileSize, ACL_MEM_MALLOC_HUGE_FIRST);
    ReadFile("./input/input_base.bin", param1FileSize, param1Host, param1FileSize);
    aclrtMemcpy(param1Device, param1FileSize, param1Host, param1FileSize, ACL_MEMCPY_HOST_TO_DEVICE);

    uint8_t* param2Host;
    uint8_t* param2Device;
    aclrtMallocHost((void**)(&param2Host), param2FileSize);
    aclrtMalloc((void**)&param2Device, param2FileSize, ACL_MEM_MALLOC_HUGE_FIRST);
    ReadFile("./input/input_exp.bin", param2FileSize, param2Host, param2FileSize);
    aclrtMemcpy(param2Device, param2FileSize, param2Host, param2FileSize, ACL_MEMCPY_HOST_TO_DEVICE);

    uint8_t* param3Host;
    uint8_t* param3Device;
    aclrtMallocHost((void**)(&param3Host), param3FileSize);
    aclrtMalloc((void**)&param3Device, param3FileSize, ACL_MEM_MALLOC_HUGE_FIRST);

    power_custom<<<numBlocks, nullptr, stream>>>(param1Device, param2Device, param3Device, srcSize, mode);
    aclrtSynchronizeStream(stream);

    aclrtFree(param1Device);
    aclrtFreeHost(param1Host);
    aclrtFree(param2Device);
    aclrtFreeHost(param2Host);

    aclrtMemcpy(param3Host, param3FileSize, param3Device, param3FileSize, ACL_MEMCPY_DEVICE_TO_HOST);
    WriteFile("./output/output.bin", param3Host, param3FileSize);

    bool goldenResult = true;
    goldenResult = CompareResult(param3Host, param3FileSize);
    if (goldenResult) {
        printf("test pass!\n");
    } else {
        printf("test failed!\n");
    }

    aclrtFree(param3Device);
    aclrtFreeHost(param3Host);

    aclrtDestroyStream(stream);
    aclrtDestroyContext(context);
    aclrtResetDevice(deviceId);
    aclFinalize();

    return 0;
}