开发者

Matlab Mexfiles and Cuda: Evaluate function handle

Hey there, I have a mex file which I want to parallelize with the help of CUDA. The current functionality is: I pass a function handle and a huge number of 'points' to the mex file and it evaluates the function on each of the points in sequential mode (on the CPU). It therefor uses something like:

mxArray* y;
const mxArray *e[2] = {functionHandle, point};
mexCallMATLAB(1, &y, 2, (mxArray **)e, "feval");

to evaluate the function Handle on the point using the matlab-function feval. Now I wonder what happens when I try to parallelize the calculations via CUDA: Will this work correctly? Because if the evaluation with mexCallMATLAB is done on the CPU, there won't be any benefit using CUDA. But how to do it than? I can't imagine any way to evaluate a function handle on开发者_如何学编程 a certain point in C directly without using the matlab-function feval...

Thanks so far! You'll help me a lot!


No, mexCallMATLAB will not be callable from the GPU. In general, host-side calls cannot be made from within the body of a CUDA Kernel.

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