FilesLast commitLast update
README

CooGet Operator

Operator Overview

The coo_get operator reads back all fields from a sparse matrix descriptor in COO (Coordinate) format. The operator corresponds to the cuSPARSE cusparseCreateCoo / cusparseCooGet / cusparseCreateConstCoo / cusparseConstCooGet APIs and belongs to the SparseAccessor (descriptor accessor) class of interfaces.

CooGet is a pure host accessor: the descriptor construction and field reading are both completed on the host side, without launching NPU kernels, without involving stream synchronization, and without GPU computation. aclsparseCreateCoo stores the device pointers to the row/column indices and values passed in by the caller in the descriptor, together with the dimensions, index type, index base, and value type. aclsparseCooGet reads these fields back from the descriptor as they are, and the device pointers read back are identical to the pointers passed in at Create time (the accessor does not copy or move device data). The semantics are fully aligned with cuSPARSE to enable seamless migration for cuSPARSE users.

The COO format describes a sparse matrix with three equal-length arrays: the row index cooRowInd, the column index cooColInd, and the non-zero values cooValues, all with length nnz. cooIdxType constrains the data type of both the row/column indices.

This operator provides the following interfaces:

Interface name Function brief
aclsparseCreateCoo Creates a COO-format sparse matrix descriptor and stores the dimensions and device pointers
aclsparseCreateConstCoo Creates a read-only (const) COO-format sparse matrix descriptor
aclsparseCooGet Reads back all fields (dimensions / device pointers / index type / index base / value type) from a COO descriptor
aclsparseConstCooGet Reads back all fields from a read-only (const) COO descriptor

Operator Execution Interfaces

aclsparseCreateCoo

Product support

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

Function prototype

aclsparseStatus_t aclsparseCreateCoo(aclsparseSpMatDescr_t *spMatDescr, int64_t rows, int64_t cols, int64_t nnz,
    void *cooRowInd, void *cooColInd, void *cooValues, aclsparseIndexType_t cooIdxType,
    aclsparseIndexBase_t idxBase, aclDataType valueType);

Parameter description

Parameter Input/Output Parameter type Description Memory location
spMatDescr Input/Output aclsparseSpMatDescr_t* COO sparse matrix descriptor (output handle); no initialization is required before the call Host
rows Input int64_t Number of matrix rows, rows >= 0 Host
cols Input int64_t Number of matrix columns, cols >= 0 Host
nnz Input int64_t Number of non-zero elements, nnz >= 0 Host
cooRowInd Input void* COO row index array with length nnz Device
cooColInd Input void* COO column index array with length nnz Device
cooValues Input void* COO non-zero value array with length nnz Device
cooIdxType Input aclsparseIndexType_t Data type of the row/column indices, constraining both cooRowInd and cooColInd Host
idxBase Input aclsparseIndexBase_t Index base value (ACL_SPARSE_INDEX_BASE_ZERO / ACL_SPARSE_INDEX_BASE_ONE) Host
valueType Input aclDataType Data type of non-zero values Host

Constraints

  • rows >= 0, cols >= 0, and nnz >= 0; otherwise ACL_SPARSE_STATUS_INVALID_VALUE is returned
  • spMatDescr (output handle) cannot be nullptr; otherwise ACL_SPARSE_STATUS_INVALID_VALUE is returned
  • idxBase supports only ACL_SPARSE_INDEX_BASE_ZERO / ACL_SPARSE_INDEX_BASE_ONE; otherwise ACL_SPARSE_STATUS_INVALID_VALUE is returned
  • valueType must be in the supported data type whitelist (FLOAT, FLOAT16, DOUBLE, INT8/16/32/64, UINT8/16/32/64, BF16, FLOAT8_E4M3FN, FLOAT8_E5M2, FLOAT4_E2M1, COMPLEX64, COMPLEX128); otherwise ACL_SPARSE_STATUS_INVALID_VALUE is returned
  • When the idxBase or valueType validation fails, *spMatDescr keeps its original value and is not overwritten with a half-finished handle
  • cooIdxType constrains both the row/column index types; ACL_SPARSE_INDEX_32I and ACL_SPARSE_INDEX_64I are supported (the accessor layer performs no NPU computation, consistent with cuSPARSE). Note: this differs from CSR — the current SpMV / SpMM computation paths of CSR support only ACL_SPARSE_INDEX_32I; the COO accessor accepts both index types
  • aclsparseCreateCoo is a pure host operation that launches no NPU kernel and involves no stream synchronization
  • The descriptor constructed by the const variant (aclsparseCreateConstCoo) can be passed only to interfaces that accept const parameters; for destruction, uniformly use aclsparseDestroySpMat (it accepts the const variant, and both const and non-const descriptors can be destroyed)

