Exemple #1
0
        public void TestImage2DReadWrite()
        {
            checkSetup();
            CommandQueue queue = CommandQueue.Create(context, device, CommandQueueFlags.ProfilingEnable);

            ImageFormat fmt = new ImageFormat(ChannelOrder.R, ChannelType.Float);
            Image2D     img = Image2D.Create(context, MemFlags.ReadOnly, fmt, 4, 2);

            Assert.AreEqual((IntPtr)4, img.ElementSize);

            float[,] pix = new float[, ] {
                { 1.0f, 0.0f, 1.0f, 0.0f }, { 2.0f, 0.0f, 2.0f, 0.0f }
            };
            float[,] ret = new float[2, 4];
            img.Write(queue, pix);

            img.Read(queue, ret);

            for (int r = 0; r < pix.GetLength(0); r++)
            {
                for (int c = 0; c < pix.GetLength(1); c++)
                {
                    Assert.AreEqual(pix[r, c], ret[r, c]);
                }
            }

            img.Dispose();
            queue.Dispose();
        }
Exemple #2
0
        public void TestEvents()
        {
            checkSetup();
            CommandQueue queue  = CommandQueue.Create(context, device, CommandQueueFlags.ProfilingEnable);
            Buffer       buffer = Buffer.Create(context, MemFlags.ReadWrite, (IntPtr)16);

            Float4 vec = new Float4();

            vec.X = 1.0f; vec.Y = 2.0f; vec.Z = 3.0f; vec.W = 4.0f;

            IntPtr vecBuff = Marshal.AllocHGlobal(16);

            try{
                Marshal.StructureToPtr(vec, vecBuff, false);

                unsafe {
                    Event writeEvent;
                    buffer.EnqueueWriteRaw(queue, (IntPtr)0, (IntPtr)16, vecBuff, out writeEvent);
                    Assert.True(writeEvent.Handle != IntPtr.Zero, "writeEvent handle was null.");

                    writeEvent.Wait();
                    Assert.AreEqual(CommandType.WriteBuffer, writeEvent.CommandType);
                    Assert.AreEqual(CommandExecutionStatus.Complete, writeEvent.ExecutionStatus);
                    Trace.WriteLine("writeEvent.ReferenceCount: " + writeEvent.ReferenceCount.ToString(), "notice");
                }
            }
            finally{
                Marshal.FreeHGlobal(vecBuff);
                buffer.Dispose();
                queue.Dispose();
            }
        }
