-
Notifications
You must be signed in to change notification settings - Fork 108
Add helper functions to clear MatX caches and allocations #1092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add helper functions to clear MatX caches and allocations #1092
Conversation
MatX caches (e.g., cuFFT plans or cuBLAS handles) and allocations (user or internal allocations via matxAlloc) are stored in static data structures. These data structures are destroyed during program exit using the corresponding destructors. However, due to ordering of static destructors and atexit handlers, it is possible for resources to be freed after the CUDA context or some other dependent resource has been destroyed. This can result in a segmentation fault during program exit. The helper function ClearMatXCachesAndAllocations() can be called prior to program exit to free resources allocated by MatX. This will prevent conflicts with other static destructors and atexit handlers and thus allow clean shutdown. The function may also be useful at other times that the user wishes to free resources allocated via MatX. There are two other helpers, ClearMatXCaches() and FreeMatXAllocations(), to dellocate data associated with the caches (plans, handles, workspaces, etc.) and allocations made via matxAlloc(), respectively. Signed-off-by: Thomas Benson <[email protected]>
|
/build |
Greptile OverviewGreptile SummaryAdded three new helper functions ( Key Changes:
Implementation Notes:
Confidence Score: 4/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant User
participant ClearCachesAndAllocations
participant ClearCaches
participant matxCache_t
participant FreeAllocations
participant MemTracker
participant CUDA
User->>ClearCachesAndAllocations: Call cleanup before exit
ClearCachesAndAllocations->>ClearCaches: Clear all caches
ClearCaches->>matxCache_t: ClearAll()
Note over matxCache_t: Lock cache_mtx
loop For each cache ID
matxCache_t->>matxCache_t: Call CacheFreeHelper.free()
matxCache_t->>matxCache_t: Clear cache entries (destructors)
end
Note over matxCache_t: Lock stream_alloc_mutex
loop For each stream allocation
matxCache_t->>MemTracker: matxFree(ptr)
Note over MemTracker: Lock memory_mtx
MemTracker->>CUDA: cudaFreeAsync()
MemTracker->>MemTracker: Erase from allocationMap
end
Note over matxCache_t: Lock ltoir_mutex
matxCache_t->>matxCache_t: Clear LTOIR cache
ClearCaches-->>ClearCachesAndAllocations: Return
ClearCachesAndAllocations->>FreeAllocations: Free remaining allocations
FreeAllocations->>MemTracker: free_all()
Note over MemTracker: Lock memory_mtx
loop While allocationMap not empty
MemTracker->>MemTracker: deallocate_internal()
MemTracker->>CUDA: cudaFree()/cudaFreeHost()
MemTracker->>MemTracker: Erase from allocationMap
end
FreeAllocations-->>ClearCachesAndAllocations: Return
ClearCachesAndAllocations-->>User: Cleanup complete
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4 files reviewed, 1 comment
|
Do we need any tests for this? |
I added a clear call to the post-test code in |
I think in some ways not clearing the cache on tests is a feature since it has exposed problems with caching in the past that clearing would hide. |
Signed-off-by: Thomas Benson <[email protected]>
|
/build |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6 files reviewed, no comments
|
/build |
3 similar comments
|
/build |
|
/build |
|
/build |
MatX caches (e.g., cuFFT plans or cuBLAS handles) and allocations (user or internal allocations via matxAlloc) are stored in static data structures. These data structures are destroyed during program exit using the corresponding destructors. However, due to ordering of static destructors and atexit handlers, it is possible for resources to be freed after the CUDA context or some other dependent resource has been destroyed. This can result in a segmentation fault during program exit.
The helper function ClearCachesAndAllocations() can be called prior to program exit to free resources allocated by MatX. This will prevent conflicts with other static destructors and atexit handlers and thus allow clean shutdown. The function may also be useful at other times that the user wishes to free resources allocated via MatX.
There are two other helpers, ClearCaches() and FreeAllocations(), to dellocate data associated with the caches (plans, handles, workspaces, etc.) and allocations made via matxAlloc(), respectively.