|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +#include <cuda.h> |
| 6 | + |
| 7 | +#include "bench_support.hpp" |
| 8 | + |
| 9 | +#include <cstdlib> |
| 10 | +#include <iostream> |
| 11 | + |
| 12 | + |
| 13 | +static void check_cu(CUresult status, const char* message) { |
| 14 | + if (status != CUDA_SUCCESS) { |
| 15 | + const char* error_name = nullptr; |
| 16 | + cuGetErrorName(status, &error_name); |
| 17 | + std::cerr << message << ": " << (error_name ? error_name : "unknown") << '\n'; |
| 18 | + std::exit(1); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +int main(int argc, char** argv) { |
| 24 | + bench::Options options = bench::parse_args(argc, argv); |
| 25 | + if (options.benchmark_name.empty()) { |
| 26 | + options.benchmark_name = "cpp.pointer_attributes.pointer_get_attribute"; |
| 27 | + } |
| 28 | + |
| 29 | + // Setup: init CUDA, allocate memory |
| 30 | + check_cu(cuInit(0), "cuInit failed"); |
| 31 | + |
| 32 | + CUdevice device; |
| 33 | + check_cu(cuDeviceGet(&device, 0), "cuDeviceGet failed"); |
| 34 | + |
| 35 | + CUcontext ctx; |
| 36 | + CUctxCreateParams ctxParams = {}; |
| 37 | + check_cu(cuCtxCreate(&ctx, &ctxParams, 0, device), "cuCtxCreate failed"); |
| 38 | + |
| 39 | + CUdeviceptr ptr; |
| 40 | + check_cu(cuMemAlloc(&ptr, 1 << 18), "cuMemAlloc failed"); |
| 41 | + |
| 42 | + unsigned int memory_type = 0; |
| 43 | + |
| 44 | + // Run benchmark |
| 45 | + auto results = bench::run_benchmark(options, [&]() { |
| 46 | + check_cu( |
| 47 | + cuPointerGetAttribute(&memory_type, CU_POINTER_ATTRIBUTE_MEMORY_TYPE, ptr), |
| 48 | + "cuPointerGetAttribute failed" |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + // Sanity check: the call actually did something |
| 53 | + if (memory_type == 0) { |
| 54 | + std::cerr << "unexpected memory_type=0\n"; |
| 55 | + } |
| 56 | + |
| 57 | + // Cleanup |
| 58 | + check_cu(cuMemFree(ptr), "cuMemFree failed"); |
| 59 | + check_cu(cuCtxDestroy(ctx), "cuCtxDestroy failed"); |
| 60 | + |
| 61 | + // Output |
| 62 | + bench::print_summary(options.benchmark_name, results); |
| 63 | + |
| 64 | + if (!options.output_path.empty()) { |
| 65 | + bench::write_pyperf_json(options.output_path, options.benchmark_name, options.loops, results); |
| 66 | + } |
| 67 | + |
| 68 | + return 0; |
| 69 | +} |
0 commit comments