Skip to content

pysim: option to capture simulation output in a python object #327

Open
@mszep

Description

@mszep

Would it make sense to have a way to capture simulation output in a python object, rather than having to use a vcd file and a separate reader program?

I'm building a physics simulator in nmigen and it would be super helpful when debugging to be able to plot a signal's trace (over eg 1000 timesteps) in matplotlib, within the same python session as the simulation.

Does this make sense as a workflow?
Is there already a way to do this?

Activity

whitequark

whitequark commented on Feb 28, 2020

@whitequark
Member

Yes, it would make perfect sense as a workflow. Do you have any preferences regarding an exported API here?

mszep

mszep commented on Feb 28, 2020

@mszep
Author

Thinking about the API, I'd have no preference -- whatever's most convenient on the nmigen side.

I guess for consistency we could have a context manager similar to write_vcd, so something like

traces = {}
with write_dict(traces):
    sim.run()

where traces would then contain the signal identifiers as keys, and perhaps a list of (timestamp, value) pairs as values?

awygle

awygle commented on Feb 28, 2020

@awygle
Contributor

I'd like us to make sure that this can be used incrementally - that is, run for N clock ticks and return the traces, do something with them, then run for M more clock ticks and return those traces. This ties into my dream of an interactive simulator.

Note that I don't think the context manager API is incompatible with the above, just making a use case known. run_until and step in place of run should cover it.

mszep

mszep commented on Feb 29, 2020

@mszep
Author

Yes, that would be awesome! I was thinking of the possibility of making interactive simulations with ipywidgets, but if making it iterative will complicate the API or the implementation of the simple case, perhaps the simple case should take priority.

whitequark

whitequark commented on Feb 29, 2020

@whitequark
Member

It wouldn't be hard to make it iterative; the VCD writer isn't, but only due to limitations of the VCD file format.

mszep

mszep commented on Mar 7, 2020

@mszep
Author

FWIW, I ended up just passing a StringIO object to the write_vcd context manager and parsing the vcd text data into plottable form.

Therefore, I think this issue can be deprioritized or closed altogether.

whitequark

whitequark commented on Mar 8, 2020

@whitequark
Member

I think it would be an appropriate and highly useful feature to have: your workaround is... let's say suboptimal (no shade but it's pretty inefficient), and what @awygle wants here is also very reasonable.

added a commit that references this issue on Aug 20, 2021
alanvgreen

alanvgreen commented on Aug 20, 2021

@alanvgreen
Contributor

This looks like a typical application for an Observer. The Observer is registered with the simulator, which then notifies it of interesting events.

I made a draft PR as an RFC. I think this is general enough to:

  • act as the interface for _VCDWriter
  • capture dictionaries of values at each step for @mszep's use case
  • intercept simulator state at defined numbers of clock cycles or timestamps for @awygle's use case

Please send comments.

whitequark

whitequark commented on Feb 3, 2023

@whitequark
Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @whitequark@mszep@alanvgreen@awygle

        Issue actions

          pysim: option to capture simulation output in a python object · Issue #327 · amaranth-lang/amaranth