views:

47

answers:

2

Hi guys, I have to build a dual-core processor simulator in C (it's actually a multilevel memory simulation, cache L1/L2, block substitution, etc). Thing is, I'm having a hard time figuring a way to synchronize the cores (which I'm programming as threads). Any ideas how I could do a global clock? Should I change from threads to child processes? Thanks in advance

A: 

So many options here: since you might end-up supporting complex interactions with multiple threads, you might want to consider using a "virtual clock" with a message passing "bus". This way, you'll have more time to focus on the core functionality instead of debugging the synchonization logic...

Using this technique, you can build a state-machine (see here) per "actor" thread (worst case) and worry less about mutexes/conditions. Once you've got this base, you'll be able to handle cases that show up mid-stream (e.g. "I forgot about this detail... no worries, just add a state here... don't have to re-shuffle my mutexes).

Also, since doing simulation is all about "virtual time" (since you can't run real-time !), then having a base architecture based on a "virtual clock" abstracts the problem to an appropriate level.

jldupont
The assignment probably requires a shared memory model instead of a message passing model because that's how real machines are built today.
Dave
@Dave: be my guess and try working this out whilst still respecting all the intricate details of a CPU. Granted I have built such simulators 15years ago and I am a bit rusty on this sort of project, but I would still go with my solution even considering my "new" 15years of experience.
jldupont
@Dave: one last thing, do you know how the simulation tools (from companies such as Mentor, Synopsys work? ... virtual clocks...)
jldupont
@thiagobrandam: is my answer satisfactory?
jldupont
First of all, thanks jldupont and dave. We just gave up using threads. Instead we chose to serialize both processors in one function and used rand() to simulate the race conditions when they are competing for the L2 cache. Both return the time they are going to be busy and two ifs (inside a loop) verify if they are ready to fetch more data from wherever they need to (since this simulation is all about fetching data), while a global clock and a 16-bit LRU counter are incremented. It's easier to block and synchronize, might not be totally realistic but the results are pretty close to.
thiagobrandam
A: 

You could have a clock thread plus N processor threads. The clock thread can explicitly make calls to each processor thread to make 1 processing step. Each processor thread gets a call from the clock thread to doStep(); it makes one step, then returns control back to the clock thread.

You could also randomize the order in which the processor threads are called to do steps so that you're more likely to catch bugs in your client code.

Dave