/**
* 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.
*/


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

constexpr int32_t BUFFER_NUM = 1;                                     // tensor num for each queue

template <typename T>
class KernelScatter {
public:
    __aicore__ inline KernelScatter() {}
    __aicore__ inline void Init(GM_ADDR srcGm, GM_ADDR dstOffsetGm, GM_ADDR dstGm, uint32_t count)
    {
        mElementCount = count;
        xGm.SetGlobalBuffer((__gm__ half *)srcGm, mElementCount);
        yGm.SetGlobalBuffer((__gm__ uint32_t *)dstOffsetGm, mElementCount);
        zGm.SetGlobalBuffer((__gm__ half *)dstGm, mElementCount);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, mElementCount * sizeof(half));
        pipe.InitBuffer(inQueueY, BUFFER_NUM, mElementCount * sizeof(uint32_t));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, mElementCount * sizeof(half));
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }

private:
    __aicore__ inline void CopyIn()
    {
        AscendC::LocalTensor<half> xLocal = inQueueX.AllocTensor<half>();
        AscendC::LocalTensor<uint32_t> yLocal = inQueueY.AllocTensor<uint32_t>();
        AscendC::DataCopy(xLocal, xGm, mElementCount);
        AscendC::DataCopy(yLocal, yGm, mElementCount);
        inQueueX.EnQue(xLocal);
        inQueueY.EnQue(yLocal);
    }
    __aicore__ inline void Compute()
    {
        AscendC::LocalTensor<half> xLocal = inQueueX.DeQue<half>();
        AscendC::LocalTensor<uint32_t> yLocal = inQueueY.DeQue<uint32_t>();
        AscendC::LocalTensor<half> zLocal = outQueueZ.AllocTensor<half>();
        for (uint32_t i = 0; i < mElementCount; ++i) {
            auto offset = yLocal.GetValue(i) / sizeof(T);
            auto srcValue = xLocal.GetValue(i);
            zLocal.SetValue(offset, srcValue);
        }
        outQueueZ.EnQue<half>(zLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueY.FreeTensor(yLocal);
    }
    __aicore__ inline void CopyOut()
    {
        AscendC::LocalTensor<half> zLocal = outQueueZ.DeQue<half>();
        AscendC::DataCopy(zGm, zLocal, mElementCount);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX, inQueueY;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::GlobalTensor<half> xGm;
    AscendC::GlobalTensor<uint32_t> yGm;
    AscendC::GlobalTensor<half> zGm;
    uint32_t mElementCount;
};
                                                                             
extern "C" __global__ __aicore__ void scatter_custom(GM_ADDR srcGm, GM_ADDR dstOffsetGm, GM_ADDR dstGm)   
{
    KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);
    uint32_t count = 128;                                                                                                
    KernelScatter<half> op;                                                                                
    op.Init(srcGm, dstOffsetGm, dstGm, count);                                                            
    op.Process();                                                                                         
}


int32_t main(int32_t argc, char* argv[])
{
    uint32_t numBlocks = 1;
    size_t inputByteSize = 128 * sizeof(uint16_t);
    size_t inputByteSize1 = 128 * sizeof(uint32_t);
    size_t outputByteSize = 128 * sizeof(uint16_t);
    
    int32_t deviceId = 0;
    aclrtSetDevice(deviceId);
    aclrtStream stream = nullptr;
    aclrtCreateStream(&stream);

    uint8_t *xHost, *yHost, *zHost;
    uint8_t *xDevice, *yDevice, *zDevice;

    aclrtMallocHost((void**)(&xHost), inputByteSize);
    aclrtMallocHost((void**)(&yHost), inputByteSize1);
    aclrtMallocHost((void**)(&zHost), outputByteSize);
    aclrtMalloc((void**)&xDevice, inputByteSize, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc((void**)&yDevice, inputByteSize1, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc((void**)&zDevice, outputByteSize, ACL_MEM_MALLOC_HUGE_FIRST);
   
    ReadFile("./input/input_x.bin", inputByteSize, xHost, inputByteSize);
    ReadFile("./input/input_y.bin", inputByteSize1, yHost, inputByteSize1);

    aclrtMemcpy(xDevice, inputByteSize, xHost, inputByteSize, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(yDevice, inputByteSize1, yHost, inputByteSize1, ACL_MEMCPY_HOST_TO_DEVICE);

    scatter_custom<<<numBlocks, nullptr, stream>>>(xDevice, yDevice, zDevice);
    aclrtSynchronizeStream(stream);

    aclrtMemcpy(zHost, outputByteSize, zDevice, outputByteSize, ACL_MEMCPY_DEVICE_TO_HOST);
    WriteFile("./output/output.bin", zHost, outputByteSize);

    aclrtFree(xDevice);
    aclrtFree(yDevice);
    aclrtFree(zDevice);
    aclrtFreeHost(xHost);
    aclrtFreeHost(yHost);
    aclrtFreeHost(zHost);

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

    return 0;
}