Skip to content

feat: add GPU filter (--gpus) to nvlink_monitor - #6

Merged
staryxchen merged 1 commit into
mainfrom
feat-monitor-gpu-filter
Jul 10, 2026
Merged

feat: add GPU filter (--gpus) to nvlink_monitor#6
staryxchen merged 1 commit into
mainfrom
feat-monitor-gpu-filter

Conversation

@staryxchen

Copy link
Copy Markdown
Owner

Summary

Third of the feature-enhancement batch. Adds -g, --gpus <id1,id2,...> to nvlink_monitor so the user can restrict monitoring to a subset of GPUs. On an 8-GPU system where a bandwidth test only touches 2 GPUs, this cuts irrelevant output and focuses on the links that matter.

Changes

monitor/arg_parser.{h,cpp}

  • New std::vector<int> gpuFilter field on MonitorCliArgs (empty = all GPUs).
  • Parses -g/--gpus as a comma-separated integer list, e.g. 0,1,3. Whitespace around tokens is tolerated. Negative ids, non-numeric tokens, missing value, and empty lists are all rejected with ok=false.

monitor/nvlink_monitor.{h,cpp}

  • Constructor takes const std::vector<int>& gpuFilter (stored as std::set<int> for O(log n) lookup).
  • New private helper isGpuSelected(id) — returns true if the filter is empty (all GPUs) or the id is in the set.
  • getNvLinkData skips GPUs not selected by the filter, so CSV/JSON/text output and bandwidth calc only cover the selected GPUs. Snapshot indices stay aligned across samples (filter is stable for the monitor's lifetime), so calculateBandwidth's index-based matching is unaffected.
  • Validation in the constructor after discovery: an out-of-range id throws std::runtime_error (surfaced as Error: GPU id N out of range (0-M) by main's catch) so a typo doesn't silently monitor nothing.
  • A Monitoring K of N GPU(s) (filter applied) notice is printed to stderr when a filter is active.
  • printHelp and main updated for the new flag.

test/test_arg_parser.cpp

+6 tests (72 total, was 66): single short (-g 3), multiple long (--gpus 0,1,3), whitespace tolerated (0, 1, 3), invalid token rejected, negative rejected, missing value rejected; Defaults asserts gpuFilter empty.

README.md

Documented -g, --gpus with the out-of-range-rejection note.

Verification

  • make check-format — clean
  • make test72/72 pass
  • make monitor — builds with -Wall -Wextra, no warnings
  • Smoke-tested on an 8x H20 system:
    • -g 0,1 -f csv → CSV output contains only GPU 0 and 1
    • -g 9 → errors with Error: GPU id 9 out of range (0-7)
    • stderr shows Monitoring 2 of 8 GPU(s) (filter applied)

Files changed

 README.md                  |  1 +
 monitor/arg_parser.cpp     | 38 ++++++++++++++++++++++++++++++++++++++
 monitor/arg_parser.h       |  3 +++
 monitor/nvlink_monitor.cpp | 34 +++++++++++++++++++++++++++++++---
 monitor/nvlink_monitor.h   | 16 ++++++++++++----
 test/test_arg_parser.cpp   | 39 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 131 insertions(+), 3 deletions(-)

Add `-g, --gpus <id1,id2,...>` to nvlink_monitor so the user can restrict
monitoring to a subset of GPUs. On an 8-GPU system where a bandwidth test
only touches 2 GPUs, this cuts the irrelevant output and lets the user
focus on the links that matter.

Behavior:
- Empty filter (default) => monitor all GPUs (unchanged).
- Non-empty filter => getNvLinkData skips GPUs whose index is not listed,
  so CSV/JSON/text output and bandwidth calc only cover the selected GPUs.
  Snapshot indices stay aligned across samples (filter is stable for the
  monitor's lifetime), so calculateBandwidth's index-based matching is
  unaffected.
- The filter is validated against discovered GPUs in the constructor after
  discovery: an out-of-range id throws std::runtime_error (surfaced as
  "Error: GPU id N out of range (0-M)" by main's catch) so a typo does not
  silently monitor nothing. A "Monitoring K of N GPU(s) (filter applied)"
  notice is printed to stderr when a filter is active.

Parsing (monitor/arg_parser.{h,cpp}): comma-separated integer list, e.g.
"0,1,3". Whitespace around tokens is tolerated. Negative ids, non-numeric
tokens, missing value, and empty lists are all rejected with ok=false.

Tests: 72 pass (was 66; +6 for --gpus: single short, multiple long,
whitespace tolerated, invalid token rejected, negative rejected, missing
value rejected; Defaults asserts gpuFilter empty). make check-format clean.
make monitor builds with -Wall -Wextra, no warnings. Verified on an 8x H20
system: -g 0,1 limits CSV output to GPUs 0 and 1; -g 9 errors with
"out of range (0-7)".

Signed-off-by: staryxchen <staryxchen@tencent.com>
@staryxchen
staryxchen merged commit bf2f148 into main Jul 10, 2026
2 checks passed
@staryxchen
staryxchen deleted the feat-monitor-gpu-filter branch July 10, 2026 08:57
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.

2 participants