how to measure throughput when using cudaHostAllocMapped in code "" cv::Mat image_resized(h_, w_, CV_8UC3, input_);
if (up_crop_ != 0)
{
std::int16_t cropRows = image.rows - up_crop_;
cv::Rect crop(0, up_crop_, image.cols, cropRows);
cv::Mat img_crop = image(crop);
cv::resize(img_crop, image_resized, cv::Size(w_, h_));
}
else
{
cv::resize(image, image_resized, cv::Size(w_, h_));
}
cudaStream_t stream;
NV_CUDA_CHECK(cudaStreamCreate(&(stream)));
code
Assistant
// Создание событий CUDA
cudaEvent_t startEvent, stopEvent;
NV_CUDA_CHECK(cudaEventCreate(&startEvent));
NV_CUDA_CHECK(cudaEventCreate(&stopEvent));
// Запись начала события перед копированием на GPU
NV_CUDA_CHECK(cudaEventRecord(startEvent, 0));
//transfer the image to GPU
///NV_CUDA_CHECK(cudaMemcpyAsync(inputGpu_, inputCpu_, h_ * w_ * 3 * sizeof(unsigned char), cudaMemcpyHostToDevice, stream));
///NV_CUDA_CHECK(cudaStreamSynchronize(stream));
// Запись окончания события после копирования на GPU
NV_CUDA_CHECK(cudaEventRecord(stopEvent, 0));
NV_CUDA_CHECK(cudaEventSynchronize(stopEvent));
// Получение времени выполнения и вычисление пропускной способности
float time;
NV_CUDA_CHECK(cudaEventElapsedTime(&time, startEvent, stopEvent));
size_t bytes = h_ * w_ * 3 * sizeof(unsigned char);
float bandwidth = bytes * 1e-6 / time;
printf("Host to Device bandwidth (GB/s): %f\n", bandwidth);
printf("Host to Device transfer time (ms): %f\n", time);
//normalize image and reshape it such that it has channels-first format (required by neural net)
NV_CUDA_CHECK(preprocessOnGpuWrapper(input_, inputCf_, h_, w_, coef_, mean_[0], mean_[1], mean_[2], std_[0], std_[1], std_[2]));
//inference
buffers_[0] = inputCf_;
buffers_[1] = outputRaw_;
for (int32_t i = 0, e = engine_->getNbIOTensors(); i < e; i++)
{
auto const name = engine_->getIOTensorName(i);
context_->setTensorAddress(name, (void **)buffers_[i]);
}
// Asynchronously enqueue the inference work
if (!context_->enqueueV3(stream))
{
std::cerr << "Error async enqueue inference ." << std::endl;
}
//compute segmentation masks from raw neural net ouput
NV_CUDA_CHECK(computeOutputOnGpuWrapper(outputRaw_, outputMask_, h_, w_, numClasses_, threshold_));
// Запись начала события перед копированием с GPU на хост
NV_CUDA_CHECK(cudaEventRecord(startEvent, 0));
//transfer result from GPU to the host
///NV_CUDA_CHECK(cudaMemcpyAsync(outputMaskCpu_, outputMaskGpu_, h_ * w_ * sizeof(unsigned char), cudaMemcpyDeviceToHost, stream)); // V1-2, 5
///NV_CUDA_CHECK(cudaStreamSynchronize(stream));
// Запись окончания события после копирования с GPU на хост
NV_CUDA_CHECK(cudaEventRecord(stopEvent, 0));
NV_CUDA_CHECK(cudaEventSynchronize(stopEvent));
NV_CUDA_CHECK(cudaEventElapsedTime(&time, startEvent, stopEvent));
bytes = h_ * w_ * sizeof(unsigned char);
bandwidth = bytes * 1e-6 / time;
printf("Device to Host bandwidth (GB/s): %f\n", bandwidth);
printf("Device to Host transfer time (ms): %f\n", time);
// Уничтожение событий
NV_CUDA_CHECK(cudaEventDestroy(startEvent));
NV_CUDA_CHECK(cudaEventDestroy(stopEvent));
// Wait for the work in the stream to complete.
// Release stream.
NV_CUDA_CHECK(cudaStreamDestroy(stream));
//resize masks to the size of the input image
cv::Mat masks(h_, w_, 0, outputMask_);
To measure throughput (effective bandwidth and transfer time) when using cudaHostAllocMapped (zero-copy memory), you need to carefully time the actual data accesses between host and device.
What is cudaHostAllocMapped?
When you allocate pinned mapped memory via cudaHostAlloc with the cudaHostAllocMapped flag, the memory is allocated physically pinned on the host and can be directly accessed by the GPU over the PCIe bus, without explicit cudaMemcpy calls. The pointer returned to the host is the same memory region the GPU sees via cudaHostGetDevicePointer.
Main challenge measuring throughput with zero-copy:
You do not explicitly memcpy data to and from the device.
Throughput is determined by timing how long the GPU kernel takes to read/write data to/from this host mapped memory.
Any GPU kernel or workflow accessing the data in that mapped host memory incurs data transfers implicitly.
How to measure throughput with your existing code?
Your current measurement measures explicit memcpy bandwidth, but with zero-copy memory, you do not call cudaMemcpy, so timing those commented out calls won't work.
For example—for data transferred from host->device implicitly—the time is basically the time your kernel needs to access the data in mapped host memory on the GPU.
Example:
cpp
// Record start event before kernel or inference
cudaEventRecord(startEvent, stream);
// Call kernel or inference function that reads from mapped host memory (input_)
preprocessOnGpuWrapper(input_, inputCf_, h_, w_, coef_, mean_[0], mean_[1], mean_[2], std_[0], std_[1], std_[2]);
// inference
context_->enqueueV3(stream);
// mask computation
computeOutputOnGpuWrapper(outputRaw_, outputMask_, h_, w_, numClasses_, threshold_);
// Record stop event after kernel / inference
cudaEventRecord(stopEvent, stream);
cudaEventSynchronize(stopEvent);
float timeMs = 0;
cudaEventElapsedTime(&timeMs, startEvent, stopEvent);
The time between these events includes data "transfer" over PCIe implicitly due to zero-copy access during kernel execution.
The measured time includes kernel compute time plus implicit zero-copy memory latency.
Zero-copy bandwidth will be significantly lower than pinned memcpy because it goes over pci bus each time the kernel accesses host memory.
To analyze only data transfer, write simple dummy kernels which do nothing except reading/writing the mapped array, to isolate pure transfer time.
You may want to run multiple iterations and average times for stable measurements.
Use appropriate cudaStreamSynchronize() to be sure all operations complete before timing.
Example adapted snippet (pseudocode):
cpp
// Assume input_ is mapped pinned host memory allocated with cudaHostAllocMapped
// and you obtained inputGpu pointer with cudaHostGetDevicePointer or passed input_ directly to kernels
cudaStream_t stream;
cudaStreamCreate(&stream);
cudaEvent_t startEvent, stopEvent;
cudaEventCreate(&startEvent);
cudaEventCreate(&stopEvent);
cudaStreamSynchronize(stream); // Warm up
cudaEventRecord(startEvent, stream);
// Launch kernel / inference that reads input_ on GPU, which is zero-copy mapped memory
preprocessOnGpuWrapper(input_, inputCf_, h_, w_, coef_, mean_[0], mean_[1], mean_[2], std_[0], std_[1], std_[2]);
context_->enqueueV3(stream);
computeOutputOnGpuWrapper(outputRaw_, outputMask_, h_, w_, numClasses_, threshold_);
cudaEventRecord(stopEvent, stream);
cudaEventSynchronize(stopEvent);
float timeMs = 0;
cudaEventElapsedTime(&timeMs, startEvent, stopEvent);
size_t bytesTransferred = h_ * w_ * 3 * sizeof(unsigned char);
float bandwidth = (bytesTransferred * 1e-6f) / timeMs;
printf("Zero-copy Host to Device effective bandwidth (GB/s): %f\n", bandwidth);
printf("Kernel + zero-copy time (ms): %f\n", timeMs);
cudaEventDestroy(startEvent);
cudaEventDestroy(stopEvent);
cudaStreamDestroy(stream);
Summary
For explicit memcpy transfers (your original code, commented out), time memcpy calls.
For cudaHostAllocMapped zero-copy memory, you measure the time kernel and inference takes to access the mapped host memory, because data transfer occurs implicitly during GPU access.
Calculate throughput from the kernel execution time and data size processed.
If you want a pure data transfer timing with zero-copy, you can write a simple kernel like:
cpp
__global__ void MemAccessKernel(unsigned char* data, size_t size)
{
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx < size) {
// just read/write to force PCIe transfer
data[idx] = data[idx] + 1;
}
}
// Then time this kernel
This will allow you to isolate the PCIe bandwidth using zero-copy.
Feel free to ask if you want me to help you write a minimal example for measuring zero-copy bandwidth!