aclsparseCreateConstCoo

Product support

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

Function prototype

aclsparseStatus_t aclsparseCreateConstCoo(aclsparseConstSpMatDescr_t *spMatDescr, int64_t rows, int64_t cols,
    int64_t nnz, const void *cooRowInd, const void *cooColInd, const void *cooValues,
    aclsparseIndexType_t cooIdxType, aclsparseIndexBase_t idxBase, aclDataType valueType);

Parameter description

Parameter Input/Output Parameter type Description Memory location
spMatDescr Input/Output aclsparseConstSpMatDescr_t* Read-only (const) COO sparse matrix descriptor (output handle) Host
rows Input int64_t Number of matrix rows, rows >= 0 Host
cols Input int64_t Number of matrix columns, cols >= 0 Host
nnz Input int64_t Number of non-zero elements, nnz >= 0 Host
cooRowInd Input const void* COO row index array with length nnz Device
cooColInd Input const void* COO column index array with length nnz Device
cooValues Input const void* COO non-zero value array with length nnz Device
cooIdxType Input aclsparseIndexType_t Data type of the row/column indices, constraining both cooRowInd and cooColInd Host
idxBase Input aclsparseIndexBase_t Index base value Host
valueType Input aclDataType Data type of non-zero values Host

Constraints

  • Constraints are the same as those of aclsparseCreateCoo (rows / cols / nnz are non-negative, and spMatDescr is not nullptr)
  • The constructed descriptor is a const variant and can be passed only to interfaces that accept const parameters (such as aclsparseConstCooGet); for destruction, uniformly use aclsparseDestroySpMat
  • Index type support is the same as that of aclsparseCreateCoo (both ACL_SPARSE_INDEX_32I and ACL_SPARSE_INDEX_64I are accepted)
  • The remaining constraints are the same as those of aclsparseCreateCoo

aclsparseCooGet

Product support

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

Function prototype

aclsparseStatus_t aclsparseCooGet(aclsparseSpMatDescr_t spMatDescr, int64_t *rows, int64_t *cols, int64_t *nnz,
    void **cooRowInd, void **cooColInd, void **cooValues, aclsparseIndexType_t *cooIdxType,
    aclsparseIndexBase_t *idxBase, aclDataType *valueType);

Parameter description

Parameter Input/Output Parameter type Description Memory location
spMatDescr Input aclsparseSpMatDescr_t COO sparse matrix descriptor Host
rows Output int64_t* Number of matrix rows Host
cols Output int64_t* Number of matrix columns Host
nnz Output int64_t* Number of non-zero elements Host
cooRowInd Output void** Pointer to the row index array (identical to the device pointer passed in at Create) Device
cooColInd Output void** Pointer to the column index array (identical to the device pointer passed in at Create) Device
cooValues Output void** Pointer to the non-zero value array (identical to the device pointer passed in at Create) Device
cooIdxType Output aclsparseIndexType_t* Data type of the row/column indices Host
idxBase Output aclsparseIndexBase_t* Index base value Host
valueType Output aclDataType* Data type of non-zero values Host

