cuda memory_error ai_generated true

CUDA error: invalid pitch argument (cudaErrorInvalidPitchValue)

ID: cuda/invalid-pitch

Also available as: JSON · Markdown
85%Fix Rate
88%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
12 active

Root Cause

The pitch value (row byte width including padding) passed to a CUDA memory function exceeds device limits or is invalid. Occurs with cudaMallocPitch, cudaMemcpy2D, or texture operations when dealing with very wide 2D arrays.

generic

Workarounds

  1. 90% success Use cudaMallocPitch to let CUDA determine the optimal pitch automatically
    size_t pitch; cudaMallocPitch(&ptr, &pitch, width_bytes, height); then use returned pitch for all operations
  2. 85% success For very wide arrays, use 1D allocation with manual row-major indexing
    cudaMalloc(&ptr, width * height * sizeof(T)); index as ptr[row * width + col]
  3. 82% success Check device limits: cudaDeviceGetAttribute for cudaDevAttrMaxPitch
    Query the maximum pitch and split large allocations into tiles that fit within the limit

Dead Ends

Common approaches that don't work:

  1. Set pitch to exactly the row width without alignment 70% fail

    CUDA requires pitched memory to be aligned to device memory transaction size; unaligned pitch causes performance issues or errors

  2. Allocate as 1D and manually compute 2D offsets with wrong pitch 75% fail

    Using incorrect pitch for 2D indexing causes silent data corruption or out-of-bounds access

Error Chain

Leads to:
Frequently confused with: