# Creating sparse diagonal matrix of CuArray

**URL:** <https://discourse.julialang.org/t/creating-sparse-diagonal-matrix-of-cuarray/97108>\
**Category:** General Usage\
**Tags:** gpuarrays, cuda, sparse\
**Created:** [April 5, 2023, 9:40am UTC](https://discourse.julialang.org/t/creating-sparse-diagonal-matrix-of-cuarray/97108 "2023-04-05T09:40:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hemsh94](https://avatars.discourse-cdn.com/v4/letter/h/c6cbf5/32.png) [@Hemsh94](https://discourse.julialang.org/u/Hemsh94)\
**Post date:** [April 5, 2023, 9:40am UTC](https://discourse.julialang.org/t/creating-sparse-diagonal-matrix-of-cuarray/97108/1 "2023-04-05T09:40:42Z")

</div>

Hello everyone,

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

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

I tried using CuSparseMatrixCSC(Diagonal(u)), it works but it is very slow. Is there a way to perform this task efficiently?

I am relatively new to CUDA so any help is appreciated.

Thank you.
