Dot
Security statement:
This example provides a minimal implementation for quickly developing and debugging operators. It focuses on demonstrating core functionality through simple code rather than providing production-ready security.
This sample code is for demonstration purposes only. You assume all responsibility for security risks if this code is implemented in a live service environment.
asdBlasSdot
The following is an example of calling the asdBlasSdot operator:
#include <iostream>
#include <vector>
#include "asdsip.h"
#include "acl/acl.h"
#include "acl_meta.h"
using namespace AsdSip;
#define ASD_STATUS_CHECK(err) \
do { \
AsdSip::AspbStatus err_ = (err); \
if (err_ != AsdSip::ErrorType::ACL_SUCCESS) { \
std::cout << "Execute failed." << std::endl; \
exit(-1); \
} \
} while (0)
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do { \
printf(message, ##__VA_ARGS__); \
} while (0)
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)
{
// Initialize ACL. This code is written in a fixed format.
auto ret = aclInit(nullptr);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetDevice(deviceId);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
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);
// Call aclrtMalloc to allocate memory on the device.
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
// Call aclrtMemcpy to copy the data on the host to the memory on the device.
ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
// Compute the strides of the contiguous tensor.
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];
}
// Call the aclCreateTensor API to create an ACL tensor.
*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)
{
// Set the device ID used by the operator.
int deviceId = 0;
// Create an execution stream. This code is written in a fixed format.
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// Create the tensor data on the host.
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;
// Create input/output tensors.
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;
ret = CreateAclTensor(tensorInXData, xShape, &inputXDeviceAddr, aclDataType::ACL_FLOAT, &inputX);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
ret = CreateAclTensor(tensorInYData, yShape, &inputYDeviceAddr, aclDataType::ACL_FLOAT, &inputY);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
ret = CreateAclTensor(resultData, resultShape, &resultDeviceAddr, aclDataType::ACL_FLOAT, &result);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
// Create an operator execution handle.
asdBlasHandle handle;
asdBlasCreate(handle);
// Create a workspace required for operator execution.
size_t lwork = 0;
void *buffer = nullptr;
asdBlasMakeDotPlan(handle);
asdBlasGetWorkspaceSize(handle, lwork);
std::cout << "lwork = " << lwork << std::endl;
if (lwork > 0) {
ret = aclrtMalloc(&buffer, static_cast<int64_t>(lwork), ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
asdBlasSetWorkspace(handle, buffer);
// Configure the operator execution information.
asdBlasSetStream(handle, stream);
// Call the API to execute the operator using the fixed calling logic.
ASD_STATUS_CHECK(asdBlasSdot(handle, n, inputX, incx, inputY, incy, result));
asdBlasSynchronize(handle);
// Destroy the operator handle after the operator is scheduled.
asdBlasDestroy(handle);
// Copy the output tensor data on the device to the host memory.
ret = aclrtMemcpy(resultData.data(),
resultSize * sizeof(float),
resultDeviceAddr,
resultSize * sizeof(float),
ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
std::cout << "------- result -------" << std::endl;
for (int64_t i = 0; i < 1; i++) {
std::cout << resultData[i] << " ";
}
std::cout << std::endl;
std::cout << "Execute successfully." << std::endl;
// Release the resources.
aclDestroyTensor(inputX);
aclDestroyTensor(inputY);
aclDestroyTensor(result);
aclrtFree(inputXDeviceAddr);
aclrtFree(inputYDeviceAddr);
aclrtFree(resultDeviceAddr);
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}
asdBlasCdotu
The following is an example of calling the asdBlasCdotu operator:
#include <iostream>
#include <vector>
#include <cmath>
#include <random>
#include <complex>
#include "asdsip.h"
#include "acl/acl.h"
#include "acl_meta.h"
using namespace AsdSip;
#define ASD_STATUS_CHECK(err) \
do { \
AsdSip::AspbStatus err_ = (err); \
if (err_ != AsdSip::ErrorType::ACL_SUCCESS) { \
std::cout << "Execute failed." << std::endl; \
exit(-1); \
} else { \
std::cout << "Execute successfully." << std::endl; \
} \
} while (0)
void printTensor(const std::complex<float> *tensorData, int64_t tensorSize)
{
for (int64_t i = 0; i < tensorSize; i++) {
std::cout << tensorData[i] << " ";
}
std::cout << std::endl;
}
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do { \
printf(message, ##__VA_ARGS__); \
} while (0)
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)
{
// Initialize ACL. This code is written in a fixed format.
auto ret = aclInit(nullptr);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetDevice(deviceId);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
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);
// Call aclrtMalloc to allocate memory on the device.
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
// Call aclrtMemcpy to copy the data on the host to the memory on the device.
ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
// Compute the strides of the contiguous tensor.
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];
}
// Call the aclCreateTensor API to create an ACL tensor.
*tensor = aclCreateTensor(shape.data(),
shape.size(),
dataType,
strides.data(),
0,
aclFormat::ACL_FORMAT_ND,
shape.data(),
shape.size(),
*deviceAddr);
return 0;
}
void printTensor(std::vector<std::complex<float>> tensorData, int64_t tensorSize)
{
for (int64_t i = 0; i < tensorSize; i++) {
std::cout << tensorData[i] << " ";
}
std::cout << std::endl;
}
int main(int argc, char **argv)
{
int deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
int64_t n = 8;
int64_t xSize = 8;
int64_t ySize = 8;
std::vector<std::complex<float>> tensorInXData;
tensorInXData.reserve(xSize);
for (int64_t i = 0; i < xSize; i++) {
tensorInXData[i] = {2.0, (float)(1.0 + i)};
}
std::vector<std::complex<float>> tensorInYData;
tensorInYData.reserve(ySize);
for (int64_t i = 0; i < ySize; i++) {
tensorInYData[i] = {3.0, 4.0};
}
int64_t resultSize = 1;
std::vector<std::complex<float>> resultData;
resultData.reserve(resultSize);
std::cout << "------- input TensorInX -------" << std::endl;
printTensor(tensorInXData.data(), xSize);
std::cout << "------- input TensorInY -------" << std::endl;
printTensor(tensorInYData.data(), ySize);
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;
ret = CreateAclTensor(tensorInXData, xShape, &inputXDeviceAddr, aclDataType::ACL_COMPLEX64, &inputX);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
ret = CreateAclTensor(tensorInYData, yShape, &inputYDeviceAddr, aclDataType::ACL_COMPLEX64, &inputY);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
ret = CreateAclTensor(resultData, resultShape, &resultDeviceAddr, aclDataType::ACL_COMPLEX64, &result);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
asdBlasHandle handle;
asdBlasCreate(handle);
size_t lwork = 0;
void *buffer = nullptr;
asdBlasMakeDotPlan(handle);
asdBlasGetWorkspaceSize(handle, lwork);
std::cout << "lwork = " << lwork << std::endl;
if (lwork > 0) {
ret = aclrtMalloc(&buffer, static_cast<int64_t>(lwork), ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
asdBlasSetWorkspace(handle, buffer);
asdBlasSetStream(handle, stream);
ASD_STATUS_CHECK(asdBlasCdotu(handle, n, inputX, 1, inputY, 1, result));
asdBlasSynchronize(handle);
asdBlasDestroy(handle);
ret = aclrtMemcpy(resultData.data(),
resultSize * sizeof(std::complex<float>),
resultDeviceAddr,
resultSize * sizeof(std::complex<float>),
ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
std::cout << "------- result -------" << std::endl;
printTensor(resultData.data(), resultSize);
aclDestroyTensor(inputX);
aclDestroyTensor(inputY);
aclDestroyTensor(result);
aclrtFree(inputXDeviceAddr);
aclrtFree(inputYDeviceAddr);
aclrtFree(resultDeviceAddr);
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}asdBlasCdotc
The following is an example of calling the asdBlasCdotc operator:
#include <iostream>
#include <vector>
#include <complex>
#include <cmath>
#include <random>
#include "asdsip.h"
#include "acl/acl.h"
#include "acl_meta.h"
using namespace AsdSip;
#define ASD_STATUS_CHECK(err) \
do { \
AsdSip::AspbStatus err_ = (err); \
if (err_ != AsdSip::ErrorType::ACL_SUCCESS) { \
std::cout << "Execute failed." << std::endl; \
exit(-1); \
} else { \
std::cout << "Execute successfully." << std::endl; \
} \
} while (0)
void printTensor(const std::complex<float> *tensorData, int64_t tensorSize)
{
for (int64_t i = 0; i < tensorSize; i++) {
std::cout << tensorData[i] << " ";
}
std::cout << std::endl;
}
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do { \
printf(message, ##__VA_ARGS__); \
} while (0)
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)
{
// Initialize ACL. This code is written in a fixed format.
auto ret = aclInit(nullptr);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetDevice(deviceId);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
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);
// Call aclrtMalloc to allocate memory on the device.
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
// Call aclrtMemcpy to copy the data on the host to the memory on the device.
ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
// Compute the strides of the contiguous tensor.
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];
}
// Call the aclCreateTensor API to create an ACL tensor.
*tensor = aclCreateTensor(shape.data(),
shape.size(),
dataType,
strides.data(),
0,
aclFormat::ACL_FORMAT_ND,
shape.data(),
shape.size(),
*deviceAddr);
return 0;
}
void printTensor(std::vector<std::complex<float>> tensorData, int64_t tensorSize)
{
for (int64_t i = 0; i < tensorSize; i++) {
std::cout << tensorData[i] << " ";
}
std::cout << std::endl;
}
int main(int argc, char **argv)
{
int deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
int64_t n = 8;
int64_t xSize = 8;
int64_t ySize = 8;
std::vector<std::complex<float>> tensorInXData;
tensorInXData.reserve(xSize);
for (int64_t i = 0; i < xSize; i++) {
tensorInXData[i] = {2.0, (float)(1.0 + i)};
}
std::vector<std::complex<float>> tensorInYData;
tensorInYData.reserve(ySize);
for (int64_t i = 0; i < ySize; i++) {
tensorInYData[i] = {3.0, 4.0};
}
int64_t resultSize = 1;
std::vector<std::complex<float>> resultData;
resultData.reserve(resultSize);
std::cout << "------- input TensorInX -------" << std::endl;
printTensor(tensorInXData.data(), xSize);
std::cout << "------- input TensorInY -------" << std::endl;
printTensor(tensorInYData.data(), ySize);
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;
ret = CreateAclTensor(tensorInXData, xShape, &inputXDeviceAddr, aclDataType::ACL_COMPLEX64, &inputX);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
ret = CreateAclTensor(tensorInYData, yShape, &inputYDeviceAddr, aclDataType::ACL_COMPLEX64, &inputY);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
ret = CreateAclTensor(resultData, resultShape, &resultDeviceAddr, aclDataType::ACL_COMPLEX64, &result);
CHECK_RET(ret == ::ACL_SUCCESS, return ret);
asdBlasHandle handle;
asdBlasCreate(handle);
size_t lwork = 0;
void *buffer = nullptr;
asdBlasMakeDotPlan(handle);
asdBlasGetWorkspaceSize(handle, lwork);
std::cout << "lwork = " << lwork << std::endl;
if (lwork > 0) {
ret = aclrtMalloc(&buffer, static_cast<int64_t>(lwork), ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
asdBlasSetWorkspace(handle, buffer);
asdBlasSetStream(handle, stream);
ASD_STATUS_CHECK(asdBlasCdotc(handle, n, inputX, 1, inputY, 1, result));
asdBlasSynchronize(handle);
asdBlasDestroy(handle);
ret = aclrtMemcpy(resultData.data(),
resultSize * sizeof(std::complex<float>),
resultDeviceAddr,
resultSize * sizeof(std::complex<float>),
ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ::ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
std::cout << "------- result -------" << std::endl;
printTensor(resultData.data(), resultSize);
aclDestroyTensor(inputX);
aclDestroyTensor(inputY);
aclDestroyTensor(result);
aclrtFree(inputXDeviceAddr);
aclrtFree(inputYDeviceAddr);
aclrtFree(resultDeviceAddr);
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}
Parent topic: BLAS