Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for randcase #42

Closed
ishitapvips opened this issue Oct 8, 2020 · 4 comments
Closed

Support for randcase #42

ishitapvips opened this issue Oct 8, 2020 · 4 comments

Comments

@ishitapvips
Copy link

Hi,

System Verilog has a rand case property, that specifies the probability of any case statement to be selected. We tried some workaround but haven't found the solution. So, can we have support for the same in pyvsc or some other way to do it?

Thanks & Regards,
Ishita

@mballance
Copy link
Member

Hi @ishitapvips,
This is an interesting question, given that Python doesn't have a builtin case statement. I could see a couple of approaches. Perhaps you can provide feedback on which would fit best in your application.
Fundamentally, rand-case is the combination of weighted selection and a case statement. We could model this in Python with a weighted selection from a collection of values that represent the case options.
Let's say we're replacing a rand-case statement like the following:

randcase
    3 : x = 1;
    1 : x = 2;
    4 : x = 3;
endcase

This approach would look something like:

branch = vsc.select({ 1 : 3, 2 : 1, 3 : 4})
if branch == 1:
  x = 1
elif branch == 2:
  x = 2
elif branch == 3:
  x = 3

In other words, we specify a map with the key being the branch and the value being the branch weight. select returns the selected branch, and we use a regular if/else statement.

Another approach would be to use lambda expressions. Lambda expressions are pretty limited in Python, so you would likely need to put the actual implementation of the case branches in separate functions. That might look like this:

vsc.randcase([
  [3, lambda : do_branch1()],
  [1, lambda : do_branch2()],
  [4, lambda : do_branch3()]
  ])

So, a couple approaches to doing this in Python with PyVSC. Any thoughts on which approach would work best for your application?

Thanks and Best Regards,
Matthew

@ishita71
Copy link

Hi Matthew,

As per your convenience, any of the approaches will work for us. But lambda function would be straight forward so can you implement that one?

Thanks & Regards,
Ishita

@mballance
Copy link
Member

Hi Ishita,
I've added support for a 'randselect' method that performs a weighted selection between a list of lambda functions. Here's an example:

def test_randselect(self):
hist = [0]*4
def task(idx):
hist[idx] += 1
for i in range(100):
vsc.randselect([
(1, lambda: task(0)),
(1, lambda: task(1)),
(10, lambda: task(2)),
(10, lambda: task(3))])
print("hist: " + str(hist))
self.assertGreater(hist[3], hist[0])
self.assertGreater(hist[2], hist[1])

I'll leave this issue open until you're able to confirm that it works for you.

Best Regards,
Matthew

@ishita71
Copy link

Hi Matthew,

Randselect is working fine for me right now.
If any issue is found later will comment here.

Thanks & Regards,
Ishita Shah

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants