gem5  v21.1.0.2
lds_state.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015 Advanced Micro Devices, Inc.
3  * All rights reserved.
4  *
5  * For use for simulation and test purposes only
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from this
19  * software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "gpu-compute/lds_state.hh"
35 
36 #include <array>
37 #include <cstdio>
38 #include <cstdlib>
39 
42 #include "gpu-compute/shader.hh"
43 
44 namespace gem5
45 {
46 
50 LdsState::LdsState(const Params &params) :
51  ClockedObject(params),
52  tickEvent(this),
53  cuPort(name() + ".port", this),
54  maximumSize(params.size),
55  range(params.range),
56  bankConflictPenalty(params.bankConflictPenalty),
57  banks(params.banks)
58 {
59  fatal_if(params.banks <= 0,
60  "Number of LDS banks should be positive number");
61  fatal_if((params.banks & (params.banks - 1)) != 0,
62  "Number of LDS banks should be a power of 2");
63  fatal_if(params.size <= 0,
64  "cannot allocate an LDS with a size less than 1");
65  fatal_if(params.size % 2,
66  "the LDS should be an even number");
67 }
68 
72 void
74 {
75  // check that this gets assigned to the same thing each time
76  fatal_if(!x_parent, "x_parent should not be nullptr");
77  fatal_if(x_parent == parent,
78  "should not be setting the parent twice");
79 
80  parent = x_parent;
81  _name = x_parent->name() + ".LdsState";
82 }
83 
87 unsigned
88 LdsState::countBankConflicts(PacketPtr packet, unsigned *bankAccesses)
89 {
90  Packet::SenderState *baseSenderState = packet->senderState;
91  while (baseSenderState->predecessor) {
92  baseSenderState = baseSenderState->predecessor;
93  }
94  const ComputeUnit::LDSPort::SenderState *senderState =
95  dynamic_cast<ComputeUnit::LDSPort::SenderState *>(baseSenderState);
96 
97  fatal_if(!senderState,
98  "did not get the right sort of sender state");
99 
100  GPUDynInstPtr gpuDynInst = senderState->getMemInst();
101 
102  return countBankConflicts(gpuDynInst, bankAccesses);
103 }
104 
105 // Count the total number of bank conflicts for the local memory packet
106 unsigned
108  unsigned *numBankAccesses)
109 {
110  int bank_conflicts = 0;
111  std::vector<int> bank;
112  // the number of LDS banks being touched by the memory instruction
113  int numBanks = std::min(parent->wfSize(), banks);
114  // if the wavefront size is larger than the number of LDS banks, we
115  // need to iterate over all work items to calculate the total
116  // number of bank conflicts
117  int groups = (parent->wfSize() > numBanks) ?
118  (parent->wfSize() / numBanks) : 1;
119  for (int i = 0; i < groups; i++) {
120  // Address Array holding all the work item addresses of an instruction
121  std::vector<Addr> addr_array;
122  addr_array.resize(numBanks, 0);
123  bank.clear();
124  bank.resize(banks, 0);
125  int max_bank = 0;
126 
127  // populate the address array for all active work items
128  for (int j = 0; j < numBanks; j++) {
129  if (gpuDynInst->exec_mask[(i*numBanks)+j]) {
130  addr_array[j] = gpuDynInst->addr[(i*numBanks)+j];
131  } else {
132  addr_array[j] = std::numeric_limits<Addr>::max();
133  }
134  }
135 
136  if (gpuDynInst->isLoad() || gpuDynInst->isStore()) {
137  // mask identical addresses
138  for (int j = 0; j < numBanks; ++j) {
139  for (int j0 = 0; j0 < j; j0++) {
140  if (addr_array[j] != std::numeric_limits<Addr>::max()
141  && addr_array[j] == addr_array[j0]) {
142  addr_array[j] = std::numeric_limits<Addr>::max();
143  }
144  }
145  }
146  }
147  // calculate bank conflicts
148  for (int j = 0; j < numBanks; ++j) {
149  if (addr_array[j] != std::numeric_limits<Addr>::max()) {
150  int bankId = addr_array[j] % banks;
151  bank[bankId]++;
152  max_bank = std::max(max_bank, bank[bankId]);
153  // Count the number of LDS banks accessed.
154  // Since we have masked identical addresses all remaining
155  // accesses will need to be serialized if they access
156  // the same bank (bank conflict).
157  (*numBankAccesses)++;
158  }
159  }
160  bank_conflicts += max_bank;
161  }
162  panic_if(bank_conflicts > parent->wfSize(),
163  "Max bank conflicts should match num of work items per instr");
164  return bank_conflicts;
165 }
166 
170 bool
172 {
173  return ownerLds->processPacket(packet);
174 }
175 
178 {
180  dynamic_cast<ComputeUnit::LDSPort::SenderState *>(
181  packet->senderState);
182  return ss->getMemInst();
183 }
184 
188 bool
190 {
191  unsigned bankAccesses = 0;
192  // the number of conflicts this packet will have when accessing the LDS
193  unsigned bankConflicts = countBankConflicts(packet, &bankAccesses);
194  // count the total number of physical LDS bank accessed
195  parent->stats.ldsBankAccesses += bankAccesses;
196  // count the LDS bank conflicts. A number set to 1 indicates one
197  // access per bank maximum so there are no bank conflicts
198  parent->stats.ldsBankConflictDist.sample(bankConflicts-1);
199 
200  GPUDynInstPtr dynInst = getDynInstr(packet);
201  // account for the LDS bank conflict overhead
202  int busLength = (dynInst->isLoad()) ? parent->loadBusLength() :
203  (dynInst->isStore()) ? parent->storeBusLength() :
205  // delay for accessing the LDS
206  Tick processingTime =
207  parent->cyclesToTicks(Cycles(bankConflicts * bankConflictPenalty)) +
208  parent->cyclesToTicks(Cycles(busLength));
209  // choose (delay + last packet in queue) or (now + delay) as the time to
210  // return this
211  Tick doneAt = earliestReturnTime() + processingTime;
212  // then store it for processing
213  return returnQueuePush(std::make_pair(doneAt, packet));
214 }
215 
219 bool
221 {
222  // TODO add time limits (e.g. one packet per cycle) and queue size limits
223  // and implement flow control
224  returnQueue.push(thePair);
225 
226  // if there is no set wakeup time, look through the queue
227  if (!tickEvent.scheduled()) {
228  process();
229  }
230 
231  return true;
232 }
233 
237 void
239 {
240  fatal("not implemented");
241 }
242 
246 void
248 {
249  // TODO verify that this is the right way to do this
250  assert(ownerLds->isRetryResp());
251  ownerLds->setRetryResp(false);
252  ownerLds->process();
253 }
254 
258 void
260 {
261  fatal("not implemented");
262 }
263 
267 bool
269 {
270  Tick now = clockEdge();
271 
272  // send back completed packets
273  while (!returnQueue.empty() && returnQueue.front().first <= now) {
274  PacketPtr packet = returnQueue.front().second;
275 
277  dynamic_cast<ComputeUnit::LDSPort::SenderState *>(
278  packet->senderState);
279 
280  GPUDynInstPtr gpuDynInst = ss->getMemInst();
281 
282  gpuDynInst->initiateAcc(gpuDynInst);
283 
284  packet->makeTimingResponse();
285 
286  returnQueue.pop();
287 
288  bool success = cuPort.sendTimingResp(packet);
289 
290  if (!success) {
291  retryResp = true;
292  panic("have not handled timing responses being NACK'd when sent"
293  "back");
294  }
295  }
296 
297  // determine the next wakeup time
298  if (!returnQueue.empty()) {
299 
300  Tick next = returnQueue.front().first;
301 
302  if (tickEvent.scheduled()) {
303 
304  if (next < tickEvent.when()) {
305 
307  tickEvent.schedule(next);
308  }
309  } else {
310  tickEvent.schedule(next);
311  }
312  }
313 
314  return true;
315 }
316 
320 void
322 {
323  ldsState->process();
324 }
325 
326 } // namespace gem5
gem5::LdsState::parent
ComputeUnit * parent
Definition: lds_state.hh:515
fatal
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:189
gem5::LdsState::TickEvent::process
virtual void process()
wake up at this time and perform specified actions
Definition: lds_state.cc:321
gem5::Event::when
Tick when() const
Get the time that the event is scheduled.
Definition: eventq.hh:508
gem5::LdsState::retryResp
bool retryResp
Definition: lds_state.hh:246
shader.hh
gem5::LdsState::CuSidePort::recvFunctional
virtual void recvFunctional(PacketPtr pkt)
receive a packet in functional mode
Definition: lds_state.cc:238
gem5::ComputeUnit::LDSPort::SenderState
SenderState is information carried along with the packet, esp.
Definition: compute_unit.hh:785
gem5::LdsState::Params
LdsStateParams Params
Definition: lds_state.hh:265
compute_unit.hh
gem5::ComputeUnit::stats
gem5::ComputeUnit::ComputeUnitStats stats
gem5::LdsState::setParent
void setParent(ComputeUnit *x_parent)
set the parent and name based on the parent
Definition: lds_state.cc:73
gem5::LdsState::LdsState
LdsState(const Params &params)
the default constructor that works with SWIG
Definition: lds_state.cc:50
gem5::ComputeUnit::ComputeUnitStats::ldsBankConflictDist
statistics::Distribution ldsBankConflictDist
Definition: compute_unit.hh:1019
std::vector< int >
gem5::ComputeUnit::storeBusLength
int storeBusLength() const
Definition: compute_unit.hh:394
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
gem5::LdsState::TickEvent::schedule
void schedule(Tick when)
Definition: lds_state.hh:148
gem5::statistics::DistBase::sample
void sample(const U &v, int n=1)
Add a value to the distribtion n times.
Definition: statistics.hh:1325
gem5::Packet::SenderState::predecessor
SenderState * predecessor
Definition: packet.hh:459
gem5::Cycles
Cycles is a wrapper class for representing cycle counts, i.e.
Definition: types.hh:78
gem5::ComputeUnit::loadBusLength
int loadBusLength() const
Definition: compute_unit.hh:395
gem5::ArmISA::j
Bitfield< 24 > j
Definition: misc_types.hh:57
gem5::ComputeUnit
Definition: compute_unit.hh:203
gem5::LdsState::getDynInstr
GPUDynInstPtr getDynInstr(PacketPtr packet)
Definition: lds_state.cc:177
gem5::Named::name
virtual std::string name() const
Definition: named.hh:47
gem5::Clocked::cyclesToTicks
Tick cyclesToTicks(Cycles c) const
Definition: clocked_object.hh:227
gem5::SimObject::params
const Params & params() const
Definition: sim_object.hh:176
gem5::Packet
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:283
gem5::LdsState::CuSidePort::recvTimingReq
virtual bool recvTimingReq(PacketPtr pkt)
receive the packet from the CU
Definition: lds_state.cc:171
gem5::Tick
uint64_t Tick
Tick count type.
Definition: types.hh:58
gem5::LdsState::earliestReturnTime
Tick earliestReturnTime() const
Definition: lds_state.hh:410
gem5::LdsState::processPacket
bool processPacket(PacketPtr packet)
process an incoming packet, add it to the return queue
Definition: lds_state.cc:189
gem5::ComputeUnit::ComputeUnitStats::ldsBankAccesses
statistics::Scalar ldsBankAccesses
Definition: compute_unit.hh:1018
gpu_dyn_inst.hh
gem5::ResponsePort::sendTimingResp
bool sendTimingResp(PacketPtr pkt)
Attempt to send a timing response to the request port by calling its corresponding receive function.
Definition: port.hh:370
ss
std::stringstream ss
Definition: trace.test.cc:45
gem5::LdsState::_name
std::string _name
Definition: lds_state.hh:517
gem5::LdsState::CuSidePort::recvRetry
virtual void recvRetry()
receive a retry
Definition: lds_state.cc:259
gem5::Packet::SenderState
A virtual base opaque structure used to hold state associated with the packet (e.g....
Definition: packet.hh:457
std::pair
STL pair class.
Definition: stl.hh:58
gem5::LdsState::bankConflictPenalty
int bankConflictPenalty
Definition: lds_state.hh:529
gem5::Packet::senderState
SenderState * senderState
This packet's sender state.
Definition: packet.hh:534
name
const std::string & name()
Definition: trace.cc:49
gem5::GPUDynInstPtr
std::shared_ptr< GPUDynInst > GPUDynInstPtr
Definition: misc.hh:51
gem5::ComputeUnit::wfSize
int wfSize() const
Definition: compute_unit.hh:396
gem5::ClockedObject
The ClockedObject class extends the SimObject with a clock and accessor functions to relate ticks to ...
Definition: clocked_object.hh:234
gem5::Clocked::clockEdge
Tick clockEdge(Cycles cycles=Cycles(0)) const
Determine the tick when a cycle begins, by default the current one, but the argument also enables the...
Definition: clocked_object.hh:177
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:203
gem5::Packet::makeTimingResponse
void makeTimingResponse()
Definition: packet.hh:1049
gem5::LdsState::countBankConflicts
unsigned countBankConflicts(PacketPtr packet, unsigned *bankAccesses)
derive the gpu mem packet from the packet and then count the bank conflicts
Definition: lds_state.cc:88
gem5::ComputeUnit::LDSPort::SenderState::getMemInst
GPUDynInstPtr getMemInst() const
Definition: compute_unit.hh:798
gem5::LdsState::returnQueue
std::queue< std::pair< Tick, PacketPtr > > returnQueue
Definition: lds_state.hh:243
gem5::LdsState::returnQueuePush
bool returnQueuePush(std::pair< Tick, PacketPtr > thePair)
add this to the queue of packets to be returned
Definition: lds_state.cc:220
gem5::LdsState::banks
int banks
Definition: lds_state.hh:532
gem5::LdsState::TickEvent::deschedule
void deschedule()
Definition: lds_state.hh:154
fatal_if
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:225
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::LdsState::process
bool process()
look for packets to return at this time
Definition: lds_state.cc:268
gem5::LdsState::CuSidePort::recvRespRetry
virtual void recvRespRetry()
receive a retry for a response
Definition: lds_state.cc:247
gem5::LdsState::cuPort
CuSidePort cuPort
Definition: lds_state.hh:513
gem5::LdsState::CuSidePort::ownerLds
LdsState * ownerLds
Definition: lds_state.hh:172
gem5::LdsState::tickEvent
TickEvent tickEvent
Definition: lds_state.hh:238
gem5::Event::scheduled
bool scheduled() const
Determine if the current event is scheduled.
Definition: eventq.hh:465
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:177
lds_state.hh

Generated on Tue Sep 21 2021 12:25:24 for gem5 by doxygen 1.8.17