8000 DTypepointer scatter fails to compile inside an object · Issue #3355 · modular/modular · GitHub
[go: up one dir, main page]

Skip to content
DTypepointer scatter fails to compile inside an object #3355
@lbartworks

Description

@lbartworks

Discussed in #3107

[EDIT Oct/2024] Updated for new UnsafePointer type

Originally posted by lbartworks June 23, 2024
In the following simple example (can be run on the playground), the compiler fails to identify the UnsafePointer.scatter parameters when the code is run within an object method, but works well when called directly. If you remove the fn scatter declaration, the code works well, with a line between comments doing essentially the same. Am I missing something or is it a bug?

Compile error:

/source/prog.mojo:22:42: error: invalid call to 'scatter': could not deduce positional-only parameter #4 of callee 'scatter'
        self._matPtr.scatter[width=nelts](indices, value, mask)           
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
/source/prog.mojo:22:52: note: failed to infer parameter #4, parameter inferred to two different values: 'dtype' and 'int8'
        self._matPtr.scatter[width=nelts](indices, value, mask)           
                                                   ^~~~~
/source/prog.mojo:1:1: note: function declared here
struct Matrix2D[dtype : DType= DType.float32]():
^
mojo: error: failed to parse the provided Mojo source module

COMPLETE CODE


struct Matrix2D[dtype : DType= DType.float32]():
    var _matPtr: UnsafePointer[Scalar[dtype]]
    var rows: Int
    var cols: Int

    @always_inline  
    fn __init__(inout self, rows: Int, cols: Int):
      self._matPtr = UnsafePointer[Scalar[dtype]].alloc(rows*cols)
      self.rows = rows
      self.cols= cols

    @always_inline
    fn __getitem__(self, x: Int, y: Int) -> SIMD[dtype,1]:
        return self._matPtr.load(x * self.cols + y)
        
    @always_inline
    fn __getitem__(self, idx: Int) -> SIMD[dtype,1]:
        return self._matPtr.load(idx) 

    fn scatter[nelts:Int](self, x:  SIMD[DType.int8,nelts], y:  SIMD[DType.int8,nelts], value: SIMD[DType.int8, nelts], mask: SIMD[DType.bool, nelts]) -> None:
        var  indices =x * self.cols + y
        self._matPtr.scatter[width=nelts](indices, value, mask)           
    
def main():
 
    var values=SIMD[DType.int8,8](10,11,12,13,14,15,16,17)
    var x=SIMD[DType.int8,8](0,0,0,1,1,1,2,2)
    var y=SIMD[DType.int8,8](0,1,2,0,1,2,0,1)
    var mask=SIMD[DType.bool,8](True, False, False, True, True, True, True, True)

    var destino=Matrix2D[DType.int8](3,3)
    var offset=x*destino.cols + y

    ####   this line compiles and works well ######################################################
    destino._matPtr.scatter[width=8](offset, values, mask)
    ##############################################################################
    for x in range(3):
        for y in range(3):
          print(destino[x,y])
```</div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    mojoIssues that are related to mojomojo-repoTag all issues with this labelwaiting for responseNeeds action/response from contributor before a PR can proceed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0