aoclsparse_?csr2m() - 5.2 English - 68552

AOCL API Guide (68552)

Document ID
68552
Release Date
2025-12-29
Version
5.2 English
aoclsparse_status aoclsparse_dcsr2m(aoclsparse_operation trans_A, const aoclsparse_mat_descr descrA, const aoclsparse_matrix csrA, aoclsparse_operation trans_B, const aoclsparse_mat_descr descrB, const aoclsparse_matrix csrB, const aoclsparse_request request, aoclsparse_matrix *csrC)#
aoclsparse_status aoclsparse_scsr2m(aoclsparse_operation trans_A, const aoclsparse_mat_descr descrA, const aoclsparse_matrix csrA, aoclsparse_operation trans_B, const aoclsparse_mat_descr descrB, const aoclsparse_matrix csrB, const aoclsparse_request request, aoclsparse_matrix *csrC)#

Sparse matrix Sparse matrix multiplication using CSR storage format for single and double precision datatypes.

aoclsparse_?csr2m multiplies a sparse \(m \times k\) matrix \(A\), defined in CSR storage format, and the sparse \(k \times n\) matrix \(B\), defined in CSR storage format and stores the result to the sparse \(m \times n\) matrix \(C\), such that

\[ C = op(A) \cdot op(B), \]
with
\[\begin{split} op(A) = \left\{ \begin{array}{ll} A, & \text{if } {\bf\mathsf{trans}\_\mathsf{A}} = \text{aoclsparse}\_\text{operation}\_\text{none} \\ A^T, & \text{if } {\bf\mathsf{trans}\_\mathsf{A}} = \text{aoclsparse}\_\text{operation}\_\text{transpose} \\ A^H, & \text{if } {\bf\mathsf{trans}\_\mathsf{A}} = \text{aoclsparse}\_\text{operation}\_\text{conjugate}\_\text{transpose} \end{array} \right. \end{split}\]
and
\[\begin{split} op(B) = \left\{ \begin{array}{ll} B, & \text{if } {\bf\mathsf{trans}\_\mathsf{B}} = \text{aoclsparse}\_\text{operation}\_\text{none} \\ B^T, & \text{if } {\bf\mathsf{trans}\_\mathsf{B}} = \text{aoclsparse}\_\text{operation}\_\text{transpose} \\ B^H, & \text{if } {\bf\mathsf{trans}\_\mathsf{B}} = \text{aoclsparse}\_\text{operation}\_\text{conjugate}\_\text{transpose} \end{array} \right. \end{split}\]

  1/* ************************************************************************
  2 * Copyright (c) 2021-2025 Advanced Micro Devices, Inc. All rights reserved.
  3 *
  4 * Permission is hereby granted, free of charge, to any person obtaining a copy
  5 * of this software and associated documentation files (the "Software"), to deal
  6 * in the Software without restriction, including without limitation the rights
  7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8 * copies of the Software, and to permit persons to whom the Software is
  9 * furnished to do so, subject to the following conditions:
 10 *
 11 * The above copyright notice and this permission notice shall be included in
 12 * all copies or substantial portions of the Software.
 13 *
 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 20 * THE SOFTWARE.
 21 *
 22 * ************************************************************************ */
 23
 24#include "aoclsparse.h"
 25
 26#include <cstdlib>
 27#include <cstring>
 28#include <iostream>
 29
 30/* Computes multiplication of 2 sparse matrices A and B in CSR format. */
 31
 32// Comment this out for single stage computation
 33#define TWO_STAGE_COMPUTATION
 34
 35#define PRINT_OUTPUT
 36
 37void check_error(aoclsparse_status status, const char *api_name)
 38{
 39    if(status != aoclsparse_status_success)
 40    {
 41        std::cerr << "ERROR in " << api_name << std::endl;
 42        exit(EXIT_FAILURE);
 43    }
 44}
 45
 46int main(void)
 47{
 48    aoclsparse_status     status;
 49    aoclsparse_int        nnz_C;
 50    aoclsparse_request    request;
 51    aoclsparse_index_base base   = aoclsparse_index_base_zero;
 52    aoclsparse_operation  transA = aoclsparse_operation_none;
 53    aoclsparse_operation  transB = aoclsparse_operation_none;
 54
 55    // Print aoclsparse version
 56    std::cout << aoclsparse_get_version() << std::endl;
 57
 58    // Initialise matrix descriptor and csr matrix structure of inputs A and B
 59    aoclsparse_mat_descr descrA;
 60    aoclsparse_mat_descr descrB;
 61    aoclsparse_matrix    csrA;
 62    aoclsparse_matrix    csrB;
 63
 64    // Create matrix descriptor of input matrices
 65    // aoclsparse_create_mat_descr set aoclsparse_matrix_type to aoclsparse_matrix_type_general
 66    // and aoclsparse_index_base to aoclsparse_index_base_zero.
 67    status = aoclsparse_create_mat_descr(&descrA);
 68
 69    check_error(status, "aoclsparse_create_mat_descr for A");
 70
 71    status = aoclsparse_create_mat_descr(&descrB);
 72
 73    check_error(status, "aoclsparse_create_mat_descr for B");
 74
 75    // Matrix sizes
 76    aoclsparse_int m = 3, n = 3, k = 3;
 77    aoclsparse_int nnz_A = 6, nnz_B = 4;
 78    // Matrix A
 79    // 	1  0  2
 80    // 	0  0  3
 81    // 	4  5  6
 82    aoclsparse_int row_ptr_A[] = {0, 2, 3, 6};
 83    aoclsparse_int col_ind_A[] = {0, 2, 2, 0, 1, 2};
 84    float          val_A[]     = {1, 2, 3, 4, 5, 6};
 85    status = aoclsparse_create_scsr(&csrA, base, m, k, nnz_A, row_ptr_A, col_ind_A, val_A);
 86
 87    check_error(status, "aoclsparse_create_scsr for A");
 88
 89    // Matrix B
 90    // 	1  2  0
 91    // 	0  0  3
 92    // 	0  4  0
 93    aoclsparse_int row_ptr_B[] = {0, 2, 3, 4};
 94    aoclsparse_int col_ind_B[] = {0, 1, 2, 1};
 95    float          val_B[]     = {1, 2, 3, 4};
 96    status = aoclsparse_create_scsr(&csrB, base, k, n, nnz_B, row_ptr_B, col_ind_B, val_B);
 97
 98    check_error(status, "aoclsparse_create_scsr for B");
 99
100    aoclsparse_matrix csrC          = NULL;
101    aoclsparse_int   *csr_row_ptr_C = NULL;
102    aoclsparse_int   *csr_col_ind_C = NULL;
103    float            *csr_val_C     = NULL;
104    aoclsparse_int    C_M, C_N;
105
106#ifdef TWO_STAGE_COMPUTATION
107    std::cout << "Invoking aoclsparse_scsr2m with aoclsparse_stage_nnz_count..";
108    // aoclsparse_stage_nnz_count : Only rowIndex array of the CSR matrix
109    // is computed internally. The output sparse CSR matrix can be
110    // extracted to measure the memory required for full operation.
111    request = aoclsparse_stage_nnz_count;
112    status  = aoclsparse_scsr2m(transA, descrA, csrA, transB, descrB, csrB, request, &csrC);
113
114    check_error(status, "aoclsparse_scsr2m with aoclsparse_stage_nnz_count");
115
116    status = aoclsparse_export_scsr(
117        csrC, &base, &C_M, &C_N, &nnz_C, &csr_row_ptr_C, &csr_col_ind_C, &csr_val_C);
118
119    check_error(status, "aoclsparse_export_scsr after nnz count");
120
121#ifdef PRINT_OUTPUT
122    std::cout << "C_M"
123              << "\t"
124              << "C_N"
125              << "\t"
126              << "nnz_C" << std::endl;
127    std::cout << C_M << "\t" << C_N << "\t" << nnz_C << std::endl;
128
129    std::cout << "csr_row_ptr_C: " << std::endl;
130    for(aoclsparse_int i = 0; i < C_M + 1; i++)
131        std::cout << csr_row_ptr_C[i] << "\t";
132    std::cout << std::endl;
133#endif
134
135    std::cout << "Invoking aoclsparse_scsr2m with aoclsparse_stage_finalize..";
136    // aoclsparse_stage_finalize : Finalize computation of remaining
137    // output arrays ( column indices and values of output matrix entries) .
138    // Has to be called only after aoclsparse_scsr2m call with
139    // aoclsparse_stage_nnz_count parameter.
140    request = aoclsparse_stage_finalize;
141    status  = aoclsparse_scsr2m(transA, descrA, csrA, transB, descrB, csrB, request, &csrC);
142
143    check_error(status, "aoclsparse_scsr2m with aoclsparse_stage_finalize");
144
145    status = aoclsparse_export_scsr(
146        csrC, &base, &C_M, &C_N, &nnz_C, &csr_row_ptr_C, &csr_col_ind_C, &csr_val_C);
147
148    check_error(status, "aoclsparse_export_scsr after finalize");
149
150#ifdef PRINT_OUTPUT
151    std::cout << "csr_col_ind_C: " << std::endl;
152    for(aoclsparse_int i = 0; i < nnz_C; i++)
153        std::cout << csr_col_ind_C[i] << "\t";
154    std::cout << std::endl;
155
156    std::cout << "csr_val_C: " << std::endl;
157    for(aoclsparse_int i = 0; i < nnz_C; i++)
158        std::cout << csr_val_C[i] << "\t";
159    std::cout << std::endl;
160#endif
161
162#else // SINGLE STAGE
163    std::cout << "Invoking aoclsparse_scsr2m with aoclsparse_stage_full_computation..";
164    // aoclsparse_stage_full_computation :  Whole computation is performed in
165    // single step.
166    request = aoclsparse_stage_full_computation;
167    status  = aoclsparse_scsr2m(transA, descrA, csrA, transB, descrB, csrB, request, &csrC);
168
169    check_error(status, "aoclsparse_scsr2m with aoclsparse_stage_full_computation");
170
171    status = aoclsparse_export_scsr(
172        csrC, &base, &C_M, &C_N, &nnz_C, &csr_row_ptr_C, &csr_col_ind_C, &csr_val_C);
173
174    check_error(status, "aoclsparse_export_scsr after full computation");
175
176#ifdef PRINT_OUTPUT
177    std::cout << "C_M"
178              << "\t"
179              << "C_N"
180              << "\t"
181              << "nnz_C" << std::endl;
182    std::cout << C_M << "\t" << C_N << "\t" << nnz_C << std::endl;
183
184    std::cout << "csr_row_ptr_C: " << std::endl;
185    for(aoclsparse_int i = 0; i < C_M + 1; i++)
186        std::cout << csr_row_ptr_C[i] << "\t";
187    std::cout << std::endl;
188
189    std::cout << "csr_col_ind_C: " << std::endl;
190    for(aoclsparse_int i = 0; i < nnz_C; i++)
191        std::cout << csr_col_ind_C[i] << "\t";
192    std::cout << std::endl;
193
194    std::cout << "csr_val_C: " << std::endl;
195    for(aoclsparse_int i = 0; i < nnz_C; i++)
196        std::cout << csr_val_C[i] << "\t";
197    std::cout << std::endl;
198#endif
199
200#endif
201
202    // Note: Error from destroy functions are ignored.
203    aoclsparse_destroy_mat_descr(descrA);
204    aoclsparse_destroy_mat_descr(descrB);
205    aoclsparse_destroy(&csrA);
206    aoclsparse_destroy(&csrB);
207    aoclsparse_destroy(&csrC);
208    return 0;
209}
Parameters:
  • trans_A[in] matrix \(A\) operation type.

  • descrA[in] descriptor of the sparse CSR matrix \(A\). Currently, only aoclsparse_matrix_type_general is supported.

  • csrA[in] sparse CSR matrix \(A\) structure.

  • trans_B[in] matrix \(B\) operation type.

  • descrB[in] descriptor of the sparse CSR matrix \(B\). Currently, only aoclsparse_matrix_type_general is supported.

  • csrB[in] sparse CSR matrix \(B\) structure.

  • request[in] Specifies full computation or two-stage algorithm aoclsparse_stage_nnz_count , Only rowIndex array of the CSR matrix is computed internally. The output sparse CSR matrix can be extracted to measure the memory required for full operation. aoclsparse_stage_finalize . Finalize computation of remaining output arrays ( column indices and values of output matrix entries) . Has to be called only after aoclsparse_dcsr2m() call with aoclsparse_stage_nnz_count parameter. aoclsparse_stage_full_computation. Perform the entire computation in a single step.

  • *csrC[out] Pointer to sparse CSR matrix \(C\) structure.

Return values:
  • aoclsparse_status_success – the operation completed successfully.

  • aoclsparse_status_invalid_size – input parameters contain an invalid value.

  • aoclsparse_status_invalid_pointerdescrA, csr, descrB, csrB, csrC is invalid.

  • aoclsparse_status_not_implementedaoclsparse_matrix_type is not aoclsparse_matrix_type_general or input matrices A or B is not in CSR format