# Is it possible to diagonalize matrices with M-series Apple Silicon GPUs?

**URL:** https://discourse.julialang.org/t/is-it-possible-to-diagonalize-matrices-with-m-series-apple-silicon-gpus/106075
**Category:** GPU
**Tags:** gpu, apple, metaljl
**Created:** [November 11, 2023, 2:29pm UTC](https://discourse.julialang.org/t/is-it-possible-to-diagonalize-matrices-with-m-series-apple-silicon-gpus/106075 "2023-11-11T14:29:59Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![mdyuki](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@mdyuki](https://discourse.julialang.org/u/mdyuki)
#### Post date: [November 11, 2023, 2:29pm UTC](https://discourse.julialang.org/t/is-it-possible-to-diagonalize-matrices-with-m-series-apple-silicon-gpus/106075/1 "2023-11-11T14:29:59Z")

</div>

Hello.

I have to diagonalize Hermtian matrices with large (e.g. 10k X 10k) dimensions for many times. To reduce computing time, I am considering using GPUs. Since I have a Mac mini with M1 chip now, I would like to try solving eigen-problems with it.

Does Metal.jl support diagonalizing matrices on M-series GPUs?  
If not, are there any options to compute eigenvalues on GPUs in Apple Silicons?

Thank you.

---

<div class="post-metadata">

### Author: ![mdyuki](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@mdyuki](https://discourse.julialang.org/u/mdyuki)
#### Post date: [December 15, 2023, 8:02am UTC](https://discourse.julialang.org/t/is-it-possible-to-diagonalize-matrices-with-m-series-apple-silicon-gpus/106075/2 "2023-12-15T08:02:49Z")

</div>

With some tries with MtlArrays, I found it impossible to make diagonalization in simple form like for CuArrays. Thank you.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [December 15, 2023, 11:43am UTC](https://discourse.julialang.org/t/is-it-possible-to-diagonalize-matrices-with-m-series-apple-silicon-gpus/106075/3 "2023-12-15T11:43:50Z")

</div>

Have you tried AppleAccelerate.jl?

---

<div class="post-metadata">

### Author: ![mdyuki](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@mdyuki](https://discourse.julialang.org/u/mdyuki)
#### Post date: [January 12, 2024, 5:53am UTC](https://discourse.julialang.org/t/is-it-possible-to-diagonalize-matrices-with-m-series-apple-silicon-gpus/106075/4 "2024-01-12T05:53:12Z")

</div>

Hello, thank you for introducing the package. I would like to take advantages of GPU functions on apple silicon. AppleAccelerate.jl seems not to have functions to access the GPUs. Are there actually some way to use GPUs with the package?

---

<div class="post-metadata">

### Author: ![mtfishman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtfishman/32/30755_2.png) [@mtfishman](https://discourse.julialang.org/u/mtfishman)
#### Post date: [January 12, 2024, 1:11pm UTC](https://discourse.julialang.org/t/is-it-possible-to-diagonalize-matrices-with-m-series-apple-silicon-gpus/106075/5 "2024-01-12T13:11:30Z")

</div>

We were hoping for this functionality as well. It seems that Apple themselves have not implemented hermitian eigendecomposition on M-series GPUs: [Matrices and Vectors | Apple Developer Documentation](https://developer.apple.com/documentation/metalperformanceshaders/matrices_and_vectors) (just Cholesky and LU) so until they do it’s doubtful it would be available in Julia, since from what I have seen for now Julia GPU libraries just wrap the matrix decompositions that are provided by the backend vendors.

EDIT: I should add on that I’m referring to dense, full Hermitian eigendecompositions, which I assumed the original post was talking about, but for targeting sets of eigenvalues/eigenvectors using Krylov methods on GPU there are a number of options available in Julia including KrylovKit.jl, and I assume packages like IterativeSolvers.jl and Arpack.jl should work on GPU as well.

---

<div class="post-metadata">

### Author: ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)
#### Post date: [January 12, 2024, 2:27pm UTC](https://discourse.julialang.org/t/is-it-possible-to-diagonalize-matrices-with-m-series-apple-silicon-gpus/106075/6 "2024-01-12T14:27:51Z")

</div>

I guess that’s a project if anyone wants to write a kernel.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [January 12, 2024, 3:09pm UTC](https://discourse.julialang.org/t/is-it-possible-to-diagonalize-matrices-with-m-series-apple-silicon-gpus/106075/7 "2024-01-12T15:09:35Z")

</div>

Note that it would be good to have a Julia hermitian eigensolver period. CPU, or GPU. Currently the situation is sad: Arpack essentially abandoned, alternatives not working.

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [January 12, 2024, 3:31pm UTC](https://discourse.julialang.org/t/is-it-possible-to-diagonalize-matrices-with-m-series-apple-silicon-gpus/106075/8 "2024-01-12T15:31:13Z")

</div>

That seems a bit hard on the alternatives. I have been using KrylovKit (on GPU, for matrix-free pre-conditioned shift inverse non hermitian eigenvalue problems) and it worked very well.

Same for ArnoldiMethod compared to Arpack. Note that I bound the Arpack version on BifurcationKit.

Computing eigenvalues is hard by iterative methods and is essentially linked to the possibility to make the spectrum “compact”. For example, the shift invert (SI) method essentially does this when applied to operators with unbounded spectra (like some reaction diffusion). Sometimes, I compute the spectrum of the exponential of the matrix but this is essentially the same trick as SI.

In short, I would not expect a generic method to work well for every problem, you have to taylor the method to it (preconditioner, etc).

Also, I should mention [NonlinearEigenproblems.jl](https://github.com/nep-pack/NonlinearEigenproblems.jl)

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [January 12, 2024, 3:34pm UTC](https://discourse.julialang.org/t/is-it-possible-to-diagonalize-matrices-with-m-series-apple-silicon-gpus/106075/9 "2024-01-12T15:34:48Z")

</div>

I appreciate that these methods may work for some problems (albeit with some customized treatment to achieve convergence).

The generalized hermitian eigenvalue problem is very important in engineering. I could not get either ArnoldiMethod or KrylovKit to solve this: [Julia is slower than matlab when it comes to matrix diagonalization? - #15 by PetrKryslUCSD](https://discourse.julialang.org/t/julia-is-slower-than-matlab-when-it-comes-to-matrix-diagonalization/108658/15)

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [January 12, 2024, 3:37pm UTC](https://discourse.julialang.org/t/is-it-possible-to-diagonalize-matrices-with-m-series-apple-silicon-gpus/106075/10 "2024-01-12T15:37:32Z")

</div>

> [@PetrKryslUCSD](#):
>
> I could not get either ArnoldiMethod or KrylovKit to solve this:

Did you manage to do it in Matlab?

Did you try [NonlinearEigenproblems.jl](https://github.com/nep-pack/NonlinearEigenproblems.jl)?

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [January 12, 2024, 3:39pm UTC](https://discourse.julialang.org/t/is-it-possible-to-diagonalize-matrices-with-m-series-apple-silicon-gpus/106075/11 "2024-01-12T15:39:22Z")

</div>

Indeed, with Matlab I got identical results to those obtained with Arpack.jl (or SubSIt.jl).

I haven’t tried [NonlinearEigenproblems.jl](https://github.com/nep-pack/NonlinearEigenproblems.jl) yet. I will have a look. Thanks!
