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;
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Compiler에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!