Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Passing GPU Pointers through MEX interface

Subject: Passing GPU Pointers through MEX interface

From: Matthew

Date: 16 Feb, 2011 12:52:03

Message: 1 of 7

Hi

If there's anyone who's experienced with using the MEX interface and/or
using the MEX interface with CUDA, i'd really appreciate your help...

I have an optimisation algorithm for which I've implemented the
cost function evaluation on TESLA GPU with CUDA. The cost function
requires that a massive candidate set of spacial tracks defined by an
average of 200-300 control points each is reduced to the fittest set
to fit MRI data (the number of tracks is between 100,000 - 1,000,000).

I am going to use a SOMA algorithm, which is already completely implemented
in MATLAB, to select population members from the track database. Hence
in each iteration, i don't want to pass a whole set of tracks to the GPU
each time, just a vector of 0s and 1s defining which tracks are to be
selected and evaluated.

Therefore there will be various datasets existing in GPU DRAM that i
want to remain there for the entire duration of the algorithm, however
what i don't know is how i use these datasets while invoking the
CUDA MEX function as part of an iterative loop in MATLAB. Can i pass pointers
to the data on the GPU through the MEX interface referencing
the appropriate datasets on the GPU and then feed that back
into the MEX function for the next iteration of the loop? Or perhaps
there is another way of doing this?

Many thanks

Matt

Subject: Passing GPU Pointers through MEX interface

From: Edric M Ellis

Date: 16 Feb, 2011 13:05:02

Message: 2 of 7

"Matthew " <matthew.rowe.09@ucl.ac.uk> writes:

> If there's anyone who's experienced with using the MEX interface
> and/or using the MEX interface with CUDA, i'd really appreciate your
> help...
>
> I have an optimisation algorithm for which I've implemented the cost function
> evaluation on TESLA GPU with CUDA. The cost function
> requires that a massive candidate set of spacial tracks defined by an
> average of 200-300 control points each is reduced to the fittest set
> to fit MRI data (the number of tracks is between 100,000 - 1,000,000).
>
> I am going to use a SOMA algorithm, which is already completely implemented
> in MATLAB, to select population members from the track database. Hence
> in each iteration, i don't want to pass a whole set of tracks to the GPU
> each time, just a vector of 0s and 1s defining which tracks are to be
> selected and evaluated.
>
> Therefore there will be various datasets existing in GPU DRAM that i
> want to remain there for the entire duration of the algorithm, however
> what i don't know is how i use these datasets while invoking the CUDA
> MEX function as part of an iterative loop in MATLAB. Can i pass
> pointers to the data on the GPU through the MEX interface referencing
> the appropriate datasets on the GPU and then feed that back into the
> MEX function for the next iteration of the loop? Or perhaps there is
> another way of doing this?

Rather than using CUDA Mex files, you should be able to do this using
GPUArrays and the CUDAKernel objects in Parallel Computing Toolbox with
R2010b. See for example:

http://www.mathworks.com/help/toolbox/distcomp/bslohnr-1.html

You should be able to create your datasets as GPUArrays and then have
your CUDAKernels operate on them.

Cheers,

Edric.

Subject: Passing GPU Pointers through MEX interface

From: Oliver Woodford

Date: 16 Feb, 2011 13:30:06

Message: 3 of 7

"Matthew" wrote:
> Hi
>
> If there's anyone who's experienced with using the MEX interface and/or
> using the MEX interface with CUDA, i'd really appreciate your help...
>
> I have an optimisation algorithm for which I've implemented the
> cost function evaluation on TESLA GPU with CUDA. The cost function
> requires that a massive candidate set of spacial tracks defined by an
> average of 200-300 control points each is reduced to the fittest set
> to fit MRI data (the number of tracks is between 100,000 - 1,000,000).
>
> I am going to use a SOMA algorithm, which is already completely implemented
> in MATLAB, to select population members from the track database. Hence
> in each iteration, i don't want to pass a whole set of tracks to the GPU
> each time, just a vector of 0s and 1s defining which tracks are to be
> selected and evaluated.
>
> Therefore there will be various datasets existing in GPU DRAM that i
> want to remain there for the entire duration of the algorithm, however
> what i don't know is how i use these datasets while invoking the
> CUDA MEX function as part of an iterative loop in MATLAB. Can i pass pointers
> to the data on the GPU through the MEX interface referencing
> the appropriate datasets on the GPU and then feed that back
> into the MEX function for the next iteration of the loop? Or perhaps
> there is another way of doing this?
>
> Many thanks
>
> Matt

If you do want to use mex files, then create a C++ class around your algorithm, and interface to it using a MATLAB class, using the approach described here:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/278243
All the pointers (whether gpu or cpu based) are stored in the class and not passed back to MATLAB (only the class pointer is passed back), so it doesn't make any difference whether you're using gpu or cpu.

Subject: Passing GPU Pointers through MEX interface

From: John Melonakos

Date: 18 Feb, 2011 03:34:04

Message: 4 of 7

Another way to do this fast is with the Jacket SDK, which will help you avoid the costly MATLAB data load times. Learn more about how the Jacket SDK trumps standard MEX, here: http://blog.accelereyes.com/blog/2010/10/29/jacket_sdk_trumps_mex/

Subject: Passing GPU Pointers through MEX interface

From: Matthew

Date: 18 Feb, 2011 09:57:03

Message: 5 of 7

All good ideas.

Many thanks for all your replies.

Subject: Passing GPU Pointers through MEX interface

From: Andrew

Date: 19 Jun, 2013 05:03:13

Message: 6 of 7

Have you resolved this issue? I'm trying to do this exact thing to minimize overhead.

I want to put data on the gpu in one mex function call from matlab. I want to return a pointer to matlab, and then call functions with this pointer as input.

My simple test setup:
mex function A moves data to GPU, returns a pointer.
mex function B take the pointer as input, copies data from gpu, outputs this data.

To output the pointers to matlab, I cast them as uint64. Then I recast in mex function B to float pointers (my test gpu only can handle floats).

What are the potential problems? I read a thread by Oliver, and I don't know why encapsulating these pointers in a handle type class would make a difference. I'm not opposed to using this structure if I understand the advantages. I'd just rather a pure C solution if I could manage. (Admittedly, I wasn't aware of how matlab managed memory; his solution seems to be careful to avoid memory leaks caused by matlab, which is something I hadn't even considered.)

Maybe this is more of a CUDA question, but my setup fails in part B to copy data from the GPU to CPU. I have verified that I pass the pointer address correctly.

Subject: Passing GPU Pointers through MEX interface

From: Oliver Woodford

Date: 19 Jun, 2013 09:10:12

Message: 7 of 7

"Andrew" wrote:
> What are the potential problems? I read a thread by Oliver, and I don't know why encapsulating these pointers in a handle type class would make a difference.

Encapsulating the pointer in the same way as this example:
http://www.mathworks.com/matlabcentral/fileexchange/38964-example-matlab-class-wrapper-for-a-c++-class
has the following benefits:
1) It won't allow you to accidentally cast any value to a pointer, creating a segmentation fault.
2) It ensures that memory is freed when you are done with the pointer, no matter how your program exits, avoiding memory leaks.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us