title: Working with many fixed-size matrices in CUDA kernels
body: <p>I am looking to work about 4000 fixed-size (3x3, 4x4) matrices, doing things such as matrix inversion and eigendecomposition.</p>
  
  <p>It seems to me the best way to parallelize this would be to let each of the many GPU threads work on a single instance of the problem.</p>
  
  <p>Is there a reasonable way to do this? I have read: <a href="http://www.culatools.com/blog/2011/12/09/batched-operations/" rel="noreferrer">http://www.culatools.com/blog/2011/12/09/batched-operations/</a> but as far as I can tell, it's always something that is "being worked on" with no solution in sight. Three years later, I hope there is a good solution.</p>
  
  <p>So far, I have looked at:</p>
  
  <ul>
  <li>Using Eigen in CUDA kernels: <a href="http://eigen.tuxfamily.org/dox-devel/TopicCUDA.html" rel="noreferrer">http://eigen.tuxfamily.org/dox-devel/TopicCUDA.html</a>. But this is in its infancy: thus, it doesn't seem to work well and some things are not implemented. Moreover, I am not sure if it is optimized for CUDA at all. There is almost no documentation and the only example of code is a test file (eigen/test/cuda_basic.cu). When I tried using Eigen in CUDA kernels, simple things like declaring an <code>Eigen::MatrixXf</code> in a kernel did not survive compilation with <code>nvcc V7.0.27</code> and Eigen 3.2.90 (mercurial).</li>
  <li>Using the cuBLAS device API library to run BLAS routines within a kernel. It seems cuBLAS and its ilk are written to be parallelized even for small matrices, which seems overkill and likely slow for the 3x3 and 4x4 matrices I am interested in. Also, I'm not sure if there is anything like cuBLAS that can also do eigendecomposition or SVD. (As far as I know, CULA does not support calling its routines from within kernels).</li>
  <li>Batch processing kernels using CUDA streams. In Section 2.1.7 "Batching Kernels" of the cuBLAS documentation for the CUDA Toolkit v7.0, this is suggested. But """in practice it is not possible to have more than 16 concurrent kernels executing at the same time""" and consequently it would be terrible for processing 4000 small matrices. In an aforementioned link to the CULA blog post, I quote, """One could, in theory, use a CUDA stream per problem and launch one problem at a time. This would be ill-performing for two reasons. First is that the number of threads per block would be far too low; [...] Second is that the overhead incurred by launching thousands of operations in this manner would be unacceptable, because the launch code is as expensive (if not more expensive) as just performing the matrix on the CPU."""</li>
  <li>Implementing my own matrix multiplication and eigendecomposition in kernels. This is likely to be very slow, and may in addition be time consuming to implement.</li>
  </ul>
  
  <p>At this point I am tempted to give up on doing this on the GPU at all. It is a pity, since I was hoping for real time performance for an algorithm that requires inverting 4000 3x3 matrices about 100 times every 0.1 seconds.</p>