﻿# Keep byte width and row pitch distinct in CUDA matrix copies

Pass byte widths and physical source and destination pitches to CUDA rectangular copies, then verify offsets, bounds and completion.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/guide-perf-memcpy2d-byte-pitch/
Language: en

A rectangular activation copy succeeds but the second row contains the wrong values. The integration passed element counts where cudaMemcpy2DAsync expects byte widths, or confused the copied row width with the physical row pitch. Diagnose the rectangle contract before interpreting the result as a tensor-layout or transfer-bandwidth problem.

## Describe the copied rectangle

Record source and destination base pointers, height in rows and useful width in bytes. Convert logical element width using the actual element size. Preserve the requested subrectangle's starting position as well. A copy routine moves the described bytes; it does not infer the application's tensor axes or transpose the mathematical matrix because the destination has another shape.

## Preserve both physical pitches

spitch and dpitch describe the storage width of each row in bytes, including padding. They can differ between source and destination. The copied width must fit both. If cudaMallocPitch produced the allocation, use the returned pitch instead of recomputing an assumed aligned value. A useful row can occupy less than its physical allocation.

## Check offsets against bounds

For a subrectangle, advance the starting pointer according to the original row pitch and byte offset, then verify that every copied row remains inside the allocation. The API forbids overlapping source and destination areas. A width that fits the nominal pitch does not alone prove that an offset rectangle fits the remaining storage or that its requested height is valid.

## Preserve asynchronous ownership

Check direction and pointer types, the call status and completion in the intended stream. Retain buffers until the actual transfer and its consumers finish. A short host call measures submission, not completed rows. Host-memory synchronization conditions remain relevant; the Async suffix does not make every host-memory arrangement equivalent or prove overlap with a compute kernel.

## Test recognizable row contents

Use different patterns per row and column, checking the first, last and interior values of the copied rectangle. Protect padding or surrounding regions when testing bounds, and use supported sanitizer checks for invalid accesses. Acceptance establishes the intended byte mapping and lifetime. It does not make an arbitrary strided tensor representable by this rectangular interface or establish the fastest transfer layout for the application.

## Primary references

[CUDA Runtime Memory Management](https://docs.nvidia.com/cuda/cuda-runtime-api/cuda_runtime_api/group__CUDART__MEMORY.html).

[CUDA API Synchronization Behavior](https://docs.nvidia.com/cuda/cuda-runtime-api/api-sync-behavior.html).

[NVIDIA Compute Sanitizer](https://docs.nvidia.com/compute-sanitizer/ComputeSanitizer/index.html).

Related reading: [Tensor strides: why transpose changes access without moving values](/journal/tensor-strides-transpose-contiguous/); [CUDA streams: express dependencies before asking for overlap](/journal/learn-stream-ordering-dependencies/).
