Ejemplo n.º 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;
            }
        }
Ejemplo n.º 2
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);
        }