vmois
vmois
MModular
Created by vmois on 3/29/2024 in #questions
When do you plan to open-source "algorithms" module?
At this moment, "algorithms" module is not available yet in the open-sourced code (source). Do you have an approximate plan when it will be open-sourced? For example, being able to extend sort to work with strings would be an amazing thing. Thank you.
1 replies
MModular
Created by vmois on 12/30/2023 in #questions
Owned convention does not destroy variable after transferring ownership
Hello everybody! I am working on a project that has a small exercises to learn Mojo. Currently, I am working on value ownership section and was playing with a code below.
# TASK
# Make file compile with no errors

# I AM NOT DONE

fn take_ptr(owned array: Pointer[Int]):
array.store(2, 23)


fn main():
# this line just allocates pointer for 3 integers
var array = Pointer[Int].alloc(3)

array.store(0, 1)
array.store(1, 2)

# this function call does not need any changes
take_ptr(array^)

array.store(2, 2)
print(array[2])
# TASK
# Make file compile with no errors

# I AM NOT DONE

fn take_ptr(owned array: Pointer[Int]):
array.store(2, 23)


fn main():
# this line just allocates pointer for 3 integers
var array = Pointer[Int].alloc(3)

array.store(0, 1)
array.store(1, 2)

# this function call does not need any changes
take_ptr(array^)

array.store(2, 2)
print(array[2])
The issue I have is that this code compiles but to my understanding of owned and ownership operator it shouldn't. If you take equivalent code for string, it has an error "a is not initialized":
fn take_text(owned text: String):
text += "🔥"
return text


fn main():
var a: String = "mojo"

take_text(a^)
print(a)
fn take_text(owned text: String):
text += "🔥"
return text


fn main():
var a: String = "mojo"

take_text(a^)
print(a)
For equivalent integer code:
fn take_value(owned x: Int):
x += 2


fn main():
var x: Int = 1
take_value(x^)
print(x)
fn take_value(owned x: Int):
x += 2


fn main():
var x: Int = 1
take_value(x^)
print(x)
It throws no error, and value of x is 1. Can someone please explain if this behaviour is correct or am I missing something? Thank you.
5 replies
MModular
Created by vmois on 10/22/2023 in #questions
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))
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
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]
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?
24 replies
MModular
Created by vmois on 9/12/2023 in #questions
Add documentation for Runtime
In a Mandelbrot notebook, we have seen examples of using Runtime, but as for now there is no documentations about arguments, use cases, etc. Any plans to provide more information? Thank you.
2 replies