Esempio n. 1
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();
            }
        }
Esempio n. 2
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();
        }
Esempio n. 3
0
        public void TestBufferCreate()
        {
            checkSetup();
            Buffer buffer = Buffer.Create(context, MemFlags.ReadWrite, (IntPtr)16);

            Assert.True(buffer.Handle != IntPtr.Zero, "Buffer handle was null.");

            buffer.Dispose();
            Assert.AreEqual(buffer.Handle, IntPtr.Zero);
        }
Esempio n. 4
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();
        }
Esempio n. 5
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();
            }
        }
Esempio n. 6
0
        public void TestBufferProperties()
        {
            checkSetup();
            Buffer buffer = Buffer.Create(context, MemFlags.ReadWrite, (IntPtr)16);

            Assert.True(buffer.Handle != IntPtr.Zero, "Buffer handle was null.");

            Assert.AreEqual(MemObjectType.Buffer, buffer.MemObjectType);
            Assert.AreEqual(MemFlags.ReadWrite, buffer.Flags);
            Assert.AreEqual((IntPtr)16, buffer.Size);
            uint mc = buffer.MapCount;
            uint rc = buffer.ReferenceCount;

            Assume.That(context.ReferenceCount == 1);
            Context bufCtx = buffer.Context;

            Assert.AreEqual(context.Handle, bufCtx.Handle);
            Assert.AreEqual(2, context.ReferenceCount);
            bufCtx.Dispose();
            Assert.AreEqual(1, context.ReferenceCount);

            buffer.Dispose();
            Assert.AreEqual(buffer.Handle, IntPtr.Zero);
        }