/**
* 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 init_global_memory.asc
 * \brief
 */

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

namespace TEST_CASE {
__aicore__ inline void PadTilingFunc(const AscendC::ShapeInfo srcShape, const uint32_t stackBufferSize,
                                     const uint32_t typeSize, PadTiling& tiling)
{
    // common
    uint32_t srcHeight = srcShape.shape[0];
    uint32_t srcWidth = srcShape.shape[1];
    uint32_t srcOriWidth = srcShape.originalShape[1];
    tiling.srcHeight = srcHeight;
    tiling.srcWidth = srcWidth;
    tiling.srcOriWidth = srcOriWidth;

    // width 32B aligned
    uint32_t widthWithoutLastBlock = srcWidth - AscendC::ONE_BLK_SIZE / typeSize;
    tiling.widthWithoutLastBlock = widthWithoutLastBlock;

    uint32_t blocksPerRow = srcWidth * typeSize / AscendC::ONE_BLK_SIZE;
    tiling.blocksPerRow = blocksPerRow;

    uint32_t heightTiling = AscendC::MAX_REPEAT_TIMES;
    uint32_t heightFractal = srcHeight / heightTiling;
    uint32_t heightFractalTail = srcHeight % heightTiling;
    tiling.heightTiling = heightTiling;
    tiling.heightFractal = heightFractal;
    tiling.heightFractalTail = heightFractalTail;

    uint32_t mainLoopOffset = heightTiling * srcWidth;
    uint32_t tailBlockOffset = heightFractal * heightTiling * srcWidth + widthWithoutLastBlock;
    tiling.mainLoopOffset = mainLoopOffset;
    tiling.tailBlockOffset = tailBlockOffset;

    // width 32B unaligned
    uint32_t baseBlockLen = 16 * AscendC::ONE_BLK_SIZE;
    uint32_t baseBlockSize = baseBlockLen / typeSize;
    uint32_t tmpBuffer1BlockNum = stackBufferSize * sizeof(uint8_t) / typeSize / baseBlockSize / 2;
    uint32_t tmpBuffer2Offset = tmpBuffer1BlockNum * baseBlockSize;
    tiling.tmpBuffer1BlockNum = tmpBuffer1BlockNum;
    tiling.tmpBuffer1RowNum = 16 * tmpBuffer1BlockNum;
    tiling.tmpBuffer2Offset = tmpBuffer2Offset;

    uint32_t widthTiling = 16 * tmpBuffer1BlockNum;
    uint32_t widthFractal = srcWidth / widthTiling;
    uint32_t widthFractalTail = srcWidth % widthTiling;
    uint32_t widthFractalTailAlingned =
        ((widthFractalTail - 1) / (AscendC::ONE_BLK_SIZE / typeSize) + 1) * (AscendC::ONE_BLK_SIZE / typeSize);
    tiling.widthTiling = widthTiling;
    tiling.widthFractal = widthFractal;
    tiling.widthFractalTail = widthFractalTail;
    tiling.widthFractalTailAlingned = widthFractalTailAlingned;

    uint32_t brcbTiling = 16 * tmpBuffer1BlockNum;
    uint32_t brcbFractal = srcHeight * srcWidth / brcbTiling;
    uint32_t brcbFractalTail = srcHeight * srcWidth % brcbTiling;
    tiling.brcbTiling = brcbTiling;
    tiling.brcbFractal = brcbFractal;
    tiling.brcbFractalTail = brcbFractalTail;

    uint32_t maxRepeatTimes = 254;
    uint32_t brcbTilingRepeatTimes = brcbTiling / 8 / maxRepeatTimes;
    uint32_t brcbTilingRepeatTimesTail = brcbTiling / 8 % maxRepeatTimes;
    uint32_t brcbFractalTailRepeatTimes = brcbFractalTail / 8 / maxRepeatTimes;
    uint32_t brcbFractalTailRepeatTimesTail = brcbFractalTail / 8 % maxRepeatTimes;
    tiling.maxRepeatTimes = maxRepeatTimes;
    tiling.brcbTilingRepeatTimes = brcbTilingRepeatTimes;
    tiling.brcbTilingRepeatTimesTail = brcbTilingRepeatTimesTail;
    tiling.brcbFractalTailRepeatTimes = brcbFractalTailRepeatTimes;
    tiling.brcbFractalTailRepeatTimesTail = brcbFractalTailRepeatTimesTail;
}

template <typename T>
class Pad {
public:
    __aicore__ inline Pad() {}
    __aicore__ inline void Init(GM_ADDR dstGm, GM_ADDR srcGm, uint32_t heightIn, uint32_t widthIn, uint32_t oriWidthIn,
                                AscendC::PadParams& padParamsIn, AscendC::TPipe* pipeIn)
    {
        pipe = pipeIn;
        height = heightIn;
        width = widthIn;
        oriWidth = oriWidthIn;
        padParams = padParamsIn;
        srcGlobal.SetGlobalBuffer((__gm__ T*)srcGm);
        dstGlobal.SetGlobalBuffer((__gm__ T*)dstGm);
        alignedWidth = ((width * sizeof(T) - 1) / 32 + 1) * 32 / sizeof(T);

        pipe->InitBuffer(inQueueSrcVecIn, 1, height * alignedWidth * sizeof(T));
        pipe->InitBuffer(inQueueSrcVecOut, 1, height * alignedWidth * sizeof(T));
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }

private:
    __aicore__ inline void CopyIn()
    {
        AscendC::LocalTensor<T> srcLocal = inQueueSrcVecIn.AllocTensor<T>();
        AscendC::DataCopy(srcLocal, srcGlobal, height * width);
        inQueueSrcVecIn.EnQue(srcLocal);
    }
    __aicore__ inline void Compute()
    {
        AscendC::LocalTensor<T> dstLocal = inQueueSrcVecIn.DeQue<T>();

        uint32_t shape[] = {height, width};
        uint32_t oriShape[] = {height, oriWidth};
        AscendC::ShapeInfo shapeInfo(2, shape, 2, oriShape, AscendC::DataFormat::ND);
        dstLocal.SetShapeInfo(shapeInfo);

        AscendC::LocalTensor<T> srcOutLocal = inQueueSrcVecOut.AllocTensor<T>();

        AscendC::LocalTensor<uint8_t> stackBuffer;
        AscendC::PopStackBuffer<uint8_t, AscendC::TPosition::LCM>(stackBuffer);
        uint32_t stackBufferSize = stackBuffer.GetSize();

        PadTiling tiling;
        PadTilingFunc(shapeInfo, stackBufferSize, sizeof(T), tiling);

        AscendC::Pad(srcOutLocal, dstLocal, padParams, tiling);

        inQueueSrcVecOut.EnQue<T>(srcOutLocal);
        inQueueSrcVecIn.FreeTensor(dstLocal);
    }
    __aicore__ inline void CopyOut()
    {
        AscendC::LocalTensor<T> srcOutLocalDe = inQueueSrcVecOut.DeQue<T>();
        AscendC::DataCopy(dstGlobal, srcOutLocalDe, height * alignedWidth);
        inQueueSrcVecOut.FreeTensor(srcOutLocalDe);
    }

private:
    AscendC::TPipe* pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, 1> inQueueSrcVecIn;
    AscendC::TQue<AscendC::QuePosition::VECOUT, 1> inQueueSrcVecOut;
    AscendC::GlobalTensor<T> srcGlobal;
    AscendC::GlobalTensor<T> dstGlobal;

