public Mandelbrot( Platform platform, int width, int height ) { openCLPlatform = platform; openCLDevices = openCLPlatform.QueryDevices(DeviceType.ALL); openCLContext = openCLPlatform.CreateDefaultContext(); openCLCQ = openCLContext.CreateCommandQueue(openCLDevices[0], CommandQueueProperties.PROFILING_ENABLE); mandelBrotProgram = openCLContext.CreateProgramWithSource(File.ReadAllText("Mandelbrot.cl")); try { mandelBrotProgram.Build(); } catch (OpenCLException) { string buildLog = mandelBrotProgram.GetBuildLog(openCLDevices[0]); MessageBox.Show(buildLog,"Build error(64 bit debug sessions in vs2008 always fail like this - debug in 32 bit or use vs2010)"); Application.Exit(); } mandelbrotKernel = mandelBrotProgram.CreateKernel("Mandelbrot"); Left = -2.0f; Top = 2.0f; Right = 2.0f; Bottom = -2.0f; BitmapWidth = width; BitmapHeight = height; mandelbrotMemBuffer = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.WRITE_ONLY), width*height*4, IntPtr.Zero); }
internal NativeKernelCallbackData(NativeKernel nk,CommandQueue cq, object o, Mem[] buffers) { NativeKernel = nk; CQ = cq; O = o; Buffers = buffers; }
// Dispose(bool disposing) executes in two distinct scenarios. // If disposing equals true, the method has been called directly // or indirectly by a user's code. Managed and unmanaged resources // can be disposed. // If disposing equals false, the method has been called by the // runtime from inside the finalizer and you should not reference // other objects. Only unmanaged resources can be disposed. private void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this.disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. Bitmap.Dispose(); Bitmap = null; Image.Dispose(); Image = null; CQ = null; Context = null; } // Call the appropriate methods to clean up // unmanaged resources here. // If disposing is false, // only the following code is executed. // Note disposing has been done. disposed = true; } }
public void ReleaseDeviceResources() { oclFullyInitialized = false; if (OCLSampler != null) { OCLSampler.Dispose(); OCLSampler = null; } if (OCLInputImage != null) { OCLInputImage.Dispose(); OCLInputImage = null; } if (OCLOutputImage != null) { OCLOutputImage.Dispose(); OCLOutputImage = null; } if (FilterKernel != null) { FilterKernel.Dispose(); FilterKernel = null; } if (oclProgram != null) { oclProgram.Dispose(); oclProgram = null; } if (oclCQ != null) { oclCQ.Dispose(); oclCQ = null; } if (oclContext != null) { oclContext.Dispose(); oclContext = null; } }
public virtual void Write(CommandQueue cq, long dstOffset, double[] srcData, int srcStartIndex, int count) { IntPtr p = cq.EnqueueMapBuffer(this, true, MapFlags.WRITE, dstOffset, (long)count * sizeof(double)); double* pBlock = (double*)p.ToPointer(); for (long i = 0; i < count; i++) pBlock[i] = srcData[i + srcStartIndex]; cq.EnqueueUnmapMemObject(this, p); cq.Finish(); }
public virtual void MemSet(CommandQueue cq, byte value) { long offset = 0; long count = MemSize.ToInt64(); IntPtr p = cq.EnqueueMapBuffer(this, true, MapFlags.WRITE, offset, count); byte* pBlock = (byte*)p.ToPointer(); for (long i = 0; i < count; i++) pBlock[i] = value; cq.EnqueueUnmapMemObject(this, p); cq.Finish(); }
/// <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(); } }
/// <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(); } }
private void TestMem(Context c, CommandQueue cq, Dictionary<string, Kernel> kernelDictionary) { int size = 8192; byte[] testData = new byte[size]; Output( "Testing Mem class" ); //Output( "Allocating "+size+" bytes of READ_WRITE memory" ); using (Mem buffer = c.CreateBuffer(MemFlags.READ_WRITE, size, IntPtr.Zero)) { for (int i = 0; i < size / 2; i++) { testData[i] = 0; testData[size/2+i] = 1; } //Output("Mem.MemSize=" + size); if (buffer.MemSize.ToInt64() != size) Error("Mem.Size!=input size"); //Output("Mem.MemType=" + buffer.MemType); if (buffer.MemType != MemObjectType.BUFFER) Error("Mem.MemType!=MemObjectType.BUFFER"); //Output("Mem.MapCount=" + buffer.MapCount); if (buffer.MapCount != 0) Error("Mem.MapCount!=0"); buffer.Write(cq, 0L, testData, 0, size); Kernel k = kernelDictionary["TestReadWriteMemory"]; k.SetArg(0, buffer); k.SetArg(1, (long)size); Event bleh; cq.EnqueueTask(k,0,null,out bleh); cq.EnqueueBarrier(); cq.Finish(); buffer.Read(cq, 0L, testData, 0, size); for (int i = 0; i < size / 2; i++) { if (testData[i] != 1) { Error("TestReadWriteMemory failed"); break; } if( testData[size / 2 + i] != 0 ) { Error("TestReadWriteMemory failed"); break; } } } //Output("Allocating " + size + " bytes of READ memory"); using (Mem buffer = c.CreateBuffer(MemFlags.READ_ONLY, size, IntPtr.Zero)) { //Output("Mem.MemSize=" + size); if (buffer.MemSize.ToInt64() != size) Error("Mem.Size!=input size"); //Output("Mem.MemType=" + buffer.MemType); if (buffer.MemType != MemObjectType.BUFFER) Error("Mem.MemType!=MemObjectType.BUFFER"); //Output("Mem.MapCount=" + buffer.MapCount); if (buffer.MapCount != 0) Error("Mem.MapCount!=0"); Kernel k = kernelDictionary["TestReadMemory"]; k.SetArg(0, buffer); k.SetArg(1, (long)size); cq.EnqueueTask(k); cq.Finish(); } //Output("Allocating " + size + " bytes of WRITE memory"); using (Mem buffer = c.CreateBuffer(MemFlags.WRITE_ONLY, size, IntPtr.Zero)) { Array.Clear(testData, 0, size); //Output("Mem.MemSize=" + size); if (buffer.MemSize.ToInt64() != size) Error("Mem.Size!=input size"); //Output("Mem.MemType=" + buffer.MemType); if (buffer.MemType != MemObjectType.BUFFER) Error("Mem.MemType!=MemObjectType.BUFFER"); //Output("Mem.MapCount=" + buffer.MapCount); if (buffer.MapCount != 0) Error("Mem.MapCount!=0"); Kernel k = kernelDictionary["TestWriteMemory"]; k.SetArg(0, buffer); k.SetArg(1, (long)size); cq.EnqueueTask(k); cq.Finish(); buffer.Read(cq, 0L, testData, 0, size); for (int i = 0; i < size; i++) { if (testData[i] != 1) { Error("TestWriteMemory failed"); break; } } } TestReadWriteCopyOps(c, cq); TestImageReadWriteCopyOps(c, cq); TestTransfersBetweenImageAndBuffers(c, cq); TestMapBuffer(c, cq); TestEnqueueNDRangeKernel(c, cq, kernelDictionary["EmptyKernel"]); TestBufferRectFunctions(c, cq); }
internal Event( Context context, CommandQueue cq, IntPtr eventID ) { Context = context; CommandQueue = cq; EventID = eventID; }
unsafe public FLACCLTask(Program _openCLProgram, int channelsCount, int channels, uint bits_per_sample, int max_frame_size, FLACCLWriter writer, int groupSize, bool gpuOnly, bool gpuRice) { this.UseGPUOnly = gpuOnly; this.UseGPURice = gpuOnly && gpuRice; this.UseMappedMemory = writer._settings.MappedMemory || writer._settings.DeviceType == OpenCLDeviceType.CPU; this.groupSize = groupSize; this.channels = channels; this.channelsCount = channelsCount; this.writer = writer; openCLProgram = _openCLProgram; #if DEBUG var prop = CommandQueueProperties.PROFILING_ENABLE; #else var prop = CommandQueueProperties.NONE; #endif openCLCQ = openCLProgram.Context.CreateCommandQueue(openCLProgram.Context.Devices[0], prop); int MAX_ORDER = this.writer.eparams.max_prediction_order; int MAX_FRAMES = this.writer.framesPerTask; int MAX_CHANNELSIZE = MAX_FRAMES * ((writer.eparams.block_size + 3) & ~3); residualTasksLen = sizeof(FLACCLSubframeTask) * 32 * channelsCount * MAX_FRAMES; bestResidualTasksLen = sizeof(FLACCLSubframeTask) * channels * MAX_FRAMES; int samplesBufferLen = writer.PCM.BlockAlign * MAX_CHANNELSIZE * channelsCount; int residualBufferLen = sizeof(int) * MAX_CHANNELSIZE * channels; // need to adjust residualOffset? int partitionsLen = sizeof(int) * ((writer.PCM.BitsPerSample > 16 ? 31 : 15) * 2 << 8) * channels * MAX_FRAMES; int riceParamsLen = sizeof(int) * (4 << 8) * channels * MAX_FRAMES; int autocorLen = sizeof(float) * (MAX_ORDER + 1) * lpc.MAX_LPC_WINDOWS * channelsCount * MAX_FRAMES; int lpcDataLen = autocorLen * 32; int resOutLen = sizeof(int) * channelsCount * (lpc.MAX_LPC_WINDOWS * lpc.MAX_LPC_ORDER + 8) * MAX_FRAMES; int wndLen = sizeof(float) * MAX_CHANNELSIZE /** 2*/ * lpc.MAX_LPC_WINDOWS; int selectedLen = sizeof(int) * 32 * channelsCount * MAX_FRAMES; int riceLen = sizeof(int) * channels * MAX_CHANNELSIZE; if (!this.UseMappedMemory) { clSamplesBytes = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, samplesBufferLen / 2); clResidual = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, residualBufferLen); clBestRiceParams = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, riceParamsLen / 4); clResidualTasks = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, residualTasksLen); clBestResidualTasks = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, bestResidualTasksLen); clWindowFunctions = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, wndLen); clSelectedTasks = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, selectedLen); clRiceOutput = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, riceLen); clSamplesBytesPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, samplesBufferLen / 2); clResidualPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, residualBufferLen); clBestRiceParamsPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, riceParamsLen / 4); clResidualTasksPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, residualTasksLen); clBestResidualTasksPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, bestResidualTasksLen); clWindowFunctionsPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, wndLen); clSelectedTasksPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, selectedLen); clRiceOutputPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, riceLen); clSamplesBytesPtr = openCLCQ.EnqueueMapBuffer(clSamplesBytesPinned, true, MapFlags.READ_WRITE, 0, samplesBufferLen / 2); clResidualPtr = openCLCQ.EnqueueMapBuffer(clResidualPinned, true, MapFlags.READ_WRITE, 0, residualBufferLen); clBestRiceParamsPtr = openCLCQ.EnqueueMapBuffer(clBestRiceParamsPinned, true, MapFlags.READ_WRITE, 0, riceParamsLen / 4); clResidualTasksPtr = openCLCQ.EnqueueMapBuffer(clResidualTasksPinned, true, MapFlags.READ_WRITE, 0, residualTasksLen); clBestResidualTasksPtr = openCLCQ.EnqueueMapBuffer(clBestResidualTasksPinned, true, MapFlags.READ_WRITE, 0, bestResidualTasksLen); clWindowFunctionsPtr = openCLCQ.EnqueueMapBuffer(clWindowFunctionsPinned, true, MapFlags.READ_WRITE, 0, wndLen); clSelectedTasksPtr = openCLCQ.EnqueueMapBuffer(clSelectedTasksPinned, true, MapFlags.READ_WRITE, 0, selectedLen); clRiceOutputPtr = openCLCQ.EnqueueMapBuffer(clRiceOutputPinned, true, MapFlags.READ_WRITE, 0, riceLen); } else { clSamplesBytes = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, (uint)samplesBufferLen / 2); clResidual = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, residualBufferLen); clBestRiceParams = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, riceParamsLen / 4); clResidualTasks = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, residualTasksLen); clBestResidualTasks = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, bestResidualTasksLen); clWindowFunctions = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, wndLen); clSelectedTasks = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, selectedLen); clRiceOutput = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, riceLen); clSamplesBytesPtr = openCLCQ.EnqueueMapBuffer(clSamplesBytes, true, MapFlags.READ_WRITE, 0, samplesBufferLen / 2); clResidualPtr = openCLCQ.EnqueueMapBuffer(clResidual, true, MapFlags.READ_WRITE, 0, residualBufferLen); clBestRiceParamsPtr = openCLCQ.EnqueueMapBuffer(clBestRiceParams, true, MapFlags.READ_WRITE, 0, riceParamsLen / 4); clResidualTasksPtr = openCLCQ.EnqueueMapBuffer(clResidualTasks, true, MapFlags.READ_WRITE, 0, residualTasksLen); clBestResidualTasksPtr = openCLCQ.EnqueueMapBuffer(clBestResidualTasks, true, MapFlags.READ_WRITE, 0, bestResidualTasksLen); clWindowFunctionsPtr = openCLCQ.EnqueueMapBuffer(clWindowFunctions, true, MapFlags.READ_WRITE, 0, wndLen); clSelectedTasksPtr = openCLCQ.EnqueueMapBuffer(clSelectedTasks, true, MapFlags.READ_WRITE, 0, selectedLen); clRiceOutputPtr = openCLCQ.EnqueueMapBuffer(clRiceOutput, true, MapFlags.READ_WRITE, 0, riceLen); //clSamplesBytesPtr = clSamplesBytes.HostPtr; //clResidualPtr = clResidual.HostPtr; //clBestRiceParamsPtr = clBestRiceParams.HostPtr; //clResidualTasksPtr = clResidualTasks.HostPtr; //clBestResidualTasksPtr = clBestResidualTasks.HostPtr; //clWindowFunctionsPtr = clWindowFunctions.HostPtr; } clSamples = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, samplesBufferLen); clLPCData = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, lpcDataLen); clAutocorOutput = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, autocorLen); clSelectedTasksSecondEstimate = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, selectedLen); clSelectedTasksBestMethod = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, selectedLen); if (UseGPUOnly) { clPartitions = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, partitionsLen); clRiceParams = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, riceParamsLen); } //openCLCQ.EnqueueMapBuffer(clSamplesBytes, true, MapFlags.WRITE, 0, samplesBufferLen / 2); clComputeAutocor = openCLProgram.CreateKernel("clComputeAutocor"); clStereoDecorr = openCLProgram.CreateKernel("clStereoDecorr"); //cudaChannelDecorr = openCLProgram.CreateKernel("clChannelDecorr"); clChannelDecorr2 = openCLProgram.CreateKernel("clChannelDecorr2"); clChannelDecorrX = openCLProgram.CreateKernel("clChannelDecorrX"); clFindWastedBits = openCLProgram.CreateKernel("clFindWastedBits"); clComputeLPC = openCLProgram.CreateKernel("clComputeLPC"); clQuantizeLPC = openCLProgram.CreateKernel("clQuantizeLPC"); //cudaComputeLPCLattice = openCLProgram.CreateKernel("clComputeLPCLattice"); clSelectStereoTasks = openCLProgram.CreateKernel("clSelectStereoTasks"); clEstimateResidual = openCLProgram.CreateKernel("clEstimateResidual"); clChooseBestMethod = openCLProgram.CreateKernel("clChooseBestMethod"); if (UseGPUOnly) { clEncodeResidual = openCLProgram.CreateKernel("clEncodeResidual"); if (openCLCQ.Device.DeviceType != DeviceType.CPU) { clCalcPartition = openCLProgram.CreateKernel("clCalcPartition"); clCalcPartition16 = openCLProgram.CreateKernel("clCalcPartition16"); } clSumPartition = openCLProgram.CreateKernel("clSumPartition"); clFindRiceParameter = openCLProgram.CreateKernel("clFindRiceParameter"); clFindPartitionOrder = openCLProgram.CreateKernel("clFindPartitionOrder"); if (UseGPURice) { clCalcOutputOffsets = openCLProgram.CreateKernel("clCalcOutputOffsets"); clRiceEncoding = openCLProgram.CreateKernel("clRiceEncoding"); } } samplesBuffer = new int[MAX_CHANNELSIZE * channelsCount]; outputBuffer = new byte[max_frame_size * MAX_FRAMES + 1]; frame = new FlacFrame(channelsCount); frame.writer = new BitWriter(outputBuffer, 0, outputBuffer.Length); if (writer._settings.DoVerify) { verify = new FlakeReader(new AudioPCMConfig((int)bits_per_sample, channels, 44100)); verify.DoCRC = false; } }
protected virtual void Initialize(DeviceType deviceType, string source) { Devices = Platform.QueryDevices(deviceType); if (Devices.Length == 0) throw new OpenCLException("No devices of type "+deviceType+" present"); Context = Platform.CreateContext(null,Devices,null, IntPtr.Zero); CQs = new CommandQueue[Devices.Length]; for( int i=0; i<CQs.Length; i++ ) CQs[i] = Context.CreateCommandQueue(Devices[i], CommandQueueProperties.PROFILING_ENABLE); CQ = CQs[0]; Program = Context.CreateProgramWithSource(source); Program.Build(); Kernels = Program.CreateKernelDictionary(); }
public Core(int Nxp,int Nyp, int Nzp, int Ntm, double Bbeta, double Flux) { Nx = Nxp; Ny = Nyp; Nz = Nzp; Nt = Ntm; betagauge = (floattype)Bbeta; flux = (floattype)Flux; N = Nx * Ny * Nz * Nt; Nspace = Nx * Ny * Nz; string strforcompiler = "-D Nt=" + Nt.ToString() + " -D Nxyz=" + (Nx * Ny * Nz).ToString() + " -D Nxy=" + (Nx*Ny).ToString() + " -D Nx="+(Nx).ToString()+" -D Ny="+(Ny).ToString()+" -D Nz="+(Nz).ToString(); strforcompiler += typeof(floattype) == typeof(double) ? " -D floattype=double -D floattype2=double2 -D floattype4=double4" : " -D floattype=float -D floattype2=float2 -D floattype4=float4"; strforcompiler += " -D phi=" + flux.ToString().Replace(',', '.') + " -D KAPPA=" + kappa.ToString().Replace(',', '.'); string fp64support = "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n"; Plocalsize = AdjustLocalSize(Nspace); Slocalsize = AdjustLocalSize(N / 2); XhermYlocalsize = AdjustLocalSize(4 * N); // Plocalsize = 16; Slocalsize = 16; PNumGroups = Nx * Ny * Nz / Plocalsize; SNumGroups = N/2 / Slocalsize; XhermYNumGroups = 4*4*N / XhermYlocalsize; BufferLength = N * 4 * 9 * 2 * sizeof(floattype); SeedBufLen = N * sizeof(Int32)/2 * 4; AllocBuffers(); openCLPlatform = OpenCL.GetPlatform(0); openCLDevices = openCLPlatform.QueryDevices(DeviceType.ALL); openCLContext = openCLPlatform.CreateDefaultContext(); openCLCQ = openCLContext.CreateCommandQueue(openCLDevices[0], CommandQueueProperties.PROFILING_ENABLE); MyKernelProgram = openCLContext.CreateProgramWithSource( (typeof(floattype)==typeof(double)?fp64support:"") + File.ReadAllText("MyKernel.cl")+File.ReadAllText("dirak_mul.cl")); try { MyKernelProgram.Build(openCLDevices, strforcompiler, null, IntPtr.Zero); } catch (OpenCLException) { string buildLog = MyKernelProgram.GetBuildLog(openCLDevices[0]); MessageBox.Show(buildLog, "Build error(64 bit debug sessions in vs2008 always fail like this - debug in 32 bit or use vs2010)"); // Application.Exit(); } MyKernelKernel = MyKernelProgram.CreateKernel("MyKernel"); PReductionKernel = MyKernelProgram.CreateKernel("PLoop"); SReductionKernel = MyKernelProgram.CreateKernel("CalcS"); DiralMulKernel = MyKernelProgram.CreateKernel("dirakMatrMul"); FillWithKernel = MyKernelProgram.CreateKernel("FillWith"); FillLinkWithKernel = MyKernelProgram.CreateKernel("FillLinkWith"); FillWithRandomKernel = MyKernelProgram.CreateKernel("FillWithRandom"); AXPYKernel = MyKernelProgram.CreateKernel("AXPY"); XhermYKernel = MyKernelProgram.CreateKernel("XhermY"); BackupLinkKernel = MyKernelProgram.CreateKernel("BackupLink"); RestoreLinkKernel = MyKernelProgram.CreateKernel("RestoreLink"); SeedMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), SeedBufLen, IntPtr.Zero); LinkMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), BufferLength, IntPtr.Zero); PGroupMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize * PNumGroups, IntPtr.Zero); PResMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize, IntPtr.Zero); SGroupMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize * SNumGroups, IntPtr.Zero); SResMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize, IntPtr.Zero); XhermYGroupMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize * 2*XhermYNumGroups, IntPtr.Zero); XhermYresMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize * 2, IntPtr.Zero); XhermYrespointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(floatsize * 2); SeedVectorMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), SeedVectorBuf.Length * sizeof(int), IntPtr.Zero); StorageMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), linksize, IntPtr.Zero); dSmem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize, IntPtr.Zero); dSpointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(floatsize); MyKernelKernel.SetArg(0, (byte)EvenOdd); MyKernelKernel.SetArg(1, (floattype)betagauge); MyKernelKernel.SetArg(2, (floattype)flux); MyKernelKernel.SetArg(3, SeedMem); MyKernelKernel.SetArg(4, LinkMem); PReductionKernel.SetArg(0, LinkMem); PReductionKernel.SetArg(1, PGroupMem); PReductionKernel.SetArg(2, PResMem); IntPtr ptr = new IntPtr(Plocalsize * floatsize); PReductionKernel.SetArg(3, ptr, IntPtr.Zero); SReductionKernel.SetArg(0, LinkMem); SReductionKernel.SetArg(1, SGroupMem); SReductionKernel.SetArg(2, SResMem); IntPtr ptr1 = new IntPtr(Slocalsize * floatsize); SReductionKernel.SetArg(3, ptr1, IntPtr.Zero); XhermYKernel.SetArg(2, XhermYresMem); XhermYKernel.SetArg(3, XhermYGroupMem); XhermYKernel.SetArg(4, new IntPtr(XhermYlocalsize*floatsize*2),IntPtr.Zero); openCLCQ.EnqueueWriteBuffer(SeedMem, true, 0, SeedBufLen, ipseed); openCLCQ.EnqueueWriteBuffer(LinkMem, true, 0, BufferLength, ip); openCLCQ.EnqueueWriteBuffer(SeedVectorMem, true, 0, SeedVectorBuf.Length*sizeof(int), ipseedvector); rhat0 = new Vector(); //init BICGStab vectors phi = new Vector(); r0 = new Vector(); //rprev = new Vector(); pi = new Vector(); vi = new Vector(); t = new Vector(); s = new Vector(); // xprev = new Vector(); // vprev = new Vector(); // pprev = new Vector(); temp = new Vector(); ri = new Vector(); x = new Vector(); //for fermion update chi = new Vector(); CalculateS(); double s1 = S[0]; BackupLink(0, 0,1, 0, 1); CalculateS(); double s2 = S[0]; RestoreLink(0, 0, 1, 0, 1); CalculateS(); double s3 = S[0]; //MessageBox.Show(s1.ToString() + s2.ToString() + s3.ToString()); }
private void TestCommandQueueMemCopy(Context c, CommandQueue cq) { Output("Testing synchronous host memory->memory copy"); AlignedArrayFloat aafSrc = new AlignedArrayFloat(1024 * 1024, 64); AlignedArrayFloat aafDst = new AlignedArrayFloat(1024 * 1024, 64); SetAAF(aafSrc, 0.0f); SetAAF(aafDst, 1.0f); /// Test HOST_PTR -> HOST_PTR copy /// The call to EnqueueMapBuffer synchronizes caches before testing the result using (Mem memSrc = c.CreateBuffer((MemFlags)((ulong)MemFlags.READ_WRITE+(ulong)MemFlags.USE_HOST_PTR), aafSrc.ByteLength, aafSrc)) { using (Mem memDst = c.CreateBuffer((MemFlags)((ulong)MemFlags.READ_WRITE+(ulong)MemFlags.USE_HOST_PTR), aafDst.ByteLength, aafDst)) { cq.EnqueueCopyBuffer(memSrc, memDst, IntPtr.Zero, IntPtr.Zero, (IntPtr)aafSrc.ByteLength); cq.EnqueueBarrier(); IntPtr mappedPtr = cq.EnqueueMapBuffer(memDst, true, MapFlags.READ_WRITE, (IntPtr)0, (IntPtr)aafDst.ByteLength); if (!TestAAF(aafDst, 0.0f)) Error("EnqueueCopyBuffer failed, destination is invalid"); cq.EnqueueUnmapMemObject(memDst, mappedPtr); cq.EnqueueBarrier(); } } /// Test COPY_HOST_PTR -> COPY_HOST_PTR copy /// Verify that original source buffers are intact and that the copy was successful SetAAF(aafSrc, 0.0f); SetAAF(aafDst, 1.0f); using (Mem memSrc = c.CreateBuffer(MemFlags.COPY_HOST_PTR, aafSrc.ByteLength, aafSrc)) { using (Mem memDst = c.CreateBuffer(MemFlags.COPY_HOST_PTR, aafSrc.ByteLength, aafDst)) { SetAAF(aafSrc, 2.0f); SetAAF(aafDst, 3.0f); cq.EnqueueCopyBuffer(memSrc, memDst, IntPtr.Zero, IntPtr.Zero, (IntPtr)aafSrc.ByteLength); cq.Finish(); if (!TestAAF(aafSrc, 2.0f)) Error("Memory copy destroyed src buffer"); if (!TestAAF(aafDst, 3.0f)) Error("Memory copy destroyed dst buffer"); Event ev; cq.EnqueueReadBuffer(memDst, false, IntPtr.Zero, (IntPtr)aafDst.ByteLength, aafDst,0, null, out ev); cq.EnqueueWaitForEvents(1, new Event[] { ev }); ev.Dispose(); cq.Finish(); if (!TestAAF(aafDst, 0.0f)) Error("Memory copy failed"); } } /// Test ALLOC_HOST_PTR -> ALLOC_HOST_PTR copy SetAAF(aafSrc, 0.0f); SetAAF(aafDst, 1.0f); using (Mem memSrc = c.CreateBuffer((MemFlags)((ulong)MemFlags.ALLOC_HOST_PTR + (ulong)MemFlags.READ_WRITE), aafSrc.ByteLength, IntPtr.Zero)) { using (Mem memDst = c.CreateBuffer((MemFlags)((ulong)MemFlags.ALLOC_HOST_PTR + (ulong)MemFlags.WRITE_ONLY), aafSrc.ByteLength, IntPtr.Zero)) { cq.EnqueueWriteBuffer(memSrc, false, (IntPtr)0, (IntPtr)aafSrc.ByteLength, aafSrc); cq.EnqueueWriteBuffer(memDst, false, (IntPtr)0, (IntPtr)aafSrc.ByteLength, aafSrc); cq.EnqueueBarrier(); cq.EnqueueCopyBuffer(memSrc, memDst, IntPtr.Zero, IntPtr.Zero, (IntPtr)aafSrc.ByteLength); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(memDst, true, IntPtr.Zero, (IntPtr)aafDst.ByteLength, aafDst); if (!TestAAF(aafDst, 0.0f)) Error("Memory copy failed"); } } /// Test DEFAULT -> DEFAULT copy SetAAF(aafSrc, 0.0f); SetAAF(aafDst, 1.0f); using (Mem memSrc = c.CreateBuffer((MemFlags)((ulong)MemFlags.ALLOC_HOST_PTR + (ulong)MemFlags.READ_ONLY), aafSrc.ByteLength, IntPtr.Zero)) { using (Mem memDst = c.CreateBuffer((MemFlags)((ulong)MemFlags.ALLOC_HOST_PTR + (ulong)MemFlags.WRITE_ONLY), aafSrc.ByteLength, IntPtr.Zero)) { cq.EnqueueWriteBuffer(memSrc, false, (IntPtr)0, (IntPtr)aafSrc.ByteLength, aafSrc); cq.EnqueueWriteBuffer(memDst, false, (IntPtr)0, (IntPtr)aafSrc.ByteLength, aafSrc); cq.EnqueueBarrier(); cq.EnqueueCopyBuffer(memSrc, memDst, IntPtr.Zero, IntPtr.Zero, (IntPtr)aafSrc.ByteLength); cq.EnqueueBarrier(); cq.EnqueueReadBuffer(memDst, true, IntPtr.Zero, (IntPtr)aafDst.ByteLength, aafDst); if (!TestAAF(aafDst, 0.0f)) Error("Memory copy failed"); } } }
/// <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(); } }
/// <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(); } }
/// <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 unsafe void TestKernel(Context c, CommandQueue cq, Kernel argIOKernel) { Mem outArgBuffer = c.CreateBuffer((MemFlags)((ulong)MemFlags.ALLOC_HOST_PTR|(ulong)MemFlags.READ_WRITE), sizeof(IOKernelArgs), IntPtr.Zero); byte[] data = new byte[sizeof(IOKernelArgs)]; Output("Testing kernel - Argument return"); argIOKernel.SetArg(0, 1); argIOKernel.SetArg(1, 65L); argIOKernel.SetArg(2, 38.4f); argIOKernel.SetArg(3, outArgBuffer); Event ev; cq.EnqueueTask(argIOKernel,0,null,out ev); cq.Finish(); if ((int)ev.ExecutionStatus < 0) { Error(cq.Device.Name + ": argIOKernel failed with error code " + (ErrorCode)ev.ExecutionStatus); ev.Dispose(); } else { outArgBuffer.Read(cq, 0L, data, 0, sizeof(IOKernelArgs)); IntPtr outArgPtr = cq.EnqueueMapBuffer(outArgBuffer, true, MapFlags.READ, IntPtr.Zero, (IntPtr)sizeof(IOKernelArgs)); IOKernelArgs args = (IOKernelArgs)Marshal.PtrToStructure(outArgPtr, typeof(IOKernelArgs)); cq.EnqueueUnmapMemObject(outArgBuffer, outArgPtr); if (args.outInt != 1) Error(cq.Device.Name + ": argIOKernel failed to return correct arguments"); if (args.outLong != 65) Error(cq.Device.Name + ": argIOKernel failed to return correct arguments"); if (args.outSingle != 38.4f) Error(cq.Device.Name + ": argIOKernel failed to return correct arguments"); } }
/// <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 virtual void MemSet(CommandQueue cq, long dstByteOffset, double value, long count) { IntPtr p = cq.EnqueueMapBuffer(this, true, MapFlags.WRITE, dstByteOffset, count * sizeof(double)); double* pBlock = (double*)p.ToPointer(); for (long i = 0; i < count; i++) pBlock[i] = value; cq.EnqueueUnmapMemObject(this, p); cq.Finish(); }
private void TestUserEventCallbacks(Context c, CommandQueue cq) { if (!(cq.Device.Platform.OpenCLMajorVersion >= 1 && cq.Device.Platform.OpenCLMinorVersion >= 1)) { Output("Skipping TestUserEventCallbacks tests(Requires OpenCL 1.1 or higher)"); return; } Output( "Testing User Events and event callbacks" ); TestUserEventCallbackCalled = false; Event event0 = c.CreateUserEvent(); event0.SetCallback(ExecutionStatus.COMPLETE, TestUserEventCallback, this); event0.SetUserEventStatus(ExecutionStatus.COMPLETE); if (TestUserEventCallbackCalled != true) { Error("Event callback not called"); return; } c.WaitForEvent(event0); }
public virtual void Read(CommandQueue cq, long srcOffset, double[] dstData, int dstStartIndex, int count) { IntPtr p = cq.EnqueueMapBuffer(this, true, MapFlags.READ, srcOffset, count * sizeof(double)); double* pBlock = (double*)p.ToPointer(); for (long i = 0; i < count; i++) dstData[dstStartIndex+i] = pBlock[i]; cq.EnqueueUnmapMemObject(this, p); cq.Finish(); }
private unsafe void TestVecKernel(Context c, CommandQueue cq, Kernel k) { Float2 f2 = new Float2(0.0f,1.0f); float[] memory = new float[2]; fixed (float* pMemory = memory) { Mem mem = c.CreateBuffer((MemFlags)((ulong)MemFlags.READ_WRITE | (ulong)MemFlags.USE_HOST_PTR), 4 * 2, pMemory); k.SetArg(0, f2); k.SetArg(1, mem); cq.EnqueueTask(k); cq.EnqueueBarrier(); IntPtr pMap = cq.EnqueueMapBuffer(mem, true, MapFlags.READ, 0, 2 * 4); cq.EnqueueUnmapMemObject(mem, pMap); } }
public void CreateContext( Platform platform, Device device ) { IntPtr[] contextProperties = new IntPtr[] { (IntPtr)ContextProperties.PLATFORM, platform.PlatformID, IntPtr.Zero, IntPtr.Zero }; Device[] devices = new Device[] { device }; oclContext = platform.CreateContext(contextProperties, devices, oclContextNotify, IntPtr.Zero); oclCQ = oclContext.CreateCommandQueue(device, CommandQueueProperties.PROFILING_ENABLE); }
private void TestCommandQueue(Context c, CommandQueue cq ) { string programName = "OpenCL" + Path.DirectorySeparatorChar + "src" + Path.DirectorySeparatorChar + "MemoryTests.cl"; Output("Testing compilation of: " + programName); OpenCLNet.Program p0 = c.CreateProgramWithSource(File.ReadAllLines(programName)); OpenCLNet.Program p = c.CreateProgramWithSource(File.ReadAllText(programName)); p0.Build(); p.Build(); Kernel k = p.CreateKernel(@"LoopAndDoNothing"); TestCommandQueueMemCopy(c, cq); TestCommandQueueAsync(c, cq, k ); }
protected virtual void Initialize(Context context, CommandQueue cq, int width, int height) { BitmapData bd; Bitmap = new Bitmap(width,height,PixelFormat.Format32bppArgb); bd = LockEntireBitmap( Bitmap, ImageLockMode.ReadOnly ); Image = context.CreateImage2D(MemFlags.COPY_HOST_PTR, OpenCLNet.ImageFormat.ARGB8U, width, height, bd.Stride, bd.Scan0); Context = context; CQ = cq; }
private void TestCommandQueueAsync(Context c, CommandQueue cq, Kernel kernel ) { List<Event> events = new List<Event>(); Event clEvent; Output("Testing asynchronous task issuing (clEnqueueTask) and waiting for events"); // Issue a bunch of slow operations kernel.SetArg(0, 5000000); for (int i = 0; i < 10; i++) { cq.EnqueueTask(kernel, 0, null, out clEvent); events.Add(clEvent); } // Issue a bunch of fast operations kernel.SetArg(0, 500); for (int i = 0; i < 10; i++) { cq.EnqueueTask(kernel, 0, null, out clEvent); events.Add(clEvent); } Event[] eventList = events.ToArray(); cq.EnqueueWaitForEvents(eventList.Length, eventList); while (events.Count > 0) { if ((int)events[0].ExecutionStatus < 0) { Output(cq.Device.Name + ": TestCommandQueueAsync failed with error code " + (ErrorCode)events[0].ExecutionStatus); } events[0].Dispose(); events.RemoveAt(0); } }
internal OpenCLBackedBitmap(Context context, CommandQueue cq, int width, int height) { Initialize(context, cq, width, height); }
private static int AddNativeKernelParams( NativeKernel nk, CommandQueue cq, object o, Mem[] buffers) { int callbackId; NativeKernelCallbackData callbackData = new NativeKernelCallbackData(nk, cq, o, buffers); bool gotMutex = false; try { gotMutex = NativeKernelParamsMutex.WaitOne(); do { callbackId = NativeKernelParamsId++; } while (NativeKernelDispatch.ContainsKey(callbackId)); NativeKernelDispatch.Add(callbackId, callbackData); } finally { if (gotMutex) NativeKernelParamsMutex.ReleaseMutex(); } return callbackId; }