2

Is Matlab Coder only able to produce single threaded applications?

I attempted to convert some Matlab scripts, used for image analysis, and found that the code produced by Matlab Coder was much slower. This confused me because I thought the produced C code would be at-least as fast or faster then the M code. I then checked how many threads were being used for both the M code and the produced C code. The result was 1 thread used by the C code and there were many threads being used by the Matlab code. At this point I can only assume the image processing toolkit implements its function as mex functions which are multi-threaded.

mpyne
  • 241
  • 2
  • 8
  • Maybe this is helpful http://stackoverflow.com/questions/18730193/when-does-matlab-choose-to-thread-when-using-codegen-and-parfor Another try is to use some compiler options like `-lpthread` – Markus Aug 25 '15 at 18:58
  • The matlab coder generates pragmas for openmp, maybe they where ignored during compile? Can you find any openmp related pragmas in your code? At least a parfor is translated to parallel code. – Daniel Aug 25 '15 at 21:39

1 Answers1

1

While in general the generated code can be expected to be faster, there are some exceptions. Some implementations which are used by matlab are not available for generated code. I have no reference about the technical background, but I assume that these are fortran and/or assembler written libraries. An example for such a function is eig which is known to produce different (correct) results in generated code.

The matlab coder comes with a code examples which explains how a parfor is translated to openmp code. As a first step make sure that your code contains the openmp relevant pragmas. If not try rewriting your code using a parfor loop.

In a last step, make sure that your compiler is configured to use openmp.

Community
  • 1
  • 1
Daniel
  • 36,610
  • 3
  • 36
  • 69