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

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

constexpr int32_t BUFFER_NUM = 1;

template <typename T>
__aicore__ inline uint32_t Align32B(uint32_t len)
{
    const int alginSize = 32 / sizeof(T);
    return (len + alginSize - 1) / alginSize * alginSize;
}

template <typename T, bool IS_REUSE_SOURCE, bool USE_SHARED_TMP_BUFFER, bool USE_CAL_COUNT>
class KernelFmod {
public:
    __aicore__ inline KernelFmod() {}
    __aicore__ inline void Init(GM_ADDR src0Gm, GM_ADDR src1Gm, GM_ADDR dstGm, uint32_t gmInCount, uint32_t gmOutCount,
                                uint32_t calCount, uint32_t sharedTmpBufferSize, AscendC::TPipe* pipeIn)
    {
        this->gmInCount = gmInCount;
        this->gmOutCount = gmOutCount;
        this->calCount = calCount;
        this->sharedTmpBufferSize = sharedTmpBufferSize;
        this->pipe = pipeIn;

        src0Global.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(src0Gm), gmInCount);
        src1Global.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(src1Gm), gmInCount);
        dstGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(dstGm), gmOutCount);

        this->pipe->InitBuffer(src0Queue, BUFFER_NUM, Align32B<T>(gmInCount) * sizeof(T));
        this->pipe->InitBuffer(src1Queue, BUFFER_NUM, Align32B<T>(gmInCount) * sizeof(T));
        this->pipe->InitBuffer(dstQueue, BUFFER_NUM, Align32B<T>(gmOutCount) * sizeof(T));
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }

    __aicore__ inline void CopyIn()
    {
        AscendC::LocalTensor<T> src0Local = src0Queue.AllocTensor<T>();
        AscendC::LocalTensor<T> src1Local = src1Queue.AllocTensor<T>();
        AscendC::DataCopyPadParams dataCopyPadParams;
        AscendC::DataCopyPad(src0Local, src0Global, {1, static_cast<uint16_t>(gmInCount * sizeof(T)), 0, 0},
                             dataCopyPadParams);
        AscendC::DataCopyPad(src1Local, src1Global, {1, static_cast<uint16_t>(gmInCount * sizeof(T)), 0, 0},
                             dataCopyPadParams);
        src0Queue.EnQue(src0Local);
        src1Queue.EnQue(src1Local);
    }
    __aicore__ inline void Compute()
    {
        AscendC::LocalTensor<T> dstLocal = dstQueue.AllocTensor<T>();
        if (gmOutCount > gmInCount || calCount < gmInCount) {
            AscendC::Duplicate(dstLocal, T(0), gmOutCount);
        }
        AscendC::LocalTensor<T> src0Local = src0Queue.DeQue<T>();
        AscendC::LocalTensor<T> src1Local = src1Queue.DeQue<T>();

#if __NPU_ARCH__ == 3101
        static constexpr AscendC::FmodConfig config = {AscendC::FmodAlgo::NORMAL, AscendC::FMOD_ITERATION_NUM_MAX};
        if constexpr (USE_SHARED_TMP_BUFFER) {
            pipe->InitBuffer(sharedTmpBufferQueue, sharedTmpBufferSize);
            AscendC::LocalTensor<uint8_t> sharedTmpBuffer = sharedTmpBufferQueue.Get<uint8_t>();
            if constexpr (USE_CAL_COUNT) {
                AscendC::Fmod<T, false, config>(dstLocal, src0Local, src1Local, sharedTmpBuffer, calCount);
            } else {
                AscendC::Fmod<T, false, config>(dstLocal, src0Local, src1Local, sharedTmpBuffer);
            }
        } else {
            if constexpr (USE_CAL_COUNT) {
                AscendC::Fmod<T, false, config>(dstLocal, src0Local, src1Local, calCount);
            } else {
                AscendC::Fmod<T, false, config>(dstLocal, src0Local, src1Local);
            }
        }
#elif __NPU_ARCH__ == 2201
        if constexpr (USE_SHARED_TMP_BUFFER) {
            pipe->InitBuffer(sharedTmpBufferQueue, sharedTmpBufferSize);
            AscendC::LocalTensor<uint8_t> sharedTmpBuffer = sharedTmpBufferQueue.Get<uint8_t>();
            if constexpr (USE_CAL_COUNT) {
                AscendC::Fmod(dstLocal, src0Local, src1Local, sharedTmpBuffer, calCount);
            } else {
                AscendC::Fmod(dstLocal, src0Local, src1Local, sharedTmpBuffer);
            }
        } else {
            if constexpr (USE_CAL_COUNT) {
                AscendC::Fmod(dstLocal, src0Local, src1Local, calCount);
            } else {
                AscendC::Fmod(dstLocal, src0Local, src1Local);
            }
        }
#endif

        dstQueue.EnQue<T>(dstLocal);
        src0Queue.FreeTensor(src0Local);
        src1Queue.FreeTensor(src1Local);
    }
    __aicore__ inline void CopyOut()
    {
        AscendC::LocalTensor<T> dstLocal = dstQueue.DeQue<T>();
        AscendC::DataCopyPad(dstGlobal, dstLocal, {1, static_cast<uint16_t>(gmOutCount * sizeof(T)), 0, 0});
        dstQueue.FreeTensor(dstLocal);
    }

private:
    AscendC::TPipe* pipe;
    AscendC::GlobalTensor<T> src0Global;
    AscendC::GlobalTensor<T> src1Global;
    AscendC::GlobalTensor<T> dstGlobal;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> src0Queue;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> src1Queue;
    AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> dstQueue;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> sharedTmpBufferQueue;
    uint32_t calCount{1};
    uint32_t gmInCount{1};
    uint32_t gmOutCount{1};
    uint32_t sharedTmpBufferSize{1};
};

__vector__ __global__ void fmod_custom(GM_ADDR src0Gm, GM_ADDR src1Gm, GM_ADDR dstGm)
{
    AscendC::TPipe pipe;
    constexpr uint32_t inCount = 159;
    constexpr uint32_t outCount = 159;
    constexpr uint32_t calCount = 159;
    constexpr uint32_t bufferSize = 2000;
    KernelFmod<float, 0, 0, 1> op;
    op.Init(src0Gm, src1Gm, dstGm, inCount, outCount, calCount, bufferSize, &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 = 159 * sizeof(float);
    size_t param2FileSize = 159 * sizeof(float);
    size_t param3FileSize = 159 * 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* param1Host;
    uint8_t* param1Device;
    aclrtMallocHost((void**)(&param1Host), param1FileSize);
    aclrtMalloc((void**)&param1Device, param1FileSize, ACL_MEM_MALLOC_HUGE_FIRST);
    ReadFile("./input/input_src0.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_src1.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);

    fmod_custom<<<numBlocks, nullptr, stream>>>(param1Device, param2Device, param3Device);
    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;
}