Commit 8104dba
committed
Fix command line parsing in the cuda.bindings example helpers
Both helpers unpack enumerate() backwards:
def check_cmd_line_flag(string_ref):
return any(string_ref == i and k < len(sys.argv) - 1 for i, k in enumerate(sys.argv))
enumerate() yields (index, value), so `i` is an int and `k` is a str, but the
body uses `i` as the argument text and `k` as the index. `string_ref == i`
compares str to int and is therefore always False:
$ python -c "import sys; from cuda.bindings._example_helpers import *; \
print(check_cmd_line_flag('device='), get_cmd_line_argument_int('device='))" device= 3
False 0
check_cmd_line_flag() always returns False and get_cmd_line_argument_int()
always returns 0, so every command line option in the examples is silently
ignored: device=, wA=, hA=, wB=, hB=, kernel=, help, ? and
use_generic_memory, via helper_cuda.find_cuda_device(),
find_cuda_device_drv(), global_to_shmem_async_copy.py,
simple_zero_copy.py and stream_ordered_allocation.py. The dead branch would
not have worked either: `k < len(sys.argv) - 1` is str < int (TypeError) and
`sys.argv[k + 1]` indexes with a str.
Alongside the unpacking:
- check_cmd_line_flag() no longer requires a following argument. That
condition belongs to the value lookup; requiring it would keep `help` and
`?` broken whenever they are the last argument, which is the normal way to
pass them.
- Both helpers skip sys.argv[0], matching the C samples' helper_string.h,
which scans from argv[1].
- get_cmd_line_argument_int() returns an int, as its name says and as its
callers require: helper_cuda.find_cuda_device() passes the result straight
to cudaSetDevice(), and find_cuda_device_drv() to cuDeviceGet(). Returning
sys.argv[k + 1] unchanged would hand those APIs a str. This is also the
only value the function has ever actually returned, since the literal 0
fallback was the sole reachable path.
Adds cuda_bindings/tests/test_example_helpers.py. Five of its assertions
fail against main.1 parent 3bd069a commit 8104dba
2 files changed
Lines changed: 77 additions & 4 deletions
Lines changed: 13 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
9 | 13 | | |
10 | 14 | | |
11 | 15 | | |
12 | | - | |
13 | | - | |
14 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
15 | 24 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
0 commit comments