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

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

constexpr int32_t USE_CORE_NUM = 1; // num of core used
constexpr int32_t TOTAL_LENGTH = USE_CORE_NUM * 512;
constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM;  // length computed of each core
constexpr int32_t TILE_NUM = 1;  
constexpr int32_t BUFFER_NUM = 1; 
constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM; 

template <class T, class U>
class KernelCast {
public:
  __aicore__ inline KernelCast() {}
  __aicore__ inline void Init(GM_ADDR x, GM_ADDR out, AscendC::TPipe* pipeIn) 
  {
    pipe = pipeIn;
    this->blockLength = BLOCK_LENGTH;
    this->tileNum = TILE_NUM;
    ASSERT(tileNum != 0 && "tile num can not be zero!");
    this->tileLength = TILE_LENGTH;
    
    xGm.SetGlobalBuffer((__gm__ T *)x +
                            this->blockLength * AscendC::GetBlockIdx(),
                        this->blockLength);
    outGm.SetGlobalBuffer((__gm__ U *)out +
                              this->blockLength * AscendC::GetBlockIdx(),
                          this->blockLength);
    pipe->InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(T));
    pipe->InitBuffer(outQueueOUT, BUFFER_NUM, this->tileLength * sizeof(U));
  }
  __aicore__ inline void Process() 
  {
    int32_t loopCount = this->tileNum * BUFFER_NUM;
    for (int32_t i = 0; i < loopCount; i++) {
      CopyIn(i);
      Compute(i);
      CopyOut(i);
    }
  }

private:
  __aicore__ inline void CopyIn(int32_t progress) 
  {
    AscendC::LocalTensor<T> xLocal = inQueueX.AllocTensor<T>();
    AscendC::DataCopy(xLocal, xGm[progress * this->tileLength],
                      this->tileLength);
    inQueueX.EnQue(xLocal);
  }
  __aicore__ inline void Compute(int32_t progress) 
  {
    AscendC::LocalTensor<T> xLocal = inQueueX.DeQue<T>();
    AscendC::LocalTensor<U> outLocal = outQueueOUT.AllocTensor<U>();
    AscendC::Cast(outLocal, xLocal, AscendC::RoundMode::CAST_CEIL, this->tileLength);
    outQueueOUT.EnQue<U>(outLocal);
    inQueueX.FreeTensor(xLocal);
  }
  __aicore__ inline void CopyOut(int32_t progress) 
  {
    AscendC::LocalTensor<U> outLocal = outQueueOUT.DeQue<U>();
    AscendC::DataCopy(outGm[progress * this->tileLength], outLocal,
                      this->tileLength);
    outQueueOUT.FreeTensor(outLocal);
  }

private:
  AscendC::TPipe* pipe;
  AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueX;
  AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> outQueueOUT;
  AscendC::GlobalTensor<T> xGm;
  AscendC::GlobalTensor<U> outGm;
  uint32_t blockLength;
  uint32_t tileNum;
  uint32_t tileLength;
};

__global__ __aicore__ void cast_custom(GM_ADDR x, GM_ADDR out) 
{
  KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);
  AscendC::TPipe pipe;
  KernelCast<half, int32_t> op;
  op.Init(x, out, &pipe);
  op.Process();
}

int32_t main(int32_t argc, char* argv[]) 
{
  uint32_t numBlocks = USE_CORE_NUM;
  size_t inputByteSize = TOTAL_LENGTH * sizeof(half);
  size_t outputByteSize = TOTAL_LENGTH * sizeof(int32_t);

  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);
  cast_custom<<<numBlocks, nullptr, stream>>>(xDevice, outDevice);
  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;
}