Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a sparse diagonal matrix of CuArray(u) #1857

Closed
HamMoh94 opened this issue Apr 5, 2023 · 3 comments · Fixed by #2458
Closed

Creating a sparse diagonal matrix of CuArray(u) #1857

HamMoh94 opened this issue Apr 5, 2023 · 3 comments · Fixed by #2458
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@HamMoh94
Copy link

HamMoh94 commented Apr 5, 2023

If we have a vector (u), we can easily convert it into a sparse diagonal matrix using the command spdiagm(u).

If we want to do the same operation on the GPU using CUDA where u is defined as CuArray(u). The command spdiagm(u) gives the scalar indexing error and therefore it does not work.

CuSparseMatrixCSC(Diagonal(u)) works but it is quite slow. Is there a way to perform this task efficiently?

Regards

@HamMoh94 HamMoh94 added the enhancement New feature or request label Apr 5, 2023
@maleadt
Copy link
Member

maleadt commented Apr 24, 2023

Currently not. It should be possible to do so quite easily by manually constructing the necessary inner arrays (colptr, rowval, nzval).

@maleadt maleadt added the good first issue Good for newcomers label Apr 24, 2023
@HamMoh94
Copy link
Author

HamMoh94 commented May 2, 2023

Thank you for your reply.
After understanding the way sparse matrices are stored, one can easily build a diagonal sparse matrix of size N with vector u as diagonal as follows:

Diag_u   = CuSparseMatrixCSC{Float64,Int32}( colptr, rowval ,  nzval ,dims)
where,
nzval = CuArray(u)
colptr  = CuArray(1:1:N)
rowval = CuArray(1:1:N) 
dims = (N,N)

@maleadt
Copy link
Member

maleadt commented May 2, 2023

Would be great to have that as an implementation of the SparseArrays.spdiagm method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants