FilesLast commitLast update
README

SpMatGetStridedBatch Operator

Operator Overview

The spmat_get_strided_batch operator reads back the batch count of a sparse matrix descriptor and corresponds to the cuSPARSE cusparseSpMatGetStridedBatch. It belongs to the SparseAccessor (descriptor accessor) class of interfaces.

This operator is a pure host accessor: it reads back batchCount from a sparse matrix descriptor of any format (COO / CSR / CSC / SELL / BLOCKED_ELL), and throughout the process does not launch NPU kernels, involves no stream synchronization, and performs no NPU computation. It accepts const descriptors, and non-const descriptors are also supported (through implicit conversion). The semantics are fully aligned with cuSPARSE, enabling seamless migration for cuSPARSE users.

Only batchCount is returned instead of batchStride, which is consistent with the cuSPARSE design: the stride signature is format dependent (COO has 1 batchStride, CSR has 2: offsetsBatchStride + columnsValuesBatchStride, and BSR has 3), and the only common quantity that a generic getter can uniformly expose across formats is batchCount. Writing the batch metadata is done by the SetStridedBatch interface of each format: the companion operators aclsparseCooSetStridedBatch (COO), aclsparseCsrSetStridedBatch (CSR), and aclsparseBsrSetStridedBatch (BSR) share the same batchCount field, and this interface uniformly reads back the values written through the three paths.

It includes the following interface:

Interface Brief Function
aclsparseSpMatGetStridedBatch Reads back the batch count of a sparse matrix descriptor (any format)

Operator Execution Interface

aclsparseSpMatGetStridedBatch

Product support

  • Ascend 950PR&950DT products: Supported
  • Atlas A3 products: Not supported
  • Atlas A2 products: Not supported

Function prototype

aclsparseStatus_t aclsparseSpMatGetStridedBatch(aclsparseConstSpMatDescr_t spMatDescr,
    int *batchCount);

Parameter description

Parameter Input/Output Parameter Type Description Memory Location
spMatDescr Input aclsparseConstSpMatDescr_t Sparse matrix descriptor (any format; non-const descriptors are also supported) Host
batchCount Output int* batch count Host

Constraints

  • spMatDescr cannot be nullptr and its signature must be valid (wrong-type or corrupted handles are rejected); batchCount cannot be nullptr; otherwise, ACL_SPARSE_STATUS_INVALID_VALUE is returned
  • Applies to sparse matrix descriptors of any format; a newly created descriptor that has never called SetStridedBatch returns 1 (not batched)
  • Pure host operation, does not launch NPU kernels, and involves no stream synchronization

Invocation example

The sample code is as follows and is for reference only. For the specific compilation and execution process, refer to Compilation and Running Sample.

#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)

using DevicePtr = std::unique_ptr<void, aclError (*)(void *)>;

static int AllocAndCopyDevice(DevicePtr &devicePtr, const void *hostPtr, size_t sizeBytes)
{
    void *rawPtr = nullptr;
    auto ret = aclrtMalloc(&rawPtr, sizeBytes, ACL_MEM_MALLOC_HUGE_FIRST);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
    devicePtr.reset(rawPtr);
    if (hostPtr != nullptr && sizeBytes > 0) {
        ret = aclrtMemcpy(devicePtr.get(), sizeBytes, hostPtr, sizeBytes, ACL_MEMCPY_HOST_TO_DEVICE);
        CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy data to Device failed. ERROR: %d\n", ret); return ret);
    }
    return ACL_SUCCESS;
}

int main()
{
    auto aclRet = aclInit(nullptr);
    CHECK_RET(aclRet == ACL_SUCCESS, return aclRet);
    aclRet = aclrtSetDevice(0);
    CHECK_RET(aclRet == ACL_SUCCESS, return aclRet);

    // 1. Prepare a 3x3 COO matrix with 3 non-zero elements
    const int64_t rows = 3;
    const int64_t cols = 3;
    const int64_t nnz = 3;
    std::vector<int32_t> hRowInd = {0, 1, 2};
    std::vector<int32_t> hColInd = {0, 2, 1};
    std::vector<float> hValues = {1.0f, 2.0f, 3.0f};

    DevicePtr dRowInd(nullptr, aclrtFree);
    DevicePtr dColInd(nullptr, aclrtFree);
    DevicePtr dValues(nullptr, aclrtFree);
    aclRet = AllocAndCopyDevice(dRowInd, hRowInd.data(), nnz * sizeof(int32_t));
    CHECK_RET(aclRet == ACL_SUCCESS, return aclRet);
    aclRet = AllocAndCopyDevice(dColInd, hColInd.data(), nnz * sizeof(int32_t));
    CHECK_RET(aclRet == ACL_SUCCESS, return aclRet);
    aclRet = AllocAndCopyDevice(dValues, hValues.data(), nnz * sizeof(float));
    CHECK_RET(aclRet == ACL_SUCCESS, return aclRet);

    // 2. Create the COO descriptor
    aclsparseSpMatDescr_t matDescr = nullptr;
    auto sparseRet = aclsparseCreateCoo(&matDescr, rows, cols, nnz,
        dRowInd.get(), dColInd.get(), dValues.get(),
        ACL_SPARSE_INDEX_32I, ACL_SPARSE_INDEX_BASE_ZERO, ACL_FLOAT);
    CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS,
        LOG_PRINT("aclsparseCreateCoo failed: %d\n", sparseRet); return sparseRet);

    // 3. Newly created descriptor (not batched): Get returns 1
    int batchCount = 0;
    sparseRet = aclsparseSpMatGetStridedBatch(matDescr, &batchCount);
    CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS,
        LOG_PRINT("aclsparseSpMatGetStridedBatch failed: %d\n", sparseRet);
        aclsparseDestroySpMat(matDescr); return sparseRet);
    LOG_PRINT("default batchCount = %d\n", batchCount);

    // 4. Read back after setting the strided batch (companion operator aclsparseCooSetStridedBatch)
    const int setBatchCount = 3;
    const int64_t setBatchStride = 4;  // >= nnz
    sparseRet = aclsparseCooSetStridedBatch(matDescr, setBatchCount, setBatchStride);
    CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS,
        LOG_PRINT("aclsparseCooSetStridedBatch failed: %d\n", sparseRet);
        aclsparseDestroySpMat(matDescr); return sparseRet);
    sparseRet = aclsparseSpMatGetStridedBatch(matDescr, &batchCount);
    CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS,
        LOG_PRINT("aclsparseSpMatGetStridedBatch failed: %d\n", sparseRet);
        aclsparseDestroySpMat(matDescr); return sparseRet);
    CHECK_RET(batchCount == setBatchCount,
        LOG_PRINT("batchCount mismatch: %d != %d\n", batchCount, setBatchCount);
        aclsparseDestroySpMat(matDescr); return ACL_SPARSE_STATUS_EXECUTION_FAILED);
    LOG_PRINT("after Set, batchCount = %d\n", batchCount);

    // 5. Destroy the descriptor
    aclsparseDestroySpMat(matDescr);
    (void)aclrtResetDevice(0);
    (void)aclFinalize();
    return 0;
}

The expected output is as follows:

default batchCount = 1
after Set, batchCount = 3

Companion Operator Description

This operator and its companion operators (the Set/Get closed loop) are both built into the same libops_sparse. The test binary embeds the companion operator implementations and supports independent single-operator build tests:

bash build.sh --ops=coo_set_strided_batch --run            # Single operator: Set
bash build.sh --ops=spmat_get_strided_batch --run          # Single operator: Get
bash build.sh --ops=coo_set_strided_batch,spmat_get_strided_batch --run  # Paired (default recommendation)