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

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

template <typename T>
class SliceData {
public:
    __aicore__ inline SliceData() {}
    __aicore__ inline void Init(__gm__ uint8_t* dstGm, __gm__ uint8_t* srcGm, AscendC::TPipe *pipeIn)
    {
        pipe = pipeIn;
        AscendC::SliceInfo srcSliceInfoIn[] = {{16, 70, 7, 3, 87}, {0, 2, 1, 1, 3}};
        AscendC::SliceInfo dstSliceInfoIn[] = {{0, 47, 0, 3, 48}, {0, 1, 0, 1, 2}};
        uint32_t dimValueIn = 2;
        uint32_t dstDataSize = 96;
        uint32_t srcDataSize = 261;
        dimValue = dimValueIn;
        for (uint32_t i = 0; i < dimValueIn; i++) {
            srcSliceInfo[i].startIndex = srcSliceInfoIn[i].startIndex;
            srcSliceInfo[i].endIndex = srcSliceInfoIn[i].endIndex;
            srcSliceInfo[i].stride = srcSliceInfoIn[i].stride;
            srcSliceInfo[i].burstLen = srcSliceInfoIn[i].burstLen;
            srcSliceInfo[i].shapeValue = srcSliceInfoIn[i].shapeValue;
            dstSliceInfo[i].startIndex = dstSliceInfoIn[i].startIndex;
            dstSliceInfo[i].endIndex = dstSliceInfoIn[i].endIndex;
            dstSliceInfo[i].stride = dstSliceInfoIn[i].stride;
            dstSliceInfo[i].burstLen = dstSliceInfoIn[i].burstLen;
            dstSliceInfo[i].shapeValue = dstSliceInfoIn[i].shapeValue;
        }
        srcGlobal.SetGlobalBuffer((__gm__ T *)srcGm);
        dstGlobal.SetGlobalBuffer((__gm__ T *)dstGm);
        pipeIn->InitBuffer(inQueueSrcVecIn, 1, dstDataSize * sizeof(T));
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        CopyOut();
    }
private:
    __aicore__ inline void CopyIn()
    {
        AscendC::LocalTensor<T> srcLocal = inQueueSrcVecIn.AllocTensor<T>();
        AscendC::DataCopy(srcLocal, srcGlobal,  dstSliceInfo, srcSliceInfo, dimValue);
        inQueueSrcVecIn.EnQue(srcLocal);
    }
    __aicore__ inline void CopyOut()
    {
        AscendC::LocalTensor<T> srcOutLocal = inQueueSrcVecIn.DeQue<T>();
        int32_t eventID = static_cast<int32_t>(GetTPipePtr()->FetchEventID(AscendC::HardEvent::MTE2_MTE3));
 	    AscendC::SetFlag<AscendC::HardEvent::MTE2_MTE3>(eventID);
 	    AscendC::WaitFlag<AscendC::HardEvent::MTE2_MTE3>(eventID);
        AscendC::DataCopy(dstGlobal, srcOutLocal, dstSliceInfo, dstSliceInfo, dimValue);
        inQueueSrcVecIn.FreeTensor(srcOutLocal);
    }

private:
    AscendC::TPipe *pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueSrcVecIn;
    AscendC::GlobalTensor<T> srcGlobal;
    AscendC::GlobalTensor<T> dstGlobal;
    AscendC::SliceInfo dstSliceInfo[AscendC::K_MAX_DIM];
    AscendC::SliceInfo srcSliceInfo[AscendC::K_MAX_DIM]; // K_MAX_DIM = 8
    uint32_t dimValue;
};

__vector__ __global__ __aicore__ void kernel_slice(GM_ADDR dstGm, GM_ADDR srcGm)
{
    AscendC::TPipe pipe;
    SliceData<float> op;
    op.Init(dstGm, srcGm, &pipe);
    op.Process();
}

int32_t main(int32_t argc, char* argv[]) {
  uint32_t numBlocks = 1;
  size_t inputByteSize = 3 * 87 * sizeof(float);
  size_t outputByteSize = 2 * 48 * sizeof(float);

  int32_t deviceId = 0;
  aclrtSetDevice(deviceId);
  aclrtStream stream = nullptr;
  aclrtCreateStream(&stream);

  uint8_t *xHost, *outHost;
  uint8_t *xDevice, *outDevice;

  aclrtMallocHost((void **)(&xHost), inputByteSize);
  aclrtMallocHost((void **)(&outHost), outputByteSize);
  aclrtMalloc((void **)&xDevice, inputByteSize, ACL_MEM_MALLOC_HUGE_FIRST);
  aclrtMalloc((void **)&outDevice, outputByteSize, ACL_MEM_MALLOC_HUGE_FIRST);

  ReadFile("./input/input_x.bin", inputByteSize, xHost, inputByteSize);

  aclrtMemcpy(xDevice, inputByteSize, xHost, inputByteSize,
              ACL_MEMCPY_HOST_TO_DEVICE);

  kernel_slice<<<numBlocks, nullptr, stream>>>(outDevice, xDevice);
  aclrtSynchronizeStream(stream);

  aclrtMemcpy(outHost, outputByteSize, outDevice, outputByteSize,
              ACL_MEMCPY_DEVICE_TO_HOST);
  WriteFile("./output/output.bin", outHost, outputByteSize);

  aclrtFree(xDevice);
  aclrtFree(outDevice);
  aclrtFreeHost(xHost);
  aclrtFreeHost(outHost);
  aclrtDestroyStream(stream);
  aclrtResetDevice(deviceId);
  aclFinalize();
  return 0;
}