Constraints

  • spMatDescr (input descriptor) cannot be nullptr; otherwise ACL_SPARSE_STATUS_INVALID_VALUE is returned
  • A descriptor that is not in COO format returns ACL_SPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED
  • Any OUT pointer can be nullptr, in which case only the requested fields are returned (the corresponding fields are not written back), consistent with the wording of aclsparseSpMatGetSize / aclsparseSpVecGet
  • aclsparseCooGet is a pure host operation that launches no NPU kernel and involves no stream synchronization

aclsparseConstCooGet

Product support

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

Function prototype

aclsparseStatus_t aclsparseConstCooGet(aclsparseConstSpMatDescr_t spMatDescr, int64_t *rows, int64_t *cols,
    int64_t *nnz, const void **cooRowInd, const void **cooColInd, const void **cooValues,
    aclsparseIndexType_t *cooIdxType, aclsparseIndexBase_t *idxBase, aclDataType *valueType);

Parameter description

Parameter Input/Output Parameter type Description Memory location
spMatDescr Input aclsparseConstSpMatDescr_t Read-only (const) COO sparse matrix descriptor Host
rows Output int64_t* Number of matrix rows Host
cols Output int64_t* Number of matrix columns Host
nnz Output int64_t* Number of non-zero elements Host
cooRowInd Output const void** Pointer to the row index array Device
cooColInd Output const void** Pointer to the column index array Device
cooValues Output const void** Pointer to the non-zero value array Device
cooIdxType Output aclsparseIndexType_t* Data type of the row/column indices Host
idxBase Output aclsparseIndexBase_t* Index base value Host
valueType Output aclDataType* Data type of non-zero values Host

Constraints

  • Constraints are the same as those of aclsparseCooGet (spMatDescr is not nullptr, and a non-COO format returns ACL_SPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED)
  • Any OUT pointer can be nullptr, in which case only the requested fields are returned
  • aclsparseConstCooGet is a pure host operation that launches no NPU kernel and involves no stream synchronization

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;
};

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

