2 * Copyright (c) 2014-2015 Advanced Micro Devices, Inc.
5 * For use for simulation and test purposes only
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
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.
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.
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.
34 #ifndef __LDS_STATE_HH__
35 #define __LDS_STATE_HH__
40 #include <unordered_map>
44 #include "gpu-compute/misc.hh"
45 #include "mem/port.hh"
46 #include "params/LdsState.hh"
47 #include "sim/clocked_object.hh"
52 * this represents a slice of the overall LDS, intended to be associated with
53 * an individual workgroup
58 LdsChunk(const uint32_t x_size):
70 read(const uint32_t index)
73 * For reads that are outside the bounds of the LDS
74 * chunk allocated to this WG we return 0.
76 if (index >= chunk.size()) {
80 T *p0 = (T *) (&(chunk.at(index)));
89 write(const uint32_t index, const T value)
92 * Writes that are outside the bounds of the LDS
93 * chunk allocated to this WG are dropped.
95 if (index >= chunk.size()) {
99 T *p0 = (T *) (&(chunk.at(index)));
104 * get the size of this chunk
106 std::vector<uint8_t>::size_type
113 // the actual data store for this slice of the LDS
114 std::vector<uint8_t> chunk;
117 // Local Data Share (LDS) State per Wavefront (contents of the LDS region
118 // allocated to the WorkGroup of this Wavefront)
119 class LdsState: public ClockedObject
124 * an event to allow event-driven execution
126 class TickEvent: public Event
130 LdsState *ldsState = nullptr;
136 TickEvent(LdsState *_ldsState) :
147 mainEventQueue[0]->schedule(this, when);
153 mainEventQueue[0]->deschedule(this);
158 * CuSidePort is the LDS Port closer to the CU side
160 class CuSidePort: public ResponsePort
163 CuSidePort(const std::string &_name, LdsState *_ownerLds) :
164 ResponsePort(_name, _ownerLds), ownerLds(_ownerLds)
172 recvTimingReq(PacketPtr pkt);
175 recvAtomic(PacketPtr pkt)
181 recvFunctional(PacketPtr pkt);
194 virtual AddrRangeList
195 getAddrRanges() const
197 AddrRangeList ranges;
198 ranges.push_back(ownerLds->getAddrRange());
204 loadData(PacketPtr packet);
208 storeData(PacketPtr packet);
212 atomicOperation(PacketPtr packet);
218 * the lds reference counter
219 * The key is the workgroup ID and dispatch ID
220 * The value is the number of wavefronts that reference this LDS, as
221 * wavefronts are launched, the counter goes up for that workgroup and when
222 * they return it decreases, once it reaches 0 then this chunk of the LDS
223 * is returned to the available pool. However,it is deallocated on the 1->0
224 * transition, not whenever the counter is 0 as it always starts with 0
225 * when the workgroup asks for space
227 std::unordered_map<uint32_t,
228 std::unordered_map<uint32_t, int32_t>> refCounter;
230 // the map that allows workgroups to access their own chunk of the LDS
231 std::unordered_map<uint32_t,
232 std::unordered_map<uint32_t, LdsChunk>> chunkMap;
234 // an event to allow the LDS to wake up at a specified time
237 // the queue of packets that are going back to the CU after a
238 // read/write/atomic op
239 // TODO need to make this have a maximum size to create flow control
240 std::queue<std::pair<Tick, PacketPtr>> returnQueue;
242 // whether or not there are pending responses
243 bool retryResp = false;
249 getDynInstr(PacketPtr packet);
252 processPacket(PacketPtr packet);
255 countBankConflicts(PacketPtr packet, unsigned *bankAccesses);
258 countBankConflicts(GPUDynInstPtr gpuDynInst,
259 unsigned *numBankAccesses);
262 typedef LdsStateParams Params;
264 LdsState(const Params *params);
266 // prevent copy construction
267 LdsState(const LdsState&) = delete;
277 return dynamic_cast<const Params *>(_params);
287 setRetryResp(const bool value)
292 // prevent assignment
294 operator=(const LdsState &) = delete;
297 * use the dynamic wave id to create or just increase the reference count
300 increaseRefCounter(const uint32_t dispatchId, const uint32_t wgId)
302 int refCount = getRefCounter(dispatchId, wgId);
303 fatal_if(refCount < 0,
304 "reference count should not be below zero");
305 return ++refCounter[dispatchId][wgId];
309 * decrease the reference count after making sure it is in the list
310 * give back this chunk if the ref counter has reached 0
313 decreaseRefCounter(const uint32_t dispatchId, const uint32_t wgId)
315 int refCount = getRefCounter(dispatchId, wgId);
317 fatal_if(refCount <= 0,
318 "reference count should not be below zero or at zero to"
321 refCounter[dispatchId][wgId]--;
323 if (refCounter[dispatchId][wgId] == 0) {
324 releaseSpace(dispatchId, wgId);
327 return refCounter[dispatchId][wgId];
332 * return the current reference count for this workgroup id
335 getRefCounter(const uint32_t dispatchId, const uint32_t wgId) const
337 auto dispatchIter = chunkMap.find(dispatchId);
338 fatal_if(dispatchIter == chunkMap.end(),
339 "could not locate this dispatch id [%d]", dispatchId);
341 auto workgroup = dispatchIter->second.find(wgId);
342 fatal_if(workgroup == dispatchIter->second.end(),
343 "could not find this workgroup id within this dispatch id"
344 " did[%d] wgid[%d]", dispatchId, wgId);
346 auto refCountIter = refCounter.find(dispatchId);
347 if (refCountIter == refCounter.end()) {
348 fatal("could not locate this dispatch id [%d]", dispatchId);
350 auto workgroup = refCountIter->second.find(wgId);
351 if (workgroup == refCountIter->second.end()) {
352 fatal("could not find this workgroup id within this dispatch id"
353 " did[%d] wgid[%d]", dispatchId, wgId);
355 return refCounter.at(dispatchId).at(wgId);
359 fatal("should not reach this point");
364 * assign a parent and request this amount of space be set aside
368 reserveSpace(const uint32_t dispatchId, const uint32_t wgId,
371 if (chunkMap.find(dispatchId) != chunkMap.end()) {
373 chunkMap[dispatchId].find(wgId) != chunkMap[dispatchId].end(),
374 "duplicate workgroup ID asking for space in the LDS "
375 "did[%d] wgid[%d]", dispatchId, wgId);
378 if (bytesAllocated + size > maximumSize) {
381 bytesAllocated += size;
383 auto value = chunkMap[dispatchId].emplace(wgId, LdsChunk(size));
384 panic_if(!value.second, "was unable to allocate a new chunkMap");
386 // make an entry for this workgroup
387 refCounter[dispatchId][wgId] = 0;
389 return &chunkMap[dispatchId][wgId];
394 * return pointer to lds chunk for wgid
397 getLdsChunk(const uint32_t dispatchId, const uint32_t wgId)
399 fatal_if(chunkMap.find(dispatchId) == chunkMap.end(),
400 "fetch for unknown dispatch ID did[%d]", dispatchId);
402 fatal_if(chunkMap[dispatchId].find(wgId) == chunkMap[dispatchId].end(),
403 "fetch for unknown workgroup ID wgid[%d] in dispatch ID did[%d]",
406 return &chunkMap[dispatchId][wgId];
410 returnQueuePush(std::pair<Tick, PacketPtr> thePair);
413 earliestReturnTime() const
415 // TODO set to max(lastCommand+1, curTick())
416 return returnQueue.empty() ? curTick() : returnQueue.back().first;
420 setParent(ComputeUnit *x_parent);
442 getComputeUnit() const
448 getBankConflictPenalty() const
450 return bankConflictPenalty;
454 * get the allocated size for this workgroup
457 ldsSize(const uint32_t x_wgId)
459 return chunkMap[x_wgId].size();
469 getPort(const std::string &if_name, PortID idx)
471 if (if_name == "cuPort") {
472 // TODO need to set name dynamically at this point?
475 fatal("cannot resolve the port name " + if_name);
480 * can this much space be reserved for a workgroup?
483 canReserve(uint32_t x_size) const
485 return bytesAllocated + x_size <= maximumSize;
490 * give back the space
493 releaseSpace(const uint32_t x_dispatchId, const uint32_t x_wgId)
495 auto dispatchIter = chunkMap.find(x_dispatchId);
497 if (dispatchIter == chunkMap.end()) {
498 fatal("dispatch id not found [%d]", x_dispatchId);
500 auto workgroupIter = dispatchIter->second.find(x_wgId);
501 if (workgroupIter == dispatchIter->second.end()) {
502 fatal("workgroup id [%d] not found in dispatch id [%d]",
503 x_wgId, x_dispatchId);
507 fatal_if(bytesAllocated < chunkMap[x_dispatchId][x_wgId].size(),
508 "releasing more space than was allocated");
510 bytesAllocated -= chunkMap[x_dispatchId][x_wgId].size();
511 chunkMap[x_dispatchId].erase(chunkMap[x_dispatchId].find(x_wgId));
515 // the port that connects this LDS to its owner CU
518 ComputeUnit* parent = nullptr;
522 // the number of bytes currently reserved by all workgroups
523 int bytesAllocated = 0;
525 // the size of the LDS, the most bytes available
528 // Address range of this memory
531 // the penalty, in cycles, for each LDS bank conflict
532 int bankConflictPenalty = 0;
534 // the number of banks in the LDS underlying data store
538 #endif // __LDS_STATE_HH__