Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions NOpenCL.Test/Intel/Optimization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace NOpenCL.Test.Intel
using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Buffer = NOpenCL.Buffer;

[TestClass]
public class Optimization
Expand Down Expand Up @@ -113,8 +112,8 @@ private unsafe void ExecuteKernel(
// allocate buffers
fixed (float* pinput = input, poutput = output)
{
using (Buffer inputBuffer = context.CreateBuffer(inFlags, sizeof(float) * taskSize, (IntPtr)pinput),
outputBuffer = context.CreateBuffer(outFlags, sizeof(float) * taskSize, (IntPtr)poutput))
using (Buffer<float> inputBuffer = context.CreateBuffer<float>(inFlags, sizeof(float) * taskSize, (IntPtr)pinput),
outputBuffer = context.CreateBuffer<float>(outFlags, sizeof(float) * taskSize, (IntPtr)poutput))
{
kernel.Arguments[0].SetValue(inputBuffer);
kernel.Arguments[1].SetValue(outputBuffer);
Expand Down
17 changes: 8 additions & 9 deletions NOpenCL.Test/NVidia/Bandwidth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace NOpenCL.Test.NVidia
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Buffer = NOpenCL.Buffer;

[TestClass]
public class Bandwidth
Expand Down Expand Up @@ -171,7 +170,7 @@ private double TestDeviceToHostTransfer(Context context, CommandQueue commandQue
private double TestDeviceToHostTransferPinned(Context context, CommandQueue commandQueue, int memSize, AccessMode accessMode)
{
// create a host buffer
using (Buffer pinnedData = context.CreateBuffer(MemoryFlags.ReadWrite | MemoryFlags.AllocateHostPointer, memSize))
using (Buffer<byte> pinnedData = context.CreateBuffer<byte>(MemoryFlags.ReadWrite | MemoryFlags.AllocateHostPointer, memSize))
{
// get a mapped pointer
IntPtr h_data;
Expand All @@ -185,7 +184,7 @@ private double TestDeviceToHostTransferPinned(Context context, CommandQueue comm
commandQueue.EnqueueUnmapMemObject(pinnedData, h_data);

// allocate device memory
using (Buffer deviceData = context.CreateBuffer(MemoryFlags.ReadWrite, memSize))
using (Buffer<byte> deviceData = context.CreateBuffer<byte>(MemoryFlags.ReadWrite, memSize))
{
// initialize device memory
commandQueue.EnqueueMapBuffer(pinnedData, true, MapFlags.Write, 0, memSize, out h_data);
Expand Down Expand Up @@ -244,7 +243,7 @@ private unsafe double TestDeviceToHostTransferPaged(Context context, CommandQueu
fixed (byte* pdata = data)
{
// allocate device memory
using (Buffer deviceData = context.CreateBuffer(MemoryFlags.ReadWrite, memSize))
using (Buffer<byte> deviceData = context.CreateBuffer<byte>(MemoryFlags.ReadWrite, memSize))
{
// initialize device memory
commandQueue.EnqueueWriteBuffer(deviceData, false, 0, memSize, (IntPtr)pdata);
Expand Down Expand Up @@ -299,7 +298,7 @@ private double TestHostToDeviceTransfer(Context context, CommandQueue commandQue
private double TestHostToDeviceTransferPinned(Context context, CommandQueue commandQueue, int memSize, AccessMode accessMode)
{
// Create a host buffer
using (Buffer pinnedData = context.CreateBuffer(MemoryFlags.ReadWrite | MemoryFlags.AllocateHostPointer, memSize))
using (Buffer<byte> pinnedData = context.CreateBuffer<byte>(MemoryFlags.ReadWrite | MemoryFlags.AllocateHostPointer, memSize))
{
// get a mapped pointer
IntPtr h_data;
Expand All @@ -313,7 +312,7 @@ private double TestHostToDeviceTransferPinned(Context context, CommandQueue comm
commandQueue.EnqueueUnmapMemObject(pinnedData, h_data);

// allocate device memory
using (Buffer deviceData = context.CreateBuffer(MemoryFlags.ReadWrite, memSize))
using (Buffer<byte> deviceData = context.CreateBuffer<byte>(MemoryFlags.ReadWrite, memSize))
{
// sync queue to host
commandQueue.Finish();
Expand Down Expand Up @@ -368,7 +367,7 @@ private unsafe double TestHostToDeviceTransferPaged(Context context, CommandQueu
fixed (byte* pdata = data)
{
// allocate device memory
using (Buffer deviceData = context.CreateBuffer(MemoryFlags.ReadWrite, memSize))
using (Buffer<byte> deviceData = context.CreateBuffer<byte>(MemoryFlags.ReadWrite, memSize))
{
// sync queue to host
commandQueue.Finish();
Expand Down Expand Up @@ -425,8 +424,8 @@ private double TestDeviceToDeviceTransfer(Context context, CommandQueue commandQ
data[i] = 0xFF;

// allocate device input and output memory and initialize the device input memory
using (Buffer d_idata = context.CreateBuffer(MemoryFlags.ReadOnly, memorySize),
d_odata = context.CreateBuffer(MemoryFlags.WriteOnly, memorySize))
using (Buffer<byte> d_idata = context.CreateBuffer<byte>(MemoryFlags.ReadOnly, memorySize),
d_odata = context.CreateBuffer<byte>(MemoryFlags.WriteOnly, memorySize))
{
unsafe
{
Expand Down
7 changes: 3 additions & 4 deletions NOpenCL.Test/NVidia/VectorAdd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace NOpenCL.Test.NVidia
{
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Buffer = NOpenCL.Buffer;

[TestClass]
public class VectorAdd
Expand Down Expand Up @@ -64,9 +63,9 @@ public unsafe void TestVectorAdd()
using (CommandQueue commandQueue = context.CreateCommandQueue(devices[0], CommandQueueProperties.None))
{
Console.WriteLine("Create buffers...");
using (Buffer deviceSrcA = context.CreateBuffer(MemoryFlags.ReadOnly, globalWorkSize * sizeof(float)),
deviceSrcB = context.CreateBuffer(MemoryFlags.ReadOnly, globalWorkSize * sizeof(float)),
deviceDst = context.CreateBuffer(MemoryFlags.WriteOnly, globalWorkSize * sizeof(float)))
using (Buffer<float> deviceSrcA = context.CreateBuffer<float>(MemoryFlags.ReadOnly, globalWorkSize * sizeof(float)),
deviceSrcB = context.CreateBuffer<float>(MemoryFlags.ReadOnly, globalWorkSize * sizeof(float)),
deviceDst = context.CreateBuffer<float>(MemoryFlags.WriteOnly, globalWorkSize * sizeof(float)))
{
string source =
@"// OpenCL Kernel Function for element by element vector addition
Expand Down
3 changes: 1 addition & 2 deletions NOpenCL.Test/TestBuffers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace NOpenCL.Test
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Buffer = NOpenCL.Buffer;

[TestClass]
public class TestBuffers
Expand All @@ -17,7 +16,7 @@ public void TestBufferDestroyedEvent()
Platform platform = Platform.GetPlatforms()[0];
using (Context context = Context.Create(platform.GetDevices()))
{
using (Buffer buffer = context.CreateBuffer(MemoryFlags.AllocateHostPointer, 1024))
using (Buffer<byte> buffer = context.CreateBuffer<byte>(MemoryFlags.AllocateHostPointer, 1024))
{
buffer.Destroyed += (sender, e) => destroyed = true;
Assert.IsFalse(destroyed);
Expand Down
2 changes: 1 addition & 1 deletion NOpenCL/BufferCreateType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace NOpenCL
{
/// <summary>
/// Describes the type of buffer object to be created by <see cref="Buffer.CreateSubBuffer"/>.
/// Describes the type of buffer object to be created by <see cref="Buffer{T}.CreateSubBuffer"/>.
/// </summary>
public enum BufferCreateType
{
Expand Down
13 changes: 7 additions & 6 deletions NOpenCL/Buffer.cs → NOpenCL/Buffer`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ namespace NOpenCL
using System;
using NOpenCL.SafeHandles;

public sealed class Buffer : MemObject<BufferSafeHandle>
public sealed class Buffer<T> : MemObject<BufferSafeHandle>
where T : struct
{
private readonly Buffer _parent;
private readonly Buffer<T> _parent;

internal Buffer(Context context, BufferSafeHandle handle)
: base(context, handle)
{
}

internal Buffer(Context context, Buffer parent, BufferSafeHandle handle)
internal Buffer(Context context, Buffer<T> parent, BufferSafeHandle handle)
: base(context, handle)
{
if (parent == null)
Expand All @@ -24,7 +25,7 @@ internal Buffer(Context context, Buffer parent, BufferSafeHandle handle)
_parent = parent;
}

public Buffer AssociatedMemObject
public Buffer<T> AssociatedMemObject
{
get
{
Expand All @@ -41,10 +42,10 @@ public UIntPtr Offset
}
}

public Buffer CreateSubBuffer(MemoryFlags flags, BufferRegion regionInfo)
public Buffer<T> CreateSubBuffer(MemoryFlags flags, BufferRegion regionInfo)
{
BufferSafeHandle subBuffer = UnsafeNativeMethods.CreateSubBuffer(Handle, flags, regionInfo);
return new Buffer(Context, this, subBuffer);
return new Buffer<T>(Context, this, subBuffer);
}
}
}
27 changes: 18 additions & 9 deletions NOpenCL/CommandQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ internal CommandQueueSafeHandle Handle
}
}

public Event EnqueueReadBuffer(Buffer buffer, bool blocking, long offset, long size, IntPtr destination, params Event[] eventWaitList)
public Event EnqueueReadBuffer<T>(Buffer<T> buffer, bool blocking, long offset, long size, IntPtr destination, params Event[] eventWaitList)
where T : struct
{
EventSafeHandle[] eventHandles = null;
if (eventWaitList != null)
Expand All @@ -107,7 +108,8 @@ public Event EnqueueReadBuffer(Buffer buffer, bool blocking, long offset, long s
return new Event(handle);
}

public Event EnqueueReadBufferRect(Buffer buffer, bool blocking, BufferCoordinates bufferOrigin, BufferCoordinates hostOrigin, BufferSize region, long bufferRowPitch, long bufferSlicePitch, long hostRowPitch, long hostSlicePitch, IntPtr destination, params Event[] eventWaitList)
public Event EnqueueReadBufferRect<T>(Buffer<T> buffer, bool blocking, BufferCoordinates bufferOrigin, BufferCoordinates hostOrigin, BufferSize region, long bufferRowPitch, long bufferSlicePitch, long hostRowPitch, long hostSlicePitch, IntPtr destination, params Event[] eventWaitList)
where T : struct
{
EventSafeHandle[] eventHandles = null;
if (eventWaitList != null)
Expand All @@ -117,7 +119,8 @@ public Event EnqueueReadBufferRect(Buffer buffer, bool blocking, BufferCoordinat
return new Event(handle);
}

public Event EnqueueWriteBuffer(Buffer buffer, bool blocking, long offset, long size, IntPtr source, params Event[] eventWaitList)
public Event EnqueueWriteBuffer<T>(Buffer<T> buffer, bool blocking, long offset, long size, IntPtr source, params Event[] eventWaitList)
where T : struct
{
EventSafeHandle[] eventHandles = null;
if (eventWaitList != null)
Expand All @@ -127,7 +130,8 @@ public Event EnqueueWriteBuffer(Buffer buffer, bool blocking, long offset, long
return new Event(handle);
}

public Event EnqueueWriteBufferRect(Buffer buffer, bool blocking, BufferCoordinates bufferOrigin, BufferCoordinates hostOrigin, BufferSize region, long bufferRowPitch, long bufferSlicePitch, long hostRowPitch, long hostSlicePitch, IntPtr source, params Event[] eventWaitList)
public Event EnqueueWriteBufferRect<T>(Buffer<T> buffer, bool blocking, BufferCoordinates bufferOrigin, BufferCoordinates hostOrigin, BufferSize region, long bufferRowPitch, long bufferSlicePitch, long hostRowPitch, long hostSlicePitch, IntPtr source, params Event[] eventWaitList)
where T : struct
{
EventSafeHandle[] eventHandles = null;
if (eventWaitList != null)
Expand All @@ -137,7 +141,8 @@ public Event EnqueueWriteBufferRect(Buffer buffer, bool blocking, BufferCoordina
return new Event(handle);
}

public Event EnqueueCopyBuffer(Buffer source, Buffer destination, long sourceOffset, long destinationOffset, long size, params Event[] eventWaitList)
public Event EnqueueCopyBuffer<T>(Buffer<T> source, Buffer<T> destination, long sourceOffset, long destinationOffset, long size, params Event[] eventWaitList)
where T : struct
{
EventSafeHandle[] eventHandles = null;
if (eventWaitList != null)
Expand All @@ -147,7 +152,8 @@ public Event EnqueueCopyBuffer(Buffer source, Buffer destination, long sourceOff
return new Event(handle);
}

public Event EnqueueCopyBufferRect(Buffer source, Buffer destination, BufferCoordinates sourceOrigin, BufferCoordinates destinationOrigin, BufferSize region, long sourceRowPitch, long sourceSlicePitch, long destinationRowPitch, long destinationSlicePitch, params Event[] eventWaitList)
public Event EnqueueCopyBufferRect<T>(Buffer<T> source, Buffer<T> destination, BufferCoordinates sourceOrigin, BufferCoordinates destinationOrigin, BufferSize region, long sourceRowPitch, long sourceSlicePitch, long destinationRowPitch, long destinationSlicePitch, params Event[] eventWaitList)
where T : struct
{
EventSafeHandle[] eventHandles = null;
if (eventWaitList != null)
Expand All @@ -157,7 +163,8 @@ public Event EnqueueCopyBufferRect(Buffer source, Buffer destination, BufferCoor
return new Event(handle);
}

public Event EnqueueMapBuffer(Buffer buffer, bool blocking, MapFlags mapFlags, long offset, long size, out IntPtr mappedPointer, params Event[] eventWaitList)
public Event EnqueueMapBuffer<T>(Buffer<T> buffer, bool blocking, MapFlags mapFlags, long offset, long size, out IntPtr mappedPointer, params Event[] eventWaitList)
where T : struct
{
EventSafeHandle[] eventHandles = null;
if (eventWaitList != null)
Expand Down Expand Up @@ -227,7 +234,8 @@ public Event EnqueueCopyImage(Image sourceImage, Image destinationImage, BufferC
return new Event(handle);
}

public Event EnqueueCopyImageToBuffer(Image sourceImage, Buffer destinationBuffer, BufferCoordinates sourceOrigin, BufferSize region, long destinationOffset, params Event[] eventWaitList)
public Event EnqueueCopyImageToBuffer<T>(Image sourceImage, Buffer<T> destinationBuffer, BufferCoordinates sourceOrigin, BufferSize region, long destinationOffset, params Event[] eventWaitList)
where T : struct
{
EventSafeHandle[] eventHandles = null;
if (eventWaitList != null)
Expand All @@ -237,7 +245,8 @@ public Event EnqueueCopyImageToBuffer(Image sourceImage, Buffer destinationBuffe
return new Event(handle);
}

public Event EnqueueCopyBufferToImage(Buffer sourceBuffer, Image destinationImage, long sourceOffset, BufferCoordinates destinationOrigin, BufferSize region, params Event[] eventWaitList)
public Event EnqueueCopyBufferToImage<T>(Buffer<T> sourceBuffer, Image destinationImage, long sourceOffset, BufferCoordinates destinationOrigin, BufferSize region, params Event[] eventWaitList)
where T : struct
{
EventSafeHandle[] eventHandles = null;
if (eventWaitList != null)
Expand Down
10 changes: 6 additions & 4 deletions NOpenCL/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ public CommandQueue CreateCommandQueue(Device device, CommandQueueProperties pro
return new CommandQueue(handle, this, device);
}

public Buffer CreateBuffer(MemoryFlags flags, long size)
public Buffer<T> CreateBuffer<T>(MemoryFlags flags, long size)
where T : struct
{
return CreateBuffer(flags, size, IntPtr.Zero);
return CreateBuffer<T>(flags, size, IntPtr.Zero);
}

public Buffer CreateBuffer(MemoryFlags flags, long size, IntPtr hostAddress)
public Buffer<T> CreateBuffer<T>(MemoryFlags flags, long size, IntPtr hostAddress)
where T : struct
{
if (size < 0)
throw new ArgumentOutOfRangeException(nameof(size));
Expand All @@ -128,7 +130,7 @@ public Buffer CreateBuffer(MemoryFlags flags, long size, IntPtr hostAddress)
throw new ArgumentException("Invalid host address.", nameof(hostAddress));

BufferSafeHandle handle = UnsafeNativeMethods.CreateBuffer(Handle, flags, (IntPtr)size, hostAddress);
return new Buffer(this, handle);
return new Buffer<T>(this, handle);
}

public Image CreateImage(MemoryFlags flags, ImageFormat format, ImageDescriptor descriptor)
Expand Down
2 changes: 1 addition & 1 deletion NOpenCL/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public UIntPtr ArraySize
}
}

public Buffer Buffer
public Buffer<byte> Buffer
{
get
{
Expand Down
3 changes: 2 additions & 1 deletion NOpenCL/KernelArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public string Name
}
}

public void SetValue(Buffer buffer)
public void SetValue<T>(Buffer<T> buffer)
where T : struct
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));
Expand Down
2 changes: 1 addition & 1 deletion NOpenCL/MemObjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum MemObjectType
/// <summary>
/// A buffer.
/// </summary>
/// <seealso cref="NOpenCL.Buffer"/>
/// <seealso cref="NOpenCL.Buffer{T}"/>
Buffer = 0x10F0,

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion NOpenCL/NOpenCL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<Compile Include="AddressQualifier.cs" />
<Compile Include="AffinityDomain.cs" />
<Compile Include="BinaryType.cs" />
<Compile Include="Buffer.cs" />
<Compile Include="Buffer`1.cs" />
<Compile Include="BufferCoordinates.cs" />
<Compile Include="BufferCreateType.cs" />
<Compile Include="BufferRegion.cs" />
Expand Down
Loading