This may simply be an issue with the memory objects being allocated by the .NET framework not being properly page-aligned, but I cannot see why zero-copy is slower for me than non-zero copy.
I'll include code inline in this question, but the complete source can be seen here: https://github.com/kwende/ClooMatrixMultiply/blob/master/GiantMatrixOnGPU/GPUMatrixMultiplier.cs.
Since this is my first attempt at getting zero-copy working, I wrote up a simple matrix multiplication example. I first initialize my OpenCL objects:
private void Initialize()
{
// get the intel integrated GPU
_integratedIntelGPUPlatform = ComputePlatform.Platforms.Where(n => n.Name.Contains("Intel")).First();
// create the compute context.
_context = new ComputeContext(
ComputeDeviceTypes.Gpu, // use the gpu
new ComputeContextPropertyList(_integratedIntelGPUPlatform), // use the intel openCL platform
null,
IntPtr.Zero);
// the command queue is the, well, queue of commands sent to the "device" (GPU)
_commandQueue = new ComputeCommandQueue(
_context, // the compute context
_context.Devices[0], // first device matching the context specifications
ComputeCommandQueueFlags.None); // no special flags
string kernelSource = null;
using (StreamReader sr = new StreamReader("kernel.cl"))
{
kernelSource = sr.ReadToEnd();
}
// create the "program"
_program = new ComputeProgram(_context, new string[] { kernelSource });
// compile.
_program.Build(null, null, null, IntPtr.Zero);
_kernel = _program.CreateKernel("ComputeMatrix");
}
...this is only executed once if my code hasn't been initialized. Then I get into the main body. For non-zero copy, I do the following:
public float[] MultiplyMatrices(float[] matrix1, float[] matrix2,
int matrix1Height, int matrix1WidthMatrix2Height, int matrix2Width)
{
if (!_initialized)
{
Initialize();
_initialized = true;
}
ComputeBuffer<float> matrix1Buffer = new ComputeBuffer<float>(_context,
ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer,
matrix1);
_kernel.SetMemoryArgument(0, matrix1Buffer);
ComputeBuffer<float> matrix2Buffer = new ComputeBuffer<float>(_context,
ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer,
matrix2);
_kernel.SetMemoryArgument(1, matrix2Buffer);
float[] ret = new float[matrix1Height * matrix2Width];
ComputeBuffer<float> retBuffer = new ComputeBuffer<float>(_context,
ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer,
ret);
_kernel.SetMemoryArgument(2, retBuffer);
_kernel.SetValueArgument<int>(3, matrix1WidthMatrix2Height);
_kernel.SetValueArgument<int>(4, matrix2Width);
_commandQueue.Execute(_kernel,
new long[] { 0 },
new long[] { matrix2Width, matrix1Height },
null, null);
unsafe
{
fixed (float* retPtr = ret)
{
_commandQueue.Read(retBuffer,
false, 0,
ret.Length,
new IntPtr(retPtr),
null);
_commandQueue.Finish();
}
}
matrix1Buffer.Dispose();
matrix2Buffer.Dispose();
retBuffer.Dispose();
return ret;
}
You can see how I'm explicitly setting CopyHostPointer for all of my ComputeBuffer allocations. This executes fine.
I then do the following adjustment to (which includes setting "UseHostPointer" and calling Map/Unmap instead of Read):
public float[] MultiplyMatricesZeroCopy(float[] matrix1, float[] matrix2,
int matrix1Height, int matrix1WidthMatrix2Height, int matrix2Width)
{
if (!_initialized)
{
Initialize();
_initialized = true;
}
ComputeBuffer<float> matrix1Buffer = new ComputeBuffer<float>(_context,
ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer,
matrix1);
_kernel.SetMemoryArgument(0, matrix1Buffer);
ComputeBuffer<float> matrix2Buffer = new ComputeBuffer<float>(_context,
ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer,
matrix2);
_kernel.SetMemoryArgument(1, matrix2Buffer);
float[] ret = new float[matrix1Height * matrix2Width];
ComputeBuffer<float> retBuffer = new ComputeBuffer<float>(_context,
ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer,
ret);
_kernel.SetMemoryArgument(2, retBuffer);
_kernel.SetValueArgument<int>(3, matrix1WidthMatrix2Height);
_kernel.SetValueArgument<int>(4, matrix2Width);
_commandQueue.Execute(_kernel,
new long[] { 0 },
new long[] { matrix2Width, matrix1Height },
null, null);
IntPtr retPtr = _commandQueue.Map(
retBuffer,
false,
ComputeMemoryMappingFlags.Read,
0,
ret.Length, null);
_commandQueue.Unmap(retBuffer, ref retPtr, null);
_commandQueue.Finish();
matrix1Buffer.Dispose();
matrix2Buffer.Dispose();
retBuffer.Dispose();
return ret;
}
The timing says it all, however. My program spits this out:
CPU Matrix multiplication: 1178.5ms
GPU Matrix multiplication (copy): 115.1ms
GPU Matrix multiplication (zero copy): 174.1ms
GPU (w/ copy) is 10.23892x faster.
GPU (zero copy) is 6.769098x faster.
...so zero copy is slower.
Thanks to huseyin tugrul buyukisik I was able to figure out what was going on.
I needed to update my Intel drivers. Once I did this, then the zero-copy was much, much faster.
For the sake of posterity, here is the final version of the zero-copy code: