Skip to content

Commit

Permalink
Merge pull request #169 from zeroasiccorp/ali/pytest_random
Browse files Browse the repository at this point in the history
Generate random seed using a pytest fixture
  • Loading branch information
azaidy authored Sep 4, 2024
2 parents 4f6b305 + 31a3762 commit 849ce47
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
16 changes: 16 additions & 0 deletions umi/sumi/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,19 @@ def sumi_dut(build_dir, request):
yield dut

dut.terminate()


def pytest_addoption(parser):
parser.addoption("--seed", type=int, action="store", help="Provide a fixed seed")


@pytest.fixture
def random_seed(request):
fixed_seed = request.config.getoption("--seed")
if fixed_seed is not None:
test_seed = fixed_seed
else:
test_seed = os.getpid()
print(f'Random seed used: {test_seed}')
yield test_seed
print(f'Random seed used: {test_seed}')
11 changes: 4 additions & 7 deletions umi/sumi/tests/test_crossbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
import pytest
import multiprocessing
import random
import numpy as np
from switchboard import UmiTxRx, random_umi_packet, delete_queue


def umi_send(x, n, ports):
import os
def umi_send(x, n, ports, seed):

random.seed(os.getpid())
np.random.seed(os.getpid())
random.seed(seed)

umi = UmiTxRx(f'client2rtl_{x}.q', '')
tee = UmiTxRx(f'tee_{x}.q', '')
Expand All @@ -32,7 +29,7 @@ def umi_send(x, n, ports):


@pytest.mark.skip(reason="Crossbar asserts output valid even when in reset")
def test_crossbar(sumi_dut, sb_umi_valid_mode, sb_umi_ready_mode):
def test_crossbar(sumi_dut, random_seed, sb_umi_valid_mode, sb_umi_ready_mode):
n = 100
ports = 4
for x in range(ports):
Expand All @@ -59,7 +56,7 @@ def test_crossbar(sumi_dut, sb_umi_valid_mode, sb_umi_ready_mode):

procs = []
for x in range(ports):
procs.append(multiprocessing.Process(target=umi_send, args=(x, n, ports,)))
procs.append(multiprocessing.Process(target=umi_send, args=(x, n, ports, (random_seed+x),)))

for proc in procs:
proc.start()
Expand Down
5 changes: 4 additions & 1 deletion umi/sumi/tests/test_fifo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
from switchboard import UmiTxRx


def test_fifo(sumi_dut, sb_umi_valid_mode, sb_umi_ready_mode):
def test_fifo(sumi_dut, random_seed, sb_umi_valid_mode, sb_umi_ready_mode):

random.seed(random_seed)
np.random.seed(random_seed)

# launch the simulation
sumi_dut.simulate(plusargs=[('valid_mode', sb_umi_valid_mode), ('ready_mode', sb_umi_ready_mode)])
Expand Down
5 changes: 4 additions & 1 deletion umi/sumi/tests/test_fifo_flex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
from switchboard import UmiTxRx


def test_fifo_flex(sumi_dut, sb_umi_valid_mode, sb_umi_ready_mode):
def test_fifo_flex(sumi_dut, random_seed, sb_umi_valid_mode, sb_umi_ready_mode):

random.seed(random_seed)
np.random.seed(random_seed)

# launch the simulation
sumi_dut.simulate(plusargs=[('valid_mode', sb_umi_valid_mode), ('ready_mode', sb_umi_ready_mode)])
Expand Down
4 changes: 3 additions & 1 deletion umi/sumi/tests/test_mem_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def apply_atomic(origdata, atomicdata, operation, maxrange):
return tempval


def test_mem_agent(sumi_dut, sb_umi_valid_mode, sb_umi_ready_mode):
def test_mem_agent(sumi_dut, random_seed, sb_umi_valid_mode, sb_umi_ready_mode):

np.random.seed(random_seed)

# launch the simulation
sumi_dut.simulate(plusargs=[('valid_mode', sb_umi_valid_mode), ('ready_mode', sb_umi_ready_mode)])
Expand Down

0 comments on commit 849ce47

Please sign in to comment.