Closed jaak-s closed 9 years ago
The problem here is (almost certainly) that while shared arrays lock on writes, there is no atomic read-write lock, so this line is not thread safe:
y[rv[k]] += nzv[k]*alphax
The problem with +=
is if
y[1] = 1
y[1] = 1
y[1]
, so sets y[1] = 2
y[1]
, so sets y[1] = 2
...whereas the correct answer (1+1+1) is 3.
This means multiplication (but not transpose multiplication, which doesn't use +=
) of shared sparse matrices will not be thread safe.
The way to fix it is to introduce a separate result vector y
for each core and to add them up in serial at the end.
Following code gives different results from standard matrix product:
Where I get
Julia version is v0.3.3. Maybe it is connected to following warning that is printed when the package is loaded:
Also the problem only occurs if there are multiple cores used. If single core is used then the error is zero.