Mathematica plug-in for CUDA 3

Posted by kashif on June 26, 2008

Since there is a Matlab plug-in for CUDA that provides some examples of off-loading computation to the GPU, I thought it might be neat to have something similar for Mathematica. So as a start, I decided to try out a simple scalar product example using MathLink.

The initial template of my function is in the scalarProd.tm file:

which describes the ScalarProd[] function in Mathematica, and links it to the scalarProd() C method, which is where we receive the two arrays from Mathematica and use CUDA to calculate their scalar product and send the result back. This and the main() function for Linux and Mac, which is what I was using, are in the scalarProd.cu file. Note that Windows has a slightly different main() method.

and in the same scalarProd.cu we now include the scalarProd_kernel.cu kernel from CUDA’s SDK together with our scalarProd() C function:

Now we are ready to run Mathematica’s mprep pre-processor from MathLink to generate a scalarProdtm.cu file, and on this we run CUDA’s compiler nvcc and compile everything with the appropriate CUDA and MathLink libraries to generate our scalarProd binary, which we can now call from within Mathematica:

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Harris Wed, 16 Jul 2008 02:16:28 UTC

    Hi, this looks promising!

    I am trying to follow your instructions, but I have 3 questions:
    Where does 3d piece of code go?
    (The piece that starts with: #include “scalarProd_kernel.cu”
    What is the filename for that piece of code?
    Should it be placed in a specific directory?

    Thanks a lot,

    Harris

  2. admin Wed, 16 Jul 2008 11:20:02 UTC

    Hi Harris,

    Ok so i have updated the post a bit, but basically the main() and scalarProd() funtions are all in the scalarProd.cu file. The mprep I run on scalarProd.tm to generate scalarProdtm.cu, and then the three .cu files (which are all in the same directory level) I compile with the nvcc compiler, making sure I have the correct MathLink library and include paths.

    I was gonna clean this up into a nice Makefile, so I will post here when its ready.

    Thanks again for the feedback.

  3. grenander Wed, 19 Nov 2008 12:11:00 UTC

    Hello,

    Did you have a chance to create a Makefile? I am trying to learn CUDA as I’d like to increase Mathematica’s ListCorrelate in certain cases and I luckily saw your blog post. I am familiar with mathlink but not with the CUDA tools like nvcc.

    Thanks,
    Art.

Comments