Inverse of a 3x3 matrix


I would like to perform two steps:

  1. Find inverse of a 3x3 matrix, A

  2. compute “inv(A) * B” where B is also a 3x3 matrix.

In order perform above two steps what is the best method?

I wrote a kernel of my own for computing the matrix inverse. but it takes 0.5 ms for kernel configuration of <<<1,441>>>. Is there a better method to compute the above two steps?


If you have a single 3x3 matrix to invert, it’s probably not an interesting problem for the GPU.

If you have multiple matrices to do at the same time, you could try the cublas functions getrfBatched and getriBatched. There are many examples demonstrating their use for inversion such as here:

and a device-callable version is demonstrated here:

Also, there is some batched solver code available here:

You’ll need to log in as a registered developer to access that link.

Hi txbob,

Thanks for providing the details.

  1. I tried the example given in link

I use Nsight eclipse IDE to compile with cuda 7.5 on ubuntu 14.04 (64 bit OS). Now when i attempt to compile the code the error below comes :

[b] make all
Building file: …/
Invoking: NVCC Compiler

…/ error: argument of type “const float **” is incompatible with parameter of type “float **”

…/ warning: argument of type “float **” is incompatible with parameter of type “const float **”

1 error detected in the compilation of “/tmp/tmpxft_00004a6b_00000000-16_matrix_invert_forum.compute_35.cpp1.ii”.
make: *** [matrix_invert_forum.o] Error 2[/b]

Could you suggest the cause for this error?

  1. Next, i tried the device-callable version is demonstrated here:

Here i get the error as: ptxas fatal : Unresolved extern function ‘cublasCreate_v2’
make: *** [matrix_invert_forum_device.o] Error 255

Upon searching i could only find options to set in Visual studio. Is there a way to resolve this error using nsight?


The first problem is because the CUBLAS API has changed slightly since that code was posted. Just cast the arguments to the necessary type.

The second problem is because you have not set up the VS project to correctly link to the CUBLAS library. Either follow one of the sample codes/projects that use the CUBLAS API, or follow these instructions:

That project will also require setup for the CUBLAS device API and RDC code generation. There is a CUBLAS sample project simpleDevLibCublas or something like that which will show all the necessary settings.


I tried to cast arguments as below:

