/**
* Copyright (c) 2026 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 add.asc
* \brief 演示CAmodel仿真模式下的编译、运行与性能数据采集流程
*/
#include "data_utils.h"
#include "acl/acl.h"
constexpr uint32_t TOTAL_LENGTH = 48 * 256;
__global__ void add_custom(float* x, float* y, float* z, uint64_t total_length)
{
int32_t idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= total_length) {
return;
}
z[idx] = x[idx] + y[idx];
}
int32_t main(int32_t argc, char* argv[])
{
size_t input_byte_size = TOTAL_LENGTH * sizeof(float);
size_t output_byte_size = TOTAL_LENGTH * sizeof(float);
aclInit(nullptr);
int32_t deviceId = 0;
aclrtSetDevice(deviceId);
aclrtStream stream = nullptr;
aclrtCreateStream(&stream);
uint8_t* xHost = nullptr;
float* xDevice = nullptr;
aclrtMallocHost((void**)&xHost, input_byte_size);
aclrtMalloc((void**)&xDevice, input_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
ReadFile("./input/x_gm.bin", input_byte_size, xHost, input_byte_size);
aclrtMemcpy(xDevice, input_byte_size, xHost, input_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
uint8_t* yHost = nullptr;
float* yDevice = nullptr;
aclrtMallocHost((void**)&yHost, input_byte_size);
aclrtMalloc((void**)&yDevice, input_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
ReadFile("./input/y_gm.bin", input_byte_size, yHost, input_byte_size);
aclrtMemcpy(yDevice, input_byte_size, yHost, input_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
uint8_t* zHost = nullptr;
float* zDevice = nullptr;
aclrtMallocHost((void**)&zHost, output_byte_size);
aclrtMalloc((void**)&zDevice, output_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
uint32_t blocks_per_grid = 48;
uint32_t threads_per_block = 256;
uint32_t dyn_ubuf_size = 0;
add_custom<<<blocks_per_grid, threads_per_block, dyn_ubuf_size, stream>>>(xDevice, yDevice, zDevice, TOTAL_LENGTH);
aclrtSynchronizeStream(stream);
aclrtMemcpy(zHost, output_byte_size, zDevice, output_byte_size, ACL_MEMCPY_DEVICE_TO_HOST);
WriteFile("./output/output.bin", zHost, output_byte_size);
aclrtFree(xDevice);
aclrtFreeHost(xHost);
aclrtFree(yDevice);
aclrtFreeHost(yHost);
aclrtFree(zDevice);
aclrtFreeHost(zHost);
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}