Changeset 2194


Ignore:
Timestamp:
Dec 10, 2010, 4:32:19 PM (11 years ago)
Author:
Sven Rech
Message:

it should be possible now to use more opencl devices simultaneously (can't test it, because I only have one).

Location:
trunk/CrypPlugins/KeySearcher
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/KeySearcher/KeySearcher.cs

    r2189 r2194  
    255255
    256256            settings = new KeySearcherSettings(this, oclManager);
    257 
    258             if (OpenCL.NumberOfPlatforms == 0)
    259             {
    260                 settings.UseOpenCL = false;
    261             }
    262257           
    263258            QuickWatchPresentation = new QuickWatch();
     
    267262
    268263            settings.PropertyChanged += SettingsPropertyChanged;
    269             ((QuickWatch)QuickWatchPresentation).IsOpenCLEnabled = settings.UseOpenCL;
     264            ((QuickWatch)QuickWatchPresentation).IsOpenCLEnabled = (settings.DeviceSettings.Count(x => x.useDevice) > 0);
    270265
    271266            localBruteForceStopwatch = new Stopwatch();
     
    281276        {
    282277            ((QuickWatch)QuickWatchPresentation).IsP2PEnabled = settings.UsePeerToPeer;
    283             ((QuickWatch)QuickWatchPresentation).IsOpenCLEnabled = settings.UseOpenCL;
     278            ((QuickWatch)QuickWatchPresentation).IsOpenCLEnabled = (settings.DeviceSettings.Count(x => x.useDevice) > 0);
    284279            p2PQuickWatchPresentation.UpdateSettings(this, settings);
    285280        }
     
    388383            int bytesToUse = (int)parameters[7];
    389384            Stack threadStack = (Stack)parameters[8];
    390             bool useOpenCL = (bool)parameters[9];
     385            var openCLDeviceSettings = (KeySearcherSettings.OpenCLDeviceSettings)parameters[9];
    391386
    392387            KeySearcherOpenCLCode keySearcherOpenCLCode = null;
    393388            KeySearcherOpenCLSubbatchOptimizer keySearcherOpenCLSubbatchOptimizer = null;
    394             if (useOpenCL)
     389            if (openCLDeviceSettings != null)
    395390            {
    396391                keySearcherOpenCLCode = new KeySearcherOpenCLCode(this, encryptedData, sender, CostMaster, 256 * 256 * 256 * 16);
    397                 keySearcherOpenCLSubbatchOptimizer = new KeySearcherOpenCLSubbatchOptimizer(settings.OpenCLMode, oclManager.CQ[settings.OpenCLDevice].Device.MaxWorkItemSizes.Aggregate(1, (x, y) => (x * (int)y)) / 8);
     392                keySearcherOpenCLSubbatchOptimizer = new KeySearcherOpenCLSubbatchOptimizer(openCLDeviceSettings.mode,
     393                        oclManager.CQ[openCLDeviceSettings.index].Device.MaxWorkItemSizes.Aggregate(1, (x, y) => (x * (int)y)) / 8);
     394
    398395                ((QuickWatch)QuickWatchPresentation).Dispatcher.BeginInvoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
    399396                {
     
    422419                        keyTranslator = ShareKeys(patterns, threadid, keysLeft, keyTranslator, threadStack);
    423420
    424                         if (!useOpenCL)         //CPU
     421                        if (openCLDeviceSettings == null)         //CPU
    425422                        {
    426423                            finish = BruteforceCPU(keyTranslator, sender, bytesToUse);
     
    434431                            catch (Exception)
    435432                            {
    436                                 useOpenCL = false;
     433                                openCLDeviceSettings.useDevice = false;
    437434                                ((QuickWatch)QuickWatchPresentation).Dispatcher.BeginInvoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
    438435                                {
     
    447444                        int progress = keyTranslator.GetProgress();
    448445
    449                         if (!useOpenCL)
     446                        if (openCLDeviceSettings == null)
    450447                        {
    451448                            doneKeysArray[threadid] += progress;
     
    477474            BigInteger[] keycounterArray = (BigInteger[])parameters[4];
    478475            BigInteger[] keysLeft = (BigInteger[])parameters[5];
     476            var openCLDeviceSettings = (KeySearcherSettings.OpenCLDeviceSettings)parameters[9];
    479477            try
    480478            {
    481479                Kernel bruteforceKernel = keySearcherOpenCLCode.GetBruteforceKernel(oclManager, keyTranslator);
    482                
    483                 int deviceIndex = settings.OpenCLDevice;
     480
     481                int deviceIndex = openCLDeviceSettings.index;
    484482               
    485483                Mem userKey;
     
    11381136        private void StartThreads(IControlEncryption sender, int bytesToUse, KeyPattern.KeyPattern[] patterns, BigInteger[] doneKeysA, BigInteger[] openCLDoneKeysA, BigInteger[] keycounters, BigInteger[] keysleft, Stack threadStack)
    11391137        {
    1140             for (int i = 0; i < patterns.Length; i++)
     1138            //First start the opencl threads:
     1139            int i = 0;
     1140            foreach (var ds in settings.DeviceSettings)
     1141            {
     1142                if (ds.useDevice)
     1143                {
     1144                    WaitCallback worker = new WaitCallback(KeySearcherJob);
     1145                    doneKeysA[i] = new BigInteger();
     1146                    openCLDoneKeysA[i] = new BigInteger();
     1147                    keycounters[i] = new BigInteger();
     1148
     1149                    ThreadPool.QueueUserWorkItem(worker, new object[] { patterns, i, doneKeysA, openCLDoneKeysA, keycounters, keysleft, sender, bytesToUse, threadStack, ds });
     1150                    i++;
     1151                }
     1152            }
     1153
     1154            //Then the CPU threads:
     1155            for (; i < patterns.Length; i++)
    11411156            {
    11421157                WaitCallback worker = new WaitCallback(KeySearcherJob);
     
    11441159                openCLDoneKeysA[i] = new BigInteger();
    11451160                keycounters[i] = new BigInteger();
    1146                 bool useOpenCL = false;
    1147 
    1148                 if (settings.UseOpenCL && (i == patterns.Length - 1))     //Last thread is the OpenCL thread
    1149                     useOpenCL = true;
    1150 
    1151                 ThreadPool.QueueUserWorkItem(worker, new object[] { patterns, i, doneKeysA, openCLDoneKeysA, keycounters, keysleft, sender, bytesToUse, threadStack, useOpenCL });
     1161
     1162                ThreadPool.QueueUserWorkItem(worker, new object[] { patterns, i, doneKeysA, openCLDoneKeysA, keycounters, keysleft, sender, bytesToUse, threadStack, null });
    11521163            }
    11531164        }
     
    11561167        {
    11571168            int threads = settings.CoresUsed;
    1158             if (settings.UseOpenCL)
    1159                 threads++;
     1169            threads += settings.DeviceSettings.Count(x => x.useDevice);
    11601170
    11611171            if (threads < 1)
  • trunk/CrypPlugins/KeySearcher/KeySearcherSettings.cs

    r2183 r2194  
    11using System;
     2using System.Collections.Generic;
    23using System.Windows;
    34using Cryptool.PluginBase;
     
    1718        private const string GroupOpenCL = "OpenCL";
    1819
     20        public class OpenCLDeviceSettings
     21        {
     22            public string name;
     23            public int index;
     24            public bool useDevice;
     25            public int mode;
     26        }
     27
     28        private List<OpenCLDeviceSettings> deviceSettings = new List<OpenCLDeviceSettings>();
     29        public List<OpenCLDeviceSettings> DeviceSettings
     30        {
     31            get
     32            {
     33                return deviceSettings;
     34            }
     35        }
     36
    1937        public event TaskPaneAttributeChangedHandler TaskPaneAttributeChanged;
    2038
     
    2442
    2543            devicesAvailable.Clear();
     44            int c = 0;
    2645            if (oclManager != null)
    2746                foreach (var device in oclManager.Context.Devices)
    28                     devicesAvailable.Add(device.Vendor + ":" + device.Name);
     47                {
     48                    string deviceName = device.Vendor + ":" + device.Name;
     49                    deviceSettings.Add(new OpenCLDeviceSettings() {name = deviceName, index = c, mode = 1, useDevice = false});
     50                    devicesAvailable.Add(deviceName);
     51                    c++;
     52                }
    2953
    3054            CoresAvailable.Clear();
     
    250274        }
    251275
    252         private bool useOpenCL;
    253         [TaskPane("Use OpenCL", "If checked, an OpenCL device is used for bruteforcing.",
    254             GroupOpenCL, 1, false, DisplayLevel.Experienced, ControlType.CheckBox)]
    255         public bool UseOpenCL
    256         {
    257             get { return useOpenCL; }
    258             set
    259             {
    260                 if (value != useOpenCL)
    261                 {
    262                     useOpenCL = value;
    263                     hasChanges = true;
    264                     OnPropertyChanged("UseOpenCL");
    265 
    266                     DeviceVisibility();
    267                 }
    268             }
    269         }
    270 
    271         private void DeviceVisibility()
    272         {
    273             if (TaskPaneAttributeChanged == null)
    274                 return;
    275 
    276             Visibility visibility = UseOpenCL ? Visibility.Visible : Visibility.Collapsed;
    277             TaskPaneAttributeChanged(this, new TaskPaneAttributeChangedEventArgs(new TaskPaneAttribteContainer("OpenCLDevice", visibility)));
    278             TaskPaneAttributeChanged(this, new TaskPaneAttributeChangedEventArgs(new TaskPaneAttribteContainer("OpenCLMode", visibility)));
    279         }
    280 
    281276        private void OpenCLGroupVisiblity()
    282277        {
     
    297292                TaskPaneAttributeChanged(this, new TaskPaneAttributeChangedEventArgs(new TaskPaneAttribteContainer("UseOpenCL", Visibility.Visible)));
    298293                TaskPaneAttributeChanged(this, new TaskPaneAttributeChangedEventArgs(new TaskPaneAttribteContainer("NoOpenCL", Visibility.Collapsed)));
    299                 DeviceVisibility();
    300294            }
    301295        }
    302296
    303297        private int openCLDevice;
    304         [TaskPane("OpenCL Device", "Choose the OpenCL device you want to use.", GroupOpenCL, 2, false, DisplayLevel.Experienced, ControlType.DynamicComboBox, new string[] { "DevicesAvailable" })]
     298        [TaskPane("OpenCL Device", "Choose the OpenCL device you want to use.", GroupOpenCL, 1, false, DisplayLevel.Experienced, ControlType.DynamicComboBox, new string[] { "DevicesAvailable" })]
    305299        public int OpenCLDevice
    306300        {
     
    311305                {
    312306                    this.openCLDevice = value;
     307                    UseOpenCL = deviceSettings[value].useDevice;
     308                    OpenCLMode = deviceSettings[value].mode;
    313309                    OnPropertyChanged("OpenCLDevice");
    314310                    HasChanges = true;
     
    317313        }
    318314
    319         private int openCLMode = 1;
     315        [TaskPane("Use selected device", "If checked, the selected OpenCL device is used for bruteforcing.",
     316            GroupOpenCL, 2, false, DisplayLevel.Experienced, ControlType.CheckBox)]
     317        public bool UseOpenCL
     318        {
     319            get { return deviceSettings[OpenCLDevice].useDevice; }
     320            set
     321            {
     322                if (value != deviceSettings[OpenCLDevice].useDevice)
     323                {
     324                    deviceSettings[OpenCLDevice].useDevice = value;
     325                    hasChanges = true;
     326                    OnPropertyChanged("UseOpenCL");
     327                    //DeviceVisibility();
     328                }
     329            }
     330        }
     331
    320332        [TaskPane("OpenCL Mode", "Choose the OpenCL mode you want to use.", GroupOpenCL, 3, false, DisplayLevel.Experienced, ControlType.RadioButton, new string[] { "Low Load", "Normal Load", "High Load (use with caution)" })]
    321333        public int OpenCLMode
    322334        {
    323             get { return this.openCLMode; }
    324             set
    325             {
    326                 if (value != this.openCLMode)
    327                 {
    328                     this.openCLMode = value;
     335            get { return deviceSettings[OpenCLDevice].mode; }
     336            set
     337            {
     338                if (value != deviceSettings[OpenCLDevice].mode)
     339                {
     340                    deviceSettings[OpenCLDevice].mode = value;
    329341                    OnPropertyChanged("OpenCLMode");
    330342                    HasChanges = true;
Note: See TracChangeset for help on using the changeset viewer.