sizes mismatch error with reduction variable inside parfor inside mex function
2 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
John Elliott
2014-6-30
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;
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!