Skip to content

Commit fef77bc

Browse files
committed
GPU: Direct memory allocation supports stacked memory
1 parent 6a06564 commit fef77bc

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

GPU/GPUTracking/Base/GPUReconstruction.cxx

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,6 @@ size_t GPUReconstruction::AllocateRegisteredMemory(int16_t ires, GPUOutputContro
712712

713713
void* GPUReconstruction::AllocateDirectMemory(size_t size, int32_t type)
714714
{
715-
if (type != GPUMemoryResource::MEMORY_HOST && (!IsGPU() || type != GPUMemoryResource::MEMORY_GPU)) {
716-
throw std::runtime_error("Requested invalid memory typo for unmanaged allocation");
717-
}
718715
if (GetProcessingSettings().memoryAllocationStrategy == GPUMemoryResource::ALLOCATION_INDIVIDUAL) {
719716
char* retVal = new (std::align_val_t(GPUCA_BUFFER_ALIGNMENT)) char[size];
720717
if ((type & GPUMemoryResource::MEMORY_STACK)) {
@@ -723,25 +720,35 @@ void* GPUReconstruction::AllocateDirectMemory(size_t size, int32_t type)
723720
mDirectMemoryChunks.emplace_back(retVal, alignedDeleter());
724721
}
725722
return retVal;
723+
}
724+
725+
if ((type & ~(GPUMemoryResource::MEMORY_HOST | GPUMemoryResource::MEMORY_GPU | GPUMemoryResource::MEMORY_STACK)) || ((type & GPUMemoryResource::MEMORY_HOST) && (type & GPUMemoryResource::MEMORY_GPU))) {
726+
throw std::runtime_error("Requested invalid memory typo for direct allocation");
727+
}
728+
if (mVolatileMemoryStart && !mDeviceMemoryAsVolatile && (type & GPUMemoryResource::MEMORY_GPU) && !(type & GPUMemoryResource::MEMORY_STACK)) {
729+
GPUError("Must not allocate direct memory while volatile chunks are allocated");
730+
throw std::bad_alloc();
731+
}
732+
733+
void*& pool = (type & GPUMemoryResource::MEMORY_GPU) ? mDeviceMemoryPool : mHostMemoryPool;
734+
void*& poolend = (type & GPUMemoryResource::MEMORY_GPU) ? mDeviceMemoryPoolEnd : mHostMemoryPoolEnd;
735+
char* retVal;
736+
if ((type & GPUMemoryResource::MEMORY_STACK)) {
737+
poolend = (char*)poolend - size;
738+
poolend = (char*)poolend - GPUProcessor::getAlignmentMod<GPUCA_MEMALIGN>(poolend);
739+
retVal = (char*)poolend;
726740
} else {
727-
if (mVolatileMemoryStart && !mDeviceMemoryAsVolatile && (type & GPUMemoryResource::MEMORY_GPU) && !(type & GPUMemoryResource::MEMORY_STACK)) {
728-
GPUError("Must not allocate direct memory while volatile chunks are allocated");
729-
throw std::bad_alloc();
730-
}
731-
void*& pool = type == GPUMemoryResource::MEMORY_GPU ? mDeviceMemoryPool : mHostMemoryPool;
732-
void*& poolend = type == GPUMemoryResource::MEMORY_GPU ? mDeviceMemoryPoolEnd : mHostMemoryPoolEnd;
733-
char* retVal;
734741
GPUProcessor::computePointerWithAlignment(pool, retVal, size);
735-
if (pool > poolend) {
736-
GPUError("Insufficient unmanaged memory: missing %ld bytes", ptrDiff(pool, poolend));
737-
throw std::bad_alloc();
738-
}
739-
UpdateMaxMemoryUsed();
740-
if (GetProcessingSettings().allocDebugLevel >= 2) {
741-
std::cout << "Allocated (unmanaged " << (type == GPUMemoryResource::MEMORY_GPU ? "gpu" : "host") << "): " << size << " - available: " << ptrDiff(poolend, pool) << "\n";
742-
}
743-
return retVal;
744742
}
743+
if (pool > poolend) {
744+
GPUError("Insufficient unmanaged memory: missing %ld bytes", ptrDiff(pool, poolend));
745+
throw std::bad_alloc();
746+
}
747+
UpdateMaxMemoryUsed();
748+
if (GetProcessingSettings().allocDebugLevel >= 2) {
749+
std::cout << "Allocated (unmanaged " << (type == GPUMemoryResource::MEMORY_GPU ? "gpu" : "host") << "): " << size << " - available: " << ptrDiff(poolend, pool) << "\n";
750+
}
751+
return retVal;
745752
}
746753

747754
void* GPUReconstruction::AllocateVolatileDeviceMemory(size_t size)

0 commit comments

Comments
 (0)