Mutex for Increment in Matlab Parfor?

I am trying to use Matlab's parfor for the following code:
parfor j=1:10
if (vtw(dta{j},A2,Mu2,Sigma2,A5,Mu5,Sigma5)==1)
results(1,1)=results(1,1)+1;
end
end
Matlab doesn't like this code saying parfor cannot be used because of the way results is being used.
To me this is a bit confusing as in C++ I would consider this a very separable operation and simply insert a mutex around the results++ (which doesn't really need one as increments are atomic). How do I fix this?

 Akzeptierte Antwort

Edric Ellis
Edric Ellis am 26 Mär. 2012

1 Stimme

It's only the indexing into results that PARFOR doesn't understand. The following should work I think:
results = 0;
parfor j=1:10
if (vtw(dta{j},A2,Mu2,Sigma2,A5,Mu5,Sigma5)==1)
results = results + 1;
end
end

5 Kommentare

Mikhail  Kandel
Mikhail Kandel am 26 Mär. 2012
Out of curiousity, is this incompatibility due to parfor's checking or is matlab unsure of results(1,1) location at run/parsetime?
Mikhail  Kandel
Mikhail Kandel am 26 Mär. 2012
That is to say, is results(1,1) contantly revaluated.Is something like &results+i+wj constantly evaluated? Or does matlab immediatly optomize it?
Edric Ellis
Edric Ellis am 26 Mär. 2012
PARFOR analyses your code before execution, and it doesn't consider "results(1,1)" to be a valid target for a reduction.
Abel
Abel am 25 Jun. 2013
so parfor will actively prevent the race-condition??
Edric Ellis
Edric Ellis am 26 Jun. 2013
There is no race condition - PARFOR understands the reduction using '+' and how to perform that in parallel.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Walter Roberson
Walter Roberson am 25 Mär. 2012

3 Stimmen

parfor j = 1 : 10
results_j(j) = vtw(dta{j},A2,Mu2,Sigma2,A5,Mu5,Sigma5)==1;
end
results = sum(results_j);

3 Kommentare

Mikhail  Kandel
Mikhail Kandel am 25 Mär. 2012
Thank you for the solution, but why does matlab not like this code?
Geoff
Geoff am 25 Mär. 2012
Even in C/C++, I would have a preference for Walter's solution over one that uses calls to the kernel.
Walter Roberson
Walter Roberson am 26 Mär. 2012
Misha, sorry, I do not have the appropriate toolbox to test this code, so I cannot say why MATLAB might not like it. If you post the error message someone might recognize it. Might be something as simple as initializing results_j ahead of time.

Melden Sie sich an, um zu kommentieren.

owr
owr am 26 Mär. 2012

1 Stimme

Be careful using if statements without matching elses inside parfor loops. Ive experienced some strange behaviour in both 2011a and 2011b when this is done with linear indexing.
This example is a bit contrived, but it illustrates the issue:
matlabpool open;
[x,y] = meshgrid(1:10,1:10);
results = nan(10);
parfor i =1:100
if( x(i) <= y(i) )
results(i) = 2;
end
end
>> results
results =
2 0 0 0 0 0 NaN NaN NaN NaN
2 2 0 0 0 0 NaN NaN NaN NaN
2 2 2 0 0 0 NaN NaN NaN NaN
2 2 2 2 0 0 NaN NaN NaN NaN
2 2 2 2 2 0 0 NaN NaN NaN
2 2 2 2 2 2 0 0 NaN NaN
2 2 2 2 2 2 2 0 NaN NaN
2 2 2 2 2 2 2 2 0 NaN
2 2 2 2 2 2 2 2 2 0
2 2 2 2 2 2 2 2 2 2
Where did the zeros come from?

1 Kommentar

Edric Ellis
Edric Ellis am 27 Mär. 2012
Hm, that looks like a bug. Thanks for reporting this.

Melden Sie sich an, um zu kommentieren.

Jan
Jan am 25 Mär. 2012

0 Stimmen

When different PARFOR loops process this line:
results(1,1) = results(1,1) + 1;
The following can happen:
  1. Thread 1: results(1,1) is evaluated and stored temporarily
  2. Thread 2: 1 is added to the temporary variable
  3. Thread 2: results(1,1) is evaluated and stored temporarily
  4. Thread 1: results(1,1) is updated
  5. Thread 2: results(1,1) is updated
Now the results(1,1) is increased by 1, and not by 2. This happens because there is no mutex to block simultaneous access to result.
If the increment is atomic, there would be no need to implement InterlockedIncrement(). While the 32 bit increment is usually atomic on modern compilers and processors, this is not guaranteed for 64 bit types as int64 and doubles. Using a critical section is recommended, or InterlockedIncrement().

1 Kommentar

Edric Ellis
Edric Ellis am 26 Mär. 2012
Actually, PARFOR understands various reduction operations by taking advantage of the mathematical properties of the expression. So, this addition can be done, but the syntax needs tweaking.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by