const float A[] = { src_d };
* A_d;
const float A_d;
cudacall(cudaMalloc<const float


But still i get errors below:

error: no instance of overloaded function “cudaMalloc” matches the argument list
argument types are: (const float **, unsigned long)

error: argument of type “const float *” is incompatible with parameter of type “void *”

error: argument of type “const float *” is incompatible with parameter of type “float **”

error: argument of type “const float *” is incompatible with parameter of type “const float **”

Could you suggest how to fix these errors.


cast the pointers immediately before using them in the cublas function.

Hi txbob,

I did casting as below:

[b] float A[] = { src_d };
* A_d;
cudacall(cudaMalloc< float*>(&A_d,sizeof(A)));

const float *A_d_new[] = {(const float *) A_d};

There are no compilation errors. But when i try to run, i get an error as below:

0.000000 3.000000 4.000000
1.000000 3.000000 10.000000
4.000000 9.000000 16.000000

CUDA Error:
File = …/
Line = 76
Reason = an illegal memory access was encountered[/b]

Could you please suggest how to resolve this.


For the code in the answer here:

The only change you have to make to get it to compile (and run correctly) is to change this:


to this:

cublascall(cublasSgetriBatched(handle,n,(const float **)A_d,lda,P,C_d,n,INFO,batchSize));

Hi txbob,

Thank you. I could compile the code successfully and obtain the inverse of matrix.

I have one more doubt. When i tried to find the inverse of matrix using device-callable version as in:

Here i get the error as: ptxas fatal : Unresolved extern function ‘cublasCreate_v2’

I am working in NVIDIA Nsight IDE on ubuntu 14.04, 64 bit OS. In this case, i do not find the options you had mentioned for Visual Studio.

Could you please suggest how to resolve this error in this scenario.


Your project settings are not correct. You need to link against the cublas library. You also need to link against the device cublas library. You also need to specify relocatable device code generation and linking. Open the simpledevlibcublas sample project:–cuda-dynamic-parallelism-

and study that. If you’re unable to do that, you could just drop the code into that sample project and compile it that way.

Hi txbob,

Thanks for sharing the link. In project settings I made changes and it works. I measured the time taken to compute the inverse of a 3x3 matrix using both the approaches:

  1. cublas functions getrfBatched and getriBatched (0.027 ms)

  2. device callable cublas functions (1.149 ms)

I observed the time taken due to second method (ie Dynamic Parallelism) is more than first method. Is this due to the overhead involved in launching kernels from the device?

Logically thinking if the data is in device memory, then dynamic parallelism should take less time right?

Could please clarify the reason why device callable function is taking more time?

No I wouldn’t be able to explain the differences.

None of this is interesting from a performance standpoint. Inversion of a 3x3 matrix is not an appropriate size problem for a GPU, and in particular these cublas functions are specifically indicated in the cublas documentation that they are not high-performance.

You would use these functions when the data is already on the GPU, as part of a larger GPU application. In such a situation, the perf difference between 0.027ms and 1.149ms should be irrelevant.

If you’re interested in high performance large matrix inversion, these are not the methodologies to use, and that is already indicated in the cublas documentation.

Am I allowed to ask why you are performing a 3x3 matrix inversion on the GPU?

As txbob has already stated there is no reason why you would ever perform a 3x3 matrix inversion on the GPU as the data transfer time is exorbitant in this case.

Do you plan to do a larger sized matrix inversion? If so using this as an example to teach yourself also doesn’t make sense.

Are you trying to learn how to do CUDA? If this is the case, there are plenty of other examples that exist that are much better.

I’ve had use-cases where I needed like N 3x3 inverses so that mapped to the GPU. But I couldn’t imagine performing a 3x3 inverse on more than just one thread.


Thanks for your responses. Basically, I need to compute inverse value for ‘N’ 3x3 matrices in parallel. Value of N will range from 2000 to 4000. These 3x3 matrices shall be extracted from large images of 588x2048 resolution

This is the reason why i was interested in measuring the time. Also this matrix inversion is one step among a sequence of computations of an algorithm.


A 3x3 inverse should actually be pretty easy to code by hand. You can likely just visit the wiki page and then translate those programs literally into code.

My thoughts exactly. 3x3 is small enough from a register usage aspect to have each thread handle the computation of one inverse matrix. This then requires 4000 threads to handle the entire batch, which represents a light load for a GPU.

If you go to the CUDA registered developer website, you can download a package called “batched solver” that provides batched matrix inversion code (BSD license). It’s a bit dated but should be enough to get you started. Look for the functions [s|c|d|z]matinv_batch():

/* smatinv_batch() inverts one or many square, non-singular matrices of single-
   precision elements. Partial pivoting is employed in the inversion process 
   for increased numerical stability.

   A     pointer to an array of the single-precision matrices to be inverted, 
         where each matrix is stored in column-major order
   Ainv  pointer to an array of the single-precision matrices which receive
         the inverses of the corresponding matrices pointed to by A, where 
         each matrix is stored in column-major order
   n     number of rows and columns of the matrices in the arrays pointed to 
         by A and Ainv. n must be greater than, or equal to 2. On sm_13 GPUs,
         n must be less than, or equal to, 62. On sm_2x and sm_3x GPUs, n must
         be less than, or equal to, 109.
   batch the number of matrices to be inverted. It must be greater than zero.


    0    operation completed successfully
   -1    n is out of bounds, batch is out of bounds
   -2    a CUDA error occured

For your use case, this should drop down to this kernel in

__global__ void matinv_3x3_matrix_per_thread (const T *A, T *Ainv, int batch)

You may use Cramer’s Rule for the inversion and expand the matrix multiplication explicitly and factor out the repeated computations. Store the matrices a 9 number linear vector may help as well.