-
template<typename T>
aoclsparse_status aoclsparse::mv(aoclsparse_operation op, const T *alpha, aoclsparse_matrix A, const aoclsparse_mat_descr descr, const T *x, const T *beta, T *y)# C++ function to compute sparse matrix-vector multiplication for real/complex single and double data precisions.
aoclsparse::mvis the C++ interface toaoclsparse_?mvthat computes sparse matrix-vector multiplication using the provided input parameters.1/* ************************************************************************ 2 * Copyright (c) 2020-2025 Advanced Micro Devices, Inc. 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 Description: In this example, we create a sparse matrix in CSR format 25 and perform a matrix-vector multiplication (SpMV) using the 26 C++ interface of aoclsparse. 27*/ 28#include "aoclsparse.hpp" 29 30#include <iostream> 31#include <vector> 32 33int main(void) 34{ 35 aoclsparse_operation trans = aoclsparse_operation_none; 36 37 double alpha = 1.0; 38 double beta = 0.0; 39 40 const aoclsparse_int m = 5; // Number of rows 41 const aoclsparse_int n = 5; // Number of columns 42 const aoclsparse_int nnz = 8; // Number of non-zero elements 43 44 // Print aoclsparse version 45 std::cout << aoclsparse_get_version() << std::endl; 46 47 // Create matrix descriptor 48 aoclsparse_mat_descr descr; 49 // aoclsparse_create_mat_descr set aoclsparse_matrix_type to aoclsparse_matrix_type_general 50 // and aoclsparse_index_base to aoclsparse_index_base_zero. 51 aoclsparse_create_mat_descr(&descr); 52 53 aoclsparse_index_base base = aoclsparse_index_base_zero; 54 55 // Initialise matrix 56 // 1 0 0 2 0 57 // 0 3 0 0 0 58 // 0 0 4 0 0 59 // 0 5 0 6 7 60 // 0 0 0 0 8 61 std::vector<aoclsparse_int> csr_row = {0, 2, 3, 4, 7, 8}; 62 std::vector<aoclsparse_int> csr_col_ind = {0, 3, 1, 2, 1, 3, 4, 4}; 63 std::vector<double> csr_val = {1, 2, 3, 4, 5, 6, 7, 8}; 64 aoclsparse_matrix A; 65 auto status = aoclsparse::create_csr( 66 &A, base, m, n, nnz, csr_row.data(), csr_col_ind.data(), csr_val.data()); 67 68 if(status != aoclsparse_status_success) 69 { 70 std::cout << "Error creating the matrix, status = " << status << std::endl; 71 return 1; 72 } 73 74 // Initialise vectors 75 std::vector<double> x = {1.0, 2.0, 3.0, 4.0, 5.0}; 76 std::vector<double> y(m, 0.0); // Output vector initialized to zero 77 78 //to identify hint id(which routine is to be executed, destroyed later) 79 aoclsparse_set_mv_hint(A, trans, descr, 1); 80 81 // Optimize the matrix, "A" 82 aoclsparse_optimize(A); 83 84 std::cout << "Invoking aoclsparse_dmv.."; 85 //Invoke SPMV API (double precision) 86 aoclsparse::mv(trans, &alpha, A, descr, x.data(), &beta, y.data()); 87 88 std::cout << "Done." << std::endl; 89 90 std::cout << "Output Vector:" << std::endl; 91 for(aoclsparse_int i = 0; i < m; i++) 92 std::cout << y[i] << std::endl; 93 94 aoclsparse_destroy_mat_descr(descr); 95 aoclsparse_destroy(&A); 96 return 0; 97}
- Template Parameters:
T – Data type supported for
Tare double, float, std::complex<double> and std::complex<float>