public void EnqueueUnmapMemObject(Mem memobj, IntPtr mapped_ptr, int num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; result = OpenCL.EnqueueUnmapMemObject(CommandQueueID, memobj.MemID, mapped_ptr.ToPointer(), (uint)num_events_in_wait_list, InteropTools.ConvertEventsToEventIDs(event_wait_list), null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueUnmapMemObject failed with error code " + result, result); }
/// <summary> /// Test all versions of: /// /// EnqueueWriteImage /// EnqueueReadImage /// EnqueueCopyImage /// /// The test just copies the entirety of a buffer and checks if the result is equal to the original. /// An error indicates that one of the above functions failed and further manual analysis is required /// to pinpoint the error. /// </summary> /// <param name="c"></param> /// <param name="cq"></param> private void TestImageReadWriteCopyOps(Context c, CommandQueue cq) { if (!cq.Device.ImageSupport) { Output("Skipping image read/write/copy tests(not supported on this device)"); return; } Output("Testing image read/write/copy functions"); OpenCLNet.Image img0 = null; OpenCLNet.Image img1 = null; OpenCLNet.Image img2 = null; int imgWidth = 1024; int imgHeight = 1024; int bufLen = imgWidth*4*imgHeight; byte[] srcData = new byte[bufLen]; byte[] cmpData = new byte[bufLen]; Event event0; Event event1; Event event2; Event event3; Event event4; Event event5; for (int i = 0; i < srcData.Length; i++) srcData[i] = (byte)(i); Array.Clear(cmpData, 0, cmpData.Length); try { img0 = c.CreateImage2D(MemFlags.READ_WRITE, ImageFormat.RGBA8U, imgWidth, imgHeight); img1 = c.CreateImage2D(MemFlags.READ_WRITE, ImageFormat.RGBA8U, imgWidth, imgHeight); img2 = c.CreateImage2D(MemFlags.READ_WRITE, ImageFormat.RGBA8U, imgWidth, imgHeight); Array.Clear(cmpData, 0, cmpData.Length); fixed (byte* pSrc = srcData) { fixed (byte* pCmp = cmpData) { { IntPtr[] origin = new IntPtr[3] { (IntPtr)0, (IntPtr)0, (IntPtr)0 }; IntPtr[] region = new IntPtr[3] { (IntPtr)imgWidth, (IntPtr)imgHeight, (IntPtr)1 }; IntPtr[] dstOrigin = new IntPtr[3] { (IntPtr)0, (IntPtr)0, (IntPtr)0 }; IntPtr[] dstRegion = new IntPtr[3] { (IntPtr)imgWidth, (IntPtr)imgHeight, (IntPtr)1 }; Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteImage(img0, true, origin, region, IntPtr.Zero, IntPtr.Zero, (IntPtr)pSrc); cq.EnqueueCopyImage(img0, img1, origin, dstOrigin, region); cq.EnqueueBarrier(); cq.EnqueueReadImage(img1, true, origin, region, IntPtr.Zero, IntPtr.Zero, (IntPtr)pCmp); if (!CompareArray(cmpData, srcData)) Error("TestImageReadWriteCopyOps: (IntPtr version)Copy not identical to source when using no event args"); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteImage(img0, true, origin, region, IntPtr.Zero, IntPtr.Zero, (IntPtr)pSrc, 0, null, out event0); cq.EnqueueCopyImage(img0, img1, origin, dstOrigin, region, 0, null, out event1); cq.EnqueueBarrier(); cq.EnqueueReadImage(img1, true, origin, region, IntPtr.Zero, IntPtr.Zero, (IntPtr)pCmp, 0, null, out event2); if (!CompareArray(cmpData, srcData)) Error("TestImageReadWriteCopyOps: (IntPtr version)Copy not identical to source with event output and no wait list"); Array.Clear(cmpData, 0, cmpData.Length); Event[] events = new Event[] { event0, event1, event2 }; cq.EnqueueWriteImage(img0, true, origin, region, IntPtr.Zero, IntPtr.Zero, (IntPtr)pSrc, 3, events); cq.EnqueueCopyImage(img0, img1, origin, dstOrigin, region, 3, events); cq.EnqueueBarrier(); cq.EnqueueReadImage(img1, true, origin, region, IntPtr.Zero, IntPtr.Zero, (IntPtr)pCmp, 3, events); if (!CompareArray(cmpData, srcData)) Error("TestImageReadWriteCopyOps: (IntPtr version)Copy not identical to source using no event output and a wait list"); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteImage(img0, true, origin, region, IntPtr.Zero, IntPtr.Zero, (IntPtr)pSrc, 3, events, out event3); cq.EnqueueCopyImage(img0, img1, origin, dstOrigin, region, 3, events, out event4); cq.EnqueueBarrier(); cq.EnqueueReadImage(img1, true, origin, region, IntPtr.Zero, IntPtr.Zero, (IntPtr)pCmp, 3, events, out event5); if (!CompareArray(cmpData, srcData)) Error("TestImageReadWriteCopyOps: (IntPtr version)Copy not identical to source using event output and a wait list"); event0.Dispose(); event1.Dispose(); event2.Dispose(); event3.Dispose(); event4.Dispose(); event5.Dispose(); } { int[] origin = new int[3] { 0, 0, 0 }; int[] region = new int[3] { imgWidth, imgHeight, 1 }; int[] dstOrigin = new int[3] { 0, 0, 0 }; int[] dstRegion = new int[3] { imgWidth, imgHeight, 1 }; Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteImage(img0, true, origin, region, 0, 0, (IntPtr)pSrc); cq.EnqueueCopyImage(img0, img1, origin, dstOrigin, region); cq.EnqueueBarrier(); cq.EnqueueReadImage(img1, true, origin, region, 0, 0, (IntPtr)pCmp); if (!CompareArray(cmpData, srcData)) Error("TestImageReadWriteCopyOps: (int version)Copy not identical to source when using no event args"); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteImage(img0, true, origin, region, 0, 0, (IntPtr)pSrc, 0, null, out event0); cq.EnqueueCopyImage(img0, img1, origin, dstOrigin, region, 0, null, out event1); cq.EnqueueBarrier(); cq.EnqueueReadImage(img1, true, origin, region, 0, 0, (IntPtr)pCmp, 0, null, out event2); if (!CompareArray(cmpData, srcData)) Error("TestImageReadWriteCopyOps: (int version)Copy not identical to source with event output and no wait list"); Array.Clear(cmpData, 0, cmpData.Length); Event[] events = new Event[] { event0, event1, event2 }; cq.EnqueueWriteImage(img0, true, origin, region, 0, 0, (IntPtr)pSrc, 3, events); cq.EnqueueCopyImage(img0, img1, origin, dstOrigin, region, 0, events); cq.EnqueueBarrier(); cq.EnqueueReadImage(img1, true, origin, region, 0, 0, (IntPtr)pCmp, 3, events); if (!CompareArray(cmpData, srcData)) Error("TestImageReadWriteCopyOps: (int version)Copy not identical to source using no event output and a wait list"); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteImage(img0, true, origin, region, 0, 0, (IntPtr)pSrc, 3, events, out event3); cq.EnqueueCopyImage(img0, img1, origin, dstOrigin, region, 0, events, out event4); cq.EnqueueBarrier(); cq.EnqueueReadImage(img1, true, origin, region, 0, 0, (IntPtr)pCmp, 3, events, out event5); if (!CompareArray(cmpData, srcData)) Error("TestImageReadWriteCopyOps: (int version)Copy not identical to source using no event output and a wait list"); event0.Dispose(); event1.Dispose(); event2.Dispose(); event3.Dispose(); event4.Dispose(); event5.Dispose(); } { long[] origin = new long[3] { 0, 0, 0 }; long[] region = new long[3] { imgWidth, imgHeight, 1 }; long[] dstOrigin = new long[3] { 0, 0, 0 }; long[] dstRegion = new long[3] { imgWidth, imgHeight, 1 }; Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteImage(img0, true, origin, region, 0L, 0L, (IntPtr)pSrc); cq.EnqueueCopyImage(img0, img1, origin, dstOrigin, region); cq.EnqueueBarrier(); cq.EnqueueReadImage(img1, true, origin, region, 0L, 0L, (IntPtr)pCmp); if (!CompareArray(cmpData, srcData)) Error("TestImageReadWriteCopyOps: (long version)Copy not identical to source when using no event args"); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteImage(img0, true, origin, region, 0L, 0L, (IntPtr)pSrc, 0, null, out event0); cq.EnqueueCopyImage(img0, img1, origin, dstOrigin, region, 0, null, out event1); cq.EnqueueBarrier(); cq.EnqueueReadImage(img1, true, origin, region, 0L, 0L, (IntPtr)pCmp, 0, null, out event2); if (!CompareArray(cmpData, srcData)) Error("TestImageReadWriteCopyOps: (long version)Copy not identical to source with event output and no wait list"); Array.Clear(cmpData, 0, cmpData.Length); Event[] events = new Event[] { event0, event1, event2 }; cq.EnqueueWriteImage(img0, true, origin, region, 0L, 0L, (IntPtr)pSrc, 3, events); cq.EnqueueCopyImage(img0, img1, origin, dstOrigin, region, 0, events); cq.EnqueueBarrier(); cq.EnqueueReadImage(img1, true, origin, region, 0L, 0L, (IntPtr)pCmp, 3, events); if (!CompareArray(cmpData, srcData)) Error("TestImageReadWriteCopyOps: (long version)Copy not identical to source using no event output and a wait list"); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteImage(img0, true, origin, region, 0L, 0L, (IntPtr)pSrc, 3, events, out event3); cq.EnqueueCopyImage(img0, img1, origin, dstOrigin, region, 0, events, out event4); cq.EnqueueBarrier(); cq.EnqueueReadImage(img1, true, origin, region, 0L, 0L, (IntPtr)pCmp, 3, events, out event5); if (!CompareArray(cmpData, srcData)) Error("TestImageReadWriteCopyOps: (long version)Copy not identical to source using no event output and a wait list"); event0.Dispose(); event1.Dispose(); event2.Dispose(); event3.Dispose(); event4.Dispose(); event5.Dispose(); } } } } catch (Exception e) { Error("Exception during testing: "+e.ToString()); } finally { if (img0 != null) img0.Dispose(); if (img1 != null) img1.Dispose(); if (img2 != null) img2.Dispose(); } }
public static IntPtr[] ConvertEventsToEventIDs(Event[] events) { IntPtr[] eventIDs; if (events == null) return null; eventIDs = new IntPtr[events.Length]; for (int i = 0; i < events.Length; i++) eventIDs[i] = events[i]; return eventIDs; }
/// <summary> /// Test all versions of: /// /// EnqueueReadBufferRect /// EnqueueWriteBufferRect /// EnqueueCopyBufferRect /// /// The test just copies the entirety of a buffer and checks if the result is equal to the original. /// An error indicates that one of the above functions failed and further manual analysis is required /// to pinpoint the error. /// </summary> /// <param name="c"></param> /// <param name="cq"></param> private void TestBufferRectFunctions(Context c, CommandQueue cq) { if (!(cq.Device.Platform.OpenCLMajorVersion >= 1 && cq.Device.Platform.OpenCLMinorVersion >= 1)) { Output("Skipping EnqueueReadBufferRect, EnqueueWriteBufferRect and EnqueueCopyBufferRect tests(Requires OpenCL 1.1 or higher)"); return; } Output("Testing EnqueueReadBufferRect, EnqueueWriteBufferRect and EnqueueCopyBufferRect"); OpenCLNet.Mem mem0 = null; OpenCLNet.Mem mem1 = null; int bufWidth = 16; int bufHeight = 16; int bufLen = bufWidth * bufHeight; byte[] srcData = new byte[bufLen]; byte[] cmpData = new byte[bufLen]; Event event0; Event event1; Event event2; Event event3; Event event4; Event event5; Array.Clear(srcData, 0, srcData.Length); for (int i = 8; i < 12; i++) for (int j = 8; j < 12; j++) srcData[bufWidth * i + j] = (byte)1; Array.Clear(cmpData, 0, cmpData.Length); try { mem0 = c.CreateBuffer(MemFlags.READ_WRITE, bufLen, IntPtr.Zero); mem1 = c.CreateBuffer(MemFlags.READ_WRITE, bufLen, IntPtr.Zero); fixed (byte* pSrc = srcData) { fixed (byte* pCmp = cmpData) { { IntPtr[] bufferOffset = new IntPtr[3] { (IntPtr)0, (IntPtr)0, (IntPtr)0 }; IntPtr[] hostOffset = new IntPtr[3] { (IntPtr)0, (IntPtr)0, (IntPtr)0 }; IntPtr[] region = new IntPtr[3] { (IntPtr)bufWidth, (IntPtr)bufHeight, (IntPtr)1 }; IntPtr bufferRowPitch = (IntPtr)bufWidth; IntPtr bufferSlicePitch = (IntPtr)0; IntPtr hostRowPitch = (IntPtr)bufWidth; IntPtr hostSlicePitch = (IntPtr)0; cq.EnqueueWriteBufferRect(mem0, true, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pSrc); cq.EnqueueCopyBufferRect(mem0, mem1, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch); cq.Finish(); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueReadBufferRect(mem1, true, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pCmp); if (!CompareArray(cmpData, srcData)) Error("Read-/Write-/CopyRect: (IntPtr version)Copy not identical to source when using no event args"); cq.EnqueueWriteBufferRect(mem0, false, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pSrc, 0, null, out event0); cq.EnqueueWaitForEvent(event0); cq.EnqueueCopyBufferRect(mem0, mem1, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, 0, null, out event1); cq.EnqueueWaitForEvent(event1); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueReadBufferRect(mem1, false, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pCmp, 0, null, out event2); cq.Finish(); if (!CompareArray(cmpData, srcData)) Error("Read-/Write-/CopyRect: (IntPtr version)Copy not identical to source when using event output and no event args"); Event[] events = new Event[] { event0, event1, event2 }; cq.EnqueueWriteBufferRect(mem0, false, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pSrc, 3, events, out event3); cq.EnqueueWaitForEvent(event3); cq.EnqueueCopyBufferRect(mem0, mem1, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, 3, events, out event4); cq.EnqueueWaitForEvent(event4); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueReadBufferRect(mem1, false, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pCmp, 3, events, out event5); cq.Finish(); if (!CompareArray(cmpData, srcData)) Error("Read-/Write-/CopyRect: (IntPtr version)Copy not identical to source when using event output and event args"); event0.Dispose(); event1.Dispose(); event2.Dispose(); event3.Dispose(); event4.Dispose(); event5.Dispose(); } { int[] bufferOffset = new int[3] { 0, 0, 0 }; int[] hostOffset = new int[3] { 0, 0, 0 }; int[] region = new int[3] { bufWidth, bufHeight, 1 }; int bufferRowPitch = bufWidth; int bufferSlicePitch = 0; int hostRowPitch = bufWidth; int hostSlicePitch = 0; cq.EnqueueWriteBufferRect(mem0, true, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pSrc); cq.EnqueueCopyBufferRect(mem0, mem1, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch); cq.Finish(); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueReadBufferRect(mem1, true, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pCmp); if (!CompareArray(cmpData, srcData)) Error("Read-/Write-/CopyRect: (int version)Copy not identical to source when using no event args"); cq.EnqueueWriteBufferRect(mem0, false, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pSrc, 0, null, out event0); cq.EnqueueWaitForEvent(event0); cq.EnqueueCopyBufferRect(mem0, mem1, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, 0, null, out event1); cq.EnqueueWaitForEvent(event1); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueReadBufferRect(mem1, false, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pCmp, 0, null, out event2); cq.Finish(); if (!CompareArray(cmpData, srcData)) Error("Read-/Write-/CopyRect: (int version)Copy not identical to source when using event output and no event args"); Event[] events = new Event[] { event0, event1, event2 }; cq.EnqueueWriteBufferRect(mem0, false, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pSrc, 3, events, out event3); cq.EnqueueWaitForEvent(event3); cq.EnqueueCopyBufferRect(mem0, mem1, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, 3, events, out event4); cq.EnqueueWaitForEvent(event4); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueReadBufferRect(mem1, false, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pCmp, 3, events, out event5); cq.Finish(); if (!CompareArray(cmpData, srcData)) Error("Read-/Write-/CopyRect: (int version)Copy not identical to source when using event output and event args"); event0.Dispose(); event1.Dispose(); event2.Dispose(); event3.Dispose(); event4.Dispose(); event5.Dispose(); } { long[] bufferOffset = new long[3] { 0L, 0L, 0L }; long[] hostOffset = new long[3] { 0L, 0L, 0L }; long[] region = new long[3] { (long)bufWidth, (long)bufHeight, (long)1 }; long bufferRowPitch = bufWidth; long bufferSlicePitch = 0; long hostRowPitch = bufWidth; long hostSlicePitch = 0; cq.EnqueueWriteBufferRect(mem0, true, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pSrc); cq.EnqueueCopyBufferRect(mem0, mem1, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch); cq.Finish(); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueReadBufferRect(mem1, true, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pCmp); if (!CompareArray(cmpData, srcData)) Error("Read-/Write-/CopyRect: (long version)Copy not identical to source when using no event args"); cq.EnqueueWriteBufferRect(mem0, false, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pSrc, 0, null, out event0); cq.EnqueueWaitForEvent(event0); cq.EnqueueCopyBufferRect(mem0, mem1, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, 0, null, out event1); cq.EnqueueWaitForEvent(event1); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueReadBufferRect(mem1, false, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pCmp, 0, null, out event2); cq.Finish(); if (!CompareArray(cmpData, srcData)) Error("Read-/Write-/CopyRect: (long version)Copy not identical to source when using event output and no event args"); Event[] events = new Event[] { event0, event1, event2 }; cq.EnqueueWriteBufferRect(mem0, false, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pSrc, 3, events, out event3); cq.EnqueueWaitForEvent(event3); cq.EnqueueCopyBufferRect(mem0, mem1, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, 3, events, out event4); cq.EnqueueWaitForEvent(event4); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueReadBufferRect(mem1, false, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, (IntPtr)pCmp, 3, events, out event5); cq.Finish(); if (!CompareArray(cmpData, srcData)) Error("Read-/Write-/CopyRect: (long version)Copy not identical to source when using event output and event args"); event0.Dispose(); event1.Dispose(); event2.Dispose(); event3.Dispose(); event4.Dispose(); event5.Dispose(); } } } } catch (Exception e) { Error("Exception during testing: " + e.ToString()); } finally { if (mem0 != null) mem0.Dispose(); if (mem1 != null) mem1.Dispose(); } }
private void TestUserEventCallback(Event e, ExecutionStatus executionStatus, object userData) { TestUserEventCallbackCalled = true; }
internal CallbackData(Event _event, EventNotify userMethod, object userData) { EventObject = _event; UserMethod = userMethod; UserData = userData; }
/// <summary> /// Block until all events in the array have fired /// </summary> /// <param name="num_events"></param> /// <param name="event_list"></param> public void WaitForEvents(int num_events, Event[] event_list) { OpenCL.WaitForEvents((uint)num_events, InteropTools.ConvertEventsToEventIDs(event_list)); }
public void EnqueueWriteBufferRect(Mem buffer, bool blocking_write, long[] buffer_offset, long[] host_offset, long[] region, long buffer_row_pitch, long buffer_slice_pitch, long host_row_pitch, long host_slice_pitch, IntPtr ptr, int num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; IntPtr* pBufferOffset = stackalloc IntPtr[3]; IntPtr* pHostOffset = stackalloc IntPtr[3]; IntPtr* pRegion = stackalloc IntPtr[3]; IntPtr* pRepackedEvents = stackalloc IntPtr[num_events_in_wait_list]; if (num_events_in_wait_list == 0) pRepackedEvents = null; InteropTools.A3ToIntPtr3(buffer_offset, pBufferOffset); InteropTools.A3ToIntPtr3(host_offset, pHostOffset); InteropTools.A3ToIntPtr3(region, pRegion); InteropTools.ConvertEventsToEventIDs(num_events_in_wait_list, event_wait_list, pRepackedEvents); result = OpenCL.EnqueueWriteBufferRect(CommandQueueID, buffer.MemID, (uint)(blocking_write ? Bool.TRUE : Bool.FALSE), pBufferOffset, pHostOffset, pRegion, buffer_row_pitch, buffer_slice_pitch, host_row_pitch, host_slice_pitch, ptr.ToPointer(), num_events_in_wait_list, pRepackedEvents, null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueWriteBufferRect failed with error code " + result, result); }
public void EnqueueWriteImage(Mem image, bool blockingWrite, IntPtr[] origin, IntPtr[] region, IntPtr input_row_pitch, IntPtr input_slice_pitch, IntPtr ptr, int num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; result = OpenCL.EnqueueWriteImage(CommandQueueID, image.MemID, (uint)(blockingWrite ? Bool.TRUE : Bool.FALSE), origin, region, input_row_pitch, input_slice_pitch, ptr.ToPointer(), (uint)num_events_in_wait_list, InteropTools.ConvertEventsToEventIDs(event_wait_list), null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueWriteImage failed with error code " + result, result); }
public void EnqueueWriteBuffer(Mem buffer, bool blockingWrite, long offset, long cb, IntPtr ptr, int num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; IntPtr* repackedEvents = stackalloc IntPtr[num_events_in_wait_list]; if (num_events_in_wait_list == 0) repackedEvents = null; InteropTools.ConvertEventsToEventIDs(num_events_in_wait_list, event_wait_list, repackedEvents); result = OpenCL.EnqueueWriteBuffer(CommandQueueID, buffer, (uint)(blockingWrite ? Bool.TRUE : Bool.FALSE), offset, cb, ptr, num_events_in_wait_list, repackedEvents, null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueWriteBuffer failed with error code " + result, result); }
public void EnqueueWriteBufferRect(Mem buffer, bool blocking_write, IntPtr[] buffer_offset, IntPtr[] host_offset, IntPtr[] region, IntPtr buffer_row_pitch, IntPtr buffer_slice_pitch, IntPtr host_row_pitch, IntPtr host_slice_pitch, IntPtr ptr, uint num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; result = OpenCL.EnqueueWriteBufferRect(CommandQueueID, buffer.MemID, blocking_write ? 1u : 0u, buffer_offset, host_offset, region, buffer_row_pitch, buffer_slice_pitch, host_row_pitch, host_slice_pitch, ptr.ToPointer(), num_events_in_wait_list, InteropTools.ConvertEventsToEventIDs(event_wait_list), null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueWriteBufferRect failed with error code " + result, result); }
public void EnqueueWriteBuffer(Mem buffer, bool blockingWrite, IntPtr offset, IntPtr cb, IntPtr ptr, int num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; result = OpenCL.EnqueueWriteBuffer(CommandQueueID, buffer.MemID, (uint)(blockingWrite ? Bool.TRUE : Bool.FALSE), offset, cb, ptr.ToPointer(), (uint)num_events_in_wait_list, InteropTools.ConvertEventsToEventIDs(event_wait_list), null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueWriteBuffer failed with error code " + result, result); }
public void EnqueueWaitForEvents(int num_events, Event[] _event_list) { OpenCL.EnqueueWaitForEvents(CommandQueueID, (uint)num_events, InteropTools.ConvertEventsToEventIDs(_event_list)); }
public void EnqueueWaitForEvent( Event _event) { Event[] waitList = new Event[] { _event }; EnqueueWaitForEvents(1, waitList); }
public void Perform(float input) { Event e1; filterKernel.SetArg(0, input); filterKernel.SetArg(1, CurrentPos); OpenCLCommandQueue.EnqueueNDRangeKernel(filterKernel, 1, null, filterKernelGlobalWorkSize, filterKernelLocalWorkSize, 1, new Event[] { LastStep }, out e1); //mapToOutputKernel.SetArg(0, CurrentPos); //OpenCLCommandQueue.EnqueueNDRangeKernel(mapToOutputKernel, 1, null, mapToOutputKernelGlobalWorkSize, mapToOutputLocalWorkSize, 1, new Event[] { e1 }, out LastStep); CurrentPos = (CurrentPos + 1) % FilterOrder; LastStep = e1; GC.Collect(); }
public void EnqueueWriteImage(Mem image, bool blockingWrite, long[] origin, long[] region, long input_row_pitch, long input_slice_pitch, IntPtr ptr, int num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; IntPtr* repackedOrigin = stackalloc IntPtr[3]; IntPtr* repackedRegion = stackalloc IntPtr[3]; IntPtr* repackedEvents = stackalloc IntPtr[num_events_in_wait_list]; if (num_events_in_wait_list == 0) repackedEvents = null; InteropTools.A3ToIntPtr3(origin, repackedOrigin); InteropTools.A3ToIntPtr3(region, repackedRegion); InteropTools.ConvertEventsToEventIDs(num_events_in_wait_list, event_wait_list, repackedEvents); result = OpenCL.EnqueueWriteImage(CommandQueueID, image.MemID, (uint)(blockingWrite ? Bool.TRUE : Bool.FALSE), repackedOrigin, repackedRegion, input_row_pitch, input_slice_pitch, ptr, num_events_in_wait_list, repackedEvents, null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueWriteImage failed with error code " + result, result); }
private static int AddCallback(Event _event, EventNotify userMethod, object userData) { int callbackId; CallbackData callbackData = new CallbackData(_event,userMethod,userData); bool gotMutex = false; try { gotMutex = CallbackMutex.WaitOne(); do { callbackId = CallbackId++; } while (CallbackDispatch.ContainsKey(callbackId)); CallbackDispatch.Add(callbackId, callbackData); } finally { if (gotMutex) CallbackMutex.ReleaseMutex(); } return callbackId; }
public void EnqueueCopyBufferRect(Mem src_buffer, Mem dst_buffer, IntPtr[] src_origin, IntPtr[] dst_origin, IntPtr[] region, IntPtr src_row_pitch, IntPtr src_slice_pitch, IntPtr dst_row_pitch, IntPtr dst_slice_pitch, uint num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; result = OpenCL.EnqueueCopyBufferRect(CommandQueueID, src_buffer.MemID, dst_buffer.MemID, src_origin, dst_origin, region, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch, num_events_in_wait_list, InteropTools.ConvertEventsToEventIDs(event_wait_list), null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyBufferRect failed with error code " + result, result); }
/// <summary> /// Block until the event fires /// </summary> /// <param name="_event"></param> public void WaitForEvent(Event _event) { Event[] event_list = new Event[1]; event_list[0] = _event; OpenCL.WaitForEvents(1, InteropTools.ConvertEventsToEventIDs(event_list)); }
public void EnqueueCopyBufferRect(Mem src_buffer, Mem dst_buffer, long[] src_origin, long[] dst_origin, long[] region, long src_row_pitch, long src_slice_pitch, long dst_row_pitch, long dst_slice_pitch, int num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; IntPtr* pSrcOrigin = stackalloc IntPtr[3]; IntPtr* pDstOrigin = stackalloc IntPtr[3]; IntPtr* pRegion = stackalloc IntPtr[3]; IntPtr* pRepackedEvents = stackalloc IntPtr[num_events_in_wait_list]; if (num_events_in_wait_list == 0) pRepackedEvents = null; InteropTools.A3ToIntPtr3(src_origin, pSrcOrigin); InteropTools.A3ToIntPtr3(dst_origin, pDstOrigin); InteropTools.A3ToIntPtr3(region, pRegion); InteropTools.ConvertEventsToEventIDs(num_events_in_wait_list, event_wait_list, pRepackedEvents); result = OpenCL.EnqueueCopyBufferRect(CommandQueueID, src_buffer.MemID, dst_buffer.MemID, pSrcOrigin, pDstOrigin, pRegion, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch, num_events_in_wait_list, pRepackedEvents, null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyBufferRect failed with error code " + result, result); }
/// <summary> /// Test all versions of: /// /// EnqueueReadBuffer /// EnqueueWriteBuffer /// EnqueueCopyBuffer /// /// The test just copies the entirety of a buffer and checks if the result is equal to the original. /// An error indicates that one of the above functions failed and further manual analysis is required /// to pinpoint the error. /// </summary> /// <param name="c"></param> /// <param name="cq"></param> private void TestReadWriteCopyOps(Context c, CommandQueue cq) { Output("Testing read/write/copy functions"); Mem buf0 = null; Mem buf1 = null; Mem buf2 = null; int bufLen = 1024 * 1024; byte[] srcData = new byte[bufLen]; byte[] cmpData = new byte[bufLen]; Event event0; Event event1; Event event2; Event event3; Event event4; Event event5; for (int i = 0; i < srcData.Length; i++) srcData[i] = (byte)(i); Array.Clear(cmpData, 0, cmpData.Length); try { buf0 = c.CreateBuffer(MemFlags.READ_WRITE, bufLen, IntPtr.Zero); buf1 = c.CreateBuffer(MemFlags.READ_WRITE, bufLen, IntPtr.Zero); buf2 = c.CreateBuffer(MemFlags.READ_WRITE, bufLen, IntPtr.Zero); #region Test EnqueueReadBuffer EnqueueWriteBuffer EnqueueCopyBuffer fixed (byte* pSrc = srcData) { fixed (byte* pCmp = cmpData) { { Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteBuffer(buf0, true, (IntPtr)0, (IntPtr)bufLen, (IntPtr)pSrc); cq.EnqueueCopyBuffer(buf0, buf1, (IntPtr)0, (IntPtr)0, (IntPtr)bufLen); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(buf1, true, (IntPtr)0, (IntPtr)bufLen, (IntPtr)pCmp); if (!CompareArray(cmpData, srcData)) Error("TestReadWriteCopyOps(IntPtr version): Copy not identical to source"); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteBuffer(buf0, true, (IntPtr)0, (IntPtr)bufLen, (IntPtr)pSrc, 0, null, out event0 ); cq.EnqueueCopyBuffer(buf0, buf1, (IntPtr)0, (IntPtr)0, (IntPtr)bufLen, 0, null, out event1); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(buf1, true, (IntPtr)0, (IntPtr)bufLen, (IntPtr)pCmp, 0, null, out event2); if (!CompareArray(cmpData, srcData)) Error("TestReadWriteCopyOps(IntPtr version): Copy not identical to source"); Event[] events = new Event[] { event0, event1, event2 }; Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteBuffer(buf0, true, (IntPtr)0, (IntPtr)bufLen, (IntPtr)pSrc, 3, events); cq.EnqueueCopyBuffer(buf0, buf1, (IntPtr)0, (IntPtr)0, (IntPtr)bufLen, 3, events); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(buf1, true, (IntPtr)0, (IntPtr)bufLen, (IntPtr)pCmp, 3, events); if (!CompareArray(cmpData, srcData)) Error("TestReadWriteCopyOps(IntPtr version): Copy not identical to source"); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteBuffer(buf0, true, (IntPtr)0, (IntPtr)bufLen, (IntPtr)pSrc, 3, events, out event3); cq.EnqueueCopyBuffer(buf0, buf1, (IntPtr)0, (IntPtr)0, (IntPtr)bufLen, 3, events, out event4); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(buf1, true, (IntPtr)0, (IntPtr)bufLen, (IntPtr)pCmp, 3, events, out event5); if (!CompareArray(cmpData, srcData)) Error("TestReadWriteCopyOps(IntPtr version): Copy not identical to source"); event0.Dispose(); event1.Dispose(); event2.Dispose(); event3.Dispose(); event4.Dispose(); event5.Dispose(); } { Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteBuffer(buf0, true, 0, bufLen, (IntPtr)pSrc); cq.EnqueueCopyBuffer(buf0, buf1, 0, 0, bufLen); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(buf1, true, 0, bufLen, (IntPtr)pCmp); if (!CompareArray(cmpData, srcData)) Error("TestReadWriteCopyOps(int version): Copy not identical to source"); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteBuffer(buf0, true, 0, bufLen, (IntPtr)pSrc, 0, null, out event0); cq.EnqueueCopyBuffer(buf0, buf1, 0, 0, bufLen, 0, null, out event1); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(buf1, true, 0, bufLen, (IntPtr)pCmp, 0, null, out event2); if (!CompareArray(cmpData, srcData)) Error("TestReadWriteCopyOps(int version): Copy not identical to source"); Event[] events = new Event[] { event0, event1, event2 }; Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteBuffer(buf0, true, 0, bufLen, (IntPtr)pSrc, 3, events); cq.EnqueueCopyBuffer(buf0, buf1, 0, 0, bufLen, 3, events); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(buf1, true, 0, bufLen, (IntPtr)pCmp, 3, events); if (!CompareArray(cmpData, srcData)) Error("TestReadWriteCopyOps(int version): Copy not identical to source"); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteBuffer(buf0, true, 0, bufLen, (IntPtr)pSrc, 3, events, out event3); cq.EnqueueCopyBuffer(buf0, buf1, 0, 0, bufLen, 3, events, out event4); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(buf1, true, 0, bufLen, (IntPtr)pCmp, 3, events, out event5); if (!CompareArray(cmpData, srcData)) Error("TestReadWriteCopyOps(int version): Copy not identical to source"); event0.Dispose(); event1.Dispose(); event2.Dispose(); event3.Dispose(); event4.Dispose(); event5.Dispose(); } { Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteBuffer(buf0, true, 0L, (long)bufLen, (IntPtr)pSrc); cq.EnqueueCopyBuffer(buf0, buf1, 0L, 0L, (long)bufLen); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(buf1, true, 0L, (long)bufLen, (IntPtr)pCmp); if (!CompareArray(cmpData, srcData)) Error("TestReadWriteCopyOps(long version): Copy not identical to source"); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteBuffer(buf0, true, 0L, (long)bufLen, (IntPtr)pSrc, 0, null, out event0); cq.EnqueueCopyBuffer(buf0, buf1, 0L, 0L, (long)bufLen, 0, null, out event1); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(buf1, true, 0L, (long)bufLen, (IntPtr)pCmp, 0, null, out event2); if (!CompareArray(cmpData, srcData)) Error("TestReadWriteCopyOps(long version): Copy not identical to source"); Event[] events = new Event[] { event0, event1, event2 }; Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteBuffer(buf0, true, 0L, (long)bufLen, (IntPtr)pSrc, 3, events); cq.EnqueueCopyBuffer(buf0, buf1, 0L, 0L, (long)bufLen, 3, events); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(buf1, true, 0L, (long)bufLen, (IntPtr)pCmp, 3, events); if (!CompareArray(cmpData, srcData)) Error("TestReadWriteCopyOps(long version): Copy not identical to source"); Array.Clear(cmpData, 0, cmpData.Length); cq.EnqueueWriteBuffer(buf0, true, 0L, (long)bufLen, (IntPtr)pSrc, 3, events, out event3); cq.EnqueueCopyBuffer(buf0, buf1, 0L, 0L, (long)bufLen, 3, events, out event4); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(buf1, true, 0L, (long)bufLen, (IntPtr)pCmp, 3, events, out event5); if (!CompareArray(cmpData, srcData)) Error("TestReadWriteCopyOps(long version): Copy not identical to source"); event0.Dispose(); event1.Dispose(); event2.Dispose(); event3.Dispose(); event4.Dispose(); event5.Dispose(); } } } #endregion } catch (Exception e) { Error("Exception during testing: " + e.ToString()); } finally { if (buf0 != null) buf0.Dispose(); if (buf1 != null) buf1.Dispose(); if (buf2 != null) buf2.Dispose(); } }
public void EnqueueCopyImage(Mem src_image, Mem dst_image, IntPtr[] src_origin, IntPtr[] dst_origin, IntPtr[] region, int num_events_in_wait_list, Event[] event_wait_list, out Event _event) { ErrorCode result; IntPtr tmpEvent; result = OpenCL.EnqueueCopyImage(CommandQueueID, src_image.MemID, dst_image.MemID, src_origin, dst_origin, region, (uint)num_events_in_wait_list, InteropTools.ConvertEventsToEventIDs(event_wait_list), &tmpEvent); _event = new Event(Context, this, tmpEvent); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyImage failed with error code " + result, result); }
/// <summary> /// Test all versions of: /// /// EnqueueCopyImageToBuffer /// EnqueueCopyBufferToImage /// /// The test just copies the entirety of a buffer and checks if the result is equal to the original. /// An error indicates that one of the above functions failed and further manual analysis is required /// to pinpoint the error. /// </summary> /// <param name="c"></param> /// <param name="cq"></param> private void TestTransfersBetweenImageAndBuffers(Context c, CommandQueue cq) { if (!cq.Device.ImageSupport) { Output("Skipping CopyImageToBuffer and CopyBufferToImage tests(not supported on this device)"); return; } Output("Testing CopyImageToBuffer and CopyBufferToImage"); OpenCLNet.Image img0 = null; OpenCLNet.Mem mem0 = null; int imgWidth = 1024; int imgHeight = 1024; int bufLen = imgWidth * 4 * imgHeight; byte[] srcData = new byte[bufLen]; byte[] cmpData = new byte[bufLen]; Event event0; Event event1; Event event2; Event event3; for (int i = 0; i < srcData.Length; i++) srcData[i] = (byte)(i); Array.Clear(cmpData, 0, cmpData.Length); try { img0 = c.CreateImage2D(MemFlags.READ_WRITE, ImageFormat.RGBA8U, imgWidth, imgHeight); mem0 = c.CreateBuffer(MemFlags.READ_WRITE, bufLen, IntPtr.Zero); Array.Clear(cmpData, 0, cmpData.Length); fixed (byte* pSrc = srcData) { fixed (byte* pCmp = cmpData) { { IntPtr[] origin = new IntPtr[3] { (IntPtr)0, (IntPtr)0, (IntPtr)0 }; IntPtr[] region = new IntPtr[3] { (IntPtr)imgWidth, (IntPtr)imgHeight, (IntPtr)1 }; IntPtr[] dstOrigin = new IntPtr[3] { (IntPtr)0, (IntPtr)0, (IntPtr)0 }; IntPtr[] dstRegion = new IntPtr[3] { (IntPtr)imgWidth, (IntPtr)imgHeight, (IntPtr)1 }; Array.Clear(cmpData, 0, cmpData.Length); mem0.Write(cq, 0L, srcData, 0, bufLen); cq.EnqueueBarrier(); cq.EnqueueCopyBufferToImage(mem0, img0, (IntPtr)0, origin, region); cq.EnqueueBarrier(); cq.EnqueueCopyImageToBuffer(img0, mem0, origin, region, (IntPtr)0); cq.EnqueueBarrier(); mem0.Read(cq, 0L, cmpData, 0, bufLen); if (!CompareArray(cmpData, srcData)) Error("EnqueueCopyBufferToImage/EnqueueCopyImageToBuffer: (IntPtr version)Copy not identical to source when using no event args"); Array.Clear(cmpData, 0, cmpData.Length); mem0.Write(cq, 0L, srcData, 0, bufLen); cq.EnqueueBarrier(); cq.EnqueueCopyBufferToImage(mem0, img0, (IntPtr)0, origin, region, 0, null, out event0); cq.EnqueueBarrier(); cq.EnqueueCopyImageToBuffer(img0, mem0, origin, region, (IntPtr)0, 0, null, out event1); cq.EnqueueBarrier(); mem0.Read(cq, 0L, cmpData, 0, bufLen); if (!CompareArray(cmpData, srcData)) Error("EnqueueCopyBufferToImage/EnqueueCopyImageToBuffer: (IntPtr version)Copy not identical to source when using event output and event args"); Array.Clear(cmpData, 0, cmpData.Length); Event[] events = new Event[] { event0, event1 }; mem0.Write(cq, 0L, srcData, 0, bufLen); cq.EnqueueBarrier(); cq.EnqueueCopyBufferToImage(mem0, img0, (IntPtr)0, origin, region, 2, events, out event2); cq.EnqueueBarrier(); cq.EnqueueCopyImageToBuffer(img0, mem0, origin, region, (IntPtr)0, 2, events, out event3); cq.EnqueueBarrier(); mem0.Read(cq, 0L, cmpData, 0, bufLen); if (!CompareArray(cmpData, srcData)) Error("EnqueueCopyBufferToImage/EnqueueCopyImageToBuffer: (IntPtr version)Copy not identical to source when using event output and a wait list"); event0.Dispose(); event1.Dispose(); event2.Dispose(); event3.Dispose(); } { int[] origin = new int[3] { 0, 0, 0 }; int[] region = new int[3] { imgWidth, imgHeight, 1 }; int[] dstOrigin = new int[3] { 0, 0, 0 }; int[] dstRegion = new int[3] { imgWidth, imgHeight, 1 }; Array.Clear(cmpData, 0, cmpData.Length); mem0.Write(cq, 0L, srcData, 0, bufLen); cq.EnqueueBarrier(); cq.EnqueueCopyBufferToImage(mem0, img0, 0, origin, region); cq.EnqueueBarrier(); cq.EnqueueCopyImageToBuffer(img0, mem0, origin, region, 0); cq.EnqueueBarrier(); mem0.Read(cq, 0L, cmpData, 0, bufLen); if (!CompareArray(cmpData, srcData)) Error("EnqueueCopyBufferToImage/EnqueueCopyImageToBuffer: (int version)Copy not identical to source when using no event args"); Array.Clear(cmpData, 0, cmpData.Length); mem0.Write(cq, 0L, srcData, 0, bufLen); cq.EnqueueBarrier(); cq.EnqueueCopyBufferToImage(mem0, img0, 0, origin, region, 0, null, out event0); cq.EnqueueBarrier(); cq.EnqueueCopyImageToBuffer(img0, mem0, origin, region, 0, 0, null, out event1); cq.EnqueueBarrier(); mem0.Read(cq, 0L, cmpData, 0, bufLen); if (!CompareArray(cmpData, srcData)) Error("EnqueueCopyBufferToImage/EnqueueCopyImageToBuffer: (int version)Copy not identical to source when using event output no event args"); Array.Clear(cmpData, 0, cmpData.Length); Event[] events = new Event[] { event0, event1 }; mem0.Write(cq, 0L, srcData, 0, bufLen); cq.EnqueueBarrier(); cq.EnqueueCopyBufferToImage(mem0, img0, 0, origin, region, 2, events, out event2); cq.EnqueueBarrier(); cq.EnqueueCopyImageToBuffer(img0, mem0, origin, region, 0, 2, events, out event3); cq.EnqueueBarrier(); mem0.Read(cq, 0L, cmpData, 0, bufLen); if (!CompareArray(cmpData, srcData)) Error("EnqueueCopyBufferToImage/EnqueueCopyImageToBuffer: (int version)Copy not identical to source when using event output and a wait list"); event0.Dispose(); event1.Dispose(); event2.Dispose(); event3.Dispose(); } { long[] origin = new long[3] { 0, 0, 0 }; long[] region = new long[3] { imgWidth, imgHeight, 1 }; long[] dstOrigin = new long[3] { 0, 0, 0 }; long[] dstRegion = new long[3] { imgWidth, imgHeight, 1 }; Array.Clear(cmpData, 0, cmpData.Length); mem0.Write(cq, 0L, srcData, 0, bufLen); cq.EnqueueBarrier(); cq.EnqueueCopyBufferToImage(mem0, img0, 0L, origin, region); cq.EnqueueBarrier(); cq.EnqueueCopyImageToBuffer(img0, mem0, origin, region, 0L); cq.EnqueueBarrier(); mem0.Read(cq, 0L, cmpData, 0, bufLen); if (!CompareArray(cmpData, srcData)) Error("EnqueueCopyBufferToImage/EnqueueCopyImageToBuffer: (long version)Copy not identical to source when using no event args"); Array.Clear(cmpData, 0, cmpData.Length); mem0.Write(cq, 0L, srcData, 0, bufLen); cq.EnqueueBarrier(); cq.EnqueueCopyBufferToImage(mem0, img0, 0L, origin, region, 0, null, out event0); cq.EnqueueBarrier(); cq.EnqueueCopyImageToBuffer(img0, mem0, origin, region, 0L, 0, null, out event1); cq.EnqueueBarrier(); mem0.Read(cq, 0L, cmpData, 0, bufLen); if (!CompareArray(cmpData, srcData)) Error("EnqueueCopyBufferToImage/EnqueueCopyImageToBuffer: (long version)Copy not identical to source when using event output and no event args"); Array.Clear(cmpData, 0, cmpData.Length); Event[] events = new Event[] { event0, event1 }; mem0.Write(cq, 0L, srcData, 0, bufLen); cq.EnqueueBarrier(); cq.EnqueueCopyBufferToImage(mem0, img0, 0L, origin, region, 2, events, out event2); cq.EnqueueBarrier(); cq.EnqueueCopyImageToBuffer(img0, mem0, origin, region, 0L, 2, events, out event3); cq.EnqueueBarrier(); mem0.Read(cq, 0L, cmpData, 0, bufLen); if (!CompareArray(cmpData, srcData)) Error("EnqueueCopyBufferToImage/EnqueueCopyImageToBuffer: (long version)Copy not identical to source when using event output and a wait list"); event0.Dispose(); event1.Dispose(); event2.Dispose(); event3.Dispose(); } } } } catch (Exception e) { Error("Exception during testing: " + e.ToString()); } finally { if (img0 != null) img0.Dispose(); if (mem0 != null) mem0.Dispose(); } }
public void EnqueueCopyImage(Mem src_image, Mem dst_image, long[] src_origin, long[] dst_origin, long[] region, int num_events_in_wait_list, Event[] event_wait_list, out Event _event) { ErrorCode result; IntPtr tmpEvent; IntPtr* repackedSrcOrigin = stackalloc IntPtr[3]; IntPtr* repackedDstOrigin = stackalloc IntPtr[3]; IntPtr* repackedRegion = stackalloc IntPtr[3]; IntPtr* repackedEvents = stackalloc IntPtr[num_events_in_wait_list]; if (num_events_in_wait_list == 0) repackedEvents = null; InteropTools.A3ToIntPtr3(src_origin, repackedSrcOrigin); InteropTools.A3ToIntPtr3(dst_origin, repackedDstOrigin); InteropTools.A3ToIntPtr3(region, repackedRegion); InteropTools.ConvertEventsToEventIDs(num_events_in_wait_list, event_wait_list, repackedEvents); result = OpenCL.EnqueueCopyImage(CommandQueueID, src_image, dst_image, repackedSrcOrigin, repackedDstOrigin, repackedRegion, num_events_in_wait_list, repackedEvents, &tmpEvent); _event = new Event(Context, this, tmpEvent); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyImage failed with error code " + result, result); }
/// <summary> /// Test all versions of: /// /// EnqueueNDRangeKernel /// /// The tests just issue a dummy kernel a bunch of times with the various overloads /// </summary> /// <param name="c"></param> /// <param name="cq"></param> private void TestEnqueueNDRangeKernel(Context c, CommandQueue cq, Kernel k ) { Output("Testing EnqueueNDRangeKernel"); Event event0 = null; Event event1 = null; try { { IntPtr[] globalWorkSize = new IntPtr[] { (IntPtr)10 }; IntPtr[] localWorkSize = new IntPtr[] { (IntPtr)1 }; cq.EnqueueNDRangeKernel(k, (uint)1, null, globalWorkSize, localWorkSize); cq.EnqueueNDRangeKernel(k, (uint)1, null, globalWorkSize, localWorkSize, 0, null, out event0); Event[] waitList = new Event[] { event0 }; cq.EnqueueNDRangeKernel(k, (uint)1, null, globalWorkSize, localWorkSize, 1, waitList, out event1); cq.Finish(); event0.Dispose(); event1.Dispose(); } { int[] globalWorkSize = new int[] { (int)10 }; int[] localWorkSize = new int[] { (int)1 }; cq.EnqueueNDRangeKernel(k, 1, null, globalWorkSize, localWorkSize); cq.EnqueueNDRangeKernel(k, 1, null, globalWorkSize, localWorkSize, 0, null, out event0); Event[] waitList = new Event[] { event0 }; cq.EnqueueNDRangeKernel(k, 1, null, globalWorkSize, localWorkSize, 1, waitList, out event1); cq.Finish(); event0.Dispose(); event1.Dispose(); } { long[] globalWorkSize = new long[] { (long)10 }; long[] localWorkSize = new long[] { (long)1 }; cq.EnqueueNDRangeKernel(k, 1, null, globalWorkSize, localWorkSize); cq.EnqueueNDRangeKernel(k, 1, null, globalWorkSize, localWorkSize, 0, null, out event0); Event[] waitList = new Event[] { event0 }; cq.EnqueueNDRangeKernel(k, 1, null, globalWorkSize, localWorkSize, 1, waitList, out event1); cq.Finish(); event0.Dispose(); event1.Dispose(); } } catch (Exception e) { Error("Exception during testing: " + e.ToString()); } finally { if (event0 != null) event0.Dispose(); if (event1 != null) event1.Dispose(); } }
public void EnqueueCopyImageToBuffer(Mem src_image, Mem dst_buffer, IntPtr[] src_origin, IntPtr[] region, IntPtr dst_offset, int num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; result = OpenCL.EnqueueCopyImageToBuffer(CommandQueueID, src_image.MemID, dst_buffer.MemID, src_origin, region, dst_offset, (uint)num_events_in_wait_list, InteropTools.ConvertEventsToEventIDs(event_wait_list), null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyImageToBuffer failed with error code " + result, result); }
/// <summary> /// Test all versions of: /// /// EnqueueMapBuffer /// EnqueueMapImage /// /// The test bounces an array from a managed byte buffer to a mapped buffer, /// to an image. The image is then mapped and copied to a new managed buffer /// where the result is compared to the original. /// /// On error, the actual point of failure will have to be identified manually. /// </summary> /// <param name="c"></param> /// <param name="cq"></param> private void TestMapBuffer(Context c, CommandQueue cq) { if (!cq.Device.ImageSupport) { Output("Skipping EnqueueMapBuffer and EnqueueMapImage tests(not supported on this device)"); return; } Output("Testing MapBuffer"); OpenCLNet.Image img0 = null; OpenCLNet.Mem mem0 = null; int imgWidth = 1024; int imgHeight = 1024; int bufLen = imgWidth * 4 * imgHeight; byte[] srcData = new byte[bufLen]; byte[] cmpData = new byte[bufLen]; Event event0; Event event1; for (int i = 0; i < srcData.Length; i++) srcData[i] = (byte)(i); Array.Clear(cmpData, 0, cmpData.Length); try { img0 = c.CreateImage2D(MemFlags.READ_WRITE, ImageFormat.RGBA8U, imgWidth, imgHeight); mem0 = c.CreateBuffer(MemFlags.READ_WRITE, bufLen, IntPtr.Zero); Array.Clear(cmpData, 0, cmpData.Length); fixed (byte* pSrc = srcData) { fixed (byte* pCmp = cmpData) { { IntPtr[] origin = new IntPtr[3] { (IntPtr)0, (IntPtr)0, (IntPtr)0 }; IntPtr[] region = new IntPtr[3] { (IntPtr)imgWidth, (IntPtr)imgHeight, (IntPtr)1 }; IntPtr[] dstOrigin = new IntPtr[3] { (IntPtr)0, (IntPtr)0, (IntPtr)0 }; IntPtr[] dstRegion = new IntPtr[3] { (IntPtr)imgWidth, (IntPtr)imgHeight, (IntPtr)1 }; IntPtr mapPtr; byte* pMapPtr; IntPtr image_row_pitch; IntPtr image_slice_pitch; Array.Clear(cmpData, 0, cmpData.Length); mapPtr = cq.EnqueueMapBuffer(mem0, true, MapFlags.WRITE, 0, bufLen); pMapPtr = (byte*)mapPtr.ToPointer(); for (int i = 0; i < bufLen; i++) pMapPtr[i] = srcData[i]; cq.EnqueueUnmapMemObject(mem0, mapPtr); cq.EnqueueCopyBufferToImage(mem0, img0, (IntPtr)0, origin, region); mapPtr = cq.EnqueueMapImage(img0, true, MapFlags.READ, origin, region, out image_row_pitch, out image_slice_pitch); pMapPtr = (byte*)mapPtr.ToPointer(); for (int y = 0; y < imgHeight; y++) { byte* pSrcRowPtr = pMapPtr + y * (int)image_row_pitch; byte* pDstRowPtr = pCmp + y*imgWidth*4; for (int x = 0; x < imgWidth * 4; x++) { pDstRowPtr[x] = pSrcRowPtr[x]; } } cq.EnqueueUnmapMemObject(img0, mapPtr); if (!CompareArray(cmpData, srcData)) Error("EnqueueEnqueueMapBuffer/EnqueueMapImage: (IntPtr version)Copy not identical to source when using no event args"); Array.Clear(cmpData, 0, cmpData.Length); mapPtr = cq.EnqueueMapBuffer(mem0, true, MapFlags.WRITE, 0, bufLen); pMapPtr = (byte*)mapPtr.ToPointer(); for (int i = 0; i < bufLen; i++) pMapPtr[i] = srcData[i]; cq.EnqueueUnmapMemObject(mem0, mapPtr); Event fdjk; cq.EnqueueCopyBufferToImage(mem0, img0, (IntPtr)0, origin, region, 0, null, out fdjk); cq.Finish(); mapPtr = cq.EnqueueMapImage(img0, false, MapFlags.READ, origin, region, out image_row_pitch, out image_slice_pitch, 0, null, out event0 ); cq.EnqueueWaitForEvent(event0); cq.Finish(); pMapPtr = (byte*)mapPtr.ToPointer(); for (int y = 0; y < imgHeight; y++) { byte* pSrcRowPtr = pMapPtr + y * (int)image_row_pitch; byte* pDstRowPtr = pCmp + y * imgWidth * 4; for (int x = 0; x < imgWidth * 4; x++) { pDstRowPtr[x] = pSrcRowPtr[x]; } } cq.EnqueueUnmapMemObject(img0, mapPtr); if (!CompareArray(cmpData, srcData)) Error("EnqueueEnqueueMapBuffer/EnqueueMapImage: (IntPtr version)Copy not identical to source when using event output and no wait list"); Event[] waitList = new Event[] { event0 }; Array.Clear(cmpData, 0, cmpData.Length); mapPtr = cq.EnqueueMapBuffer(mem0, true, MapFlags.WRITE, 0, bufLen); pMapPtr = (byte*)mapPtr.ToPointer(); for (int i = 0; i < bufLen; i++) pMapPtr[i] = srcData[i]; cq.EnqueueUnmapMemObject(mem0, mapPtr); cq.EnqueueCopyBufferToImage(mem0, img0, (IntPtr)0, origin, region); mapPtr = cq.EnqueueMapImage(img0, false, MapFlags.READ, origin, region, out image_row_pitch, out image_slice_pitch, 1, waitList, out event1); cq.EnqueueWaitForEvent(event1); cq.Finish(); pMapPtr = (byte*)mapPtr.ToPointer(); for (int y = 0; y < imgHeight; y++) { byte* pSrcRowPtr = pMapPtr + y * (int)image_row_pitch; byte* pDstRowPtr = pCmp + y * imgWidth * 4; for (int x = 0; x < imgWidth * 4; x++) { pDstRowPtr[x] = pSrcRowPtr[x]; } } cq.EnqueueUnmapMemObject(img0, mapPtr); if (!CompareArray(cmpData, srcData)) Error("EnqueueEnqueueMapBuffer/EnqueueMapImage: (IntPtr version)Copy not identical to source when using event output and no wait list"); event0.Dispose(); event1.Dispose(); } { int[] origin = new int[3] { (int)0, (int)0, (int)0 }; int[] region = new int[3] { (int)imgWidth, (int)imgHeight, (int)1 }; IntPtr mapPtr; byte* pMapPtr; int image_row_pitch; int image_slice_pitch; Array.Clear(cmpData, 0, cmpData.Length); mapPtr = cq.EnqueueMapBuffer(mem0, true, MapFlags.WRITE, 0, bufLen); pMapPtr = (byte*)mapPtr.ToPointer(); for (int i = 0; i < bufLen; i++) pMapPtr[i] = srcData[i]; cq.EnqueueUnmapMemObject(mem0, mapPtr); cq.EnqueueCopyBufferToImage(mem0, img0, 0, origin, region); mapPtr = cq.EnqueueMapImage(img0, true, MapFlags.READ, origin, region, out image_row_pitch, out image_slice_pitch); pMapPtr = (byte*)mapPtr.ToPointer(); for (int y = 0; y < imgHeight; y++) { byte* pSrcRowPtr = pMapPtr + y * (int)image_row_pitch; byte* pDstRowPtr = pCmp + y*imgWidth*4; for (int x = 0; x < imgWidth * 4; x++) { pDstRowPtr[x] = pSrcRowPtr[x]; } } cq.EnqueueUnmapMemObject(img0, mapPtr); if (!CompareArray(cmpData, srcData)) Error("EnqueueEnqueueMapBuffer/EnqueueMapImage: (int version)Copy not identical to source when using no event args"); Array.Clear(cmpData, 0, cmpData.Length); mapPtr = cq.EnqueueMapBuffer(mem0, true, MapFlags.WRITE, 0, bufLen); pMapPtr = (byte*)mapPtr.ToPointer(); for (int i = 0; i < bufLen; i++) pMapPtr[i] = srcData[i]; cq.EnqueueUnmapMemObject(mem0, mapPtr); cq.EnqueueCopyBufferToImage(mem0, img0, 0, origin, region); mapPtr = cq.EnqueueMapImage(img0, false, MapFlags.READ, origin, region, out image_row_pitch, out image_slice_pitch, 0, null, out event0 ); cq.EnqueueWaitForEvent(event0); cq.Finish(); pMapPtr = (byte*)mapPtr.ToPointer(); for (int y = 0; y < imgHeight; y++) { byte* pSrcRowPtr = pMapPtr + y * (int)image_row_pitch; byte* pDstRowPtr = pCmp + y * imgWidth * 4; for (int x = 0; x < imgWidth * 4; x++) { pDstRowPtr[x] = pSrcRowPtr[x]; } } cq.EnqueueUnmapMemObject(img0, mapPtr); if (!CompareArray(cmpData, srcData)) Error("EnqueueEnqueueMapBuffer/EnqueueMapImage: (int version)Copy not identical to source when using event output and no wait list"); Event[] waitList = new Event[] { event0 }; Array.Clear(cmpData, 0, cmpData.Length); mapPtr = cq.EnqueueMapBuffer(mem0, true, MapFlags.WRITE, 0, bufLen); pMapPtr = (byte*)mapPtr.ToPointer(); for (int i = 0; i < bufLen; i++) pMapPtr[i] = srcData[i]; cq.EnqueueUnmapMemObject(mem0, mapPtr); cq.EnqueueCopyBufferToImage(mem0, img0, 0, origin, region); mapPtr = cq.EnqueueMapImage(img0, false, MapFlags.READ, origin, region, out image_row_pitch, out image_slice_pitch, 1, waitList, out event1); cq.EnqueueWaitForEvent(event1); cq.Finish(); pMapPtr = (byte*)mapPtr.ToPointer(); for (int y = 0; y < imgHeight; y++) { byte* pSrcRowPtr = pMapPtr + y * (int)image_row_pitch; byte* pDstRowPtr = pCmp + y * imgWidth * 4; for (int x = 0; x < imgWidth * 4; x++) { pDstRowPtr[x] = pSrcRowPtr[x]; } } cq.EnqueueUnmapMemObject(img0, mapPtr); if (!CompareArray(cmpData, srcData)) Error("EnqueueEnqueueMapBuffer/EnqueueMapImage: (int version)Copy not identical to source when using event output and no wait list"); event0.Dispose(); event1.Dispose(); } { long[] origin = new long[3] { (long)0, (long)0, (long)0 }; long[] region = new long[3] { (long)imgWidth, (long)imgHeight, (long)1 }; IntPtr mapPtr; byte* pMapPtr; long image_row_pitch; long image_slice_pitch; Array.Clear(cmpData, 0, cmpData.Length); mapPtr = cq.EnqueueMapBuffer(mem0, true, MapFlags.WRITE, 0, bufLen); pMapPtr = (byte*)mapPtr.ToPointer(); for (int i = 0; i < bufLen; i++) pMapPtr[i] = srcData[i]; cq.EnqueueUnmapMemObject(mem0, mapPtr); cq.EnqueueCopyBufferToImage(mem0, img0, (long)0, origin, region); mapPtr = cq.EnqueueMapImage(img0, true, MapFlags.READ, origin, region, out image_row_pitch, out image_slice_pitch); pMapPtr = (byte*)mapPtr.ToPointer(); for (int y = 0; y < imgHeight; y++) { byte* pSrcRowPtr = pMapPtr + y * image_row_pitch; byte* pDstRowPtr = pCmp + y * imgWidth * 4; for (int x = 0; x < imgWidth * 4; x++) { pDstRowPtr[x] = pSrcRowPtr[x]; } } cq.EnqueueUnmapMemObject(img0, mapPtr); if (!CompareArray(cmpData, srcData)) Error("EnqueueEnqueueMapBuffer/EnqueueMapImage: (long version)Copy not identical to source when using no event args"); Array.Clear(cmpData, 0, cmpData.Length); mapPtr = cq.EnqueueMapBuffer(mem0, true, MapFlags.WRITE, 0, bufLen); pMapPtr = (byte*)mapPtr.ToPointer(); for (int i = 0; i < bufLen; i++) pMapPtr[i] = srcData[i]; cq.EnqueueUnmapMemObject(mem0, mapPtr); cq.EnqueueCopyBufferToImage(mem0, img0, (long)0, origin, region); mapPtr = cq.EnqueueMapImage(img0, false, MapFlags.READ, origin, region, out image_row_pitch, out image_slice_pitch, 0, null, out event0); cq.EnqueueWaitForEvent(event0); cq.Finish(); pMapPtr = (byte*)mapPtr.ToPointer(); for (int y = 0; y < imgHeight; y++) { byte* pSrcRowPtr = pMapPtr + y * image_row_pitch; byte* pDstRowPtr = pCmp + y * imgWidth * 4; for (int x = 0; x < imgWidth * 4; x++) { pDstRowPtr[x] = pSrcRowPtr[x]; } } cq.EnqueueUnmapMemObject(img0, mapPtr); if (!CompareArray(cmpData, srcData)) Error("EnqueueEnqueueMapBuffer/EnqueueMapImage: (long version)Copy not identical to source when using event output and no wait list"); Event[] waitList = new Event[] { event0 }; Array.Clear(cmpData, 0, cmpData.Length); mapPtr = cq.EnqueueMapBuffer(mem0, true, MapFlags.WRITE, 0, bufLen); pMapPtr = (byte*)mapPtr.ToPointer(); for (int i = 0; i < bufLen; i++) pMapPtr[i] = srcData[i]; cq.EnqueueUnmapMemObject(mem0, mapPtr); cq.EnqueueCopyBufferToImage(mem0, img0, (long)0, origin, region); mapPtr = cq.EnqueueMapImage(img0, false, MapFlags.READ, origin, region, out image_row_pitch, out image_slice_pitch, 1, waitList, out event1); cq.EnqueueWaitForEvent(event1); cq.Finish(); pMapPtr = (byte*)mapPtr.ToPointer(); for (int y = 0; y < imgHeight; y++) { byte* pSrcRowPtr = pMapPtr + y * image_row_pitch; byte* pDstRowPtr = pCmp + y * imgWidth * 4; for (int x = 0; x < imgWidth * 4; x++) { pDstRowPtr[x] = pSrcRowPtr[x]; } } cq.EnqueueUnmapMemObject(img0, mapPtr); if (!CompareArray(cmpData, srcData)) Error("EnqueueEnqueueMapBuffer/EnqueueMapImage: (long version)Copy not identical to source when using event output and no wait list"); event0.Dispose(); event1.Dispose(); } } } } catch (Exception e) { Error("Exception during testing: " + e.ToString()); } finally { if (img0 != null) img0.Dispose(); if (mem0 != null) mem0.Dispose(); } }
public void EnqueueCopyImageToBuffer(Mem src_image, Mem dst_buffer, long[] src_origin, long[] region, long dst_offset, int num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; IntPtr* repackedSrcOrigin = stackalloc IntPtr[3]; IntPtr* repackedRegion = stackalloc IntPtr[3]; IntPtr* repackedEvents = stackalloc IntPtr[num_events_in_wait_list]; if (num_events_in_wait_list == 0) repackedEvents = null; InteropTools.A3ToIntPtr3(src_origin, repackedSrcOrigin); InteropTools.A3ToIntPtr3(region, repackedRegion); InteropTools.ConvertEventsToEventIDs(num_events_in_wait_list, event_wait_list, repackedEvents); result = OpenCL.EnqueueCopyImageToBuffer(CommandQueueID, src_image, dst_buffer, repackedSrcOrigin, repackedRegion, dst_offset, num_events_in_wait_list, repackedEvents, null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyImageToBuffer failed with error code " + result, result); }
public static unsafe void ConvertEventsToEventIDs(int num, Event[] events, IntPtr* pHandles) { if (events == null) return; if (num > events.Length) throw new ArgumentOutOfRangeException(); for (int i = 0; i < num; i++) pHandles[i] = events[i].EventID; }
/// <summary> /// Execute a simple kernel /// </summary> /// <param name="kernel"></param> /// <param name="numEventsInWaitList"></param> /// <param name="event_wait_list"></param> public void EnqueueTask(Kernel kernel, int numEventsInWaitList, Event[] event_wait_list) { ErrorCode result; result = (ErrorCode)OpenCL.EnqueueTask(CommandQueueID, kernel.KernelID, (uint)numEventsInWaitList, InteropTools.ConvertEventsToEventIDs(event_wait_list), null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueTask failed with error code " + result, result); }