* 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.
*/
#include <iostream>
#include <vector>
#include "asdsip.h"
#include "acl/acl.h"
#include "acl_meta.h"
using namespace AsdSip;
int64_t GetShapeSize(const std::vector<int64_t> &shape)
{
int64_t shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
int Init(int32_t deviceId, aclrtStream *stream)
{
aclInit(nullptr);
aclrtSetDevice(deviceId);
aclrtCreateStream(stream);
return 0;
}
template <typename T>
int CreateAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> &shape, void **deviceAddr,
aclDataType dataType, aclTensor **tensor)
{
auto size = GetShapeSize(shape) * sizeof(T);
aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
*tensor = aclCreateTensor(shape.data(),
shape.size(),
dataType,
strides.data(),
0,
aclFormat::ACL_FORMAT_ND,
shape.data(),
shape.size(),
*deviceAddr);
return 0;
}
int main(int argc, char **argv)
{
int deviceId = 0;
aclrtStream stream;
Init(deviceId, &stream);
int64_t n = 5;
int64_t incx = 1;
int64_t incy = 1;
int64_t xSize = 5;
std::vector<float> tensorInXData;
tensorInXData.reserve(xSize);
for (int64_t i = 0; i < xSize; i++) {
tensorInXData[i] = 1.0 + i;
}
int64_t ySize = 5;
std::vector<float> tensorInYData;
tensorInYData.reserve(xSize);
for (int64_t i = 0; i < ySize; i++) {
tensorInYData[i] = 10.0 + i;
}
int64_t resultSize = 1;
std::vector<float> resultData;
resultData.reserve(resultSize);
std::cout << "------- input x -------" << std::endl;
for (int64_t i = 0; i < xSize; i++) {
std::cout << tensorInXData[i] << " ";
}
std::cout << std::endl;
std::cout << "------- input y -------" << std::endl;
for (int64_t i = 0; i < ySize; i++) {
std::cout << tensorInYData[i] << " ";
}
std::cout << std::endl;
std::vector<int64_t> xShape = {xSize};
std::vector<int64_t> yShape = {ySize};
std::vector<int64_t> resultShape = {resultSize};
aclTensor *inputX = nullptr;
aclTensor *inputY = nullptr;
aclTensor *result = nullptr;
void *inputXDeviceAddr = nullptr;
void *inputYDeviceAddr = nullptr;
void *resultDeviceAddr = nullptr;
CreateAclTensor(tensorInXData, xShape, &inputXDeviceAddr, aclDataType::ACL_FLOAT, &inputX);
CreateAclTensor(tensorInYData, yShape, &inputYDeviceAddr, aclDataType::ACL_FLOAT, &inputY);
CreateAclTensor(resultData, resultShape, &resultDeviceAddr, aclDataType::ACL_FLOAT, &result);
asdBlasHandle handle;
asdBlasCreate(handle);
size_t lwork = 0;
void *buffer = nullptr;
asdBlasMakeDotPlan(handle);
asdBlasGetWorkspaceSize(handle, lwork);
if (lwork > 0) {
aclrtMalloc(&buffer, static_cast<int64_t>(lwork), ACL_MEM_MALLOC_HUGE_FIRST);
}
asdBlasSetWorkspace(handle, buffer);
asdBlasSetStream(handle, stream);
asdBlasSdot(handle, n, inputX, incx, inputY, incy, result);
asdBlasSynchronize(handle);
asdBlasDestroy(handle);
aclrtMemcpy(resultData.data(),
resultSize * sizeof(float),
resultDeviceAddr,
resultSize * sizeof(float),
ACL_MEMCPY_DEVICE_TO_HOST);
std::cout << "------- result -------" << std::endl;
for (int64_t i = 0; i < 1; i++) {
std::cout << resultData[i] << " ";
}
std::cout << std::endl;
aclDestroyTensor(inputX);
aclDestroyTensor(inputY);
aclDestroyTensor(result);
aclrtFree(inputXDeviceAddr);
aclrtFree(inputYDeviceAddr);
aclrtFree(resultDeviceAddr);
if (lwork > 0) {
aclrtFree(buffer);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}