Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0

# A clang-format style derived from Python's PEP 7 (4-space indent, Stroustrup braces,
# return type on its own line), except that the column limit is 120 rather than 79.
# Based on https://gist.github.com/pganssle/0e3a5f828b4d07d79447f6ced8e7e4db
BasedOnStyle: Google
BreakAfterReturnType: AllDefinitions
AllowShortIfStatementsOnASingleLine: Never
BreakBeforeBraces: Stroustrup
ColumnLimit: 120
DerivePointerAlignment: false
IndentWidth: 4
Language: Cpp
PointerAlignment: Right

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Optional, a slight preference for Left. Before this PR, _cpp/rt/ and the benchmarks wrote type* name in nearly every declaration, and Left keeps that style. On this branch, the switch changes 696 lines in 34 files. CPython and the PEP 7 examples use type *name, so keep Right if you prefer to stay with PEP 7.

Suggested change
PointerAlignment: Right
PointerAlignment: Left

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think we should stick to PEP7 where we can. For C++ constructs like lambdas that it doesn't address, that's a judgement call for us, of course.

ReflowComments: Always
SpaceBeforeParens: ControlStatements
SpacesInParens: Never
TabWidth: 4
UseTab: Never

# Keep include order as written; reordering can break order-sensitive builds.
SortIncludes: {Enabled: false}

# Ruff-like wrapping: break after "(", indent arguments, put ")" on its own line.
# This does not contradict PEP7, but extends it to the consistent style where
# calls either fit on a single line or they are broken across multiple lines with
# one argument per line (indented).
AlignAfterOpenBracket: true
BreakAfterOpenBracketBracedList: true
BreakAfterOpenBracketFunction: true
BreakAfterOpenBracketIf: true
BreakBeforeCloseBracketBracedList: true
BreakBeforeCloseBracketFunction: true
BreakBeforeCloseBracketIf: true
PackArguments: {BinPack: OnePerLine}
PackParameters: {BinPack: OnePerLine}
AllowAllArgumentsOnNextLine: false
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ repos:
- id: ruff-format
exclude: .*\.pyi$

- repo: https://github.com/pre-commit/mirrors-clang-format
rev: 98ad4ddb9c057de2f5b9e93e44e6db959807d68c # frozen: v23.1.3
hooks:
- id: clang-format
types_or: [c, c++, cuda]
exclude: ^cuda_core/(cuda/core/_include/(dlpack|aoti_shim)\.h|third_party/)

- repo: local
hooks:
- id: check-precommit-installed
Expand Down
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ between commits, leaving stale headers or out-of-date stubs in the history.
If the hook isn't installed, `pre-commit run` will print a visible warning
reminding you to run `pre-commit install`.

C, C++ and CUDA sources are formatted with `clang-format` using the
[`.clang-format`](.clang-format) at the repository root, which follows PEP 7
except for a 120-column limit. The
pre-commit hook installs its own pinned `clang-format` (from PyPI) into the
hook environment, so no system-wide installation is required. Vendored code
(for example `dlpack.h` and `cuda_core/third_party/`) is excluded in
`.pre-commit-config.yaml`. To format in your editor, install the same version
with `pip install clang-format==23.1.3`.

## CodeRabbit

This section explains how to use CodeRabbit for CUDA Python pull request review.
Expand Down
34 changes: 12 additions & 22 deletions benchmarks/cuda_bindings/benchmarks/cpp/bench_ctx_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
#include <cstdlib>
#include <iostream>


