TLDR; Is there a way to solve linear equations of a sparse matrix (discretized laplace operator) efficiently using CUDALink in Mathematica? I didn't find a CUDALinearSolve or CUDAMatrixInverse or something in the documentation. This is my first time working with CUDA link.
The long version: Our prof gave us a project to solve a Poisson equation (laplace Phi = 4pi * rho) in 2D. I discretized the Laplacian to an n² x n² matrix given by ({-4 for i=j, 1 for |i-j|=1 or |i-j|=n}). (*)
I get solutions in reasonable time for up to n=100, using LinearSolve[N[laplace],rho], but our programs are judged by performance and this isn't really too well performing....
Unfortunately we were specifically tasked to only use Java, Mathematica or Python, I don't know python and well, Java is Java, so Mathematica is really my only shot. But since I am basically throwing around matrices all day I though my a GPU might be better equipped to handle this and Mathematica has CUDA integration...
I looked through the CUDALink documentation and didn't find anything helpful, which is super surprising to me, since this should be a fairly standard use-case of CUDA. Does anyone have an idea what I could use?
I found cusolver in the CUDA documentation and know that CUDAFunctionImport is a thing, but I have no clue how to bring these components together
(*) - There is something weird with this discretization though, as my results seem to represent the surface of a cylinder not a flat plane, but this is besides the point
SparseArray. Try alsoLinearSolvewith the option'Method->"Pardiso". On my machine,"Pardiso"factorizes the 5-stencil Laplacian of a $1120 \times 1120$ in about 4.2 seconds while the standard solver for sparse matrices ('Method->"Pardiso") needs about 14.8 seconds for the same task. – Henrik Schumacher May 28 '18 at 22:15SparseArrayin the documentation. (my sparse Laplacian matrix is of size $1256641 \times 1256641$ and needs only 190 MB - because spare arrays store only the nonzero-entries and its positions. – Henrik Schumacher May 28 '18 at 22:32Normalconverts back to a dense matrix. Now that you ask me, I realize that I was using a finite-element Laplacian which is build differently. Anyways, this one should work better:A = SparseArray[{ {i_, i_} :> -4, {i_, j_} /; Abs[i - j] == n :> 1, {i_, j_} /; j == i + 1 && Mod[i, n] != 0 :> 1, {j_, i_} /; j == i + 1 && Mod[i, n] != 0 :> 1 }, {n^2, n^2}]. Plot it for smalln = 4withMatrixPlotand see what the difference is (and think about why there has to be this difference). – Henrik Schumacher May 28 '18 at 23:28A = 1. AdjacencyMatrix[GridGraph[{n, n}]] - 4. IdentityMatrix[n^2, SparseArray];. Run also thisGridGraph[{n, n}, VertexLabels -> "Name"]for smallnto see how the vertices are numbered. – Henrik Schumacher May 28 '18 at 23:40With this laplacian n=1000 is no problem (well, the plotting is a problem, but screw that :D)
– Chalky May 28 '18 at 23:48Aber nochmal Danke!
– Chalky May 28 '18 at 23:56FiniteDifferenceDerivative: I'm listing a bunch of approaches in my answer to Schrödinger eigenvalue problem in two dimensions (Harmonic Oscillator). – Jens May 29 '18 at 01:17