/**
* 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 brcb.asc
* \brief
*/
#include "acl/acl.h"
#include "data_utils.h"
#include "kernel_operator.h"
class KernelBrcb {
public:
__aicore__ inline KernelBrcb()
{}
__aicore__ inline void Init(__gm__ uint8_t *x, __gm__ uint8_t *y, AscendC::TPipe* pipeIn)
{
pipe = pipeIn;
x_gm.SetGlobalBuffer(reinterpret_cast<__gm__ uint16_t *>(x));
y_gm.SetGlobalBuffer(reinterpret_cast<__gm__ uint16_t *>(y));
pipe->InitBuffer(vecIn, 1, 16 * sizeof(uint16_t));
pipe->InitBuffer(vecOut, 1, 256 * sizeof(uint16_t));
}
__aicore__ inline void Process()
{
CopyIn();
Compute();
CopyOut();
}
__aicore__ inline void CopyIn()
{
auto x_buf = vecIn.AllocTensor<uint16_t>();
AscendC::DataCopy(x_buf, x_gm, 16);
vecIn.EnQue(x_buf);
}
__aicore__ inline void Compute()
{
auto x_buf = vecIn.DeQue<uint16_t>();
auto y_buf = vecOut.AllocTensor<uint16_t>();
AscendC::Brcb(y_buf, x_buf, 2, {1,8});
vecOut.EnQue(y_buf);
vecIn.FreeTensor(x_buf);
}
__aicore__ inline void CopyOut()
{
auto y_buf = vecOut.DeQue<uint16_t>();
AscendC::DataCopy(y_gm, y_buf, 256);
vecOut.FreeTensor(y_buf);
}
private:
AscendC::GlobalTensor<uint16_t> x_gm;
AscendC::GlobalTensor<uint16_t> y_gm;
AscendC::TPipe* pipe;
AscendC::TQue<AscendC::TPosition::VECIN, 1> vecIn;
AscendC::TQue<AscendC::TPosition::VECOUT, 1> vecOut;
};
__global__ __aicore__ void brcb_custom(GM_ADDR x, GM_ADDR y)
{
KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);
KernelBrcb op;
AscendC::TPipe pipe;
op.Init(x, y, &pipe);
op.Process();
}
int32_t main(int32_t argc, char* argv[])
{
size_t inputByteSize = 16 * sizeof(uint16_t);
size_t outputByteSize = 256 * sizeof(uint16_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);
brcb_custom<<<1, 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;
}