sammy17
sammy17

Reputation: 75

How to use multi-processing in a Python cocotb test?

I have a working cocotb setup as below. I want to use multi-processing in Python to run iterations in the loop to parallelize the execution.

Current setup:

from cocotb.decorators import coroutine
factory = TestFactory(Run)

@coroutine
def Run(dut,arg1, arg2):
    for i in range(N):
        ret = yield host.run_test(arg1, arg2)

What I tried:

from cocotb.decorators import coroutine
from multiprocessing import Process, Queue, Manager, Value

factory = TestFactory(Run)

@coroutine
def run_thread(arg1, arg2):
    for i in range(N/n_threads):
        ret = yield host.run_test(arg1, arg2)

@coroutine
def Run(dut,arg1, arg2):
    threads = []
    for n in range(0, n_threads):
        threads.append(Process(target=run_thread, args=(arg1, arg2)))
    for n in range(0, n_threads):
        threads[n].start()
    for n in range(0, n_threads):
        threads[n].join()
        

As shown above, I tried putting all the content of the loop into a function and calling it using multiprocessing.Process. But when I execute it this way, run_test is not getting called properly.

I believe this is because I have a yield command for run_test and run_thread function returns a generator because of that. However, I don't see any way to iterate through the output of run_thread function because I am calling it in a thread.

Is there any way that I can get this working? Any guidance is much appreciated.

PS: I made a slight mistake in the second code in run_thread function. We need to have a for loop there are well. I corrected it now.

Upvotes: 1

Views: 1213

Answers (1)

Bob
Bob

Reputation: 14664

cocotb operates simulating a single instance of the design, if you are trying to run multiple tests in parallel you have to replicate your design as well. In that case it is better to parallelize in the shell, so that you can start multiple simulator instances.

If you simply want parallelism in the testbench so that each job runs simultaneously with respect to simulation, you should consider concurrent execution.

Upvotes: 1

Related Questions