Search code examples
c++matrixcudagpucublas

How to do element wise exponential for a matrix in Cuda programming


How to do element wise exponential for a matrix in Cuda programming?

for example:

A = [1 3 4; 6 5 2];

I want to compute:

B = [exp(1),exp(3),exp(4); exp(6);exp(5);(2)]

Is there a way of doing it efficiently and doing it in place (i.e, B replaces A)? It seems cublas does not provide element wise operation on matrix.


Solution

  • I don't know if libraries exist that do element wise operations on matrices, but you could easily set up a CUDA kernel to do this job. You could for instance give one element of the A matrix to each thread, and they could perform the exponential and write the answer in B. You then call your CUDA kernel as usual. Take a look at this to get an idea of how to implement your kernel and how to call it (but instead of multiplying two vectors like they do in gpuMM you would do an exponential).

    EDIT: It looks like you can do element wise operations using Thrust and the set of macros Newton, as is shown in this SO question.