    uint32_t height;
    uint32_t width;
    uint32_t oriWidth;
    uint32_t alignedWidth;
    AscendC::PadParams padParams;
};

} // namespace TEST_CASE

extern "C" __global__ __aicore__ void pad_custom(GM_ADDR src_Gm, GM_ADDR dst_Gm)
{
    KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);
    AscendC::TPipe pipe;
    TEST_CASE::Pad<float> op;
    AscendC::PadParams padParams{0, 1, 321};
    op.Init(dst_Gm, src_Gm, 16, 31, 31, padParams, &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) {
        printf("wrongNum: %ld\n", wrongNum);
        return false;
    } else {
        printf("CompareResult golden.bin success!\n");
        return true;
    }
}

int32_t main(int32_t argc, char* argv[])
{
    size_t inputSize = 496 * sizeof(float);
    size_t yFileSize = 512 * sizeof(float);
    uint32_t numBlocks = 1;

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

    uint8_t* xHost;
    uint8_t* xDevice;

    aclrtMallocHost((void**)(&xHost), inputSize);
    aclrtMalloc((void**)&xDevice, inputSize, ACL_MEM_MALLOC_HUGE_FIRST);

    ReadFile("./input/input_x.bin", inputSize, xHost, inputSize);
    aclrtMemcpy(xDevice, inputSize, xHost, inputSize, ACL_MEMCPY_HOST_TO_DEVICE);

    uint8_t* yHost;
    uint8_t* yDevice;

    aclrtMallocHost((void**)(&yHost), yFileSize);
    aclrtMalloc((void**)&yDevice, yFileSize, ACL_MEM_MALLOC_HUGE_FIRST);

    pad_custom<<<numBlocks, nullptr, stream>>>(xDevice, yDevice);

    aclrtSynchronizeStream(stream);

    aclrtFree(xDevice);
    aclrtFreeHost(xHost);

    aclrtMemcpy(yHost, yFileSize, yDevice, yFileSize, ACL_MEMCPY_DEVICE_TO_HOST);

    WriteFile("./output/output.bin", yHost, yFileSize);

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

    aclrtFree(yDevice);
    aclrtFreeHost(yHost);

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

    return 0;
}