sizes mismatch error with reduction variable inside parfor inside mex function

조회 수: 2 (최근 30일)
Rudi
Rudi 2014년 6월 27일
댓글: Rudi 2014년 6월 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!

답변 (1개)

John Elliott
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;
  댓글 수: 1
Rudi
Rudi 2014년 6월 30일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by