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.
33 * Authors: John Kalamatianos,
37 #ifndef __LDS_STATE_HH__
38 #define __LDS_STATE_HH__
43 #include <unordered_map>
47 #include "enums/MemType.hh"
48 #include "gpu-compute/misc.hh"
49 #include "mem/mem_object.hh"
50 #include "mem/port.hh"
51 #include "params/LdsState.hh"
56 * this represents a slice of the overall LDS, intended to be associated with an
57 * individual workgroup
62 LdsChunk(const uint32_t x_size):
74 read(const uint32_t index)
76 fatal_if(!chunk.size(), "cannot read from an LDS chunk of size 0");
77 fatal_if(index >= chunk.size(), "out-of-bounds access to an LDS chunk");
78 T *p0 = (T *) (&(chunk.at(index)));
87 write(const uint32_t index, const T value)
89 fatal_if(!chunk.size(), "cannot write to an LDS chunk of size 0");
90 fatal_if(index >= chunk.size(), "out-of-bounds access to an LDS chunk");
91 T *p0 = (T *) (&(chunk.at(index)));
96 * get the size of this chunk
98 std::vector<uint8_t>::size_type
105 // the actual data store for this slice of the LDS
106 std::vector<uint8_t> chunk;
109 // Local Data Share (LDS) State per Wavefront (contents of the LDS region
110 // allocated to the WorkGroup of this Wavefront)
111 class LdsState: public MemObject
116 * an event to allow event-driven execution
118 class TickEvent: public Event
122 LdsState *ldsState = nullptr;
128 TickEvent(LdsState *_ldsState) :
139 mainEventQueue[0]->schedule(this, when);
145 mainEventQueue[0]->deschedule(this);
150 * CuSidePort is the LDS Port closer to the CU side
152 class CuSidePort: public SlavePort
155 CuSidePort(const std::string &_name, LdsState *_ownerLds) :
156 SlavePort(_name, _ownerLds), ownerLds(_ownerLds)
164 recvTimingReq(PacketPtr pkt);
167 recvAtomic(PacketPtr pkt)
173 recvFunctional(PacketPtr pkt);
186 virtual AddrRangeList
187 getAddrRanges() const
189 AddrRangeList ranges;
190 ranges.push_back(ownerLds->getAddrRange());
196 loadData(PacketPtr packet);
200 storeData(PacketPtr packet);
204 atomicOperation(PacketPtr packet);
209 // the lds reference counter
210 // The key is the workgroup ID and dispatch ID
211 // The value is the number of wavefronts that reference this LDS, as
212 // wavefronts are launched, the counter goes up for that workgroup and when
213 // they return it decreases, once it reaches 0 then this chunk of the LDS is
214 // returned to the available pool. However,it is deallocated on the 1->0
215 // transition, not whenever the counter is 0 as it always starts with 0 when
216 // the workgroup asks for space
217 std::unordered_map<uint32_t,
218 std::unordered_map<uint32_t, int32_t>> refCounter;
220 // the map that allows workgroups to access their own chunk of the LDS
221 std::unordered_map<uint32_t,
222 std::unordered_map<uint32_t, LdsChunk>> chunkMap;
224 // an event to allow the LDS to wake up at a specified time
227 // the queue of packets that are going back to the CU after a
228 // read/write/atomic op
229 // TODO need to make this have a maximum size to create flow control
230 std::queue<std::pair<Tick, PacketPtr>> returnQueue;
232 // whether or not there are pending responses
233 bool retryResp = false;
239 getDynInstr(PacketPtr packet);
242 processPacket(PacketPtr packet);
245 countBankConflicts(PacketPtr packet, unsigned *bankAccesses);
248 countBankConflicts(GPUDynInstPtr gpuDynInst,
249 unsigned *numBankAccesses);
252 typedef LdsStateParams Params;
254 LdsState(const Params *params);
256 // prevent copy construction
257 LdsState(const LdsState&) = delete;
267 return dynamic_cast<const Params *>(_params);
277 setRetryResp(const bool value)
282 // prevent assignment
284 operator=(const LdsState &) = delete;
287 * use the dynamic wave id to create or just increase the reference count
290 increaseRefCounter(const uint32_t dispatchId, const uint32_t wgId)
292 int refCount = getRefCounter(dispatchId, wgId);
293 fatal_if(refCount < 0,
294 "reference count should not be below zero");
295 return ++refCounter[dispatchId][wgId];
299 * decrease the reference count after making sure it is in the list
300 * give back this chunk if the ref counter has reached 0
303 decreaseRefCounter(const uint32_t dispatchId, const uint32_t wgId)
305 int refCount = getRefCounter(dispatchId, wgId);
307 fatal_if(refCount <= 0,
308 "reference count should not be below zero or at zero to"
311 refCounter[dispatchId][wgId]--;
313 if (refCounter[dispatchId][wgId] == 0) {
314 releaseSpace(dispatchId, wgId);
317 return refCounter[dispatchId][wgId];
322 * return the current reference count for this workgroup id
325 getRefCounter(const uint32_t dispatchId, const uint32_t wgId) const
327 auto dispatchIter = chunkMap.find(dispatchId);
328 fatal_if(dispatchIter == chunkMap.end(),
329 "could not locate this dispatch id [%d]", dispatchId);
331 auto workgroup = dispatchIter->second.find(wgId);
332 fatal_if(workgroup == dispatchIter->second.end(),
333 "could not find this workgroup id within this dispatch id"
334 " did[%d] wgid[%d]", dispatchId, wgId);
336 auto refCountIter = refCounter.find(dispatchId);
337 if (refCountIter == refCounter.end()) {
338 fatal("could not locate this dispatch id [%d]", dispatchId);
340 auto workgroup = refCountIter->second.find(wgId);
341 if (workgroup == refCountIter->second.end()) {
342 fatal("could not find this workgroup id within this dispatch id"
343 " did[%d] wgid[%d]", dispatchId, wgId);
345 return refCounter.at(dispatchId).at(wgId);
349 fatal("should not reach this point");
354 * assign a parent and request this amount of space be set aside
358 reserveSpace(const uint32_t dispatchId, const uint32_t wgId,
361 if (chunkMap.find(dispatchId) != chunkMap.end()) {
363 chunkMap[dispatchId].find(wgId) != chunkMap[dispatchId].end(),
364 "duplicate workgroup ID asking for space in the LDS "
365 "did[%d] wgid[%d]", dispatchId, wgId);
368 fatal_if(bytesAllocated + size > maximumSize,
369 "request would ask for more space than is available");
371 bytesAllocated += size;
373 chunkMap[dispatchId].emplace(wgId, LdsChunk(size));
374 // make an entry for this workgroup
375 refCounter[dispatchId][wgId] = 0;
377 return &chunkMap[dispatchId][wgId];
381 returnQueuePush(std::pair<Tick, PacketPtr> thePair);
384 earliestReturnTime() const
386 // TODO set to max(lastCommand+1, curTick())
387 return returnQueue.empty() ? curTick() : returnQueue.back().first;
391 setParent(ComputeUnit *x_parent);
413 getComputeUnit() const
419 getBankConflictPenalty() const
421 return bankConflictPenalty;
425 * get the allocated size for this workgroup
428 ldsSize(const uint32_t x_wgId)
430 return chunkMap[x_wgId].size();
439 virtual BaseSlavePort &
440 getSlavePort(const std::string& if_name, PortID idx)
442 if (if_name == "cuPort") {
443 // TODO need to set name dynamically at this point?
446 fatal("cannot resolve the port name " + if_name);
451 * can this much space be reserved for a workgroup?
454 canReserve(uint32_t x_size) const
456 return bytesAllocated + x_size <= maximumSize;
461 * give back the space
464 releaseSpace(const uint32_t x_dispatchId, const uint32_t x_wgId)
466 auto dispatchIter = chunkMap.find(x_dispatchId);
468 if (dispatchIter == chunkMap.end()) {
469 fatal("dispatch id not found [%d]", x_dispatchId);
471 auto workgroupIter = dispatchIter->second.find(x_wgId);
472 if (workgroupIter == dispatchIter->second.end()) {
473 fatal("workgroup id [%d] not found in dispatch id [%d]",
474 x_wgId, x_dispatchId);
478 fatal_if(bytesAllocated < chunkMap[x_dispatchId][x_wgId].size(),
479 "releasing more space than was allocated");
481 bytesAllocated -= chunkMap[x_dispatchId][x_wgId].size();
482 chunkMap[x_dispatchId].erase(chunkMap[x_dispatchId].find(x_wgId));
486 // the port that connects this LDS to its owner CU
489 ComputeUnit* parent = nullptr;
493 // the number of bytes currently reserved by all workgroups
494 int bytesAllocated = 0;
496 // the size of the LDS, the most bytes available
499 // Address range of this memory
502 // the penalty, in cycles, for each LDS bank conflict
503 int bankConflictPenalty = 0;
505 // the number of banks in the LDS underlying data store
509 #endif // __LDS_STATE_HH__