Skip to content

[Core][Modes] New buffer object to track underlying mode allocations … #490

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

Merged
merged 17 commits into from
Sep 15, 2021

Conversation

noelchalmers
Copy link
Contributor

@noelchalmers noelchalmers commented Mar 23, 2021

Description

This PR slightly changes how reference to backend memory allocations are counted so they can be more reliably destroyed, and prevent memory access violations via invalidated pointers.

The changes are entirely internal to OCCA and do not change the public API, or alter current behavior of OCCA applications.

Summary of issue:

Currently modeMemory_t objects do not track whether they were created by slicing another modeMemory_t object. Consequently, only the original modeMemory_t object can free the underlying device memory allocation, and will do so even if there still exist modeMemory_t objects referencing the device memory. This results in dangling references.

E.g.

{
  occa::memory o_a;
  {
    occa::memory o_A = device.malloc<float>(2*entries); //Create a new buffer
    o_a = o_A + entries; //  o_a is a slice into o_A
  } //o_A is destroyed here because it's reference counting is separate from o_a.
    // The underlying device memory is also destroyed

  // o_a is now invalid, but doesn't know the original device memory is free'd

  // Using o_a will result in an access violation
  myKernel(entries, o_a); //Fault

} //o_a destroyed here, but since it's not the origin, there's no double free

This PR addresses this behavior. The new buffer objects wrap device memory allocations and track their modeMemory_t references, created via slicing. The buffer and wrapped device allocation are destroyed once their reference count reaches zero.

@codecov
Copy link

codecov bot commented Mar 23, 2021

Codecov Report

Merging #490 (618fa3a) into main (5028b14) will increase coverage by 0.00%.
The diff coverage is 97.32%.

❗ Current head 618fa3a differs from pull request most recent head f7d0371. Consider uploading reports for the commit f7d0371 to get more accurate results
Impacted file tree graph

@@           Coverage Diff            @@
##             main     #490    +/-   ##
========================================
  Coverage   76.24%   76.24%            
========================================
  Files         261      256     -5     
  Lines       19399    19275   -124     
========================================
- Hits        14790    14696    -94     
+ Misses       4609     4579    -30     
Impacted Files Coverage Δ
src/core/device.cpp 92.43% <ø> (-0.04%) ⬇️
src/occa/internal/modes/serial/buffer.cpp 91.30% <91.30%> (ø)
src/core/memory.cpp 79.85% <91.66%> (-1.23%) ⬇️
src/core/kernelArg.cpp 81.73% <100.00%> (ø)
src/occa/internal/core/buffer.cpp 100.00% <100.00%> (ø)
src/occa/internal/core/device.cpp 75.00% <100.00%> (ø)
src/occa/internal/core/memory.cpp 100.00% <100.00%> (ø)
src/occa/internal/modes/serial/device.cpp 90.76% <100.00%> (-0.05%) ⬇️
src/occa/internal/modes/serial/memory.cpp 100.00% <100.00%> (ø)
include/occa/dtype/utils.hpp 66.66% <0.00%> (-33.34%) ⬇️
... and 25 more

@noelchalmers noelchalmers marked this pull request as ready for review March 24, 2021 00:19
@tcew tcew closed this Sep 1, 2021
@tcew tcew reopened this Sep 1, 2021
@noelchalmers noelchalmers force-pushed the bugfix/sliceReferences branch from 7a1911f to 4db9957 Compare September 1, 2021 18:55
@noelchalmers noelchalmers force-pushed the bugfix/sliceReferences branch from 4db9957 to 6fbdf29 Compare September 1, 2021 19:06
Copy link
Member

@dmed256 dmed256 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is freaking great, glad we're making memory slices safer ❤️
I have a few comments but overall it looks great!

@dmed256 dmed256 merged commit 7d02eac into libocca:main Sep 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants