/**
* 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 math.asc
* \brief
*/
#include <iostream>
#include <iterator>
#include <vector>
#include "acl/acl.h"
#include "tiling/platform/platform_ascendc.h"
#include "simt_api/math_functions.h"
__global__ void math_custom(float* x, float* y, float* z, uint64_t total_length)
{
// Calculate global thread ID
int32_t idx = blockIdx.x * blockDim.x + threadIdx.x;
// Maps to the row index of output tensor
if (idx >= total_length) {
return;
}
z[idx] = tanf(x[idx]);
z[idx] = tanhf(x[idx]);
z[idx] = atanf(x[idx]);
z[idx] = atanhf(x[idx]);
z[idx] = expf(x[idx]);
z[idx] = logf(x[idx]);
z[idx] = cosf(x[idx]);
z[idx] = coshf(x[idx]);
z[idx] = sinf(x[idx]);
z[idx] = sinhf(x[idx]);
}
std::vector<float> add(std::vector<float>& x, std::vector<float>& y)
{
size_t total_byte_size =x.size() * sizeof(float);
int32_t device_id = 0;
aclrtStream stream = nullptr;
uint8_t* x_host = reinterpret_cast<uint8_t *>(x.data());
uint8_t* y_host = reinterpret_cast<uint8_t *>(y.data());
uint8_t* z_host = nullptr;
float* x_device = nullptr;
float* y_device = nullptr;
float* z_device = nullptr;
// Init
aclInit(nullptr);
aclrtSetDevice(device_id);
aclrtCreateStream(&stream);
// Malloc memory in host and device
aclrtMallocHost((void **)(&z_host), total_byte_size);
aclrtMalloc((void **)&x_device, total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMalloc((void **)&y_device, total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMalloc((void **)&z_device, total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMemcpy(x_device, total_byte_size, x_host, total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
aclrtMemcpy(y_device, total_byte_size, y_host, total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
// Calc splite params
uint32_t block_num = 48;
uint32_t thread_num_per_block = 256;
uint32_t dyn_ubuf_size = 0; // No need to alloc dynamic memory.
// Call kernel funtion with <<<...>>>
math_custom<<<block_num, thread_num_per_block, dyn_ubuf_size, stream>>>(x_device, y_device, z_device, x.size());
aclrtSynchronizeStream(stream);
// Copy result from device to host
aclrtMemcpy(z_host, total_byte_size, z_device, total_byte_size, ACL_MEMCPY_DEVICE_TO_HOST);
std::vector<float> output((float *)z_host, (float *)(z_host + total_byte_size));
// Free memory
aclrtFree(x_device);
aclrtFree(y_device);
aclrtFree(z_device);
aclrtFreeHost(z_host);
// DeInt
aclrtDestroyStream(stream);
aclrtResetDevice(device_id);
aclFinalize();
return output;
}
uint32_t verify_result(std::vector<float>& output, std::vector<float>& golden)
{
auto print_tensor = [](std::vector<float>& tensor, const char* name) {
constexpr size_t max_print_size = 20;
std::cout << name << ": ";
std::copy(tensor.begin(), tensor.begin() + std::min(tensor.size(), max_print_size),
std::ostream_iterator<float>(std::cout, " "));
if (tensor.size() > max_print_size) {
std::cout << "...";
}
std::cout << std::endl;
};
print_tensor(output, "Output");
print_tensor(golden, "Golden");
if (std::equal(output.begin(), output.end(), golden.begin())) {
std::cout << "[Success] Case accuracy is verification passed." << std::endl;
return 0;
} else {
std::cout << "[Failed] Case accuracy is verification failed!" << std::endl;
return 1;
}
return 0;
}
int32_t main(int32_t argc, char* argv[])
{
constexpr uint32_t in_shape = 48 * 256;
std::vector<float> x(in_shape);
for (uint32_t i = 0; i < in_shape; i++) {
x[i] = i * 1.1f;
}
std::vector<float> y(in_shape);
for (uint32_t i = 0; i < in_shape; i++) {
y[i] = i + 3.4f;
}
std::vector<float> golden(in_shape);
for (uint32_t i = 0; i < in_shape; i++) {
golden[i] = x[i] + y[i];
}
std::vector<float> output = add(x, y);
return verify_result(output, golden);
}