-
template<typename T>
aoclsparse_status aoclsparse::sp2m(aoclsparse_operation opA, const aoclsparse_mat_descr descrA, const aoclsparse_matrix A, aoclsparse_operation opB, const aoclsparse_mat_descr descrB, const aoclsparse_matrix B, aoclsparse_request request, aoclsparse_matrix *C)# aoclsparse::sp2mis the C++ interface to aoclsparse_sp2m that multiplies two sparse matrices in CSR storage format and the result is stored in a newly allocated sparse matrix in CSR format.1/* ************************************************************************ 2 * Copyright (c) 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.hpp" 25 26#include <cstdlib> 27#include <cstring> 28#include <iostream> 29 30/* Computes multiplication of 2 sparse matrices A and B in CSR format using C++ interface. */ 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 86 // Use C++ template interface to create CSR matrix 87 status = aoclsparse::create_csr<float>(&csrA, base, m, k, nnz_A, row_ptr_A, col_ind_A, val_A); 88 89 check_error(status, "aoclsparse::create_csr<float> for A"); 90 91 // Matrix B 92 // 1 2 0 93 // 0 0 3 94 // 0 4 0 95 aoclsparse_int row_ptr_B[] = {0, 2, 3, 4}; 96 aoclsparse_int col_ind_B[] = {0, 1, 2, 1}; 97 float val_B[] = {1, 2, 3, 4}; 98 99 // Use C++ template interface to create CSR matrix 100 status = aoclsparse::create_csr<float>(&csrB, base, k, n, nnz_B, row_ptr_B, col_ind_B, val_B); 101 check_error(status, "aoclsparse::create_csr<float> for B"); 102 103 aoclsparse_matrix csrC = nullptr; 104 aoclsparse_int *csr_row_ptr_C = nullptr; 105 aoclsparse_int *csr_col_ind_C = nullptr; 106 float *csr_val_C = nullptr; 107 aoclsparse_int C_M, C_N; 108 109#ifdef TWO_STAGE_COMPUTATION 110 std::cout << "Invoking aoclsparse::sp2m (C++ interface) with aoclsparse_stage_nnz_count.."; 111 // aoclsparse_stage_nnz_count : Only rowIndex array of the CSR matrix 112 // is computed internally. The output sparse CSR matrix can be 113 // extracted to measure the memory required for full operation. 114 request = aoclsparse_stage_nnz_count; 115 status = aoclsparse::sp2m<float>(transA, descrA, csrA, transB, descrB, csrB, request, &csrC); 116 117 check_error(status, "aoclsparse::sp2m (C++ interface) with aoclsparse_stage_nnz_count"); 118 119 status = aoclsparse_export_scsr( 120 csrC, &base, &C_M, &C_N, &nnz_C, &csr_row_ptr_C, &csr_col_ind_C, &csr_val_C); 121 122 check_error(status, "aoclsparse_export_scsr"); 123 124#ifdef PRINT_OUTPUT 125 std::cout << "C_M" 126 << "\t" 127 << "C_N" 128 << "\t" 129 << "nnz_C" << std::endl; 130 std::cout << C_M << "\t" << C_N << "\t" << nnz_C << std::endl; 131 132 std::cout << "csr_row_ptr_C: " << std::endl; 133 for(aoclsparse_int i = 0; i < C_M + 1; i++) 134 std::cout << csr_row_ptr_C[i] << "\t"; 135 std::cout << std::endl; 136#endif 137 138 std::cout << "Invoking aoclsparse::sp2m (C++ interface) with aoclsparse_stage_finalize.."; 139 // aoclsparse_stage_finalize : Finalize computation of remaining 140 // output arrays ( column indices and values of output matrix entries) . 141 // Has to be called only after aoclsparse::sp2m call with 142 // aoclsparse_stage_nnz_count parameter. 143 request = aoclsparse_stage_finalize; 144 status = aoclsparse::sp2m<float>(transA, descrA, csrA, transB, descrB, csrB, request, &csrC); 145 146 check_error(status, "aoclsparse::sp2m (C++ interface) with aoclsparse_stage_finalize"); 147 148 status = aoclsparse_export_scsr( 149 csrC, &base, &C_M, &C_N, &nnz_C, &csr_row_ptr_C, &csr_col_ind_C, &csr_val_C); 150 151 check_error(status, "aoclsparse_export_scsr after finalize"); 152 153#ifdef PRINT_OUTPUT 154 std::cout << "C_M" 155 << "\t" 156 << "C_N" 157 << "\t" 158 << "nnz_C" << std::endl; 159 std::cout << C_M << "\t" << C_N << "\t" << nnz_C << std::endl; 160 161 std::cout << "csr_row_ptr_C: " << std::endl; 162 for(aoclsparse_int i = 0; i < C_M + 1; i++) 163 std::cout << csr_row_ptr_C[i] << "\t"; 164 std::cout << std::endl; 165 166 std::cout << "csr_col_ind_C: " << std::endl; 167 for(aoclsparse_int i = 0; i < nnz_C; i++) 168 std::cout << csr_col_ind_C[i] << "\t"; 169 std::cout << std::endl; 170 171 std::cout << "csr_val_C: " << std::endl; 172 for(aoclsparse_int i = 0; i < nnz_C; i++) 173 std::cout << csr_val_C[i] << "\t"; 174 std::cout << std::endl; 175#endif 176 177#else // SINGLE STAGE 178 std::cout 179 << "Invoking aoclsparse::sp2m (C++ interface) with aoclsparse_stage_full_computation.."; 180 // aoclsparse_stage_full_computation : Whole computation is performed in 181 // single step. 182 request = aoclsparse_stage_full_computation; 183 status = aoclsparse::sp2m<float>(transA, descrA, csrA, transB, descrB, csrB, request, &csrC); 184 check_error(status, "aoclsparse::sp2m (C++ interface) with aoclsparse_stage_full_computation"); 185 186 status = aoclsparse_export_scsr( 187 csrC, &base, &C_M, &C_N, &nnz_C, &csr_row_ptr_C, &csr_col_ind_C, &csr_val_C); 188 189 check_error(status, "aoclsparse_export_scsr after full computation"); 190 191#ifdef PRINT_OUTPUT 192 std::cout << "C_M" 193 << "\t" 194 << "C_N" 195 << "\t" 196 << "nnz_C" << std::endl; 197 std::cout << C_M << "\t" << C_N << "\t" << nnz_C << std::endl; 198 199 std::cout << "csr_row_ptr_C: " << std::endl; 200 for(aoclsparse_int i = 0; i < C_M + 1; i++) 201 std::cout << csr_row_ptr_C[i] << "\t"; 202 std::cout << std::endl; 203 204 std::cout << "csr_col_ind_C: " << std::endl; 205 for(aoclsparse_int i = 0; i < nnz_C; i++) 206 std::cout << csr_col_ind_C[i] << "\t"; 207 std::cout << std::endl; 208 209 std::cout << "csr_val_C: " << std::endl; 210 for(aoclsparse_int i = 0; i < nnz_C; i++) 211 std::cout << csr_val_C[i] << "\t"; 212 std::cout << std::endl; 213#endif 214 215#endif 216 217 // Note: Errors from destroy functions are ignored. 218 aoclsparse_destroy_mat_descr(descrA); 219 aoclsparse_destroy_mat_descr(descrB); 220 aoclsparse_destroy(&csrA); 221 aoclsparse_destroy(&csrB); 222 aoclsparse_destroy(&csrC); 223 return 0; 224}
- Template Parameters:
T – Data type supported for
Tare double, float, std::complex<double> or std::complex<float>