Exemple #3
0
        public void TestBufferReadWrite()
        {
            checkSetup();
            CommandQueue queue  = CommandQueue.Create(context, device, CommandQueueFlags.ProfilingEnable);
            Buffer       buffer = Buffer.Create(context, MemFlags.ReadWrite, (IntPtr)32);

            float[]  vals = new float[] { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
            Float4[] vecs = new Float4[2];

            buffer.Write(queue, vals);

            buffer.Read(queue, vecs);

            Assert.AreEqual(1.0f, vecs[0].X);
            Assert.AreEqual(8.0f, vecs[1].W);

            float[] tooSmall = new float[6];
            bool    caught   = false;

            try{
                buffer.Read(queue, tooSmall);
            }
            catch (OpenCLBufferSizeException) {
                caught = true;
            }
            Assert.True(caught);
            buffer.Dispose();
            queue.Dispose();
        }
Exemple #4
0
        public void TestCommandQueue()
        {
            checkSetup();
            CommandQueue queue = CommandQueue.Create(context, device, CommandQueueFlags.ProfilingEnable);

            checkProperties(queue);

            Assert.AreEqual(CommandQueueFlags.ProfilingEnable, queue.Flags);
            queue.SetProperty(CommandQueueFlags.OutOfOrderExecModeEnable, true);
            Assert.AreEqual(CommandQueueFlags.ProfilingEnable | CommandQueueFlags.OutOfOrderExecModeEnable, queue.Flags);

            queue.Dispose();
            Assert.AreEqual(IntPtr.Zero, queue.Handle);
        }
Exemple #5
0
        public void TestImage2DProperties()
        {
            checkSetup();
            CommandQueue queue = CommandQueue.Create(context, device, CommandQueueFlags.ProfilingEnable);

            ImageFormat fmt = new ImageFormat(ChannelOrder.Ra, ChannelType.Float);
            Image2D     img = Image2D.Create(context, MemFlags.ReadOnly, fmt, 2, 2);

            Assert.AreEqual(2, img.Width);
            Assert.AreEqual(2, img.Height);
            Assert.AreEqual(fmt, img.ImageFormat);
            Assert.AreEqual((IntPtr)8, img.ElementSize);

            img.Dispose();
            queue.Dispose();
        }
Exemple #6
0
        public void TestKernelExecution()
        {
            int elemCount = 16;
            int localSize = 8;

            checkSetup();
            CommandQueue queue  = CommandQueue.Create(context, device, CommandQueueFlags.ProfilingEnable);
            Kernel       kernel = Kernel.Create(program, "vec_add");
            Buffer       a      = Buffer.Create(context, MemFlags.ReadWrite, (IntPtr)(elemCount * sizeof(float)));
            Buffer       b      = Buffer.Create(context, MemFlags.ReadWrite, (IntPtr)(elemCount * sizeof(float)));
            Buffer       c      = Buffer.Create(context, MemFlags.ReadWrite, (IntPtr)(elemCount * sizeof(float)));

            float[] aLoc = new float[elemCount];
            float[] bLoc = new float[elemCount];
            float[] cLoc = new float[elemCount];

            for (int i = 0; i < elemCount; i++)
            {
                aLoc[i] = (float)rand.NextDouble();
                bLoc[i] = (float)rand.NextDouble();
            }

            a.Write(queue, aLoc);
            b.Write(queue, bLoc);

            kernel.SetArgs(a, b, c);
            Event launchEvt;

            kernel.EnqueueLaunch(queue, elemCount, localSize, null, out launchEvt);
            launchEvt.Wait();
            launchEvt.Dispose();

            c.Read(queue, cLoc);

            for (int i = 0; i < elemCount; i++)
            {
                Assert.That(Math.Abs(aLoc[i] + bLoc[i] - cLoc[i]) < 0.05f);
            }

            c.Dispose();
            b.Dispose();
            a.Dispose();
            kernel.Dispose();
            queue.Dispose();
        }
Exemple #7
0
        public void TestBufferReadWriteRaw()
        {
            checkSetup();
            CommandQueue queue  = CommandQueue.Create(context, device, CommandQueueFlags.ProfilingEnable);
            Buffer       buffer = Buffer.Create(context, MemFlags.ReadWrite, (IntPtr)16);

            Float4 vec = new Float4();

            vec.X = 1.0f; vec.Y = 2.0f; vec.Z = 3.0f; vec.W = 4.0f;

            IntPtr vecBuff = Marshal.AllocHGlobal(16);

            try{
                Marshal.StructureToPtr(vec, vecBuff, false);

                unsafe {
                    Event writeEvent;
                    buffer.EnqueueWriteRaw(queue, (IntPtr)0, (IntPtr)16, vecBuff, out writeEvent);
                    writeEvent.Wait();

                    float v0, v1, v2, v3;
                    buffer.ReadRaw(queue, (IntPtr)0, (IntPtr)4, (IntPtr)(&v0));
                    buffer.ReadRaw(queue, (IntPtr)4, (IntPtr)4, (IntPtr)(&v1));
                    buffer.ReadRaw(queue, (IntPtr)8, (IntPtr)4, (IntPtr)(&v2));
                    buffer.ReadRaw(queue, (IntPtr)12, (IntPtr)4, (IntPtr)(&v3));

                    Assert.AreEqual(1.0f, v0);
                    Assert.AreEqual(2.0f, v1);
                    Assert.AreEqual(3.0f, v2);
                    Assert.AreEqual(4.0f, v3);

                    float k = 9.0f;
                    buffer.WriteRaw(queue, (IntPtr)4, (IntPtr)4, (IntPtr)(&k));
                    buffer.ReadRaw(queue, (IntPtr)4, (IntPtr)4, (IntPtr)(&v1));
                    Assert.AreEqual(9.0f, v1);
                }
            }
            finally{
                Marshal.FreeHGlobal(vecBuff);
                buffer.Dispose();
                queue.Dispose();
            }
        }
Exemple #8
0
        public void TestBufferMap()
        {
            checkSetup();
            CommandQueue queue  = CommandQueue.Create(context, device, CommandQueueFlags.ProfilingEnable);
            Buffer       buffer = Buffer.Create(context, MemFlags.ReadWrite, (IntPtr)16);

            IntPtr writePtr = buffer.Map(queue, MapFlags.Write, (IntPtr)0, (IntPtr)16);

            try{
                unsafe {
                    float *arr = (float *)writePtr;
                    arr[0] = 2.0f;
                    arr[1] = 4.0f;
                    arr[2] = 6.0f;
                    arr[3] = 8.0f;
                }
            }
            finally{
                buffer.Unmap(queue, ref writePtr);
            }
            Assert.AreEqual(IntPtr.Zero, writePtr);

            float  sum     = 0.0f;
            IntPtr readPtr = buffer.Map(queue, MapFlags.Read, (IntPtr)0, (IntPtr)16);

            try{
                unsafe {
                    float *arr = (float *)readPtr;
                    sum = arr[0] + arr[1] + arr[2] + arr[3];
                }
            }
            finally{
                buffer.Unmap(queue, ref readPtr);
            }
            Assert.AreEqual(IntPtr.Zero, readPtr);
            Assert.AreEqual(20.0f, sum);
        }