Problem with af.mean in parallel loop
matkraj opened this issue · comments
When I run
data3d = af.randu(1,1,5)
means = af.constant(0,5)
meansP = af.constant(0,5)
for n in range(5):
means[n]=af.mean(data3d[:,:,n])
for n in af.ParallelRange(5):
meansP[n]=af.mean(data3d[:,:,n])
af.sync
print(means)
print(meansP)
results in:
arrayfire.Array()
Type: float[5 1 1 1]
0.4703
0.5648
0.2519
0.2058
0.3630arrayfire.Array()
Type: float[5 1 1 1]
0.3712
0.3712
0.3712
0.3712
0.3712
You can't use a function that returns a scalar inside parallel for.
You can achieve the same thing by doing means = af.mean(data, dim=2)
thank you @pavanky, also is this error expected?
diff=0.0
for x in range(10000):
data3d = af.randn(100,100,2)
data2d = af.moddims(data3d,10000,2)
diff+=np.abs(
1.0-af.mean(data2d[:,0])/af.mean(data2d,dim=0)[0,0])
print('Difference = ',diff/10000)
Difference = [1.4164448e-07]
@matkraj yes the number of threads launched for af.mean(data2d[:, 0])
is different from af.mean(data2d, dim=0)
. This results in small floating point differences because floating point arithmetic is not associative.
If you used something like af.min
or af.max
which dont perform floating point arithmetic you will see identical results.
Thanks!