GithubHelp home page GithubHelp logo

Comments (5)

leonardt avatar leonardt commented on July 28, 2024

Is the idea here to dynamically set one index of arr? If so, you could try using set_index

def set_index(target: Array, value, idx: UInt):
"""
Returns a new value where index `idx` of value `target` is set to `value`
* `target` - a value of type `Array[N, T]`
* `value` - a value of type `T`
* `idx` - a value of type `UInt[clog2(N)]`
"""

class Test1(m.Circuit):
    io = m.IO(
        A=m.In(m.UInt[2]),
        B=m.In(m.UInt[4]),
        O=m.Out(m.UInt[4]),
    )
    arr = m.Register(m.Array[4, m.UInt[4]])()
    arr.I @= m.set_index(arr.O, io.B, io.A)
    io.O @= arr.O[0]

I'll have to investigate this error which is not very helpful, but I don't think this syntax would necessarily work in general because you'd want to specify a default value for the values that aren't written. The function set_index style implies that the value is unchanged for the indices which I think is more explicit.

from magma.

rkshthrmsh avatar rkshthrmsh commented on July 28, 2024

Hi @leonardt, yes, the intention was to dynamically set specific indices of a register array. As you mentioned, in the arr.I[io.A] @= io.B syntax, not specifying a default value for other indices is the issue.

In a Register, could the previous O values be considered as the default for Is that are not explicitly wired? Are there potential issues with that?

from magma.

rkshthrmsh avatar rkshthrmsh commented on July 28, 2024

As an extension, how can one dynamically set an index of an N-D array?
Based on the function signature of set_index, it appears that idx is UInt only.

class Test1(m.Circuit):
    io = m.IO(
        X=m.In(m.UInt[2]),
        Y=m.In(m.UInt[2]),
        B=m.In(m.UInt[4]),
        O=m.Out(m.UInt[4]),
    )
    arr = m.Register(m.Array[(4, 4), m.UInt[4]])()
    # Intention:
    # arr.I[io.Y][io.X] @= io.B
    arr.I @= m.set_index(arr.O, io.B, (io.X, io.Y)) # TypeError: Expected value to be the same type as `target`'s contents
    io.O @= arr.O[0][0]

from magma.

rkshthrmsh avatar rkshthrmsh commented on July 28, 2024

Figured out this can be done recursively like so:

    arr.I @= m.set_index(arr.O, m.set_index(arr.O[io.Y], io.B, io.X), io.Y)
    # Equivalent to:
    # arr.I[io.Y][io.X] @= io.B and all other inputs wired to defaults

However, the syntax can look messy when dealing with N-D arrays having multiple indices. Thought it might be better to simplify it to:

    arr.I @= m.set_index(arr.O, io.B, [io.Y, io.X])

Have opened a PR for this #1330.

from magma.

rkshthrmsh avatar rkshthrmsh commented on July 28, 2024

Merged in #1353

from magma.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.