NVIDIA CUPTI Python Library

13.2.0 · active · verified Thu Apr 16

The `cupti-python` library provides Python bindings for the NVIDIA CUDA Profiling Tools Interface (CUPTI). It exposes low-level C functions to enable detailed instrumentation and profiling of CUDA applications. While it offers direct access to CUPTI's C API, it's also a dependency for higher-level profiling tools like `cupy_cupti.profiler` (which is part of the same distribution) that simplify starting and stopping profiling sessions. It is currently at version 13.2.0 and aligns its releases with major CUDA Toolkit versions.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `cupy_cupti.profiler` to start and stop a profiling session around a CuPy CUDA operation. It requires CuPy to be installed for meaningful GPU activity. Note that `start()` and `stop()` primarily mark regions; actual data collection usually involves external tools like Nsight Systems or custom CUPTI callbacks.

import os
import cupy_cupti.profiler as cupti_profiler
import cupy as cp
import sys

# This quickstart demonstrates starting and stopping CUPTI profiling.
# Actual profiling data collection (e.g., via callbacks or external tools)
# is beyond the scope of this basic example and typically requires tools like Nsight Systems.

if not cp.cuda.is_available():
    print("CUDA is not available. Cannot run CUPTI profiling example.")
    sys.exit(1)
else:
    print("CUPTI Profiling Quickstart (requires CuPy installed):")
    print("--------------------------------------------------")
    
    # Define a simple CuPy operation to profile
    def run_cuda_kernel():
        a = cp.random.rand(100, 100).astype(cp.float32)
        b = cp.random.rand(100, 100).astype(cp.float32)
        c = a @ b
        cp.cuda.Stream.null.synchronize() # Ensure ops complete before profiler stops
        print(f"Executed a CuPy matrix multiplication. Result shape: {c.shape}")

    try:
        print("Starting CUPTI profiler...")
        cupti_profiler.start()

        run_cuda_kernel()

        cupti_profiler.stop()
        print("CUPTI profiler stopped.")
        print("\nNote: For actual profile data, you would typically integrate with NVIDIA Nsight Systems ")
        print("or set up CUPTI callbacks using the lower-level API. This script only marks a profiling region.")
    except Exception as e:
        print(f"An error occurred during profiling: {e}")
        print("Ensure CUPTI libraries are discoverable (e.g., via LD_LIBRARY_PATH) and CUDA is properly set up.")

view raw JSON →