{aclsparseXxx} 算子文档
【免费下载链接】ops-sparse本项目是CANN提供的高性能稀疏矩阵计算的算子库,专注于优化稀疏矩阵的计算效率。
项目地址: https://gitcode.com/cann/ops-sparse
算子概述
{功能描述 + 数学表达式}
接口列表
产品支持情况
| 芯片系列 | 支持情况 |
|---|
| Ascend910B | ✅ |
| Ascend910_93 | ✅ |
| Ascend950 | ✅ |
| Ascend310P | ✅ |
接口详情
{aclsparseXxx}
函数原型
aclsparseStatus_t {aclsparseXxx}(...);
参数说明
约束说明
支持的稀疏格式
调用示例
Generic API 风格(仅 Generic 算子保留,删除 Legacy 示例)
// 1. 创建 handle aclsparseHandle_t handle; aclsparseCreate(&handle); aclsparseSetStream(handle, stream); // 2. 创建稀疏矩阵描述符 aclsparseConstSpMatDescr_t matA; aclsparseCreateConstCsr(&matA, rows, cols, nnz, csrRowOffsets, csrColInd, csrValues, ACL_SPARSE_INDEX_32I, ACL_SPARSE_INDEX_32I, ACL_SPARSE_INDEX_BASE_ZERO, ACL_FLOAT); // 3. 创建稠密向量描述符 aclsparseConstDnVecDescr_t vecX; aclsparseCreateConstDnVec(&vecX, n, x, ACL_FLOAT); aclsparseDnVecDescr_t vecY; aclsparseCreateDnVec(&vecY, m, y, ACL_FLOAT); // 4. 获取缓冲区大小 size_t bufferSize; aclsparseXxxGetBufferSize(handle, ..., &bufferSize); // 5. 分配缓冲区 void *buffer; aclrtMalloc(&buffer, bufferSize, ACL_MEM_MALLOC_HUGE_FIRST); // 6. 预处理(若需要) aclsparseXxxPreprocess(handle, ..., buffer); // 7. 执行计算 aclsparseXxx(handle, ..., buffer); // 8. 同步 aclrtSynchronizeStream(stream); // 9. 清理 aclsparseDestroySpMat(matA); aclsparseDestroyDnVec(vecX); aclsparseDestroyDnVec(vecY); aclrtFree(buffer); aclsparseDestroy(handle);
Legacy API 风格(仅 Legacy 算子保留,删除 Generic 示例)
// 1. 创建 handle aclsparseHandle_t handle; aclsparseCreate(&handle); aclsparseSetStream(handle, stream); // 2. 准备数据指针(直接扁平传入,无描述符) float *d_dl, *d_d, *d_du, *d_x; aclrtMalloc((void**)&d_dl, (m-1) * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST); aclrtMalloc((void**)&d_d, m * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST); aclrtMalloc((void**)&d_du, (m-1) * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST); aclrtMalloc((void**)&d_x, m * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST); aclrtMemcpy(d_dl, ..., h_dl, ..., ACL_MEMCPY_HOST_TO_DEVICE); aclrtMemcpy(d_d, ..., h_d, ..., ACL_MEMCPY_HOST_TO_DEVICE); aclrtMemcpy(d_du, ..., h_du, ..., ACL_MEMCPY_HOST_TO_DEVICE); aclrtMemcpy(d_x, ..., h_b, ..., ACL_MEMCPY_HOST_TO_DEVICE); // 3. MatDescr(若接口需要) aclsparseMatDescr_t matDescr; aclsparseCreateMatDescr(&matDescr); aclsparseSetMatType(matDescr, ACL_SPARSE_MATRIX_TYPE_GENERAL); aclsparseSetMatIndexBase(matDescr, ACL_SPARSE_INDEX_BASE_ZERO); // 4. 获取 workspace 大小(若接口提供 bufferSize 函数) size_t bufferSize = 0; aclsparseS{xxx}_bufferSize(handle, ..., &bufferSize); void *pBuffer = nullptr; if (bufferSize > 0) { aclrtMalloc(&pBuffer, bufferSize, ACL_MEM_MALLOC_HUGE_FIRST); } // 5. 调用算子(带精度前缀) aclsparseS{xxx}(handle, ..., pBuffer); // 6. 同步 aclrtSynchronizeStream(stream); // 7. 清理 if (pBuffer) aclrtFree(pBuffer); aclsparseDestroyMatDescr(matDescr); // 若有 aclrtFree(d_dl); aclrtFree(d_d); aclrtFree(d_du); aclrtFree(d_x); aclsparseDestroy(handle);
【免费下载链接】ops-sparse本项目是CANN提供的高性能稀疏矩阵计算的算子库,专注于优化稀疏矩阵的计算效率。
项目地址: https://gitcode.com/cann/ops-sparse