ModularM
Modular•3y ago•
3 replies
Stijn

cannot be copied into its destination

Hi 👋 ,
I want to extend my matrix class with the transpose functionality

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


But i get:
error: value of type 'Matrix' cannot be copied into its destination
        return transposed


I experience similar behaviour when trying to return something of type Matrix, in other functions as well.
Any idea on how to overcome this?
Was this page helpful?