Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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)

Risposta accettata

Joss Knight
Joss Knight il 26 Lug 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 Commenti
Qun Wu
Qun Wu il 26 Lug 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 il 28 Lug 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.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by