LoopVectorization.jl icon indicating copy to clipboard operation
LoopVectorization.jl copied to clipboard

Support vectorizing `rand`?

Open SkyWorld117 opened this issue 4 years ago • 4 comments
trafficstars

I have no idea if it is possible to vectorize rand.

I hope for a usage like below:

julia> using LoopVectorization

julia> m = zeros(Float32, 4,4)
4×4 Matrix{Float32}:
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0

julia> a = rand(Float32, 4,4)
4×4 Matrix{Float32}:
 0.251334  0.409923  0.591477  0.04535
 0.4116    0.52253   0.34138   0.870923
 0.24401   0.116969  0.583924  0.0549548
 0.528378  0.898528  0.519574  0.172627

julia> b = rand(Float32, 4,4)
4×4 Matrix{Float32}:
 0.874233   0.263186  0.000970364  0.411874
 0.0680491  0.951124  0.803958     0.197752
 0.669292   0.996646  0.0915098    0.652465
 0.395592   0.875268  0.601194     0.518282

julia> @avxt for i in eachindex(m)
           m[i] = rand(a[i]:1.0f3:b[i])
       end

I have tried VectorizedRNG but I did not find out how to set boundries.

SkyWorld117 avatar Jun 06 '21 09:06 SkyWorld117

Yeah, I'm still planning on adding support for rand through integrating VectorizedRNG someday.

But, what exactly do you want to do here? That code doesn't work without @avxt either.

chriselrod avatar Jun 06 '21 10:06 chriselrod

But, what exactly do you want to do here? That code doesn't work without @avxt either.

Oops... I forgot to check the size. It actually should be like this:

julia> for i in eachindex(m)
           m[i] = rand(min(a[i],b[i]):1.0f-3:max(a[i], b[i]))
       end

I use this for recombination in gene algorithm.

SkyWorld117 avatar Jun 06 '21 11:06 SkyWorld117

Do you need it to be along steps of size 1f-3, or does this work (uniform on the interval)?

julia> using VectorizedRNG

julia> m2 = zeros(Float32, 4,4)
4×4 Matrix{Float32}:
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0

julia> a = rand(Float32, 4,4)
4×4 Matrix{Float32}:
 0.891385   0.249538  0.235754   0.760519
 0.904723   0.286706  0.705764   0.120201
 0.0895076  0.190633  0.460065   0.267225
 0.688647   0.121161  0.0102969  0.21347

julia> b = rand(Float32, 4,4)
4×4 Matrix{Float32}:
 0.828519  0.434766  0.448172  0.503536
 0.121096  0.34873   0.3915    0.685774
 0.766     0.714819  0.106276  0.146263
 0.693267  0.539196  0.499459  0.371783

julia> bminusa = b .- a;

julia> rand!(local_rng(), m2, VectorizedRNG.StaticInt(0), a, bminusa)
4×4 Matrix{Float32}:
 0.874131  0.348076  0.352481  0.701237
 0.123227  0.312257  0.420919  0.324506
 0.703747  0.274069  0.30034   0.208021
 0.692104  0.231548  0.454872  0.24622

julia> rand!(local_rng(), m2, VectorizedRNG.StaticInt(0), a, bminusa)
4×4 Matrix{Float32}:
 0.839076  0.330651  0.364552   0.514605
 0.853187  0.337562  0.607029   0.676503
 0.690698  0.692861  0.131177   0.213698
 0.690897  0.446571  0.0353261  0.249456

julia> rand!(local_rng(), m2, VectorizedRNG.StaticInt(0), a, bminusa)
4×4 Matrix{Float32}:
 0.835538  0.272899  0.31261   0.517794
 0.743744  0.310633  0.432453  0.35896
 0.494039  0.306173  0.346723  0.265134
 0.693041  0.499025  0.25609   0.281413

julia> rand!(local_rng(), m2, VectorizedRNG.StaticInt(0), a, bminusa)
4×4 Matrix{Float32}:
 0.884543  0.393225  0.436571  0.608758
 0.744019  0.331253  0.677331  0.152471
 0.683262  0.413723  0.217542  0.193462
 0.688877  0.455783  0.213734  0.225289

julia> rand!(local_rng(), m2, VectorizedRNG.StaticInt(0), a, bminusa)
4×4 Matrix{Float32}:
 0.860115  0.398083  0.364881  0.593411
 0.465632  0.34009   0.534855  0.466152
 0.173389  0.344643  0.273047  0.184566
 0.691994  0.264059  0.303162  0.256998

julia> rand!(local_rng(), m2, VectorizedRNG.StaticInt(0), a, bminusa)
4×4 Matrix{Float32}:
 0.870036  0.387869  0.257197  0.753752
 0.872836  0.34466   0.412791  0.277017
 0.290672  0.704794  0.353264  0.2086
 0.692604  0.336881  0.144114  0.3456

chriselrod avatar Jun 06 '21 11:06 chriselrod

Do you need it to be along steps of size 1f-3, or does this work (uniform on the interval)?

This will work! I don't really care about the step size, thanks!

SkyWorld117 avatar Jun 06 '21 11:06 SkyWorld117