cannot be copied into its destination
Hi 👋 ,
I want to extend my matrix class with the transpose functionality
But i get:
I experience similar behaviour when trying to return something of type Matrix, in other functions as well.
Any idea on how to overcome this?
alias dtype = DType.float32
alias type = Float32
struct Matrix:
var data: DTypePointer[dtype]
var rows: Int
var cols: Int
fn __init__(inout self, rows: Int, cols: Int):
self.data = DTypePointer[dtype].alloc(rows * cols)
rand(self.data, rows * cols)
self.rows = rows
self.cols = cols
fn __del__(owned self):
self.data.free()
fn zero(inout self):
memset_zero(self.data, self.rows * self.cols)
@always_inline
fn __getitem__(self, y: Int, x: Int) -> type:
return self.load[1](y, x)
@always_inline
fn __setitem__(self, y: Int, x: Int, val: type):
return self.store[1](y, x, val)
@always_inline
fn load[nelts: Int](self, y: Int, x: Int) -> SIMD[dtype, nelts]:
return self.data.simd_load[nelts](y * self.cols + x)
@always_inline
fn store[nelts: Int](self, y: Int, x: Int, val: SIMD[dtype, nelts]):
return self.data.simd_store[nelts](y * self.cols + x, val)
fn __str__(inout self) -> StringLiteral:
let matrix_str: StringLiteral = ""
"""
TODO
"""
return matrix_str
@always_inline
fn T(inout self) -> Matrix:
let transposed = Matrix(self.cols, self.rows)
for i in range(self.rows):
for j in range(self.cols):
transposed[j, i] = self[i, j]
return transposed
alias dtype = DType.float32
alias type = Float32
struct Matrix:
var data: DTypePointer[dtype]
var rows: Int
var cols: Int
fn __init__(inout self, rows: Int, cols: Int):
self.data = DTypePointer[dtype].alloc(rows * cols)
rand(self.data, rows * cols)
self.rows = rows
self.cols = cols
fn __del__(owned self):
self.data.free()
fn zero(inout self):
memset_zero(self.data, self.rows * self.cols)
@always_inline
fn __getitem__(self, y: Int, x: Int) -> type:
return self.load[1](y, x)
@always_inline
fn __setitem__(self, y: Int, x: Int, val: type):
return self.store[1](y, x, val)
@always_inline
fn load[nelts: Int](self, y: Int, x: Int) -> SIMD[dtype, nelts]:
return self.data.simd_load[nelts](y * self.cols + x)
@always_inline
fn store[nelts: Int](self, y: Int, x: Int, val: SIMD[dtype, nelts]):
return self.data.simd_store[nelts](y * self.cols + x, val)
fn __str__(inout self) -> StringLiteral:
let matrix_str: StringLiteral = ""
"""
TODO
"""
return matrix_str
@always_inline
fn T(inout self) -> Matrix:
let transposed = Matrix(self.cols, self.rows)
for i in range(self.rows):
for j in range(self.cols):
transposed[j, i] = self[i, j]
return transposed
error: value of type 'Matrix' cannot be copied into its destination
return transposed
error: value of type 'Matrix' cannot be copied into its destination
return transposed
2 Replies
Just add a
__copyinit__
to your struct:
...
fn __copyinit__(inout self, other: Self):
self.data = other.data
self.rows = other.rows
self.cols = other.cols
...
fn __copyinit__(inout self, other: Self):
self.data = other.data
self.rows = other.rows
self.cols = other.cols
As soon as I start using the copies I'm getting:
Any idea?
Solved it:
free(): double free detected in tcache 2
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0. Program arguments: mojo run main.mojo
#0 0x00005602b1ba3797 (mojo+0x5bc797)
#1 0x00005602b1ba136e (mojo+0x5ba36e)
#2 0x00005602b1ba3e6f (mojo+0x5bce6f)
#3 0x00007f7086242520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007f7086296a7c __pthread_kill_implementation ./nptl/./nptl/pthread_kill.c:44:76
#5 0x00007f7086296a7c __pthread_kill_internal ./nptl/./nptl/pthread_kill.c:78:10
#6 0x00007f7086296a7c pthread_kill ./nptl/./nptl/pthread_kill.c:89:10
#7 0x00007f7086242476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#8 0x00007f70862287f3 abort ./stdlib/./stdlib/abort.c:81:7
#9 0x00007f70862896f6 __libc_message ./libio/../sysdeps/posix/libc_fatal.c:155:5
#10 0x00007f70862a0d7c ./malloc/./malloc/malloc.c:5668:3
#11 0x00007f70862a312b _int_free ./malloc/./malloc/malloc.c:4687:2
#12 0x00007f70862a54d3 __libc_free ./malloc/./malloc/malloc.c:3394:3
#13 0x00007f6fc400150f
Aborted
free(): double free detected in tcache 2
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0. Program arguments: mojo run main.mojo
#0 0x00005602b1ba3797 (mojo+0x5bc797)
#1 0x00005602b1ba136e (mojo+0x5ba36e)
#2 0x00005602b1ba3e6f (mojo+0x5bce6f)
#3 0x00007f7086242520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007f7086296a7c __pthread_kill_implementation ./nptl/./nptl/pthread_kill.c:44:76
#5 0x00007f7086296a7c __pthread_kill_internal ./nptl/./nptl/pthread_kill.c:78:10
#6 0x00007f7086296a7c pthread_kill ./nptl/./nptl/pthread_kill.c:89:10
#7 0x00007f7086242476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#8 0x00007f70862287f3 abort ./stdlib/./stdlib/abort.c:81:7
#9 0x00007f70862896f6 __libc_message ./libio/../sysdeps/posix/libc_fatal.c:155:5
#10 0x00007f70862a0d7c ./malloc/./malloc/malloc.c:5668:3
#11 0x00007f70862a312b _int_free ./malloc/./malloc/malloc.c:4687:2
#12 0x00007f70862a54d3 __libc_free ./malloc/./malloc/malloc.c:3394:3
#13 0x00007f6fc400150f
Aborted
@always_inline
fn __copyinit__(inout self, other: Matrix[dtype, n_rows, n_cols]):
self.data = DTypePointer[dtype].alloc(n_rows * n_cols)
for y in range(n_rows):
for x in range(n_cols):
self.data.simd_store[1](y * n_cols + x, other.data.simd_load[1](y * n_cols + x))
self.rows = other.rows
self.cols = other.cols
@always_inline
fn __copyinit__(inout self, other: Matrix[dtype, n_rows, n_cols]):
self.data = DTypePointer[dtype].alloc(n_rows * n_cols)
for y in range(n_rows):
for x in range(n_cols):
self.data.simd_store[1](y * n_cols + x, other.data.simd_load[1](y * n_cols + x))
self.rows = other.rows
self.cols = other.cols