Linpack on Nvidia GPUs
I always like to run the mostly-meaningless Linpack benchmark on any new hardware I come across. So herewith a record of my attempts on Nvidia graphics cards. This page covers compiled languages, a separate page exists for Linpack using python and PyTorch and JAX.
Magma
The MAGMA library is for "Matrix Algebra on GPU and Multi-core Architectures" and contains routines equivalent to many Lapack routines, including gesv, the core of Linpack.
On Ubuntu, installation is easy:
# apt-get install libmagma-dev
The C source on my Linpack sources page needed a small number of changes.
- Adding
#include <magma_v2.h> - Adding a call to
magma_init()just before the timed section - Changing the Fortran-style
#define dgesv dgesv_ dgesv(&nn,&one,a,&nn,ipvt,b,&nn,&info);to C-likemagma_dgesv(nn,1,a,nn,ipvt,b,nn,&info); - Adding a call to
magma_finalize()just before the timed section
The modified source
being linpack_magma.c.
Compilation is simple:
$ gcc -O linpack_magma.c -lmagma
As is running it, either as a.out
or a.out 5000 if one wishes to specify the matrix
size.
Results on an overclocked GTX 1650:
GPU Single core All cores
1000x1000: 3 GFLOPS 44 GFLOPS 44 GFLOPS
2000x2000: 19 GFLOPS 47 GFLOPS 47 GFLOPS
5000x5000: 77 GFLOPS 53 GFLOPS 53 GFLOPS
10000x10000: 103 GFLOPS 56 GFLOPS 257 GFLOPS
20000x20000: 108 GFLOPS 58 GFLOPS 289 GFLOPS
The host computer was a Ryzen 5 3600. Like the graphics card, old and cheap. The GPU has a large start-up overhead, and is beaten by a single core for array sizes below 3000x3000. But for large arrays it can beat a single core by a reasonable margin. The multicore library does not attempt to use more than a single core for matrix sizes of 5000 and below. Once the matrix is big enough for multiple cores, it can beat the GPU easily.
The GPU also suffers from its high core count. This GPU has "only" 896 cores, but using 896 cores effectively when performing row operations on a matrix whose rows have only a couple of thousand elements will be hard.
(The GTX 1650 with GDDR6 memory was introduced in 2020 at $150, the Zen 2-based Ryzen 3600 in 2019 at $200.)
But what happens if we change to single precision, by
replacing double by float,
and dgesv by sgesv? (See
linpack_magma_s.c.)
GPU Single core All cores
1000x1000: 3 GFLOPS 76 GFLOPS 76 GFLOPS
2000x2000: 27 GFLOPS 92 GFLOPS 92 GFLOPS
5000x5000: 286 GFLOPS 101 GFLOPS 101 GFLOPS
10000x10000: 970 GFLOPS 110 GFLOPS 110 GFLOPS
20000x20000: 1667 GFLOPS 111 GFLOPS 111 GFLOPS
30000x30000: 1905 GFLOPS 114 GFLOPS 114 GFLOPS
Very different. The 4GB graphics card can now accept a slightly larger array, and is almost twenty times faster. The expected speed difference is anything up to a factor of thirty-two. The CPU is expected to be twice as fast on a single core, and is. Strangely the OpenBLAS library failed to use more than a single core, so the all core performance was worse than the double precision result.
cuSOLVER
The cuSOLVER library has some LAPACK-equivalent routines. But they
take different arguments, possibly use a different algorithm for the
solution, and do not handle copying data from the CPU to the GPU
themselves. So I wrote a wrapper to
enable cusolverDnDDgesv to be used as a drop-in
replacement for dgesv, and similarly for the single
precision version. The wrapper,
cuda_gesv.c was around
150 lines.
$ gcc -O linpack.c cuda_gesv.c -lcusolver -lcudart
The performance is slightly slower than MAGMA, much slower for small sizes, possibly because the initialisation is now in the timed section. But the cuSOLVER library needs more workspace, and the largest double precision array is now 15000x15000, dispatched at 103 GFLOPS, and the largest single precision array is 21500x21500, which runs at 1522 GFLOPS.
So MAGMA was much easier to use, and ran slightly faster. MAGMA also offers a mode in which the arrays are taken directly from the graphics card, rather than copied from the host.
But cuSOLVER does have one useful trick. It can use a mixed precision solver, which first solves the problem using single precision arithmetic, and then iterates to achieve full double precision accuracy. At an array size of 15000x15000, the residual (an estimate of the error) calculated by this method is actually slightly lower than the double precision MAGMA routine. But whereas MAGMA takes 21 seconds, cuSOLVER takes under 2 seconds. MAGMA can also do this trick, but it does not offer such a routine for the case where the arrays are on the CPU. It would need a large wrapper very similar to the cuSOLVER one to replace a dgesv call using this approach.
If using the above wrapper, consider
replacing cusolverDnDDgesv by
cusolverDnDSgesv (both the buffer size and main
execution calls).