[llvm] [flang][cuda][rt] Track asynchronous allocation stream for deallocation (PR #137073)
Zhen Wang via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 23 15:17:09 PDT 2025
================
@@ -21,6 +22,106 @@
#include "cuda_runtime.h"
namespace Fortran::runtime::cuda {
+
+struct DeviceAllocation {
+ void *ptr;
+ std::size_t size;
+ cudaStream_t stream;
+};
+
+// Compare address values. nullptr will be sorted at the end of the array.
+int compareDeviceAlloc(const void *a, const void *b) {
+ const DeviceAllocation *deva = (const DeviceAllocation *)a;
+ const DeviceAllocation *devb = (const DeviceAllocation *)b;
+ if (deva->ptr == nullptr && devb->ptr == nullptr)
+ return 0;
+ if (deva->ptr == nullptr)
+ return 1;
+ if (devb->ptr == nullptr)
+ return -1;
+ return deva->ptr < devb->ptr ? -1 : (deva->ptr > devb->ptr ? 1 : 0);
+}
+
+// Dynamic array for tracking asynchronous allocations.
+static DeviceAllocation *deviceAllocations = nullptr;
+Lock lock;
+static int maxDeviceAllocations{512}; // Initial size
+static int numDeviceAllocations{0};
+static constexpr int allocNotFound{-1};
+
+static void initAllocations() {
+ if (!deviceAllocations) {
+ deviceAllocations = static_cast<DeviceAllocation *>(
+ malloc(maxDeviceAllocations * sizeof(DeviceAllocation)));
+ if (!deviceAllocations) {
+ Terminator terminator{__FILE__, __LINE__};
+ terminator.Crash("Failed to allocate tracking array");
+ }
+ }
+}
+
+// Double the size of the allocation array when size if
----------------
wangzpgi wrote:
uncomplete sentence?
https://github.com/llvm/llvm-project/pull/137073
More information about the llvm-commits
mailing list