Le jeudi 01 octobre 2015 à 07:13 -0700, Matt a écrit :
> Ok here is the weird thing:
> x = randn(1000)
> for i in 1:100
>     @assert !any(isnan, similar(x))
> end
> Always fails in my trials
> 
> x = randn(1000000)
> for i in 1:100
>     @assert !any(isnan, similar(x))
> end
> Never fails in my trials
> 
> Do you understand why?
I think it depends on where the memory comes from. If it comes from a
different process, the OS will fill it with zeros for security reasons.
Maybe when creating small objects, Julia can reuse previous memory
slices, but when they are bigger it needs to allocate new (zeroed)
memory.

This illustrates the problem with the current situation: the returned
array is quite often full of zeros, which can lead people to believe
this is a guarantee. At least if it always contained obvious garbage,
the problem would be more visible.


Regards

> On Thursday, October 1, 2015 at 9:58:52 AM UTC-4, Milan Bouchet-Valat
> wrote:
> > Le jeudi 01 octobre 2015 à 06:46 -0700, Matt a écrit : 
> > > Similar(x::Vector{Float64}) creates a new vector with NaN in some
> > > cases.  There is no mention of this in the documentation. Is it a
> > > bug? It was a source of bug in my code since I was using
> > something 
> > > like scale!(x, 0) instead of fill!(x, 0) to define a general 
> > > operation akin to BLAS gemm(\beta, A, B, \alpha, x)  (where x may
> > be 
> > > a vector create by similar()). 
> > > I can't reproduce such a behavior with simple vectors such as 
> > > similar(randn(10000). 
> > Actually, similar() here simply creates an uninitialized array,
> > just 
> > like calling Array(). Sometimes the memory contains only zeros, so 
> > you'll get the impression that similar() returns arrays of zeros,
> > but 
> > it can also contain anything, including NaN or Inf. FWIW, 
> > sum(similar(randn(1000))) frequently returns NaN here. 
> > 
> > The fact that it sometimes returns NaN can actually be considered
> > as a 
> > nice feature, since it allowed you to find a bug. If it had
> > returned 
> > finite values, you may not even have noticed it. 
> > 
> > There's currently a debate about always returning an array of
> > zeros, 
> > but no consensus has been reached yet: 
> > https://github.com/JuliaLang/julia/issues/9147 
> > 
> > 
> > Regards 
> > 
> > > Here the command that fails: 
> > > 
> > >  x = 
> > >
> > [93.9,95.4,98.5,96.4,95.5,88.4,90.1,89.8,95.4,101.1,102.9,108.2,111
> > .7 
> > >
> > ,116.2,117.1,123.0,121.4,123.2,119.6,119.1,116.3,113.0,114.5,116.3,
> > 11 
> > >
> > 4.0,112.1,105.6,108.6,107.9,109.1,125.0,121.0,123.2,113.9,117.1,115
> > .6 
> > >
> > ,113.8,115.2,109.6,125.0,128.3,133.1,121.8,122.3,121.7,124.7,124.6,
> > 12 
> > >
> > 6.8,113.8,113.5,111.1,107.1,107.1,107.9,106.1,102.2,96.8,88.9,81.2,
> > 79 
> > >
> > .0,103.4,102.6,100.3,98.9,102.9,104.0,102.9,100.3,104.1,103.9,108.0
> > ,1 
> > >
> > 09.7,114.8,119.1,122.6,127.3,126.5,131.8,128.7,127.4,128.0,123.1,12
> > 5. 
> > >
> > 8,126.0,122.3,121.5,118.3,113.1,116.8,126.0,142.0,138.3,140.0,136.8
> > ,1 
> > >
> > 35.8,124.3,123.9,123.0,121.0,123.5,124.4,126.7,127.1,128.0,126.4,12
> > 6. 
> > >
> > 1,121.9,120.2,118.6,115.4,110.8,104.8,102.8,99.7,97.5,90.1,82.4,77.
> > 8, 
> > >
> > 68.7,67.5,156.2,143.5,147.0,144.5,145.6,143.2,144.7,120.0,117.6,110
> > .8 
> > >
> > ,109.3,112.4,110.2,113.4,117.3,117.5,117.4,118.0,116.4,114.7,114.1,
> > 11 
> > >
> > 2.5,111.0,108.5,109.0,104.8,100.6,91.5,86.7,83.6,165.8,170.9,170.8,
> > 16 
> > >
> > 1.3,158.2,164.8,175.5,155.0,161.1,156.3,154.7,151.3,147.6,153.0,153
> > .3 
> > >
> > ,155.5,150.2,150.5,152.6,154.1,149.6,144.0,144.5,142.4,141.0,137.1,
> > 13 
> > >
> > 1.7,127.2,118.8,120.0,246.4,235.3,246.5,295.9,249.8,224.3,212.2,200
> > .4 
> > >
> > ,213.0,220.6,209.4,182.7,176.5,167.7,136.7,130.3,128.5,129.7,131.9,
> > 13 
> > >
> > 2.0,124.5,124.6,122.1,117.2,113.0,105.4,101.1,84.1,82.2,82.6,137.5,
> > 12 
> > >
> > 5.5,131.1,131.3,132.8,129.3,121.2,123.6,123.0,126.0,132.6,136.0,131
> > .9 
> > >
> > ,130.3,133.1,133.4,135.4,139.0,132.8,131.8,127.6,124.7,124.2,124.7,
> > 12 
> > >
> > 5.1,119.5,113.8,110.1,98.2,95.9,108.3,107.6,103.5,104.8,106.7,105.0
> > ,1 
> > >
> > 08.6,109.9,115.7,117.0,119.8,123.7,122.9,125.9,127.9,130.6,131.0,13
> > 4. 
> > >
> > 0,131.7,131.2,128.6,126.3,128.8,129.0,129.3,124.1,117.1,113.8,109.6
> > ,1 
> > >
> > 09.2,97.0,90.1,95.2,94.5,98.3,99.1,100.6,102.4,108.5,126.1,121.8,12
> > 5. 
> > >
> > 6,123.3,125.1,125.0,122.8,117.5,115.2,114.1,111.5,111.3,103.6,100.7
> > ,9 
> > >
> > 6.7,95.0,84.5,78.4,90.1,85.4,85.1,145.5,144.5,145.5,144.2,141.8,132
> > .6 
> > >
> > ,130.4,124.8,125.6,126.6,124.4,131.9,131.8,134.4,134.0,136.7,135.3,
> > 13 
> > >
> > 5.2,133.0,130.7,127.9,124.0,121.6,118.2,109.5,107.6,104.6,94.1,96.1
> > ,9 
> > >
> > 4.8,138.5,134.3,135.3,132.1,134.8,134.3,132.9,134.6,139.3,149.2,156
> > .0 
> > >
> > ,159.6,162.4,166.6,173.0,150.9,148.9,146.9,148.5,147.7,143.0,137.8,
> > 13 
> > >
> > 5.3,137.6,134.0,134.0,132.5,128.3,127.2,128.2,114.9,109.7,116.0,108
> > .2 
> > >
> > ,113.7,109.2,107.9,108.5,108.4,109.4,110.6,116.1,120.5,124.4,125.5,
> > 12 
> > >
> > 7.1,124.2,124.6,132.9,116.2,115.6,111.2,109.4,104.1,101.1,100.2,94.
> > 4, 
> > >
> > 95.4,97.1,95.2,115.8,111.2,107.8,101.7,101.2,100.1,102.6,114.0,102.
> > 8, 
> > >
> > 111.0,115.2,118.6,123.4,127.7,127.9,127.1,126.4,127.1,132.0,130.9,1
> > 27 
> > >
> > .6,121.7,115.7,109.4,105.2,103.2,96.5,94.3,91.8,90.0,126.3,127.0,12
> > 8. 
> > >
> > 7,134.0,139.3,142.8,146.3,155.8,163.5,179.4,201.9,212.4,223.0,230.9
> > ,2 
> > >
> > 29.4,224.7,214.9,215.3,209.7,210.6,201.1,183.2,182.4,179.8,171.2,17
> > 3. 
> > >
> > 2,171.6,182.5,170.8,167.6,114.0,114.0,117.5,119.7,120.0,117.3,115.9
> > ,1 
> > >
> > 15.9,119.8,125.3,126.7,129.9,133.6,139.6,140.0,142.7,140.1,143.8,14
> > 4. 
> > >
> > 0,143.9,133.7,128.9,125.0,121.2,116.5,110.9,103.6,101.5,107.2,108.5
> > ,1 
> > >
> > 38.4,137.4,139.1,135.1,136.0,135.6,135.2,128.5,133.2,136.5,138.0,14
> > 2. 
> > >
> > 1,140.7,144.9,145.6,143.9,138.5,141.2,138.9,139.5,135.4,135.5,127.9
> > ,1 
> > >
> > 19.0,125.0,125.0,122.4,117.5,116.1,114.7,122.7,122.2,121.0,121.0,12
> > 1. 
> > >
> > 7,121.7,122.3,123.5,126.7,133.2,137.3,144.8,146.1,137.1,135.7,134.1
> > ,1 
> > >
> > 31.9,131.5,137.1,131.3,128.2,122.5,121.9,121.8,119.3,115.0,109.0,10
> > 2. 
> > >
> > 8,97.6,91.7,142.2,138.7,136.5,131.6,130.2,128.0,129.7,124.3,121.4,1
> > 17 
> > >
> > .9,121.2,124.3,126.1,116.9,118.9,120.5,118.2,120.5,123.2,122.3,119.
> > 8, 
> > >
> > 116.0,117.2,115.8,113.8,111.7,104.4,99.4,93.5,90.5,126.7,125.0,131.
> > 8, 
> > >
> > 134.5,134.4,134.2,133.4,128.6,126.1,131.4,133.7,138.8,136.8,138.0,1
> > 40 
> > >
> > .3,141.8,140.9,140.7,139.5,137.3,128.3,128.2,126.6,126.5,122.1,121.
> > 1, 
> > >
> > 116.6,114.4,110.3,107.6,112.6,104.7,109.2,110.7,114.4,113.7,112.3,1
> > 04 
> > >
> > .3,116.4,96.8,106.8,110.6,111.5,116.7,117.2,118.9,118.3,117.7,120.8
> > ,1 
> > >
> > 19.4,113.2,110.8,113.0,104.3,108.8,94.1,92.3,90.7,86.2,83.8,83.9,86
> > .1 
> > >
> > ,83.5,86.9,88.1,89.0,91.0,93.4,105.4,112.1,115.0,117.1,116.8,120.9,
> > 12 
> > >
> > 2.1,124.9,123.9,127.0,125.3,125.8,122.3,116.4,115.3,113.2,110.0,109
> > .0 
> > >
> > ,108.3,101.8,105.6,103.9,133.2,130.0,130.2,139.3,138.3,137.3,137.2,
> > 12 
> > >
> > 1.3,127.6,130.0,132.1,135.4,135.6,139.5,140.8,141.8,140.2,142.1,140
> > .5 
> > >
> > ,139.7,134.1,130.0,129.2,128.8,128.7,127.4,122.8,119.1,119.9,122.3,
> > 11 
> > >
> > 8.9,112.8,118.0,114.9,118.0,116.1,117.4,111.2,115.6,122.2,119.9,121
> > .9 
> > >
> > ,123.7,124.9,127.0,127.2,120.3,122.0,121.1,122.4,113.7,110.1,103.6,
> > 97 
> > >
> > .8,91.7,87.1,86.2,84.7,82.9,86.6,118.5,106.3,110.5,106.1,110.0,109.
> > 5, 
> > >
> > 108.4,108.1,108.6,104.9,106.6,110.5,114.1,118.1,117.7,117.4,116.1,1
> > 16 
> > >
> > .3,117.0,117.1,110.8,107.7,105.1,103.1,101.3,92.9,93.8,89.9,92.4,90
> > .6 
> > >
> > ,212.7,206.3,192.7,184.0,175.8,189.7,198.6,189.5,190.5,198.6,201.5,
> > 20 
> > >
> > 4.7,205.2,201.4,190.8,187.0,183.3,177.7,171.9,165.1,159.2,136.6,146
> > .7 
> > >
> > ,142.6,147.7,141.9,137.9,137.3,115.5,110.0,221.4,223.9,233.8,287.6,
> > 29 
> > >
> > 7.9,264.0,248.5,265.7,278.0,296.2,279.0,269.8,269.1,290.5,278.8,269
> > .6 
> > >
> > ,254.6,247.8,245.4,239.8,232.9,215.1,201.1,195.9,195.1,180.4,172.9,
> > 15 
> > >
> > 2.4,144.8,143.7,141.4,135.6,138.0,144.9,131.1,130.2,121.2,120.7,125
> > .1 
> > >
> > ,128.4,120.2,121.4,122.3,122.4,122.8,123.9,123.8,124.3,127.9,126.3,
> > 12 
> > >
> > 1.3,117.8,116.9,115.5,111.6,107.8,103.0,98.6,86.7,86.6,92.5,89.9,96
> > .3 
> > >
> > ,96.5,95.5,99.9,87.0,90.0,92.6,99.3,98.9,100.3,103.1,102.4,102.4,10
> > 3. 
> > >
> > 1,101.0,102.7,103.0,97.5,96.3,88.9,88.0,88.2,82.3,77.7,74.4,70.8,69
> > .9 
> > >
> > ,71.4,146.9,141.2,138.5,122.4,123.2,124.9,122.4,119.0,123.3,119.9,1
> > 18 
> > >
> > .7,121.6,123.9,124.6,125.5,126.4,124.4,127.6,130.1,128.5,124.6,119.
> > 3, 
> > >
> > 115.9,114.0,108.9,106.2,100.1,93.7,86.4,82.4,103.3,102.0,103.2,99.5
> > ,9 
> > >
> > 6.3,97.0,96.9,93.8,98.5,103.8,108.7,110.5,117.9,125.4,122.2,121.9,1
> > 21 
> > >
> > .3,123.7,125.7,126.8,119.6,109.4,103.2,99.8,92.3,87.1,84.1,77.1,85.
> > 2, 
> > >
> > 74.4,130.4,129.4,134.8,135.5,136.4,129.8,128.8,121.6,124.6,124.4,12
> > 0. 
> > >
> > 5,122.1,122.5,124.6,127.3,131.3,130.9,133.5,132.8,134.0,130.0,127.1
> > ,1 
> > >
> > 26.7,126.3,124.6,122.4,118.6,115.5,113.2,112.3,113.4,111.5,115.6,11
> > 5. 
> > >
> > 9,117.9,113.0,108.7,108.4,115.4,121.7,124.1,130.5,132.9,138.6,140.4
> > ,1 
> > >
> > 43.6,141.6,141.6,143.7,147.0,140.0,128.1,124.2,119.9,113.1,103.6,97
> > .5 
> > >
> > ,88.4,87.8,86.3,128.3,117.4,120.4,122.9,125.5,117.1,114.7,107.3,106
> > .3 
> > >
> > ,109.0,110.7,114.2,114.6,118.8,120.1,122.3,122.6,124.0,125.2,123.3,
> > 12 
> > >
> > 5.3,115.3,115.8,113.9,100.6,107.6,107.1,101.3,102.5,96.2,144.6,140.
> > 3, 
> > >
> > 133.2,140.2,144.5,142.9,117.8,123.9,123.2,134.4,142.0,146.1,154.7,1
> > 50 
> > >
> > .2,148.8,146.8,145.8,149.3,151.2,146.3,135.8,136.9,133.4,136.3,124.
> > 4, 
> > >
> > 138.0,120.8,101.4,103.6,100.1,88.2,89.7,90.1,93.4,95.1,95.7,104.2,1
> > 03 
> > >
> > .6,115.0,118.7,125.5,129.7,130.5,136.8,137.2,140.4,135.7,138.3,136.
> > 1, 
> > >
> > 136.0,131.1,127.0,125.4,126.6,126.6,124.4,122.4,118.6,121.5,121.9,1
> > 06 
> > >
> > .0,99.4,98.7,98.8,99.6,100.4,102.5,92.7,96.7,103.0,103.5,108.4,113.
> > 5, 
> > >
> > 116.7,115.6,116.9,117.4,114.7,115.7,113.0,109.8,105.7,104.4,97.0,95
> > .8 
> > >
> > ,91.9,87.4,88.3,91.8,93.0,110.3,105.5,108.1,109.9,111.5,109.2,107.3
> > ,9 
> > >
> > 9.8,106.3,111.5,109.7,114.8,117.4,121.7,124.6,127.3,127.2,130.4,129
> > .1 
> > >
> > ,131.4,129.0,125.1,128.7,129.0,130.6,125.3,124.7,121.8,120.6,121.0,
> > 11 
> > >
> > 3.4,110.7,120.5,106.1,109.0,108.4,111.3,106.4,108.9,108.6,110.4,114
> > .7 
> > >
> > ,116.0,121.4,124.2,126.6,126.4,129.7,129.0,131.2,126.4,117.2,115.9,
> > 11 
> > >
> > 3.7,105.8,96.5,94.5,85.6,79.6,77.2,72.0,62.3,65.0,65.7,64.3,64.3,65
> > .6 
> > >
> > ,65.5,67.7,71.3,72.7,75.6,75.8,77.9,78.0,79.6,79.1,74.8,77.6,73.6,6
> > 9. 
> > >
> > 0,66.3,66.5,64.4,67.7,55.0,57.0,53.4,53.5,55.0,133.5,129.1,122.9,13
> > 0. 
> > >
> > 4,127.4,128.8,134.6,122.6,124.4,138.0,146.8,151.8,155.5,171.1,169.4
> > ,1 
> > >
> > 62.4,160.9,161.6,163.8,162.3,153.8,144.3,144.5,131.2,128.3,128.7,12
> > 0. 
> > >
> > 9,124.3,120.9,126.5,122.8,118.5,123.3,120.2,120.9,122.8,123.9,124.3
> > ,1 
> > >
> > 28.4,137.0,143.1,149.6,152.7,158.1,157.7,155.9,151.8,148.9,149.9,14
> > 7. 
> > >
> > 4,144.7,136.8,134.6,135.8,133.0,129.5,122.5,118.9,109.1,108.2,99.1,
> > 95 
> > >
> > .4,98.3,86.9,99.7,101.5,100.2,96.7,97.0,88.5,91.0,98.6,99.5,100.3,9
> > 9. 
> > >
> > 3,101.3,101.4,101.4,112.7,106.6,102.4,96.2,96.5,94.2,91.3,88.2,86.1
> > ,8 
> > >
> > 3.4,78.7,81.1,111.8,109.0,110.5,111.2,113.6,119.8,112.1,114.5,111.5
> > ,1 
> > >
> > 17.5,116.6,119.9,123.2,129.7,133.9,131.6,122.1,122.3,120.5,119.8,11
> > 5. 
> > >
> > 7,111.9,109.1,112.1,107.5,109.1,104.0,104.1,100.1,98.0,110.4,106.2,
> > 10 
> > >
> > 9.4,106.3,108.3,111.4,111.5,106.4,105.4,108.8,109.5,111.8,113.5,115
> > .4 
> > >
> > ,117.2,116.7,117.1,117.6,119.9,115.6,106.3,105.6,107.0,105.4,106.0,
> > 10 
> > >
> > 2.6,100.3,94.1,95.5,96.2,125.6,134.0,137.6,133.3,137.4,136.5,135.7,
> > 13 
> > >
> > 2.2,131.7,140.0,141.2,145.8,160.7,161.5,160.4,160.3,168.6,158.1,163
> > .1 
> > >
> > ,157.7,141.2,128.9,125.7,124.8,110.4,114.3,111.4,96.9,109.1,110.8];
> > >     mean(x) 
> > >     mean(similar(x)) 
> > > 

Reply via email to