Note that there are some explanatory texts on larger screens.

plurals
  1. POPARFOR with if/else clause based on external boolean
    text
    copied!<p>I have written a simple chemical simulation which calculates the properties of a large number of grid-boxes in parallel. Accordingly, I index the Y-dimension using a parallel loop:</p> <pre><code>function[outputArray] = stackTest() numX = 10; numY = 10; numZ = 10; outputArray = zeros(numX,numY,numZ); for iX = 1:numX parfor iY = 1:numY coreArray = outputArray(iX,iY,:); for iZ = 1:numZ tempNum = iX*iY*iZ; coreArray(1,1,iZ) = tempNum; end outputArray(iX,iY,:) = coreArray; end end end </code></pre> <p>This works fine. However, I am using booleans to control whether or not certain actions are performed, as shown in the following code. This works fine when using a simple <code>for</code> loop on Y, but when using <code>parfor</code>, the code fails with a claim that <code>optionalArg</code> is not defined:</p> <pre><code>function[outputArray] = stackTest(controlArg) numX = 10; numY = 10; numZ = 10; outputArray = zeros(numX,numY,numZ); if (controlArg) optionalArg = 10; end for iX = 1:numX parfor iY = 1:numY coreArray = outputArray(iX,iY,:); for iZ = 1:numZ tempNum = iX*iY*iZ; if controlArg tempNum = tempNum * optionalArg; end coreArray(1,1,iZ) = tempNum; end outputArray(iX,iY,:) = coreArray; end end end </code></pre> <p><code>stackTest</code> now works fine if <code>controlArg = true</code>, but not if <code>controlArg = false</code>; the only way around it I have found is to define <code>optionalArg</code> independent of <code>controlArg</code>. Needless to say this is a simplified version of the problem, but I would be grateful to anyone that can explain this to me; I suspect it's a subset of the problems that <code>parfor</code> loops have with globals, but since I am defining no globals I'm a little confused.</p> <p>Regards,</p> <p>Skipsh</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload