simulate.cc (9750:4e1f22617336) simulate.cc (9983:2cce74fe359e)
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * Copyright (c) 2013 Advanced Micro Devices, Inc.
4 * Copyright (c) 2013 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the

--- 13 unchanged lines hidden (view full) ---

24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 * Steve Reinhardt
30 */
31
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met: redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer;
11 * redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the

--- 13 unchanged lines hidden (view full) ---

26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Authors: Nathan Binkert
31 * Steve Reinhardt
32 */
33
34#include <mutex>
35#include <thread>
36
32#include "base/misc.hh"
33#include "base/pollevent.hh"
34#include "base/types.hh"
35#include "sim/async.hh"
36#include "sim/eventq_impl.hh"
37#include "sim/sim_events.hh"
38#include "sim/sim_exit.hh"
39#include "sim/simulate.hh"
40#include "sim/stat_control.hh"
41
37#include "base/misc.hh"
38#include "base/pollevent.hh"
39#include "base/types.hh"
40#include "sim/async.hh"
41#include "sim/eventq_impl.hh"
42#include "sim/sim_events.hh"
43#include "sim/sim_exit.hh"
44#include "sim/simulate.hh"
45#include "sim/stat_control.hh"
46
47//! Mutex for handling async events.
48std::mutex asyncEventMutex;
49
50//! Global barrier for synchronizing threads entering/exiting the
51//! simulation loop.
52Barrier *threadBarrier;
53
54//! forward declaration
55Event *doSimLoop(EventQueue *);
56
57/**
58 * The main function for all subordinate threads (i.e., all threads
59 * other than the main thread). These threads start by waiting on
60 * threadBarrier. Once all threads have arrived at threadBarrier,
61 * they enter the simulation loop concurrently. When they exit the
62 * loop, they return to waiting on threadBarrier. This process is
63 * repeated until the simulation terminates.
64 */
65static void
66thread_loop(EventQueue *queue)
67{
68 while (true) {
69 threadBarrier->wait();
70 doSimLoop(queue);
71 }
72}
73
42/** Simulate for num_cycles additional cycles. If num_cycles is -1
43 * (the default), do not limit simulation; some other event must
44 * terminate the loop. Exported to Python via SWIG.
45 * @return The SimLoopExitEvent that caused the loop to exit.
46 */
74/** Simulate for num_cycles additional cycles. If num_cycles is -1
75 * (the default), do not limit simulation; some other event must
76 * terminate the loop. Exported to Python via SWIG.
77 * @return The SimLoopExitEvent that caused the loop to exit.
78 */
47SimLoopExitEvent *
79GlobalSimLoopExitEvent *
48simulate(Tick num_cycles)
49{
80simulate(Tick num_cycles)
81{
82 // The first time simulate() is called from the Python code, we need to
83 // create a thread for each of event queues referenced by the
84 // instantiated sim objects.
85 static bool threads_initialized = false;
86 static std::vector<std::thread *> threads;
87
88 if (!threads_initialized) {
89 threadBarrier = new Barrier(numMainEventQueues);
90
91 // the main thread (the one we're currently running on)
92 // handles queue 0, so we only need to allocate new threads
93 // for queues 1..N-1. We'll call these the "subordinate" threads.
94 for (uint32_t i = 1; i < numMainEventQueues; i++) {
95 threads.push_back(new std::thread(thread_loop, mainEventQueue[i]));
96 }
97
98 threads_initialized = true;
99 }
100
50 inform("Entering event queue @ %d. Starting simulation...\n", curTick());
51
52 if (num_cycles < MaxTick - curTick())
53 num_cycles = curTick() + num_cycles;
54 else // counter would roll over or be set to MaxTick anyhow
55 num_cycles = MaxTick;
56
101 inform("Entering event queue @ %d. Starting simulation...\n", curTick());
102
103 if (num_cycles < MaxTick - curTick())
104 num_cycles = curTick() + num_cycles;
105 else // counter would roll over or be set to MaxTick anyhow
106 num_cycles = MaxTick;
107
57 Event *limit_event =
58 new SimLoopExitEvent("simulate() limit reached", 0);
59 mainEventQueue.schedule(limit_event, num_cycles);
108 GlobalEvent *limit_event = new GlobalSimLoopExitEvent(num_cycles,
109 "simulate() limit reached", 0, 0);
60
110
111 GlobalSyncEvent *quantum_event = NULL;
112 if (numMainEventQueues > 1) {
113 if (simQuantum == 0) {
114 fatal("Quantum for multi-eventq simulation not specified");
115 }
116
117 quantum_event = new GlobalSyncEvent(simQuantum, simQuantum,
118 EventBase::Progress_Event_Pri, 0);
119
120 inParallelMode = true;
121 }
122
123 // all subordinate (created) threads should be waiting on the
124 // barrier; the arrival of the main thread here will satisfy the
125 // barrier, and all threads will enter doSimLoop in parallel
126 threadBarrier->wait();
127 Event *local_event = doSimLoop(mainEventQueue[0]);
128 assert(local_event != NULL);
129
130 inParallelMode = false;
131
132 // locate the global exit event and return it to Python
133 BaseGlobalEvent *global_event = local_event->globalEvent();
134 assert(global_event != NULL);
135
136 GlobalSimLoopExitEvent *global_exit_event =
137 dynamic_cast<GlobalSimLoopExitEvent *>(global_event);
138 assert(global_exit_event != NULL);
139
140 // if we didn't hit limit_event, delete it.
141 if (global_exit_event != limit_event) {
142 assert(limit_event->scheduled());
143 limit_event->deschedule();
144 delete limit_event;
145 }
146
147 //! Delete the simulation quantum event.
148 if (quantum_event != NULL) {
149 quantum_event->deschedule();
150 delete quantum_event;
151 }
152
153 return global_exit_event;
154}
155
156/**
157 * Test and clear the global async_event flag, such that each time the
158 * flag is cleared, only one thread returns true (and thus is assigned
159 * to handle the corresponding async event(s)).
160 */
161static bool
162testAndClearAsyncEvent()
163{
164 bool was_set = false;
165 asyncEventMutex.lock();
166
167 if (async_event) {
168 was_set = true;
169 async_event = false;
170 }
171
172 asyncEventMutex.unlock();
173 return was_set;
174}
175
176/**
177 * The main per-thread simulation loop. This loop is executed by all
178 * simulation threads (the main thread and the subordinate threads) in
179 * parallel.
180 */
181Event *
182doSimLoop(EventQueue *eventq)
183{
184 // set the per thread current eventq pointer
185 curEventQueue(eventq);
186 eventq->handleAsyncInsertions();
187
61 while (1) {
62 // there should always be at least one event (the SimLoopExitEvent
63 // we just scheduled) in the queue
188 while (1) {
189 // there should always be at least one event (the SimLoopExitEvent
190 // we just scheduled) in the queue
64 assert(!mainEventQueue.empty());
65 assert(curTick() <= mainEventQueue.nextTick() &&
191 assert(!eventq->empty());
192 assert(curTick() <= eventq->nextTick() &&
66 "event scheduled in the past");
67
193 "event scheduled in the past");
194
68 Event *exit_event = mainEventQueue.serviceOne();
195 Event *exit_event = eventq->serviceOne();
69 if (exit_event != NULL) {
196 if (exit_event != NULL) {
70 // hit some kind of exit event; return to Python
71 // event must be subclass of SimLoopExitEvent...
72 SimLoopExitEvent *se_event;
73 se_event = dynamic_cast<SimLoopExitEvent *>(exit_event);
74
75 if (se_event == NULL)
76 panic("Bogus exit event class!");
77
78 // if we didn't hit limit_event, delete it
79 if (se_event != limit_event) {
80 assert(limit_event->scheduled());
81 limit_event->squash();
82 hack_once("be nice to actually delete the event here");
83 }
84
85 return se_event;
197 return exit_event;
86 }
87
198 }
199
88 if (async_event) {
200 if (async_event && testAndClearAsyncEvent()) {
89 async_event = false;
90 if (async_statdump || async_statreset) {
91 Stats::schedStatEvent(async_statdump, async_statreset);
92 async_statdump = false;
93 async_statreset = false;
94 }
95
96 if (async_exit) {

--- 11 unchanged lines hidden (view full) ---

108 async_exception = false;
109 return NULL;
110 }
111 }
112 }
113
114 // not reached... only exit is return on SimLoopExitEvent
115}
201 async_event = false;
202 if (async_statdump || async_statreset) {
203 Stats::schedStatEvent(async_statdump, async_statreset);
204 async_statdump = false;
205 async_statreset = false;
206 }
207
208 if (async_exit) {

--- 11 unchanged lines hidden (view full) ---

220 async_exception = false;
221 return NULL;
222 }
223 }
224 }
225
226 // not reached... only exit is return on SimLoopExitEvent
227}
116