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:

HarrisHi, 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

adminPost authorHi Harris,

Ok so i have updated the post a bit, but basically the

main()andscalarProd()funtions are all in thescalarProd.cufile. ThemprepI run onscalarProd.tmto generatescalarProdtm.cu, and then the three.cufiles (which are all in the same directory level) I compile with thenvcccompiler, 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.

grenanderHello,

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.