mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[BUG]: Code showing non deterministic behaviour

Open soraros opened this issue 1 year ago • 0 comments

Bug description

As title. Originally reported by @Harambe on Discord. Doesn't seem to reproduce on M3 Mac according to @mikowals.

Steps to reproduce

from algorithm import vectorize

struct Vec:
  var length: Int
  var data: DTypePointer[DType.float32]

  fn __init__(inout self, length: Int):
    self.length = length
    self.data = DTypePointer[DType.float32].alloc(self.length)
    for item in range(self.length):
      self.data.store(item, 1)

  fn __copyinit__(inout self, other: Self):
    self.length = other.length
    self.data = DTypePointer[DType.float32].alloc(self.length)
    memcpy(self.data, other.data, self.length)

  fn __del__(owned self):
    self.data.free()

  fn non_deterministic(self) -> Vec:
    let a = Vec(self.length)
    var b = Vec(self.length)
    for i in range(b.length):
      @parameter
      fn f[simd_width: Int](j: Int):
        _ = j  # unused
        b.data.store(i, a.data.load(i))
      vectorize[1, f](self.length)
    return b

fn print_vec(self: Vec):
  for i in range(self.length):
    print_no_newline(self.data[i])
    print_no_newline(",")
  print("\n")

fn main():
  let a = Vec(2)
  let b = a.non_deterministic()
  print_vec(b)

System information

Mojo 0.7.0 on Docker, Intel Mac.

soraros avatar Feb 15 '24 21:02 soraros