Changeset 2110


Ignore:
Timestamp:
Nov 20, 2010, 12:03:24 AM (11 years ago)
Author:
Sven Rech
Message:

KeySearcher OpenCL optimization.
Doesn't work perfect yet :)

Location:
trunk
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPluginBase/Control/KeyTranslator.cs

    r2087 r2110  
    7272        /// </summary>
    7373        /// <param name="code"></param>
    74         /// <param name="maxKeys"></param>
     74        /// <param name="approximateNumberOfKeys"></param>
    7575        /// <returns></returns>
    76         string ModifyOpenCLCode(string code, int maxKeys);
     76        string ModifyOpenCLCode(string code, int approximateNumberOfKeys);
    7777
    7878        /// <summary>
  • trunk/CrypPlugins/AES/OpenCL/AESOpenCL.cl

    r2086 r2110  
    881881}
    882882
    883 kernel void bruteforceKernel(global unsigned char *userKey, global float *results)
     883kernel void bruteforceKernel(global unsigned char *userKey, global float *results, int add)
    884884{
    885885        size_t x = get_global_id(0);
    886886       
    887887        AES_KEY key;   
    888         AES_set_decrypt_key(userKey, $$BITS$$, &(key), x);
     888        AES_set_decrypt_key(userKey, $$BITS$$, &(key), (x+add));
    889889
    890890        //possibility for cost function to initialize some stuff here:
  • trunk/CrypPlugins/KeySearcher/KeySearcher.cs

    r2098 r2110  
    371371
    372372            KeySearcherOpenCLCode keySearcherOpenCLCode = null;
     373            KeySearcherOpenCLSubbatchOptimizer keySearcherOpenCLSubbatchOptimizer = null;
    373374            if (useOpenCL)
    374                 keySearcherOpenCLCode = new KeySearcherOpenCLCode(this, encryptedData, sender, CostMaster, 256 * 64);
     375            {
     376                keySearcherOpenCLCode = new KeySearcherOpenCLCode(this, encryptedData, sender, CostMaster, 256 * 256 * 256 * 16);
     377                keySearcherOpenCLSubbatchOptimizer = new KeySearcherOpenCLSubbatchOptimizer(oclManager.CQ[settings.OpenCLDevice].Device.MaxWorkItemSizes.Aggregate(1, (x, y) => (x * (int)y)));
     378            }
    375379
    376380            try
     
    399403                            try
    400404                            {
    401                                 finish = BruteforceOpenCL(keySearcherOpenCLCode, keyTranslator, sender, bytesToUse);
     405                                finish = BruteforceOpenCL(keySearcherOpenCLCode, keySearcherOpenCLSubbatchOptimizer, keyTranslator, sender, bytesToUse, parameters);
    402406                            }
    403407                            catch (Exception)
     
    410414                        int progress = keyTranslator.GetProgress();
    411415
    412                         doneKeysArray[threadid] += progress;
    413                         keycounterArray[threadid] += progress;
    414                         keysLeft[threadid] -= progress;
     416                        if (!useOpenCL)
     417                        {
     418                            doneKeysArray[threadid] += progress;
     419                            keycounterArray[threadid] += progress;
     420                            keysLeft[threadid] -= progress;
     421                        }
    415422
    416423                    } while (!finish && !stop);
     
    429436        }
    430437
    431         private unsafe bool BruteforceOpenCL(KeySearcherOpenCLCode keySearcherOpenCLCode, IKeyTranslator keyTranslator, IControlEncryption sender, int bytesToUse)
    432         {
    433             float[] costArray = null;
     438        private unsafe bool BruteforceOpenCL(KeySearcherOpenCLCode keySearcherOpenCLCode, KeySearcherOpenCLSubbatchOptimizer keySearcherOpenCLSubbatchOptimizer, IKeyTranslator keyTranslator, IControlEncryption sender, int bytesToUse, object[] parameters)
     439        {
     440            int threadid = (int)parameters[1];
     441            BigInteger[] doneKeysArray = (BigInteger[])parameters[2];
     442            BigInteger[] keycounterArray = (BigInteger[])parameters[3];
     443            BigInteger[] keysLeft = (BigInteger[])parameters[4];
    434444            try
    435445            {
    436446                Kernel bruteforceKernel = keySearcherOpenCLCode.GetBruteforceKernel(oclManager, keyTranslator);
    437                 costArray = new float[keyTranslator.GetOpenCLBatchSize()];
     447               
    438448                int deviceIndex = settings.OpenCLDevice;
    439449               
    440                 Mem costs = oclManager.Context.CreateBuffer(MemFlags.READ_ONLY, costArray.Length * 4);
    441                 IntPtr[] globalWorkSize = { (IntPtr)keyTranslator.GetOpenCLBatchSize() };
    442 
    443450                Mem userKey;
    444451                var key = keyTranslator.GetKey();
     
    447454
    448455                bruteforceKernel.SetArg(0, userKey);
     456
     457                int subbatches = keySearcherOpenCLSubbatchOptimizer.GetAmountOfSubbatches(keyTranslator);
     458                GuiLogMessage(string.Format("Now using {0} subbatches", subbatches), NotificationLevel.Info);
     459
     460                int subbatchSize = keyTranslator.GetOpenCLBatchSize() / subbatches;
     461                float[] costArray = new float[subbatchSize];
     462                Mem costs = oclManager.Context.CreateBuffer(MemFlags.READ_ONLY, costArray.Length * 4);
    449463                bruteforceKernel.SetArg(1, costs);
    450464
    451                 oclManager.CQ[deviceIndex].EnqueueNDRangeKernel(bruteforceKernel, 1, null, globalWorkSize, null);
    452                 oclManager.CQ[deviceIndex].EnqueueBarrier();
    453 
    454                 Event e;
    455                 fixed (float* costa = costArray)
    456                     oclManager.CQ[deviceIndex].EnqueueReadBuffer(costs, true, 0, costArray.Length * 4, new IntPtr((void*)costa), 0, null, out e);
    457 
    458                 e.Wait();
     465                IntPtr[] globalWorkSize = { (IntPtr)subbatchSize };
     466
     467                keySearcherOpenCLSubbatchOptimizer.BeginMeasurement();
     468
     469                for (int i = 0; i < subbatches; i++)
     470                {
     471                    bruteforceKernel.SetArg(2, i * subbatchSize);
     472                    oclManager.CQ[deviceIndex].EnqueueNDRangeKernel(bruteforceKernel, 1, null, globalWorkSize, null);
     473                    oclManager.CQ[deviceIndex].EnqueueBarrier();
     474
     475                    Event e;
     476                    fixed (float* costa = costArray)
     477                        oclManager.CQ[deviceIndex].EnqueueReadBuffer(costs, true, 0, costArray.Length * 4, new IntPtr((void*)costa), 0, null, out e);
     478
     479                    e.Wait();
     480
     481                    checkOpenCLResults(keyTranslator, costArray, sender, bytesToUse, i * subbatchSize);
     482
     483                    doneKeysArray[threadid] += subbatchSize;
     484                    keycounterArray[threadid] += subbatchSize;
     485                    keysLeft[threadid] -= subbatchSize;
     486                }
     487
     488                keySearcherOpenCLSubbatchOptimizer.EndMeasurement();
     489
    459490                costs.Dispose();
    460491            }
     
    466497            }
    467498
    468             //Check results:
     499            return !keyTranslator.NextOpenCLBatch();
     500        }
     501
     502        private void checkOpenCLResults(IKeyTranslator keyTranslator, float[] costArray, IControlEncryption sender, int bytesToUse, int add)
     503        {
    469504            var op = this.costMaster.getRelationOperator();
    470505            for (int i = 0; i < costArray.Length; i++)
     
    474509                    || (op == RelationOperator.LessThen) && (cost < value_threshold))
    475510                {
    476                     ValueKey valueKey = new ValueKey {value = cost, key = keyTranslator.GetKeyRepresentation(i)};
     511                    ValueKey valueKey = new ValueKey { value = cost, key = keyTranslator.GetKeyRepresentation(i + add) };
    477512                    valueKey.keya = keyTranslator.GetKeyFromRepresentation(valueKey.key);
    478513                    valueKey.decryption = sender.Decrypt(this.encryptedData, valueKey.keya, InitVector, bytesToUse);
     
    480515                }
    481516            }
    482 
    483             return !keyTranslator.NextOpenCLBatch();
    484517        }
    485518
     
    714747            while (!stop)
    715748            {
    716                 Thread.Sleep(1000);
     749                Thread.Sleep(2000);
    717750
    718751                updateToplist();
  • trunk/CrypPlugins/KeySearcher/KeySearcher.csproj

    r2094 r2110  
    120120    <Compile Include="KeyPattern\KeyMovement.cs" />
    121121    <Compile Include="KeySearcher.cs" />
     122    <Compile Include="OpenCL\KeySearcherOpenCLSubbatchOptimizer.cs" />
    122123    <Compile Include="KeyTranslators\ByteArrayKeyTranslator.cs" />
    123124    <Compile Include="OpenCL\KeySearcherOpenCLCode.cs" />
  • trunk/CrypPlugins/KeySearcher/KeyTranslators/ByteArrayKeyTranslator.cs

    r2087 r2110  
    228228        }
    229229
    230         public string ModifyOpenCLCode(string code, int maxKeys)
     230        public string ModifyOpenCLCode(string code, int approximateNumberOfKeys)
    231231        {
    232232            string[] byteReplaceStrings = new string[32];
     
    236236            //Find out how many wildcards/keys we can bruteforce at once:
    237237            int j = movementStatus.Length - 1;
    238             int size = 1;
    239             while ((size < maxKeys) && (j >= 0) && (movementStatus[j] == 0))
     238            long size = 1;
     239            while (((size * keyMovements[j].Count()) <= approximateNumberOfKeys) && (j >= 0) && (movementStatus[j] == 0))
    240240                size *= keyMovements[j--].Count();
    241241
    242242            if (size < 256)
    243                 return null;    //it's futile to use OpenCL for so few keys
     243                throw new Exception("Amount of keys to small to process with OpenCL.");    //it's futile to use OpenCL for so few keys
    244244
    245245            //generate the key movement string:
     
    296296            //progress:
    297297            openCLIndex = j;
    298             openCLSize = size;
     298            openCLSize = (int)size;
    299299
    300300            return code;
  • trunk/CrypPlugins/KeySearcher/OpenCL/KeySearcherOpenCLCode.cs

    r2098 r2110  
    1515        private IControlCost controlCost;
    1616        private IControlEncryption encryptionController;
    17         private int maxKeys;
     17        private int approximateNumberOfKeys;
    1818
    1919        private IKeyTranslator keyTranslatorOfCode = null;
     
    2121        private Kernel openCLKernel = null;
    2222
    23         public KeySearcherOpenCLCode(KeySearcher keySearcher, byte[] encryptedData, IControlEncryption encryptionController, IControlCost controlCost, int maxKeys)
     23        public KeySearcherOpenCLCode(KeySearcher keySearcher, byte[] encryptedData, IControlEncryption encryptionController, IControlCost controlCost, int approximateNumberOfKeys)
    2424        {
    2525            this.keySearcher = keySearcher;
     
    2727            this.encryptionController = encryptionController;
    2828            this.controlCost = controlCost;
    29             this.maxKeys = maxKeys;
     29            this.approximateNumberOfKeys = approximateNumberOfKeys;
    3030        }
    3131
     
    5959
    6060            //put key movement of pattern into code:
    61             code = keyTranslator.ModifyOpenCLCode(code, maxKeys);
     61            code = keyTranslator.ModifyOpenCLCode(code, approximateNumberOfKeys);
    6262
    6363            keyTranslatorOfCode = keyTranslator;
  • trunk/CrypPlugins/SDES/SDES.cs

    r2087 r2110  
    789789        }
    790790
    791         public string ModifyOpenCLCode(string code, int maxKeys)
     791        public string ModifyOpenCLCode(string code, int approximateNumberOfKeys)
    792792        {
    793793            throw new NotImplementedException();
Note: See TracChangeset for help on using the changeset viewer.