Matlab Mexfiles and Cuda: Evaluate function handle

조회 수: 1 (최근 30일)
Max
Max 2011년 5월 5일
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
Oliver Woodford
Oliver Woodford 2011년 5월 9일
Is the handle to a function written in MATLAB or C/C++?

댓글을 달려면 로그인하십시오.

답변 (2개)

Richard Alcock
Richard Alcock 2011년 5월 7일
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
Here's an example copied straight from the documentation.
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
Richard Alcock
Richard Alcock 2011년 5월 8일
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.
Max
Max 2011년 5월 8일
Hm okay thanks though!

댓글을 달려면 로그인하십시오.


Max
Max 2011년 5월 7일
Thanks and how would I than write a matlab-function which evaluates a function handle for certain parameters on the GPU? thanks!

카테고리

Help CenterFile Exchange에서 GPU Computing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by