sizes mismatch error with reduction variable inside parfor inside mex function

2 views (last 30 days)
Minimal example:
function [A] = mexwoe(n,a,b)
A = zeros(a,b);
parfor i = 1:n
B = rand(a,b);
A = A + B;
end
Run as normal function:
>> mexwoe(4,2,3)
ans =
1.6644 2.6927 3.3542
1.6938 2.4979 1.1478
Then, successfully compile into a C++ mex function called mexwoe_mex: MEX configured to use 'Microsoft Visual C++ 2013 Professional (C)' for C language compilation.
n, a, and b are defined as double scalars in the Coder app.
>> coder -build mexwoe.prj
Code generation successful: View report
But,
>> mexwoe_mex(4,2,3)
Sizes mismatch: [0][0] ~= [2][3].
More information
Error in mexwoe (line 10)
A = A + B;
"More information" links to the help topic "Incompatibilities with MATLAB in Variable-Size Support for Code Generation", but I don't understand how anything listed there applies to this example?
Possibly useful info: Matlab 2014a. Windows 7 x64. 32GB RAM. Intel i7 CPU. I don't seem to have other issues with compiling and using mex functions containing parfor loops - it seems to be the reduction variable in the parfor. If I change to "for" instead of "parfor", the mexed version then works. Without the reduction statement, it works. It seems to think A is empty - why? Thanks for any help!

Answers (1)

John Elliott
John Elliott on 30 Jun 2014
As a workaround for this problem, you could try moving the sum operation into a sub-function.
Instead of
A=A+B;
use
A=f(A,B);
where f is defined as
function A=f(A,B)
A=A+B;
  1 Comment
Rudi
Rudi on 30 Jun 2014
I can confirm that this works - thanks a lot. I'll wait to "accept" just in case anyone has an explanation for this apparent bug in either Matlab or Microsoft Visual C++ 2013.

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!