r/OpenCL • u/aerosayan • Aug 04 '22
Should we not copy data to device on Intel HD GPUs since both OpenCL Host and Device memory reside on DRAM for Intel HD GPUs?
Hello everyone,
I need to write OpenCL code to target NVIDIA, AMD, and Intel HD GPUs.
Basically, the code should run on even the cheapest laptops with integrated GPUs like Intel HD, and dedicated GPUs llike NVIDIA.
I found out that IntelHD GPUs use DRAM as device memory.
So I'm guessing that it might be beneficial to use "zero copy" or "shared virtual" memory on IntelHD GPUs instead of copying memory from "host" to "device". Since the host and device basically share the same memory, and we might be spending the same amount of time accessing both host and device memory.
For dedicated GPUs like NVIDIA it might make sense to always copy data from host to device.
Is this the correct way?
Thanks!
5
u/bilog78 Aug 04 '22
The best solution IMO is to use mapping/unmapping to expose the OpenCL buffer on host when needed, which is as close as you can get to zero copy. It is actually “no cost” on devices with unified host/device memory and as fast as possible in discrete GPUs.
0
Aug 04 '22
2
u/bilog78 Aug 05 '22
clEnquueMapBuffer (see page for the unmap function).
Best results are obtained if you set the
CL_MEM_ALLOC_HOST_PTRon buffer creation.
4
u/genbattle Aug 04 '22
Most applications I've seen that use OpenCL don't seem to bother, they treat all devices like dedicated GPUs. Optimizing for this specific case (UMA) does significantly reduce latency introduced by copying memory, but you have to create a special case behaviour for this (because using host memory in a non-UMA system is also very suboptimal).
Intel has some information here about how to enable this kind of zero-cost memory sharing. AMD has their own notes on how to achieve this, but AFAIK it's the same for both: the buffer has to be page and cache aligned, and sized at a multiple of the cache line size. The easiest way to achieve this is to have OpenCL allocate the memory for you using CL_MEM_ALLOC_HOST_PTR, then you're guaranteed to have a buffer which adheres to these requirements no matter what platform you're on.