Beispiel #1
0
        // 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;
            }
        }
Beispiel #2
0
        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;
        }
Beispiel #3
0
        /// <summary>
        /// 想着不做内存拷贝,直接读取数据的,失败了 by 林子
        /// </summary>
        /// <param name="oclContext"></param>
        /// <param name="oclCQ"></param>
        /// <param name="oclBitmap"></param>
        /// <returns></returns>
        public static Bitmap ToBitmap2(this Context oclContext, CommandQueue oclCQ, CL.Image oclBitmap)
        {
            Bitmap     bitmap = new Bitmap(oclBitmap.Width.ToInt32(), oclBitmap.Height.ToInt32(), PixelFormat.Format32bppArgb);
            BitmapData bd     = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
            IntPtr     p      = oclCQ.EnqueueMapBuffer(oclBitmap, true, MapFlags.READ, IntPtr.Zero, (IntPtr)(bd.Height * bd.Stride));

            oclCQ.EnqueueBarrier();
            oclCQ.Finish();
            bitmap.UnlockBits(bd);
            return(bitmap);
        }
Beispiel #4
0
        public static Bitmap ToBitmap(this Context oclContext, CommandQueue oclCQ, CL.Image oclBitmap)
        {
            Bitmap     bitmap = new Bitmap(oclBitmap.Width.ToInt32(), oclBitmap.Height.ToInt32(), PixelFormat.Format32bppArgb);
            BitmapData bd     = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
            Mem        buffer = oclContext.CreateBuffer((MemFlags.WRITE_ONLY | MemFlags.USE_HOST_PTR), bd.Height * bd.Stride, bd.Scan0);

            IntPtr[] origin = new IntPtr[] { IntPtr.Zero, IntPtr.Zero, IntPtr.Zero };
            IntPtr[] region = new IntPtr[] { (IntPtr)bitmap.Width, (IntPtr)bitmap.Height, new IntPtr(1) };
            oclCQ.EnqueueCopyImageToBuffer(oclBitmap, buffer, origin, region, IntPtr.Zero);
            oclCQ.EnqueueBarrier();
            IntPtr p = oclCQ.EnqueueMapBuffer(buffer, true, MapFlags.READ, IntPtr.Zero, (IntPtr)(bd.Height * bd.Stride));

            oclCQ.EnqueueUnmapMemObject(buffer, p);
            oclCQ.Finish();
            buffer.Dispose();
            bitmap.UnlockBits(bd);
            return(bitmap);
        }
Beispiel #5
0
        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;
            }
        }
Beispiel #6
0
        private static void Main()
        {
            OpenCL.GetPlatformIDs(32, new IntPtr[32], out uint num_platforms);
            var devices = new List <Device>();

            for (int i = 0; i < num_platforms; i++)
            {
                devices.AddRange(OpenCL.GetPlatform(i).QueryDevices(DeviceType.ALL));
            }
            int device = SelectForm.Show((from Device d in devices select d.Name).ToArray());

            if (device == -1)
            {
                return;
            }
            platform   = devices[device].Platform;                                                                                                                                                                              //平台
            oclDevice  = devices[device];                                                                                                                                                                                       //选中运算设备
            oclContext = platform.CreateContext(new[] { (IntPtr)ContextProperties.PLATFORM, platform.PlatformID, IntPtr.Zero, IntPtr.Zero }, new[] { oclDevice }, new ContextNotify(OpenCLContextNotifyCallBack), IntPtr.Zero); //根据配置建立上下文
            oclCQ      = oclContext.CreateCommandQueue(oclDevice, CommandQueueProperties.PROFILING_ENABLE);                                                                                                                     //创建请求队列
            if (!oclDevice.ImageSupport)
            {
                return;                                     //如果失败返回
            }
            if (!oclContext.SupportsImageFormat(MemFlags.READ_WRITE, MemObjectType.IMAGE2D, ChannelOrder.RGBA, ChannelType.UNSIGNED_INT8))
            {
                return;
            }
            sampler      = oclContext.CreateSampler(true, AddressingMode.NONE, FilterMode.NEAREST);
            FilterKernel = oclContext.MakeCode("FilterImage", CLCode1);
            Kernel K2 = oclContext.MakeCode("vector_add_gpu", CLCode2);

            int aaa = K2.PECount(oclDevice);

            aaa = aaa;
            #region 试一下用GPU做运算
            int[]  a  = new[] { 1, 2, 3, 1722 };
            int[]  b  = new[] { 456, 2, 1, 56 };
            int[]  c  = new[] { 0, 0, 0, 0 };
            CL.Mem n1 = oclContext.CreateBuffer(MemFlags.READ_WRITE | MemFlags.COPY_HOST_PTR, a.Length * sizeof(int), a.ToIntPtr());
            CL.Mem n2 = oclContext.CreateBuffer(MemFlags.READ_WRITE | MemFlags.COPY_HOST_PTR, b.Length * sizeof(int), b.ToIntPtr());
            CL.Mem n3 = null;
            unchecked { n3 = oclContext.CreateBuffer(MemFlags.READ_WRITE, b.Length * sizeof(int), IntPtr.Zero); }
            K2.SetArg(0, n1);
            K2.SetArg(1, n2);
            K2.SetArg(2, n3);
            K2.SetArg(3, (int)c.Length);
            oclCQ.EnqueueNDRangeKernel(K2, 1, null, new[] { c.Length, 0 }, null);
            oclCQ.EnqueueBarrier();
            oclCQ.Finish();
            // */
            //oclContext.WriterValues(oclCQ, n3, B);
            c = oclContext.ReadIntValues(oclCQ, n3, c.Length);

            c = c;
            // */
            #endregion

            var sdi = new ShowDeviceInfo();
            var lb  = sdi.listBox1.Items;
            lb.Add($"Name:{oclDevice.Name}");
            lb.Add($"DeviceType:{oclDevice.DeviceType.ToString()}");
            lb.Add($"MaxComputeUnits(最大计算单元):{oclDevice.MaxComputeUnits}");
            lb.Add($"ImageSupport:{oclDevice.ImageSupport}");
            lb.Add($"AddressBits:{oclDevice.AddressBits}");
            lb.Add($"DriverVersion:{oclDevice.DriverVersion}");
            lb.Add($"MaxClockFrequency(最大时钟频率):{oclDevice.MaxClockFrequency}MHz");
            lb.Add($"MaxMemAllocSize(最大内存):{oclDevice.MaxMemAllocSize / 1024 / 1024 / 1024}GB");
            lb.Add($"MaxWorkItemDimensions(最大工作维度):{oclDevice.MaxWorkItemDimensions}");
            lb.Add($"MaxWorkGroupSize(最大工作组数量):{oclDevice.MaxWorkGroupSize }");
            lb.Add($"Version(OpenCL版本):{oclDevice.Version}");
            lb.Add($"GlobalMemSize(显存):{oclDevice.GlobalMemSize / 1024 / 1024 / 1024}GB");
            lb.Add($"GlobalMemCacheSize(显存缓存):{oclDevice.GlobalMemCacheSize / 1024}KB");
            lb.Add($"GlobalMemCacheLineSize:{oclDevice.GlobalMemCacheLineSize}");
            lb.Add($"Vendor(厂商):{oclDevice.Vendor}");
            lb.Add($"HostUnifiedMemory(是否和Host共用内存):{oclDevice.HostUnifiedMemory}");
            sdi.ShowDialog();
            #region 调用编译好的生命游戏程序
            //if (oclDevice.DeviceType == DeviceType.GPU)
            {
                OutImage1 = oclContext.CreateImage2D(MemFlags.READ_WRITE, CL.ImageFormat.RGBA8U, TestImage.Width, TestImage.Height, 0, IntPtr.Zero);
                OutImage2 = oclContext.CreateImage2D(MemFlags.READ_WRITE, CL.ImageFormat.RGBA8U, TestImage.Width, TestImage.Height, 0, IntPtr.Zero);
                FilterKernel.SetArg(0, 1.0f);
                FilterKernel.SetArg(1, 1.0f);
                FilterKernel.SetArg(2, oclContext.ToCLImage(TestImage));
                FilterKernel.SetArg(3, OutImage1);
                FilterKernel.SetArg(4, sampler);
                oclCQ.EnqueueNDRangeKernel(FilterKernel, 2, null, new IntPtr[] { OutImage1.Width, OutImage1.Height, IntPtr.Zero }, null);
                oclCQ.EnqueueBarrier();
                oclCQ.Finish();
                new MainForm().ShowDialog();
            }
            // */
            #endregion

            Application.Exit();
        }
Beispiel #7
0
        // 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;

            }
        }
Beispiel #8
0
        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;
        }
Beispiel #9
0
        private static void Main()
        {
            OpenCL.GetPlatformIDs(32, new IntPtr[32], out uint num_platforms);
            List <Device> pt = new List <Device>();

            for (int i = 0; i < num_platforms; pt.AddRange(OpenCL.GetPlatform(i++).QueryDevices(DeviceType.ALL)))
            {
                ;
            }
            int PT = 0;            // SelectForm.Show((from Device d in pt select d.Name).ToArray());

            if (PT == -1)
            {
                return;
            }
            platform   = pt[PT].Platform;                                                                                                                                                                                       //平台
            oclDevice  = pt[PT];                                                                                                                                                                                                //选中运算设备
            oclContext = platform.CreateContext(new[] { (IntPtr)ContextProperties.PLATFORM, platform.PlatformID, IntPtr.Zero, IntPtr.Zero }, new[] { oclDevice }, new ContextNotify(OpenCLContextNotifyCallBack), IntPtr.Zero); //根据配置建立上下文
            oclCQ      = oclContext.CreateCommandQueue(oclDevice, CommandQueueProperties.PROFILING_ENABLE);                                                                                                                     //创建请求队列
            if (!oclDevice.ImageSupport)
            {
                return;                                     //如果失败返回
            }
            if (!oclContext.SupportsImageFormat(MemFlags.READ_WRITE, MemObjectType.IMAGE2D, ChannelOrder.RGBA, ChannelType.UNSIGNED_INT8))
            {
                return;
            }
            sampler      = oclContext.CreateSampler(true, AddressingMode.NONE, FilterMode.NEAREST);
            FilterKernel = oclContext.MakeCode("FilterImage", CLCode1);
            Kernel K2 = oclContext.MakeCode("vector_add_gpu", CLCode2);

            #region
            int[]  A  = new[] { 1, 2, 3, 1722 };
            int[]  B  = new[] { 456, 2, 1, 56 };
            int[]  C  = new[] { 0, 0, 0, 0 };
            CL.Mem n1 = oclContext.CreateBuffer(MemFlags.READ_WRITE | MemFlags.COPY_HOST_PTR, A.Length * sizeof(int), A.ToIntPtr());
            CL.Mem n2 = oclContext.CreateBuffer(MemFlags.READ_WRITE | MemFlags.COPY_HOST_PTR, B.Length * sizeof(int), B.ToIntPtr());
            CL.Mem n3 = oclContext.CreateBuffer(MemFlags.READ_WRITE, B.Length * sizeof(int), IntPtr.Zero);
            K2.SetArg(0, n1);
            K2.SetArg(1, n2);
            K2.SetArg(2, n3);
            K2.SetArg(3, (int)C.Length);
            oclCQ.EnqueueNDRangeKernel(K2, 1, null, new[] { C.Length, 0 }, null);
            oclCQ.EnqueueBarrier();
            oclCQ.Finish();
            // */
            //oclContext.WriterValues(oclCQ, n3, B);
            C = oclContext.ReadIntValues(oclCQ, n3, C.Length);

            C = C;
            // */
            #endregion

            #region 调用编译好的程序
            OutImage1 = oclContext.CreateImage2D(MemFlags.READ_WRITE, CL.ImageFormat.RGBA8U, TestImage.Width, TestImage.Height, 0, IntPtr.Zero);
            OutImage2 = oclContext.CreateImage2D(MemFlags.READ_WRITE, CL.ImageFormat.RGBA8U, TestImage.Width, TestImage.Height, 0, IntPtr.Zero);
            FilterKernel.SetArg(0, 1.0f);
            FilterKernel.SetArg(1, 1.0f);
            FilterKernel.SetArg(2, oclContext.ToCLImage(TestImage));
            FilterKernel.SetArg(3, OutImage1);
            FilterKernel.SetArg(4, sampler);
            oclCQ.EnqueueNDRangeKernel(FilterKernel, 2, null, new IntPtr[] { OutImage1.Width, OutImage1.Height, IntPtr.Zero }, null);
            oclCQ.EnqueueBarrier();
            oclCQ.Finish();
            // */
            #endregion

            new MainForm().ShowDialog();
            Application.Exit();
        }
Beispiel #10
0
        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;
            }
        }
Beispiel #11
0
 public void CreateOCLImages(Context context)
 {
     OCLInputImage = CreateOCLBitmapFromBitmap(TestImage);
     OCLOutputImage = oclContext.CreateImage2D(MemFlags.WRITE_ONLY, CL.ImageFormat.RGBA8U, panelScaled.Width, panelScaled.Height, 0, IntPtr.Zero);
     OCLSampler = oclContext.CreateSampler(true, AddressingMode.CLAMP_TO_EDGE, FilterMode.LINEAR);
 }
Beispiel #12
0
 public void CreateOCLImages(Context context)
 {
     OCLInputImage  = CreateOCLBitmapFromBitmap(TestImage);
     OCLOutputImage = oclContext.CreateImage2D(MemFlags.WRITE_ONLY, CL.ImageFormat.RGBA8U, panelScaled.Width, panelScaled.Height, 0, IntPtr.Zero);
     OCLSampler     = oclContext.CreateSampler(true, AddressingMode.CLAMP_TO_EDGE, FilterMode.LINEAR);
 }