Uh oh!
There was an error while loading. Please reload this page.
fix(bindings): two silent wrong results in the vector_add examples - #2575
Open
LeSingh1 wants to merge 1 commit into
Open
fix(bindings): two silent wrong results in the vector_add examples#2575LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
1. A wrong value in the LAST element is reported as a pass.
Both examples transliterate the C sample's success test:
for i in range(n):
sum_all = h_a[i] + h_b[i]
if math.fabs(h_c[i] - sum_all) > 1e-7:
break
...
if i + 1 != n:
print("Result = FAIL", file=sys.stderr)
sys.exit(1)
In C, `if (i == N)` works because a completed loop leaves `i == N`. In
Python a completed loop leaves `i == n - 1`, hence the `i + 1 != n`
rewrite -- but `break` at the final index `n - 1` produces `i + 1 == n`
too. So a kernel that computes h_c[n-1] wrongly exits 0 and prints nothing.
Use an explicit flag, which does not depend on where the loop stopped.
2. simple_malloc_multi_device_mmap grants access to only the last device.
access_descriptors = [cuda.CUmemAccessDesc()] * len(mapping_devices)
List multiplication stores N references to ONE mutable CUmemAccessDesc, so
the loop that fills in location.id overwrites the same object N times and
cuMemSetAccess receives mapping_devices[-1] repeated N times. It succeeds;
the devices that were supposed to be granted access instead fault on first
touch. The helper is written as a general multi-device routine -- that is
what its docstring and its mapping_devices parameter are for -- and is
masked today only because main() passes a single device.
Neither is reachable from CI: tests/test_examples.py runs each example with no
arguments on a machine where the kernel is correct.Contributor
LeSingh1
commented
Aug 9, 2026
ContributorAuthor
Heads-up on overlap: #2266 (samples migration) deletes both of these files. If that lands first, these two fixes should travel with the migrated copies instead — happy to close this and re-target, or to leave it as the record of what needs carrying over. Flagging rather than guessing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two independent defects in the
vector_addexamples, both of which produce a wrong result rather than an error. Grouped because they are the same pair of files and the same failure mode.1. A wrong value in the last element is reported as a pass
vector_add_drv.py:104-117andvector_add_mmap.py:294-307:This is the C sample's
if (i == N) PASS, which works only because a completed Cforloop leavesi == N. A completed Python loop leavesi == n - 1, so the check was rewritten asi + 1 != n— butbreakat the final indexn - 1yieldsi + 1 == nas well. A kernel that computesh_c[n-1]incorrectly therefore exits 0 and prints nothing.Fixed with an explicit flag, which does not depend on where the loop stopped.
2.
simple_malloc_multi_device_mmapgrants access to only the last devicevector_add_mmap.py:161:List multiplication stores N references to one
CUmemAccessDesc, which is mutable, so the loop overwrites the same object N times andcuMemSetAccessreceivesmapping_devices[-1]repeated N times. The call succeeds; the devices that were supposed to be granted access instead fault on first touch.The helper is written as a general multi-device routine — that is what
mapping_devicesand the surrounding comments are for — and the bug is masked today only becausemain()passes a single device (vector_add_mmap.py:235). Fixed with a list comprehension.What I ran
Environment: macOS, no CUDA driver and no CUDA toolkit.
cuda_bindings/tests/test_examples.py— both need a GPU, andvector_add_mmapadditionally needs VMM support. Note thattest_examples.pywould not catch either defect anyway: it runs each example with no arguments on a machine where the kernel is correct, so neither the last-element path nor a multi-devicemapping_devicesis ever exercised.python -m py_compile,ruff check,ruff format --checkon both files — clean, no new findings against amainbaseline for the same files.iis not read anywhere else in either function after the loop, and[...] * len(...)on a mutable element appears nowhere else incuda_bindings/examplesorcuda_core/examples.