// Helper: allocates Device memory and copies Host data; the memory is released automatically by DevicePtr
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 aclsparseCooGetTest(AclContext &ctx)
{
    (void)ctx.Stream();  // the accessor does not use handle / stream; this is only a placeholder

    // 1. Prepare Host-side COO data: a 3x3 matrix with 3 non-zero elements
    //    [1.0  0.0  0.0]
    //    [0.0  0.0  2.0]
    //    [0.0  3.0  0.0]
    int64_t rows = 3;
    int64_t cols = 3;
    int64_t nnz = 3;

    std::vector<int32_t> hCooRowInd = {0, 1, 2};
    std::vector<int32_t> hCooColInd = {0, 2, 1};
    std::vector<float> hCooValues = {1.0f, 2.0f, 3.0f};

    // 2. Copy row/col/values to Device
    DevicePtr dCooRowInd(nullptr, aclrtFree);
    DevicePtr dCooColInd(nullptr, aclrtFree);
    DevicePtr dCooValues(nullptr, aclrtFree);
    auto aclRet = AllocAndCopyDevice(dCooRowInd, hCooRowInd.data(), nnz * sizeof(int32_t));
    CHECK_RET(aclRet == ACL_SUCCESS, return aclRet);
    aclRet = AllocAndCopyDevice(dCooColInd, hCooColInd.data(), nnz * sizeof(int32_t));
    CHECK_RET(aclRet == ACL_SUCCESS, return aclRet);
    aclRet = AllocAndCopyDevice(dCooValues, hCooValues.data(), nnz * sizeof(float));
    CHECK_RET(aclRet == ACL_SUCCESS, return aclRet);

    // 3. aclsparseCreateCoo: stores the dimensions and device pointers in the descriptor
    aclsparseSpMatDescr_t matDescr = nullptr;
    auto sparseRet = aclsparseCreateCoo(&matDescr, rows, cols, nnz,
        dCooRowInd.get(), dCooColInd.get(), dCooValues.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);

    // 4. aclsparseCooGet: reads back all fields of the descriptor
    int64_t outRows = 0, outCols = 0, outNnz = 0;
    void *outRowInd = nullptr, *outColInd = nullptr, *outValues = nullptr;
    aclsparseIndexType_t outIdxType{};
    aclsparseIndexBase_t outIdxBase{};
    aclDataType outValueType{};
    sparseRet = aclsparseCooGet(matDescr, &outRows, &outCols, &outNnz,
        &outRowInd, &outColInd, &outValues, &outIdxType, &outIdxBase, &outValueType);
    CHECK_RET(sparseRet == ACL_SPARSE_STATUS_SUCCESS,
        LOG_PRINT("aclsparseCooGet failed: %d\n", sparseRet); aclsparseDestroySpMat(matDescr); return sparseRet);

    // 5. Accessor semantics assertion: the fields read back are consistent with those passed in at Create
    CHECK_RET(outRows == rows && outCols == cols && outNnz == nnz,
        LOG_PRINT("size mismatch: rows=%lld/%lld cols=%lld/%lld nnz=%lld/%lld\n",
            static_cast<long long>(outRows), static_cast<long long>(rows),
            static_cast<long long>(outCols), static_cast<long long>(cols),
            static_cast<long long>(outNnz), static_cast<long long>(nnz));
        aclsparseDestroySpMat(matDescr); return ACL_SPARSE_STATUS_EXECUTION_FAILED);
    CHECK_RET(outIdxType == ACL_SPARSE_INDEX_32I && outIdxBase == ACL_SPARSE_INDEX_BASE_ZERO &&
              outValueType == ACL_FLOAT,
        LOG_PRINT("attr mismatch\n"); aclsparseDestroySpMat(matDescr); return ACL_SPARSE_STATUS_EXECUTION_FAILED);
    // The device pointers read back should be identical to those passed in at Create (the accessor does not copy device data)
    CHECK_RET(outRowInd == dCooRowInd.get() && outColInd == dCooColInd.get() &&
              outValues == dCooValues.get(),
        LOG_PRINT("device pointer mismatch\n"); aclsparseDestroySpMat(matDescr);
        return ACL_SPARSE_STATUS_EXECUTION_FAILED);

    LOG_PRINT("aclsparseCreateCoo / aclsparseCooGet succeeded.\n");
    LOG_PRINT("read back: rows=%lld, cols=%lld, nnz=%lld\n",
        static_cast<long long>(outRows), static_cast<long long>(outCols), static_cast<long long>(outNnz));
    LOG_PRINT("  attrs match: idxType=ACL_SPARSE_INDEX_32I, idxBase=ACL_SPARSE_INDEX_BASE_ZERO, valueType=ACL_FLOAT\n");
    LOG_PRINT("  devPtr match: row=%d, col=%d, val=%d\n",
        outRowInd == dCooRowInd.get() ? 1 : 0,
        outColInd == dCooColInd.get() ? 1 : 0,
        outValues == dCooValues.get() ? 1 : 0);

    // 6. Destroy the descriptor (uniformly use aclsparseDestroySpMat)
    aclsparseDestroySpMat(matDescr);
    LOG_PRINT("aclsparseDestroySpMat done.\n");

    return ACL_SPARSE_STATUS_SUCCESS;
}

int main()
{
    AclContext ctx(0);
    auto ret = ctx.Init();
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    ret = aclsparseCooGetTest(ctx);
    CHECK_RET(ret == ACL_SPARSE_STATUS_SUCCESS, LOG_PRINT("aclsparseCooGetTest failed: %d\n", ret); return ret);
    return 0;
}

The expected output is as follows:

aclsparseCreateCoo / aclsparseCooGet succeeded.
read back: rows=3, cols=3, nnz=3
  attrs match: idxType=ACL_SPARSE_INDEX_32I, idxBase=ACL_SPARSE_INDEX_BASE_ZERO, valueType=ACL_FLOAT
  devPtr match: row=1, col=1, val=1
aclsparseDestroySpMat done.