ModularM
Modular3y ago
23 replies
vmois

SIMD produces weird results without print statement at the end

The code below produces weird results:

from algorithm import vectorize
from tensor import Tensor

alias type = DType.int64
alias nelts = simdwidthof[type]()

def main():
    let size = 10
    var a = Tensor[type](size)
    var b = Tensor[type](size)
    var c = Tensor[type](size)

    for i in range(size):
        a[i] = i + 1
        b[i] = i
    
    @parameter
    fn diff[nelts : Int](x : Int):
        #print(a.simd_load[nelts](x))
        c.simd_store[nelts](x, a.simd_load[nelts](x) - b.simd_load[nelts](x))
        #print(nelts, x)
    vectorize[nelts, diff](size)

    for i in range(size):
        print(c[i])

    #print(a.simd_load[nelts](0))


For example:

2305843009213693952
4611686018780248321
3
1
1
1
1
1
1
1407374883553281


If I uncomment last print statement, suddenly results are correct:

1
1
1
1
1
1
1
1
1
1
[1, 2]


Can someone please explain this behaviour? Am I doing something wrong with SIMD?
Was this page helpful?