{OperatorName} Operator
Operator Overview
{A one-paragraph description of the operator's functional positioning and core operation. For example: "The {op} operator implements {FunctionDescription}, and the core operation is {OperationDescription}."}
Mathematical expression:
{Mathematical formula, in plain text. LaTeX format ($$...$$) may be used if needed}
It contains the following interfaces:
| Interface name | Brief description |
|---|---|
| aclsparseS{op} | {Single-precision function description} |
| aclsparseD{op} | {Double-precision function description} |
Operator Execution Interfaces
aclsparseS{op}
Product support
- Ascend 950PR&950DT products: {supported/not supported}
- Atlas A3 products: {supported/not supported}
- Atlas A2 products: {supported/not supported}
Function prototype
aclsparseStatus_t aclsparseS{op}(aclsparseHandle_t handle, int m, int n, const float *A, ...)
Parameter description
| Parameter | Input/Output | Type | Description |
|---|---|---|---|
| handle | Input | aclsparseHandle_t | ops-sparse library context handle, carrying the stream; host memory |
| m | Input | int | {ParameterMeaning}; host memory |
| A | Input | const float* (FP32) | {ParameterMeaning}; device memory |
Constraints
- m >= 0
- n >= 0
{If there are no constraints, write "None"; leaving it blank is not allowed.}
Supported sparse formats
{If the operator involves sparse matrix formats (CSR/COO/CSC, etc.), list the support status in the following table; if not involved, remove this section.}
| Format | Supported | Description |
|---|---|---|
| CSR | {✅/❌} | {Description} |
| COO | {✅/❌} | {Description} |
| CSC | {✅/❌} | {Description} |
Invocation example
The example code is as follows, for reference only. For the detailed build and execution process, see Build and Run Example.
#include <cstdio>
#include <memory>
#include <vector>
#include "acl/acl.h"
#include "cann_ops_sparse.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)
class AclContext {
public:
explicit AclContext(int32_t deviceId) : deviceId_(deviceId) {}
~AclContext()
{
if (stream_ != nullptr) {
aclrtDestroyStream(stream_);
stream_ = nullptr;
}
if (deviceSet_) {
aclrtResetDevice(deviceId_);
deviceSet_ = false;
}
if (aclInited_) {
aclFinalize();
aclInited_ = false;
}
}
int Init()
{
auto ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
aclInited_ = true;
ret = aclrtSetDevice(deviceId_);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
deviceSet_ = true;
ret = aclrtCreateStream(&stream_);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
return ACL_SUCCESS;
}
aclrtStream Stream() const { return stream_; }
private:
int32_t deviceId_;
aclrtStream stream_ = nullptr;
bool aclInited_ = false;
bool deviceSet_ = false;
};
int aclsparseS{op}Test(AclContext& ctx)
{
aclrtStream stream = ctx.Stream();
// 1. Create the ops-sparse handle
aclsparseHandle_t rawHandle = nullptr;
auto sparseRet = aclsparseCreate(&rawHandle);
CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseCreate failed. ERROR: %d\n", sparseRet);
return sparseRet);
std::unique_ptr<aclsparseContext, aclsparseStatus_t (*)(aclsparseHandle_t)> handlePtr(rawHandle, aclsparseDestroy);
sparseRet = aclsparseSetStream(static_cast<aclsparseHandle_t>(handlePtr.get()), stream);
CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseSetStream failed. ERROR: %d\n", sparseRet);
return sparseRet);
// 2. Set the PointerMode (required if the operator has scalar pointer parameters such as alpha and beta; otherwise it can be omitted)
// sparseRet = aclsparseSetPointerMode(static_cast<aclsparseHandle_t>(handlePtr.get()), ACL_SPARSE_POINTER_MODE_HOST);
// CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseSetPointerMode failed. ERROR: %d\n", sparseRet);
// return sparseRet);
// 3. Create the MatDescr (create and configure it if the operator requires aclsparseMatDescr_t; otherwise it can be omitted)
// aclsparseMatDescr_t matDescr = nullptr;
// sparseRet = aclsparseCreateMatDescr(&matDescr);
// CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseCreateMatDescr failed. ERROR: %d\n", sparseRet);
// return sparseRet);
// aclsparseSetMatType(matDescr, ACL_SPARSE_MATRIX_TYPE_GENERAL);
// aclsparseSetMatIndexBase(matDescr, ACL_SPARSE_INDEX_BASE_ZERO);
// 4. Prepare the host data
// {Define the parameters and initialize the host data according to the operator's requirements, such as std::vector<float> hA(m * n, 0.0f);}
// 5. Allocate device memory and copy the data
// {Use aclrtMalloc + std::unique_ptr + aclrtFree to manage device memory as required by the operator}
// {Use aclrtMemcpy to copy the data to the device as required by the operator}
// 6. Invoke aclsparseS{op}
// sparseRet = aclsparseS{op}(static_cast<aclsparseHandle_t>(handlePtr.get()), ...);
// CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseS{op} failed. ERROR: %d\n", sparseRet);
// return sparseRet);
// 7. Synchronize and wait for the task to finish
auto aclRet = aclrtSynchronizeStream(stream);
CHECK_RET(aclRet == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", aclRet); return aclRet);
// 8. Copy the results from the device back to the host and print them
// {Use aclrtMemcpy to copy the results back to the host and print them for verification, as required by the operator}
// 9. Clean up the MatDescr (if created in Step 3)
// aclsparseDestroyMatDescr(matDescr);
return ACL_SPARSE_STATUS_SUCCESS;
}
int main()
{
AclContext ctx(0);
auto ret = ctx.Init();
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = aclsparseS{op}Test(ctx);
CHECK_RET(ret == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseS{op}Test failed. ERROR: %d\n", ret); return ret);
return 0;
}
The expected output is as follows:
{Fill in according to the operator's actual output, for example:}
result[0] is: xxx
result[1] is: xxx
aclsparseD{op}
Product support
- Ascend 950PR&950DT products: {supported/not supported}
- Atlas A3 products: {supported/not supported}
- Atlas A2 products: {supported/not supported}
Function prototype
aclsparseStatus_t aclsparseD{op}(aclsparseHandle_t handle, int m, int n, const double *A, ...)
Parameter description
| Parameter | Input/Output | Type | Description |
|---|---|---|---|
| handle | Input | aclsparseHandle_t | ops-sparse library context handle, carrying the stream; host memory |
| m | Input | int | {ParameterMeaning}; host memory |
| A | Input | const double* (FP64) | {ParameterMeaning}; device memory |
Constraints
- m >= 0
- n >= 0
{If there are no constraints, write "None"; leaving it blank is not allowed.}
Supported sparse formats
{If the operator involves sparse matrix formats (CSR/COO/CSC, etc.), list the support status in the following table; if not involved, remove this section.}
| Format | Supported | Description |
|---|---|---|
| CSR | {✅/❌} | {Description} |
| COO | {✅/❌} | {Description} |
| CSC | {✅/❌} | {Description} |
Invocation example
The example code is as follows, for reference only. For the detailed build and execution process, see Build and Run Example.
#include <cstdio>
#include <memory>
#include <vector>
#include "acl/acl.h"
#include "cann_ops_sparse.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)
class AclContext {
public:
explicit AclContext(int32_t deviceId) : deviceId_(deviceId) {}
~AclContext()
{
if (stream_ != nullptr) {
aclrtDestroyStream(stream_);
stream_ = nullptr;
}
if (deviceSet_) {
aclrtResetDevice(deviceId_);
deviceSet_ = false;
}
if (aclInited_) {
aclFinalize();
aclInited_ = false;
}
}
int Init()
{
auto ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
aclInited_ = true;
ret = aclrtSetDevice(deviceId_);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
deviceSet_ = true;
ret = aclrtCreateStream(&stream_);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
return ACL_SUCCESS;
}
aclrtStream Stream() const { return stream_; }
private:
int32_t deviceId_;
aclrtStream stream_ = nullptr;
bool aclInited_ = false;
bool deviceSet_ = false;
};
int aclsparseD{op}Test(AclContext& ctx)
{
aclrtStream stream = ctx.Stream();
// 1. Create the ops-sparse handle
aclsparseHandle_t rawHandle = nullptr;
auto sparseRet = aclsparseCreate(&rawHandle);
CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseCreate failed. ERROR: %d\n", sparseRet);
return sparseRet);
std::unique_ptr<aclsparseContext, aclsparseStatus_t (*)(aclsparseHandle_t)> handlePtr(rawHandle, aclsparseDestroy);
sparseRet = aclsparseSetStream(static_cast<aclsparseHandle_t>(handlePtr.get()), stream);
CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseSetStream failed. ERROR: %d\n", sparseRet);
return sparseRet);
// 2. Set the PointerMode (required if the operator has scalar pointer parameters such as alpha and beta; otherwise it can be omitted)
// sparseRet = aclsparseSetPointerMode(static_cast<aclsparseHandle_t>(handlePtr.get()), ACL_SPARSE_POINTER_MODE_HOST);
// CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseSetPointerMode failed. ERROR: %d\n", sparseRet);
// return sparseRet);
// 3. Create the MatDescr (create and configure it if the operator requires aclsparseMatDescr_t; otherwise it can be omitted)
// aclsparseMatDescr_t matDescr = nullptr;
// sparseRet = aclsparseCreateMatDescr(&matDescr);
// CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseCreateMatDescr failed. ERROR: %d\n", sparseRet);
// return sparseRet);
// aclsparseSetMatType(matDescr, ACL_SPARSE_MATRIX_TYPE_GENERAL);
// aclsparseSetMatIndexBase(matDescr, ACL_SPARSE_INDEX_BASE_ZERO);
// 4. Prepare the host data
// {Define the parameters and initialize the host data according to the operator's requirements, such as std::vector<double> hA(m * n, 0.0);}
// 5. Allocate device memory and copy the data
// {Use aclrtMalloc + std::unique_ptr + aclrtFree to manage device memory as required by the operator}
// {Use aclrtMemcpy to copy the data to the device as required by the operator}
// 6. Invoke aclsparseD{op}
// sparseRet = aclsparseD{op}(static_cast<aclsparseHandle_t>(handlePtr.get()), ...);
// CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseD{op} failed. ERROR: %d\n", sparseRet);
// return sparseRet);
// 7. Synchronize and wait for the task to finish
auto aclRet = aclrtSynchronizeStream(stream);
CHECK_RET(aclRet == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", aclRet); return aclRet);
// 8. Copy the results from the device back to the host and print them
// {Use aclrtMemcpy to copy the results back to the host and print them for verification, as required by the operator}
// 9. Clean up the MatDescr (if created in Step 3)
// aclsparseDestroyMatDescr(matDescr);
return ACL_SPARSE_STATUS_SUCCESS;
}
int main()
{
AclContext ctx(0);
auto ret = ctx.Init();
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = aclsparseD{op}Test(ctx);
CHECK_RET(ret == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseD{op}Test failed. ERROR: %d\n", ret); return ret);
return 0;
}
The expected output is as follows:
{Fill in according to the operator's actual output, for example:}
result[0] is: xxx
result[1] is: xxx