NickJJ
MModular
•Created by NickJJ on 12/30/2023 in #questions
Adding __takeinit__ makes a difference in assignment
I'm playing with
This is super weird to me. It calls
HeapArray
and I cannot understand what is going on.
struct HeapArray:
var data: Pointer[Int]
var size: Int
fn __init__(inout self, size: Int, val: Int):
self.data = Pointer[Int].alloc(size)
self.size = size
for i in range(size):
self.data.store(i, val)
fn dump(self):
for i in range(self.size):
print(self.data.load(i))
fn __copyinit__(inout self, existing: Self):
print("HeapArray copyinit")
self.data = Pointer[Int].alloc(existing.size)
self.size = existing.size
for i in range(self.size):
self.data.store(i, existing.data.load(i))
fn __moveinit__(inout self, owned existing: Self):
print("HeapArray moveinit")
print("existing address: ", existing.data.__as_index())
self.data = existing.data
self.size = existing.size
existing.data = Pointer[Int].get_null()
existing.size = 0
fn __takeinit__(inout self, inout existing: Self):
print("HeapArray takeinit")
self.data = existing.data
self.size = existing.size
existing.data = Pointer[Int].get_null()
existing.size = 0
fn __del__(owned self):
print("Memory freed at address: ", self.data.__as_index())
fn main():
let a = HeapArray(3, 2)
print("a address: ", a.data.__as_index())
let b = a
print("b address: ", b.data.__as_index())
a.dump()
b.dump()
struct HeapArray:
var data: Pointer[Int]
var size: Int
fn __init__(inout self, size: Int, val: Int):
self.data = Pointer[Int].alloc(size)
self.size = size
for i in range(size):
self.data.store(i, val)
fn dump(self):
for i in range(self.size):
print(self.data.load(i))
fn __copyinit__(inout self, existing: Self):
print("HeapArray copyinit")
self.data = Pointer[Int].alloc(existing.size)
self.size = existing.size
for i in range(self.size):
self.data.store(i, existing.data.load(i))
fn __moveinit__(inout self, owned existing: Self):
print("HeapArray moveinit")
print("existing address: ", existing.data.__as_index())
self.data = existing.data
self.size = existing.size
existing.data = Pointer[Int].get_null()
existing.size = 0
fn __takeinit__(inout self, inout existing: Self):
print("HeapArray takeinit")
self.data = existing.data
self.size = existing.size
existing.data = Pointer[Int].get_null()
existing.size = 0
fn __del__(owned self):
print("Memory freed at address: ", self.data.__as_index())
fn main():
let a = HeapArray(3, 2)
print("a address: ", a.data.__as_index())
let b = a
print("b address: ", b.data.__as_index())
a.dump()
b.dump()
a address: 94060226983856
HeapArray copyinit
HeapArray moveinit
existing address: 94060215912848
b address: 94060215912848
2
2
2
Memory freed at address: 94060226983856
2
2
2
Memory freed at address: 94060215912848
a address: 94060226983856
HeapArray copyinit
HeapArray moveinit
existing address: 94060215912848
b address: 94060215912848
2
2
2
Memory freed at address: 94060226983856
2
2
2
Memory freed at address: 94060215912848
copyinit
and moveinit
, why?22 replies
MModular
•Created by NickJJ on 12/15/2023 in #questions
Is Modular planning on implementing an official DL framework?
Like pytorch, flax.
#ai
1 replies