NVRTC Native Runtime Libraries for CUDA 11

11.8.89 · active · verified Sat Apr 11

NVRTC (NVIDIA Runtime Compilation) is a runtime compilation library for CUDA C++ that enables just-in-time (JIT) compilation of CUDA kernels from source code into PTX (Parallel Thread Execution) code. This Python package (`nvidia-cuda-nvrtc-cu11`) provides the native shared libraries for NVRTC specifically for CUDA 11.x environments. It acts as a foundational component for higher-level Python bindings and frameworks that leverage dynamic CUDA kernel generation. The current version is 11.8.89, with its initial release on October 3, 2022, and subsequent wheel metadata updates on August 16, 2024.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to compile CUDA C++ source code into PTX using the `pynvrtc` Python binding, which relies on the native NVRTC libraries provided by `nvidia-cuda-nvrtc-cu11`. The `Program` class handles the compilation process. The resulting PTX code can then be loaded and executed on an NVIDIA GPU using lower-level CUDA driver APIs.

import os
from pynvrtc.compiler import Program, ProgramException

# Example CUDA C++ kernel source code
cuda_source_code = '''
extern "C" __global__
void add(int *a, int *b, int *c, int N)
{
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < N)
    {
        c[idx] = a[idx] + b[idx];
    }
}
'''

try:
    # Compile the CUDA source code to PTX using the Program API
    # The nvidia-cuda-nvrtc-cu11 library is implicitly used by pynvrtc
    program = Program(cuda_source_code, 'add_kernel.cu')
    ptx_code = program.compile(['-arch=compute_60']) # Adjust arch for your GPU
    print("PTX code generated successfully. First 200 chars:\n", ptx_code[:200], '...')

    # In a real application, ptx_code would then be loaded and executed
    # using a CUDA driver API wrapper (e.g., from `cuda-python` or `pycuda`)
    # This part requires more setup (context, module, kernel launch) and is omitted for brevity.

except ProgramException as e:
    print(f"Error during NVRTC compilation: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →