Matlab Mexfiles and Cuda: Evaluate function handle
2 views (last 30 days)
Show older comments
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!
1 Comment
Answers (2)
Richard Alcock
on 7 May 2011
In particular have a look at GPUArray and Executing MATLAB Code on the GPU - these don't enable exactly what you are asking, but do allow you to write code in MATLAB that runs on a GPU.
EDIT: Added Example
In this example, a small function applies correction data to an array of measurement data. The function defined in the file myCal.m is:
function c = myCal(rawdata, gain, offst)
c = (rawdata .* gain) + offst;
The function performs only element-wise operations when applying a gain factor and offset to each element of the rawdata array.
Create some nominal measurement:
meas = ones(1000)*3; % 1000-by-1000 matrix
The function allows the gain and offset to be arrays of the same size as rawdata, so that unique corrections can be applied to individual measurements. In a typical situation, you might keep the correction data on the GPU so that you do not have to transfer it for each application:
gn = gpuArray(rand(1000))/100 + 0.995;
offs = gpuArray(rand(1000))/50 - 0.01;
Run your calibration function on the GPU:
corrected = arrayfun(@myCal, meas, gn, offs);
This runs on the GPU because the input arguments gn and offs are already in GPU memory.
Retrieve the corrected results from the GPU to the MATLAB workspace:
results = gather(corrected);
3 Comments
Richard Alcock
on 8 May 2011
Bjorn, I understand this isn't what you wanted to do, but you can not call mexCallMATLAB from the GPU.
GPUArrays and arrayfun allow you to write MATLAB code that runs on the GPU.
See Also
Categories
Find more on GPU Computing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!