示例#1
0
        public void TestOclChangeDefaultDevice()
        {
            using (VectorOfOclPlatformInfo oclPlatformInfos = OclInvoke.GetPlatformsInfo())
            {
                if (oclPlatformInfos.Size > 0)
                {
                    for (int i = 0; i < oclPlatformInfos.Size; i++)
                    {
                        OclPlatformInfo platformInfo = oclPlatformInfos[i];

                        for (int j = 0; j < platformInfo.DeviceNumber; j++)
                        {
                            OclDevice device = platformInfo.GetDevice(j);

                            Trace.WriteLine(String.Format("{0}Setting device to {1}", Environment.NewLine, device.Name));
                            //OclDevice d = new OclDevice();
                            //d.Set(device.NativeDevicePointer);


                            OclDevice defaultDevice = OclDevice.Default;
                            defaultDevice.Set(device.NativeDevicePointer);

                            Trace.WriteLine(String.Format("Current OpenCL default device: {0}", defaultDevice.Name));

                            UMat m = new UMat(2048, 2048, DepthType.Cv8U, 3);
                            m.SetTo(new MCvScalar(100, 100, 100));
                            CvInvoke.GaussianBlur(m, m, new Size(3, 3), 3);

                            Stopwatch watch = Stopwatch.StartNew();
                            m.SetTo(new MCvScalar(100, 100, 100));
                            CvInvoke.GaussianBlur(m, m, new Size(3, 3), 3);
                            watch.Stop();
                            Trace.WriteLine(String.Format("Device '{0}' time: {1} milliseconds", defaultDevice.Name, watch.ElapsedMilliseconds));
                            CvInvoke.OclFinish();
                        }
                    }
                }

                Trace.WriteLine(Environment.NewLine + "Disable OpenCL");
                CvInvoke.UseOpenCL = false;
                UMat m2 = new UMat(2048, 2048, DepthType.Cv8U, 3);
                m2.SetTo(new MCvScalar(100, 100, 100));
                CvInvoke.GaussianBlur(m2, m2, new Size(3, 3), 3);

                Stopwatch watch2 = Stopwatch.StartNew();
                m2.SetTo(new MCvScalar(100, 100, 100));
                CvInvoke.GaussianBlur(m2, m2, new Size(3, 3), 3);
                watch2.Stop();
                Trace.WriteLine(String.Format("CPU time: {0} milliseconds", watch2.ElapsedMilliseconds));
                CvInvoke.UseOpenCL = true;
            }
        }
示例#2
0
        public void TestOclKernel()
        {
            if (CvInvoke.HaveOpenCL && CvInvoke.UseOpenCL)
            {
                OclDevice defaultDevice = OclDevice.Default;

                UMat umat = new UMat(256, 256, DepthType.Cv8U, 1);
                umat.SetTo(new MCvScalar(8));

                int rowsPerWI = 1;
                int cn        = 1;

                String buildOpts = String.Format("-D rowsPerWI={0} -D cn={1} -D srcT1_C1=uchar -DdstT_C1=uchar", rowsPerWI, cn);

                String sourceStr = @"
__kernel void mytest(__global const uchar * srcptr1, int srcstep1, int srcoffset1, 
                 __global uchar *dstptr, int dststep, int dstoffset,
                 int rows, int cols )
{
               int x = get_global_id(0);
               int y0 = get_global_id(1) * rowsPerWI;

               if (x < cols)
               {
                  int src1_index = mad24(y0, srcstep1, mad24(x, (int)sizeof(srcT1_C1) * cn, srcoffset1));
                  int dst_index = mad24(y0, dststep, mad24(x, (int)sizeof(dstT_C1) * cn, dstoffset));

                  for (int y = y0, y1 = min(rows, y0 + rowsPerWI); y < y1; ++y, src1_index += srcstep1, dst_index += dststep)
                  {
                     *(__global uchar*) (dstptr + dst_index)= *(srcptr1 + src1_index);
                  }
               }
            }";



                using (CvString errorMsg = new CvString())
                    using (OclProgramSource ps = new OclProgramSource(sourceStr))
                        using (OclKernel kernel = new OclKernel())
                        {
                            bool success = kernel.Create("mytest", ps, buildOpts, errorMsg);
                            bool empty   = kernel.Empty;
                        }
            }
        }
示例#3
0
        protected override View OnCreateDialogView()
        {
            LayoutInflater inflator = LayoutInflater.FromContext(this.Context);
            View           dialog   = inflator.Inflate(Resource.Layout.opencl_preference, null);

            _openCLRadioGroup = dialog.FindViewById <RadioGroup>(Resource.Id.opencl_preference_radio_group);

            AppPreference preference = new AppPreference();

            RadioButton checkedButton = null;
            RadioButton cpuButton     = new RadioButton(this.Context);

            cpuButton.Text = "CPU";

            _openCLRadioGroup.AddView(cpuButton);
            //int selectedIdx = -1;
            if (preference.UseOpenCL == false)
            {
                checkedButton = cpuButton;
            }
            cpuButton.Click += (sender, args) =>
            {
                preference.UseOpenCL = false;
                //Toast.MakeText(this.Context, "cpu clicked", ToastLength.Short).Show();
            };

            String selectedDeviceName = preference.OpenClDeviceName;

            if (selectedDeviceName == null && CvInvoke.HaveOpenCL && preference.UseOpenCL)
            {
                selectedDeviceName = OclDevice.Default.Name;
            }
            //int counter = 1;
            using (VectorOfOclPlatformInfo oclPlatformInfos = OclInvoke.GetPlatformsInfo())
            {
                if (oclPlatformInfos.Size > 0)
                {
                    for (int i = 0; i < oclPlatformInfos.Size; i++)
                    {
                        OclPlatformInfo platformInfo = oclPlatformInfos[i];

                        for (int j = 0; j < platformInfo.DeviceNumber; j++)
                        {
                            OclDevice   device       = platformInfo.GetDevice(j);
                            RadioButton deviceButton = new RadioButton(this.Context);
                            deviceButton.Text = "OpenCL: " + device.Name;

                            if (preference.UseOpenCL == true && device.Name.Equals(selectedDeviceName))
                            {
                                checkedButton = deviceButton;
                            }
                            _openCLRadioGroup.AddView(deviceButton);

                            //counter++;
                            deviceButton.Click += (sender, args) =>
                            {
                                preference.UseOpenCL        = true;
                                preference.OpenClDeviceName = device.Name;
                                //Toast.MakeText(this.Context, device.Name + " clicked", ToastLength.Short).Show();
                            };
                        }
                    }
                }
            }
            if (checkedButton != null)
            {
                _openCLRadioGroup.Check(checkedButton.Id);
            }
            //_openCLRadioGroup.in

            /*
             * _openCLToggleButton.Checked = preference.UseOpenCL;
             *
             * _openCLToggleButton.CheckedChange += (sender, args) =>
             * {
             *    bool isChecked = args.IsChecked;
             *
             *    if (isChecked && !CvInvoke.HaveOpenCL)
             *    {
             *       _openCLToggleButton.Checked = false;
             *       Toast.MakeText(Context, "No OpenCL compatible device found.", ToastLength.Long).Show();
             *       isChecked = false;
             *    }
             *
             *    preference.UseOpenCL = isChecked;
             * };
             */
            return(dialog);
        }