static void check_cu(CUresult status, const char* message) {
static void
check_cu(CUresult status, const char *message)
{
if (status != CUDA_SUCCESS) {
const char* error_name = nullptr;
const char *error_name = nullptr;
cuGetErrorName(status, &error_name);
std::cerr << message << ": " << (error_name ? error_name : "unknown") << '\n';
std::exit(1);
}
}


int main(int argc, char** argv) {
int
main(int argc, char **argv)
{
bench::Options options = bench::parse_args(argc, argv);

// Setup: init CUDA and create a context
Expand All @@ -45,25 +47,19 @@ int main(int argc, char** argv) {

// --- ctx_set_current ---
{
suite.run("ctx_device.ctx_set_current", [&]() {
check_cu(cuCtxSetCurrent(ctx), "cuCtxSetCurrent failed");
});
suite.run("ctx_device.ctx_set_current", [&]() { check_cu(cuCtxSetCurrent(ctx), "cuCtxSetCurrent failed"); });
}

// --- ctx_get_device ---
{
CUdevice dev;
suite.run("ctx_device.ctx_get_device", [&]() {
check_cu(cuCtxGetDevice(&dev), "cuCtxGetDevice failed");
});
suite.run("ctx_device.ctx_get_device", [&]() { check_cu(cuCtxGetDevice(&dev), "cuCtxGetDevice failed"); });
}

// --- device_get ---
{
CUdevice dev;
suite.run("ctx_device.device_get", [&]() {
check_cu(cuDeviceGet(&dev, 0), "cuDeviceGet failed");
});
suite.run("ctx_device.device_get", [&]() { check_cu(cuDeviceGet(&dev, 0), "cuDeviceGet failed"); });
}

// --- device_get_attribute ---
Expand All @@ -80,21 +76,15 @@ int main(int argc, char** argv) {
// --- device_primary_ctx_retain ---
// Outer retain so the benchmarked retain/release pair just bumps the refcount.
CUcontext primary_outer = nullptr;
check_cu(
cuDevicePrimaryCtxRetain(&primary_outer, device),
"cuDevicePrimaryCtxRetain (setup) failed"
);
check_cu(cuDevicePrimaryCtxRetain(&primary_outer, device), "cuDevicePrimaryCtxRetain (setup) failed");
{
CUcontext primary = nullptr;
suite.run("ctx_device.device_primary_ctx_retain", [&]() {
check_cu(cuDevicePrimaryCtxRetain(&primary, device), "cuDevicePrimaryCtxRetain failed");
check_cu(cuDevicePrimaryCtxRelease(device), "cuDevicePrimaryCtxRelease failed");
});
}
check_cu(
cuDevicePrimaryCtxRelease(device),
"cuDevicePrimaryCtxRelease (teardown) failed"
);
check_cu(cuDevicePrimaryCtxRelease(device), "cuDevicePrimaryCtxRelease (teardown) failed");

// Cleanup
check_cu(cuCtxDestroy(ctx), "cuCtxDestroy failed");
Expand Down
20 changes: 9 additions & 11 deletions benchmarks/cuda_bindings/benchmarks/cpp/bench_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
#include <cstdlib>
#include <iostream>


static void check_cu(CUresult status, const char* message) {
static void
check_cu(CUresult status, const char *message)
{
if (status != CUDA_SUCCESS) {
const char* error_name = nullptr;
const char *error_name = nullptr;
cuGetErrorName(status, &error_name);
std::cerr << message << ": " << (error_name ? error_name : "unknown") << '\n';
std::exit(1);
}
}


int main(int argc, char** argv) {
int
main(int argc, char **argv)
{
bench::Options options = bench::parse_args(argc, argv);

// Setup
Expand All @@ -47,9 +49,7 @@ int main(int argc, char** argv) {
bench::BenchmarkSuite suite(options);
// Drain the persistent stream after calibration so event_record (which
// enqueues onto the stream) and event_synchronize start from a known state.
suite.set_post_calibrate([&]() {
check_cu(cuStreamSynchronize(stream), "post-calibrate sync failed");
});
suite.set_post_calibrate([&]() { check_cu(cuStreamSynchronize(stream), "post-calibrate sync failed"); });

// --- event_create_destroy ---
{
Expand All @@ -62,9 +62,7 @@ int main(int argc, char** argv) {

// --- event_record ---
{
suite.run("event.event_record", [&]() {
check_cu(cuEventRecord(event, stream), "cuEventRecord failed");
});
suite.run("event.event_record", [&]() { check_cu(cuEventRecord(event, stream), "cuEventRecord failed"); });
}

// Re-sync so event is in a known completed state after the record benchmark
Expand Down
Loading
Loading