Mathematica plug-in for CUDA

Your ads will be inserted here by

Easy AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

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:

3 thoughts on “Mathematica plug-in for CUDA

  1. Harris

    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 Post author

    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

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>