-
Notifications
You must be signed in to change notification settings - Fork 143
[Java] Introduce scoped resource access #1089
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
[Java] Introduce scoped resource access #1089
Conversation
|
I do very much like this approach and API. It closely aligns with the intent of the C API, while allowing to enforce higher-level constraints through typical java patterns, e.g. the decorator pattern. The consume is free to decide if, and how to, enforce thread semantics. |
…-check-on-resource-access # Conflicts: # java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/BruteForceIndexImpl.java # java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/CagraIndexImpl.java # java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/CuVSResourcesImpl.java # java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/HnswIndexImpl.java # java/cuvs-java/src/test/java/com/nvidia/cuvs/CagraBuildAndSearchIT.java
|
@mythrocks I've rebased this PR to include the latest merges (TY!) |
|
/ok to test 4ba4537 |
|
Linking cjnolet/nv_elastic#22 |
|
@mythrocks I merged #1028 and fixed/cleaned up a few things besides adding resource access:
Some of the changes (e.g. |
|
Still missing: the new code is allocating params directly (e.g. |
Will raise a PR and change this. @ldematte |
Agreed. I had held up #1028 long enough; I was hoping that could be handled in a follow-up. Thank you for pointing it out, @ldematte. Also, thank you for picking that up, @punAhuja. |
…matte/cuvs into java/thread-check-on-resource-access
|
/ok to test e2e044d |
| int returnValue = cuvsTieredIndexDestroy(tieredIndexReference.getMemorySegment()); | ||
| checkCuVSError(returnValue, "cuvsTieredIndexDestroy"); | ||
| if (dataset != null) { | ||
| dataset.close(); |
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.
Ah, this is resource leak you mentioned.
| // Create tensor from device memory | ||
| long[] datasetShape = {rows, cols}; | ||
| MemorySegment datasetTensor = | ||
| prepareTensor(localArena, datasetDP, datasetShape, kDLFloat(), 32, 2, kDLCUDA(), 1); |
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.
Ok, there's the magic numbers you mentioned. Yep, your way is better.
| checkCuVSError(returnValue, "cuvsRMMAlloc"); | ||
| // TieredIndex REQUIRES device memory - allocate it | ||
| long datasetSize = C_FLOAT_BYTE_SIZE * rows * cols; | ||
| MemorySegment datasetDP = allocateRMMSegment(cuvsRes, datasetSize); |
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.
Nitpick: Not to be handled in the current PR.
Do we stand the risk of leaking the allocation from RMM if there is an exception before the cleanup?
Might need to look at this more closely, separately.
|
/ok to test 0a620fb |
|
/merge |
…as missed for tieredIndex tests
#1089 Scoped Resource access changes were missed in the TieredIndex test causing them to fail. - Using CheckedCuVSResources.create() now - Skip TieredIndex tests if CuVS native support is unavailable Authors: - Puneet Ahuja (https://github.com/punAhuja) Approvers: - Mayya Sharipova (https://github.com/mayya-sharipova) - MithunR (https://github.com/mythrocks) URL: #1156
As a follow up to #1089 and #1082, this PR introduces a decorator to ensure that access to a shared CuVSResource is synchronized. This could be useful in cases where application logic is complex and it's not easy to efficiently and cleanly guarantee that access to a CuVSResources is done by at most one thread at each given time. Authors: - Lorenzo Dematté (https://github.com/ldematte) - MithunR (https://github.com/mythrocks) Approvers: - MithunR (https://github.com/mythrocks) URL: #1209
…#1156) rapidsai#1089 Scoped Resource access changes were missed in the TieredIndex test causing them to fail. - Using CheckedCuVSResources.create() now - Skip TieredIndex tests if CuVS native support is unavailable Authors: - Puneet Ahuja (https://github.com/punAhuja) Approvers: - Mayya Sharipova (https://github.com/mayya-sharipova) - MithunR (https://github.com/mythrocks) URL: rapidsai#1156
) As a follow up to rapidsai#1089 and rapidsai#1082, this PR introduces a decorator to ensure that access to a shared CuVSResource is synchronized. This could be useful in cases where application logic is complex and it's not easy to efficiently and cleanly guarantee that access to a CuVSResources is done by at most one thread at each given time. Authors: - Lorenzo Dematté (https://github.com/ldematte) - MithunR (https://github.com/mythrocks) Approvers: - MithunR (https://github.com/mythrocks) URL: rapidsai#1209
…#1156) rapidsai#1089 Scoped Resource access changes were missed in the TieredIndex test causing them to fail. - Using CheckedCuVSResources.create() now - Skip TieredIndex tests if CuVS native support is unavailable Authors: - Puneet Ahuja (https://github.com/punAhuja) Approvers: - Mayya Sharipova (https://github.com/mayya-sharipova) - MithunR (https://github.com/mythrocks) URL: rapidsai#1156
) As a follow up to rapidsai#1089 and rapidsai#1082, this PR introduces a decorator to ensure that access to a shared CuVSResource is synchronized. This could be useful in cases where application logic is complex and it's not easy to efficiently and cleanly guarantee that access to a CuVSResources is done by at most one thread at each given time. Authors: - Lorenzo Dematté (https://github.com/ldematte) - MithunR (https://github.com/mythrocks) Approvers: - MithunR (https://github.com/mythrocks) URL: rapidsai#1209
This PR wraps access to the cuVS resources handle into a autocloseable accessor. The reason is to provide a clear scope for resource access, with the possibility to add application logic to the scope begin/end.
An example of this logic is the
CheckedCuVSResourcesdecorator introduced in the test suite, which checks that theCuVSResourcesit decorates has not been destroyed yet, and that access to the underlying cuVS resources handle is not concurrent (is not done by more than 1 thread at every time).I preferred to write it as a decorator, as this gives the API consumers more freedom: if they want the check, they can use
CheckedCuVSResources(or a similar class); if they want direct, straight, fast access, they can just use the accessor; if they need more logic (e.g. logging or anything else), that is possible too.Tests are updated to use
CheckedCuVSResources, so we can spot issues quickly in tests without burdening production code with additional logic.