aclnnTriangularSolve
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Supported
Function
- Description: Solves a system of equations with a square upper or lower triangular invertible matrix A and multiple right-hand sides b.
- Formula: Where is an upper triangular square matrix (or a lower triangular square matrix when upper is false), with no zero elements on its main diagonal. and are two-dimensional matrices or batches of two-dimensional matrices. When the input is a batch, the returned output is also the corresponding batch. When the main diagonal of contains zeros, or elements very close to zero, and unitriangular is false, the output may contain .
Function Prototype
Each operator uses a two-phase API. First call the aclnnTriangularSolveGetWorkspaceSize API to obtain the workspace size required for computation and the executor that contains the operator computation process. Then call the aclnnTriangularSolve API to execute the computation.
aclnnStatus aclnnTriangularSolveGetWorkspaceSize(
const aclTensor* self,
const aclTensor* A,
bool upper,
bool transpose,
bool unitriangular,
aclTensor* xOut,
aclTensor* mOut,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnTriangularSolve(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
const aclrtStream stream)aclnnTriangularSolveGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self (aclTensor*) Input b in the formula, the right-hand side of the equation. - The data type is consistent with A.
- self[-2]=A[-2].
- Except for the last two dimensions, the remaining dimensions of A and self satisfy the broadcast relationship.
FLOAT, DOUBLE, COMPLEX64, COMPLEX128 ND 2-8 β A (aclTensor*) Input A in the formula, the coefficient matrix. - Data types are consistent with self.
- The last two axes are equal.
- Except for the last two dimensions, the remaining dimensions of A and self satisfy the broadcast relationship.
FLOAT, DOUBLE, COMPLEX64, COMPLEX128 ND 2-8 β upper (bool) Input Computation attribute controlling whether A in the formula participates in the computation as an upper or lower triangular matrix. Defaults to true, indicating that A is an upper triangular square matrix. When upper is false, A is a lower triangular square matrix. BOOL - - - transpose (bool) Input Calculation attribute controlling whether A or AT is used in the formula. Defaults to false. When transpose is true, ATX=b is computed. BOOL - - - unitriangular(bool) Input Computation attribute controlling whether A in the formula is processed as a unitriangular matrix. - Defaults to false.
- When unitriangular is true, the main diagonal elements of A are treated as 1 instead of being referenced from A.
- When unitriangular is true, the data types of inputs self and A and outputs xOut and mOut support only FLOAT.
BOOL - - - xOut(aclTensor*) Output X in the formula, that is, the solution result of the equation. FLOAT, DOUBLE, COMPLEX64, COMPLEX128 ND - β mOut (aclTensor*) Output Copy of the upper triangle (lower triangle) of A after broadcast. - Data type consistent with self.
- Dimensions after A and self satisfy the broadcast relationship, with the last axis dim=A[-1].
FLOAT, DOUBLE, COMPLEX64, COMPLEX128 ND - β workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation process. - - - - - Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 Null pointer in self, A, xOut, or mOut. ACLNN_ERR_PARAM_INVALID 161002 The data type or data format of self, A, xOut, or mOut is out of the supported range. The shape of self, A, xOut, or mOut does not satisfy the constraints.
aclnnTriangularSolve
Parameters
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device. workspaceSize Input Workspace size obtained by the first-phase API aclnnTriangularSolveGetWorkspaceSize.executor Input Op executor containing the operator computation flow. stream Input Stream for task execution. Return Value
aclnnStatus: return status code. For details, see Error Codes.
Constraints
- Deterministic computation description:
aclnnTriangularSolvedefaults to a deterministic implementation.
Example
The sample code is as follows and is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_triangular_solve.h"
#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) {
// Boilerplate, resource initialization
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 host-side data to device-side memory.
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 a 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 aclCreateTensor to create an aclTensor.
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main() {
// 1. (Boilerplate) Initialize device/stream. For details, see the acl API manual.
// Set deviceId based on the actual device.
int32_t 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);
// 2. Construct the inputs and outputs. Customize the construction based on the API.
std::vector<int64_t> selfShape = {3, 1};
std::vector<int64_t> aShape = {3, 3};
std::vector<int64_t> xOutShape = {3, 1};
std::vector<int64_t> mOutShape = {3, 3};
void* selfDeviceAddr = nullptr;
void* aDeviceAddr = nullptr;
void* xOutDeviceAddr = nullptr;
void* mOutDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* A = nullptr;
aclTensor* xOut = nullptr;
aclTensor* mOut = nullptr;
bool upper = true;
bool transpose = false;
bool unitriangular = false;
std::vector<float> selfHostData = {1, 2, 3};
std::vector<float> aHostData = {1, 2, 3, 0, 4, 5, 0, 0, 6};
std::vector<float> xOutHostData = {-0.2500, -0.1250, 0.5000};
std::vector<float> mOutHostData = {1, 2, 3, 0, 4, 5, 0, 0, 6};
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the A aclTensor.
ret = CreateAclTensor(aHostData, aShape, &aDeviceAddr, aclDataType::ACL_FLOAT, &A);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the xOut aclTensor.
ret = CreateAclTensor(xOutHostData, xOutShape, &xOutDeviceAddr, aclDataType::ACL_FLOAT, &xOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the mOut aclTensor.
ret = CreateAclTensor(mOutHostData, mOutShape, &mOutDeviceAddr, aclDataType::ACL_FLOAT, &mOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace it with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the aclnnTriangularSolve first-phase API.
ret = aclnnTriangularSolveGetWorkspaceSize(self, A, upper, transpose, unitriangular, xOut, mOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTriangularSolveGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize computed by the first-phase API.
void* workspaceAddr = nullptr;
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
// Call the aclnnTriangularSolve second-phase API.
ret = aclnnTriangularSolve(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTriangularSolve failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Synchronize and wait for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output value. Copy the result from device memory to the host. Modify it based on the API definition.
auto xSize = GetShapeSize(xOutShape);
std::vector<float> resultData(xSize, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), xOutDeviceAddr,
xSize * sizeof(resultData[0]), 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);
for (int64_t i = 0; i < xSize; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
}
auto mSize = GetShapeSize(mOutShape);
std::vector<float> mResultData(mSize, 0);
ret = aclrtMemcpy(mResultData.data(), mResultData.size() * sizeof(mResultData[0]), mOutDeviceAddr,
mSize * sizeof(mResultData[0]), 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);
for (int64_t i = 0; i < mSize; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, mResultData[i]);
}
// 6. Release aclTensor and aclScalar. Modify it based on the API definition.
aclDestroyTensor(self);
aclDestroyTensor(A);
aclDestroyTensor(xOut);
aclDestroyTensor(mOut);
// 7. Release device resources. Modify it based on the API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(aDeviceAddr);
aclrtFree(xOutDeviceAddr);
aclrtFree(mOutDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}