Question

Well, Matlab Coder is able to convert .m files into mexfunctions under some restrictions.

For example, one can not call mex functions inside the .m file that is to be converted.

But is this restriction only meant for those mex functions that we don't have the source code?

That is to say, if we have the source code in C mex of a function, and the function is called inside a Matlab function, like this:

function result = fun() %#codegen
a = ...; %complex large-scale matrix operations 
result = cFun(a);
end

as described above, cFun() can be written in C using mex to improve performance, but it might be better to write the matrix operations in Matlab instead of C.

Therefore, if we can supply the source code of cFun() and use Matlab Coder to convert fun() into mex, we would profit from both the convenience of matrix operations in Matlab, and the performance of some operations in C, or even OpenCL.

But is it possible?

Thanks!

Was it helpful?

Solution

Firstly: you can call back into MATLAB from your generated C code or generated MEX file, using coder.extrinsic. If for some reason you have an existing MEX file without source code that you need to call, you can do this in the same way as for any other MATLAB command.

Secondly: if you have some existing C source code that you would like to call from your generated C code or generated MEX file, you can do so with a call to coder.ceval.

I don't think there's a reason you couldn't do that with OpenCL code, although if you're looking to take advantage of GPU code I would think that the built-in GPU functionality from Parallel Computing Toolbox would be more convenient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top