Clear Filters
Clear Filters

How to pass a user-defined function as a argument to mex file?

1 view (last 30 days)
I am building a GUI for simulation of a control system. Users input a model in GUI, and I can obtain it as string type and then convert it into a function by str2fun. I am thinking about accelerating my control algorithm by cuda programming. As far as I know, GPU programming can be integrated with Matlab via cuda kernal which is a mex file(.cu). Since the mex file have only one entrance for passing arguments, i.e.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Thus I think it is hard to pass a function within the mexFunction. I was wondering if I can pass the function within the main function of cuda kernal, which begins with
__global__ void functionname(.....){
}
or if I can use head file (.h) to pass the function handle. Any suggestion is welcome. Thank you in advance.
Qun(a newbie of GPU programming)

Accepted Answer

Joss Knight
Joss Knight on 26 Jul 2017
Simple advice for a newbie to GPU programming - don't write a custom MEX function or CUDAKernel until you've tried using the GPU via gpuArray and simple vectorization of your code. It sounds to me like you are unfamiliar with some basics like the difference between MEX and CUDAKernel, and the sorts of things a CUDAKernel can do, so you shouldn't leap straight to the advanced programming until you've tried the simple stuff that MATLAB goes to a lot of effort to make easy for you.
So I suggest you read the doc and get started trying to convert your code to use the GPU, and leave CUDA programming to another day!
  2 Comments
Qun Wu
Qun Wu on 26 Jul 2017
I just checked the available methods for gpuArray in command window typing 'methods gpuArray'. Although there are plenty of methods, I didn't find the one I need, which is ode45. I coded the ode45(RK4) algorithm by myself, but as I mentioned before I need the user-defined ODE model as one argument. Would you give me some advises please? Thank you.
Joss Knight
Joss Knight on 28 Jul 2017
You need to show me what your user-defined ODE model looks like so we can discuss how to make it GPU-compatible. ODEs are not parallel algorithms because each iteration must run serially. So we need to look at what your model is and whether it's processing enough data to be able to make good use of the GPU during each iteration.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!