Last active
June 18, 2023 22:38
-
-
Save ivan-pi/6dfc2022e0db11064f289874773daf64 to your computer and use it in GitHub Desktop.
CSR SpMV with oneAPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compile with: | |
// icpx -fsycl csr_example.cpp | |
// | |
#include <algorithm> | |
#include <oneapi/mkl.hpp> | |
#include <CL/sycl.hpp> | |
int main(int argc, char const *argv[]) | |
{ | |
using namespace oneapi::mkl; | |
constexpr int NR = 4; | |
constexpr int NC = 6; | |
std::vector<int> ia{0,2,4,7,8}; | |
std::vector<int> ja{0,1,1,3,2,3,4,5}; | |
std::vector<double> va{10,20,30,40,50,60,70,80}; | |
std::array<double,NC> x; | |
std::array<double,NR> y; | |
std::fill(x.begin(),x.end(),1.0); | |
sycl::device device{sycl::default_selector{}}; | |
sycl::queue Q{device}; | |
std::cout << "Running SpMV product on " << device.get_info<sycl::info::device::name>() << ".\n"; | |
{ | |
sycl::buffer<double,1> b_x(x.data(),x.size()); | |
sycl::buffer<double,1> b_y(y.data(),y.size()); | |
sparse::matrix_handle_t handle; | |
sparse::init_matrix_handle(&handle); | |
sycl::buffer<int,1> b_ia(ia.data(),NR+1); | |
sycl::buffer<int,1> b_ja(ja.data(),ja.size()); | |
sycl::buffer<double,1> b_va(va.data(),va.size()); | |
sparse::set_csr_data(handle,NR,NC,index_base::zero,b_ia,b_ja,b_va); | |
sparse::optimize_gemv(Q,transpose::N,handle); | |
const double alpha = 1.0; | |
const double beta = 0.0; | |
// | |
// y := alpha*op(A)*x + beta*y | |
// | |
sparse::gemv(Q,transpose::N,alpha,handle,b_x,beta,b_y); | |
sparse::release_matrix_handle(&handle); | |
} | |
for (int i = 0; i < y.size(); ++i) { | |
std::cout << "y[" << i << "] = " << y[i] << '\n'; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment