Ejemplo n.º 1
0
        /// <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();
            }
        }