Advantages/Disadvantages of using pinned memory

const int ds = 10;
int *data;
cudaHostAlloc(&data, ds*sizeof(data[0]), cudaHostAllocDefault);
for (int i = 0; i < ds; i++) data[i] = i;
cudaMemcpy(d_data, data, ds*sizeof(data[0]), cudaMemcpyHostToDevice);

no buffer copying

please think about the implication of this carefully before responding “but my data is already in a buffer”.

It had to get into that buffer somehow. And that buffer had to be allocated somehow.

At the point of allocation, allocate whatever starting buffer you have as a pinned buffer, and from there on out use it exactly as you would have used your ordinary buffer.

I also up front acknowledge that this may not fit every case. I’m not suggesting that pinned buffers are a panacea, a universal drop-in replacement with no issues and automatically faster. I’m not claiming that. I’m simply trying to point out one or 2 possible methods by which you might be able to dispense with any buffer copying.

If it doesn’t fit your use case, it doesn’t fit. Again, I’m not suggesting pinned memory is a universal win. But it can be a win in some cases. It’s OK if you don’t believe me. It’s usually better for people to develop comparative test cases and benchmark things themselves. This gives the most direct answer possible.

If you’ve already concluded that in your application, the only way to use a pinned buffer is by copying data from an already-existing buffer into it, then it may not be a win. I’m not claiming or arguing that.

1 Like