开发者

Compile a matlab m-file into a usable C-function

Hey there, is there a way to compile (or better to say: 'translate') a matlab m-function into a C-function so that I can use it in the CUDA kernel of my mex file? than开发者_Go百科ks a lot!


MATLAB Coder will generate C code for mex files. I do not yet have a copy to evaluate, so I can't speak with any authority about the quality and nature of the generated code.

However, if I had to guess, I'd say the generated code would likely require a lot of massaging to get it working on your GPU. You may have better luck with a product like Jacket, depending on what you're doing.


You can call a matlab (m or mex) function from C / fortran using this function call. You could then interface that with along with your CUDA kernel.

However it may not be the most efficient way to do things. You could write your own C code for the m file that you have or look it up on matlab central if any one else has done it.


The C function will call eventually set-up device variables and call a CUDA kernel?

I originally wanted to try this for a project because I thought this method would be easier than converting all of my MATLAB code to C first, but I ended up doing that anyway.

There are some user created MATLAB scripts to help provide this functionality, but since they aren't from the Mathworks you'll have to use them at your own risk. I tried them and never found anything malicious, but you never know. I couldn't get them to work with my project due to its specific complications but it should work for simpler tasks.

1) NvMEX: This is directly from Nvidia. http://developer.stage.nvidia.com/matlab-cuda http://www.mathworks.com/discovery/matlab-gpu.html

2) CUDA MEX: This is from a user. http://www.mathworks.com/matlabcentral/fileexchange/25314-cuda-mex


This is not really a direct answer to your question, but if your goal is simply to have your MATLAB code run on the GPU, then you may find that if you have access to Parallel Computing Toolbox, you can use GPUArrays with arrayfun. For example, if the function you wish to evaluate across many points looks like this:

function y = myFcn( x )
y = 1;
for ii = 1:10
  y = sin(x * y);
end

Then you could call this on the GPU like so:

gx = gpuArray( rand(1000) );
gy = arrayfun( @myFcn, gx );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