|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Intel Corporation |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included |
| 12 | + * in all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#include "runtime/command_stream/command_stream_receiver.h" |
| 24 | +#include "runtime/command_stream/experimental_command_buffer.h" |
| 25 | +#include "runtime/command_stream/linear_stream.h" |
| 26 | +#include "runtime/device/device.h" |
| 27 | +#include "runtime/memory_manager/memory_constants.h" |
| 28 | +#include "runtime/memory_manager/memory_manager.h" |
| 29 | +#include <cstring> |
| 30 | +#include <type_traits> |
| 31 | + |
| 32 | +namespace OCLRT { |
| 33 | + |
| 34 | +ExperimentalCommandBuffer::ExperimentalCommandBuffer(CommandStreamReceiver *csr) : commandStreamReceiver(csr), |
| 35 | + currentStream(nullptr), |
| 36 | + timestampsOffset(0), |
| 37 | + experimentalAllocationOffset(0), |
| 38 | + defaultPrint(true) { |
| 39 | + timestamps = csr->getMemoryManager()->allocateGraphicsMemory(MemoryConstants::pageSize); |
| 40 | + memset(timestamps->getUnderlyingBuffer(), 0, timestamps->getUnderlyingBufferSize()); |
| 41 | + experimentalAllocation = csr->getMemoryManager()->allocateGraphicsMemory(MemoryConstants::pageSize); |
| 42 | + memset(experimentalAllocation->getUnderlyingBuffer(), 0, experimentalAllocation->getUnderlyingBufferSize()); |
| 43 | + timerResolution = commandStreamReceiver->getMemoryManager()->device->getDeviceInfo().profilingTimerResolution; |
| 44 | +} |
| 45 | + |
| 46 | +ExperimentalCommandBuffer::~ExperimentalCommandBuffer() { |
| 47 | + auto timestamp = static_cast<uint64_t *>(timestamps->getUnderlyingBuffer()); |
| 48 | + for (uint32_t i = 0; i < timestampsOffset / (2 * sizeof(uint64_t)); i++) { |
| 49 | + auto stop = static_cast<uint64_t>(*(timestamp + 1) * timerResolution); |
| 50 | + auto start = static_cast<uint64_t>(*timestamp * timerResolution); |
| 51 | + auto delta = stop - start; |
| 52 | + printDebugString(defaultPrint, stdout, "#%u: delta %llu start %llu stop %llu\n", i, delta, start, stop); |
| 53 | + timestamp += 2; |
| 54 | + } |
| 55 | + MemoryManager *memManager = commandStreamReceiver->getMemoryManager(); |
| 56 | + if (memManager) { |
| 57 | + memManager->freeGraphicsMemory(timestamps); |
| 58 | + memManager->freeGraphicsMemory(experimentalAllocation); |
| 59 | + |
| 60 | + if (currentStream.get()) { |
| 61 | + memManager->storeAllocation(std::unique_ptr<GraphicsAllocation>(currentStream->getGraphicsAllocation()), REUSABLE_ALLOCATION); |
| 62 | + currentStream->replaceGraphicsAllocation(nullptr); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +void ExperimentalCommandBuffer::getCS(size_t minRequiredSize) { |
| 68 | + if (!currentStream) { |
| 69 | + currentStream.reset(new LinearStream(nullptr)); |
| 70 | + } |
| 71 | + minRequiredSize += CSRequirements::minCommandQueueCommandStreamSize; |
| 72 | + if (currentStream->getAvailableSpace() < minRequiredSize) { |
| 73 | + MemoryManager *memManager = commandStreamReceiver->getMemoryManager(); |
| 74 | + // If not, allocate a new block. allocate full pages |
| 75 | + minRequiredSize = alignUp(minRequiredSize, MemoryConstants::pageSize); |
| 76 | + |
| 77 | + auto requiredSize = minRequiredSize + CSRequirements::csOverfetchSize; |
| 78 | + |
| 79 | + GraphicsAllocation *allocation = memManager->obtainReusableAllocation(requiredSize, false).release(); |
| 80 | + if (!allocation) { |
| 81 | + allocation = memManager->allocateGraphicsMemory(requiredSize); |
| 82 | + } |
| 83 | + allocation->setAllocationType(GraphicsAllocation::AllocationType::LINEAR_STREAM); |
| 84 | + // Deallocate the old block, if not null |
| 85 | + auto oldAllocation = currentStream->getGraphicsAllocation(); |
| 86 | + if (oldAllocation) { |
| 87 | + memManager->storeAllocation(std::unique_ptr<GraphicsAllocation>(oldAllocation), REUSABLE_ALLOCATION); |
| 88 | + } |
| 89 | + currentStream->replaceBuffer(allocation->getUnderlyingBuffer(), minRequiredSize - CSRequirements::minCommandQueueCommandStreamSize); |
| 90 | + currentStream->replaceGraphicsAllocation(allocation); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void ExperimentalCommandBuffer::makeResidentAllocations() { |
| 95 | + commandStreamReceiver->makeResident(*currentStream->getGraphicsAllocation()); |
| 96 | + commandStreamReceiver->makeResident(*timestamps); |
| 97 | + commandStreamReceiver->makeResident(*experimentalAllocation); |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace OCLRT |
0 commit comments