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

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

template <typename T>
class KernelWhere {
public:
    __aicore__ inline KernelWhere() {}
    __aicore__ inline void Init(GM_ADDR dstGm, GM_ADDR src0Gm, GM_ADDR src1Gm, GM_ADDR conditionGm, uint32_t shape,
                                uint32_t count, uint32_t mode, AscendC::TPipe* pipeIn)
    {
        pipe = pipeIn;
        dstGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(dstGm), shape);
        src0Global.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(src0Gm), shape);
        src1Global.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(src1Gm), shape);
        conditionGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ bool*>(conditionGm), (shape + 31) / 32 * 32);
        pipe->InitBuffer(inQueueX, 1, shape * sizeof(T));
        pipe->InitBuffer(inQueueY, 1, shape * sizeof(T));
        pipe->InitBuffer(inQueueZ, 1, shape * sizeof(bool));
        pipe->InitBuffer(outQueue, 1, shape * sizeof(T));
        dataSize = count;
        this->mode = mode;
        this->shape = shape;
    }
    __aicore__ inline void Process()
    {
        AscendC::AscendCUtils::SetOverflow(1);
        CopyIn();
        Compute();
        CopyOut();
        AscendC::AscendCUtils::SetOverflow(0);
    }

    __aicore__ inline void CopyIn()
    {
        AscendC::LocalTensor<T> src0Local = inQueueX.AllocTensor<T>();
        AscendC::LocalTensor<T> src1Local = inQueueY.AllocTensor<T>();
        AscendC::LocalTensor<bool> conditionLocal = inQueueZ.AllocTensor<bool>();
        AscendC::DataCopy(src0Local, src0Global, shape);
        AscendC::DataCopy(src1Local, src1Global, shape);
        AscendC::DataCopy(conditionLocal, conditionGlobal, (shape + 31) / 32 * 32);
        inQueueX.EnQue(src0Local);
        inQueueY.EnQue(src1Local);
        inQueueZ.EnQue(conditionLocal);
    }
    __aicore__ inline void Compute()
    {
        AscendC::LocalTensor<T> dstLocal = outQueue.AllocTensor<T>();
        AscendC::LocalTensor<T> src0Local = inQueueX.DeQue<T>();
        AscendC::LocalTensor<T> src1Local = inQueueY.DeQue<T>();
        AscendC::LocalTensor<bool> conditionLocal = inQueueZ.DeQue<bool>();
        AscendC::Duplicate(dstLocal, (T)0, shape);

        if (mode == 0) {
            AscendC::Where<T>(dstLocal, src0Local, src1Local, conditionLocal, dataSize);
        } else if (mode == 1) {
            event_t eventIdMTE2ToS = static_cast<event_t>(GetTPipePtr()->FetchEventID(AscendC::HardEvent::MTE2_S));
            AscendC::SetFlag<AscendC::HardEvent::MTE2_S>(eventIdMTE2ToS);
            AscendC::WaitFlag<AscendC::HardEvent::MTE2_S>(eventIdMTE2ToS);
            T src0 = src0Local.GetValue(0);
            event_t eventIdSToV = static_cast<event_t>(GetTPipePtr()->FetchEventID(AscendC::HardEvent::S_V));
            AscendC::SetFlag<AscendC::HardEvent::S_V>(eventIdSToV);
            AscendC::WaitFlag<AscendC::HardEvent::S_V>(eventIdSToV);
            AscendC::Where<T>(dstLocal, src0, src1Local, conditionLocal, dataSize);
        } else if (mode == 2) {
            event_t eventIdMTE2ToS = static_cast<event_t>(GetTPipePtr()->FetchEventID(AscendC::HardEvent::MTE2_S));
            AscendC::SetFlag<AscendC::HardEvent::MTE2_S>(eventIdMTE2ToS);
            AscendC::WaitFlag<AscendC::HardEvent::MTE2_S>(eventIdMTE2ToS);
            T src1 = src1Local.GetValue(0);
            event_t eventIdSToV = static_cast<event_t>(GetTPipePtr()->FetchEventID(AscendC::HardEvent::S_V));
            AscendC::SetFlag<AscendC::HardEvent::S_V>(eventIdSToV);
            AscendC::WaitFlag<AscendC::HardEvent::S_V>(eventIdSToV);
            AscendC::Where<T>(dstLocal, src0Local, src1, conditionLocal, dataSize);
        } else if (mode == 3) {
            event_t eventIdMTE2ToS = static_cast<event_t>(GetTPipePtr()->FetchEventID(AscendC::HardEvent::MTE2_S));
            AscendC::SetFlag<AscendC::HardEvent::MTE2_S>(eventIdMTE2ToS);
            AscendC::WaitFlag<AscendC::HardEvent::MTE2_S>(eventIdMTE2ToS);
            T src0 = src0Local.GetValue(0);
            T src1 = src1Local.GetValue(0);
            event_t eventIdSToV = static_cast<event_t>(GetTPipePtr()->FetchEventID(AscendC::HardEvent::S_V));
            AscendC::SetFlag<AscendC::HardEvent::S_V>(eventIdSToV);
            AscendC::WaitFlag<AscendC::HardEvent::S_V>(eventIdSToV);
            AscendC::Where<T>(dstLocal, src0, src1, conditionLocal, dataSize);
        }
        outQueue.EnQue<T>(dstLocal);
        inQueueX.FreeTensor(src0Local);
        inQueueY.FreeTensor(src1Local);
        inQueueZ.FreeTensor(conditionLocal);
    }
    __aicore__ inline void CopyOut()
    {
        AscendC::LocalTensor<T> dstLocal = outQueue.DeQue<T>();
        AscendC::DataCopy(dstGlobal, dstLocal, shape);
        outQueue.FreeTensor(dstLocal);
    }

