Hello all,
I have faced an issue when using parfor, and I would like to know why parfor cannot handle such logic.
My aim from this question is to just know the cause of the issue, which would help me to not do it again in the future. My code is huge to be pasted here, so I will write a toy example:
clear allocatedVar
myFlag = 0;
loopMax = 4;
if( myFlag == 1)
allocatedVar = cell(loopMax, 1);
for i = 1 : loopMax
allocatedVar{i, 1} = ones(10, 1);
end
end
parfor i = 1 : loopMax
if( myFlag == 1)
b = zeros(10, 1);
for j = 1 : 10
b(j) = allocatedVar{i, 1}(3, 1);
end
end
c = 4;
end
The error is
Error: Invalid syntax for calling function 'allocatedVar' on the path. Use a valid syntax or explicitly
initialize 'allocatedVar' to make it a variable.
So, the idea is that when the input "myFlag = 1", I will execute a specific code section. While when it does not equal 1, I do not want to even allocate the variables needed to execute this code section; I'm using this to save memory space.
When using a for loop, there is no issue, while when using parfor, MATLAB seems to not accept such a thing.
I can solve the issue by simply commenting the code section when I do not want to execute it, but then this is not practical (commenting and not commenting when I want to run the simulation). So, using a flag would definitely be a better solution.
Regards,
0 Comments
Sign in to comment.