Ejemplo n.º 1
0
        /// <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();
            }
        }
Ejemplo n.º 2
0
        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");
                }
            }
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
0
        /// <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();
            }
        }
Ejemplo n.º 5
0
        /// <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();
            }
        }
Ejemplo n.º 6
0
        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);
        }