private:
    AscendC::TPipe* pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, 1> inQueueX, inQueueY, inQueueZ;
    AscendC::TQue<AscendC::QuePosition::VECOUT, 1> outQueue;
    AscendC::GlobalTensor<T> src0Global, src1Global;
    AscendC::GlobalTensor<T> dstGlobal;
    AscendC::GlobalTensor<bool> conditionGlobal;
    uint32_t shape = 0;
    uint32_t dataSize = 0;
    uint32_t mode = 0;
};

__global__ __vector__ void where_custom(GM_ADDR src0Gm, GM_ADDR src1Gm, GM_ADDR conditionGm, GM_ADDR dstGm)
{
    AscendC::TPipe pipe;
    constexpr uint32_t shape = 32;
    constexpr uint32_t count = 32;
    constexpr uint32_t mode = 0;
    constexpr uint32_t isDynamic = 0;
    KernelWhere<float> op;
    op.Init(dstGm, src0Gm, src1Gm, conditionGm, shape, count, 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 = 32 * sizeof(float);
    size_t param2FileSize = 32 * sizeof(float);
    size_t param3FileSize = 32 * sizeof(bool);
    size_t param4FileSize = 4 * sizeof(uint32_t);
    size_t param5FileSize = 32 * 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);
    ReadFile("./input/input_condition.bin", param3FileSize, param3Host, param3FileSize);
    aclrtMemcpy(param3Device, param3FileSize, param3Host, param3FileSize, ACL_MEMCPY_HOST_TO_DEVICE);
    
    uint8_t* param4Host;
    uint8_t* param4Device;
    aclrtMallocHost((void**)(&param4Host), param4FileSize);
    aclrtMalloc((void**)&param4Device, param4FileSize, ACL_MEM_MALLOC_HUGE_FIRST);
    ReadFile("./input/input_tiling.bin", param4FileSize, param4Host, param4FileSize);
    aclrtMemcpy(param4Device, param4FileSize, param4Host, param4FileSize, ACL_MEMCPY_HOST_TO_DEVICE);

    uint8_t* param5Host;
    uint8_t* param5Device;
    aclrtMallocHost((void**)(&param5Host), param5FileSize);
    aclrtMalloc((void**)&param5Device, param5FileSize, ACL_MEM_MALLOC_HUGE_FIRST);

    where_custom<<<numBlocks, nullptr, stream>>>(param1Device, param2Device, param3Device, param5Device);
    aclrtSynchronizeStream(stream);

    aclrtFree(param1Device);
    aclrtFreeHost(param1Host);
    aclrtFree(param2Device);
    aclrtFreeHost(param2Host);
    aclrtFree(param3Device);
    aclrtFreeHost(param3Host);
    aclrtFree(param4Device);
    aclrtFreeHost(param4Host);

    aclrtMemcpy(param5Host, param5FileSize, param5Device, param5FileSize, ACL_MEMCPY_DEVICE_TO_HOST);
    WriteFile("./output/output.bin", param5Host, param5FileSize);

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

    aclrtFree(param5Device);
    aclrtFreeHost(param5Host);

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

    return 0;
}