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

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

template <uint32_t isAr, bool isReuse, typename T, bool useTemp>
class KernelReduceMean {
public:
    __aicore__ inline KernelReduceMean() {}
    __aicore__ inline void Init(GM_ADDR srcGm, GM_ADDR dstGm, uint32_t firstIn, uint32_t lastIn, AscendC::TPipe* pipeIn)
    {
        pipe = pipeIn;
        first = firstIn;
        last = lastIn;
        size = first * last;
        outputSize = isAr == 0 ? last : first;
        uint32_t k = 0, firstCopy = first;
        while (firstCopy > 0) {
            k++;
            firstCopy >>= 1;
        }
        uint32_t splitK = 1 << (k - 1);
        constexpr uint32_t elePerRep = 256 / sizeof(T);
        constexpr uint32_t elePerBlk = 32 / sizeof(T);
        srcGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(srcGm), size);
        dstGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(dstGm), outputSize * sizeof(T));

        pipe->InitBuffer(inQueueX, 1, size * sizeof(T));
        pipe->InitBuffer(outQueue, 1, outputSize * sizeof(T));
        if constexpr (useTemp == 1) {
            if constexpr (isAr == 0) {
                pipe->InitBuffer(bufQueue, splitK * last * sizeof(T));
            } else {
                if (last < elePerRep) {
                    pipe->InitBuffer(bufQueue, first * elePerBlk * sizeof(T));
                } else if (last >= elePerRep) {
                    pipe->InitBuffer(bufQueue, first * elePerRep * sizeof(T));
                }
            }
        }
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }

    __aicore__ inline void CopyIn()
    {
        AscendC::LocalTensor<T> srcLocal = inQueueX.AllocTensor<T>();
        DataCopy(srcLocal, srcGlobal, size);
        inQueueX.EnQue(srcLocal);
    }
    __aicore__ inline void Compute()
    {
        AscendC::LocalTensor<T> srcLocal = inQueueX.DeQue<T>();
        AscendC::LocalTensor<T> dstLocal = outQueue.AllocTensor<T>();
        AscendC::Duplicate<T>(dstLocal, static_cast<T>(0), outputSize);
        AscendC::PipeBarrier<PIPE_V>();
        uint32_t shape[2] = {first, last};

        if constexpr (useTemp == 1) {
            AscendC::LocalTensor<uint8_t> tmpLocal = bufQueue.Get<uint8_t>();
            if constexpr (isAr == 1) {
                AscendC::ReduceMean<T, AscendC::Pattern::Reduce::AR, isReuse>(dstLocal, srcLocal, tmpLocal, shape,
                                                                              true);
            } else {
                AscendC::ReduceMean<T, AscendC::Pattern::Reduce::RA, isReuse>(dstLocal, srcLocal, tmpLocal, shape,
                                                                              true);
            }
        } else {
            if constexpr (isAr == 1) {
                AscendC::ReduceMean<T, AscendC::Pattern::Reduce::AR, isReuse>(dstLocal, srcLocal, shape, true);
            } else {
                AscendC::ReduceMean<T, AscendC::Pattern::Reduce::RA, isReuse>(dstLocal, srcLocal, shape, true);
            }
        }

        outQueue.EnQue<T>(dstLocal);
        inQueueX.FreeTensor(srcLocal);
    }

    __aicore__ inline void CopyOut()
    {
        AscendC::LocalTensor<T> dstLocal = outQueue.DeQue<T>();

        AscendC::DataCopyExtParams copyParams{};
        copyParams.blockCount = 1;
        copyParams.blockLen = outputSize * sizeof(T);
        copyParams.srcStride = 0;
        copyParams.dstStride = 0;

        AscendC::DataCopyPadExtParams<T> padParams{};
        padParams.isPad = false;
        padParams.leftPadding = 0;
        padParams.rightPadding = 0;
        padParams.paddingValue = 0;

        AscendC::DataCopyPad<T>(dstGlobal, dstLocal, copyParams);

        outQueue.FreeTensor(dstLocal);
    }

private:
    AscendC::TPipe* pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, 1> inQueueX;
    AscendC::TQue<AscendC::QuePosition::VECOUT, 1> outQueue;
    AscendC::TBuf<AscendC::QuePosition::VECIN> bufQueue;
    AscendC::GlobalTensor<T> srcGlobal;
    AscendC::GlobalTensor<T> dstGlobal;
    uint32_t size = 0;
    uint32_t outputSize = 0;
    uint32_t first = 0;
    uint32_t last = 0;
};

__global__ __aicore__ void reducemean_custom(GM_ADDR srcGm, GM_ADDR dstGm)
{
    KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);
    AscendC::TPipe pipe;
    constexpr uint32_t first = 1;
    constexpr uint32_t last = 32;
    constexpr uint32_t isAr = 1;
    constexpr uint32_t isReuseIn = 1;
    constexpr uint32_t useTempIn = 0;
    constexpr bool isReuse = (isReuseIn == 1);
    constexpr bool useTemp = (useTempIn == 999);
    KernelReduceMean<isAr, isReuse, float, useTemp> op;
    op.Init(srcGm, dstGm, first, last, &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 param1FileSize = 32 * sizeof(float);
    size_t param2FileSize = 1 * sizeof(float);
    size_t param3FileSize = 2 * sizeof(uint32_t);
    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_x.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);

    uint8_t* param3Host;
    uint8_t* param3Device;
    aclrtMallocHost((void**)(&param3Host), param3FileSize);
    aclrtMalloc((void**)&param3Device, param3FileSize, ACL_MEM_MALLOC_HUGE_FIRST);
    ReadFile("./input/input_shape.bin", param3FileSize, param3Host, param3FileSize);
    aclrtMemcpy(param3Device, param3FileSize, param3Host, param3FileSize, ACL_MEMCPY_HOST_TO_DEVICE);

    reducemean_custom<<<numBlocks, nullptr, stream>>>(param1Device, param2Device);
    aclrtSynchronizeStream(stream);

    aclrtFree(param1Device);
    aclrtFreeHost(param1Host);
    aclrtFree(param3Device);
    aclrtFreeHost(param3Host);

    aclrtMemcpy(param2Host, param2FileSize, param2Device, param2FileSize, ACL_MEMCPY_DEVICE_TO_HOST);
    WriteFile("./output/output.bin", param2Host, param2FileSize);

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

    aclrtFree(param2Device);
    aclrtFreeHost(param2Host);

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

    return 0;
}