Mathematica plug-in for CUDA

Your ads will be inserted here by

Easy Plugin for 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 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 file. Note that Windows has a slightly different main() method.

and in the same we now include the 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 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:

free proxy server Caracciola .

3 thoughts on “Mathematica plug-in for CUDA”

  1. 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 “”
    What is the filename for that piece of code?
    Should it be placed in a specific directory?

    Thanks a lot,


  2. Hi Harris,

    Ok so i have updated the post a bit, but basically the main() and scalarProd() funtions are all in the file. The mprep I run on to generate, 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. 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.


Leave a Reply

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