Changeset 1798


Ignore:
Timestamp:
Aug 10, 2010, 6:20:11 PM (11 years ago)
Author:
Sven Rech
Message:

fixed some quadratic sieve stuff

Location:
trunk
Files:
7 edited

Legend:

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

    r1773 r1798  
    6565        private PeerToPeerStatusUpdater peerToPeerStatusUpdater;
    6666        private bool usePeer2Peer;
     67        private bool otherPeerFinished;
    6768        private bool useGnuplot = false;
    6869        private StreamWriter gnuplotFile;
     
    191192
    192193                userStopped = false;
     194                otherPeerFinished = false;
    193195
    194196                if (InputNumber != 0)
     
    261263                        Debug.Assert(factorManager.CalculateNumber() == InputNumber);
    262264                        OutputFactors = factorManager.getPrimeFactors();
     265
     266                        ProgressChanged(1, 1);
    263267                    }
    264268                    else
     
    278282                        , null);
    279283                    }
    280 
    281                     ProgressChanged(1, 1);
    282 
    283284                }
    284285                if (useGnuplot)
     
    462463        /// Called by msieve
    463464        ///
    464         /// </summary>
     465        /// </summary>       
    465466        /// <param name="conf">pointer to configuration</param>
    466467        /// <param name="update">number of relations found</param>
    467468        /// <param name="core_sieve_fcn">pointer to internal sieve function of msieve</param>
    468         private void prepareSieving(IntPtr conf, int update, IntPtr core_sieve_fcn, int max_relations)
    469         {
    470             int threads = Math.Min(settings.CoresUsed, Environment.ProcessorCount-1);
    471             MethodInfo getObjFromConf = msieve.GetMethod("getObjFromConf");
    472             this.obj = (IntPtr)getObjFromConf.Invoke(null, new object[] { conf });           
    473             relationPackageQueue = Queue.Synchronized(new Queue());
    474             conf_list = new ArrayList();
    475 
    476             String message = "Start sieving using " + (threads + 1) + " cores!";
    477             GuiLogMessage(message, NotificationLevel.Info);
    478             quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
    479             {
    480                 quadraticSieveQuickWatchPresentation.logging.Text = message;
    481                 if (usePeer2Peer)
    482                     quadraticSieveQuickWatchPresentation.relationsInfo.Content = "";
    483             }
    484             , null);         
    485 
    486             ProgressChanged(0.1, 1.0);
    487 
    488             running = true;
    489             //start helper threads:
    490             relationsPerMS = new double[threads + 1];
    491             for (int i = 0; i < threads+1; i++)
    492             {
    493                 MethodInfo cloneSieveConf = msieve.GetMethod("cloneSieveConf");
    494                 IntPtr clone = (IntPtr)cloneSieveConf.Invoke(null, new object[] { conf });               
    495                 conf_list.Add(clone);
    496                 WaitCallback worker = new WaitCallback(MSieveJob);
    497                 ThreadPool.QueueUserWorkItem(worker, new object[] { clone, update, core_sieve_fcn, relationPackageQueue, i });
    498             }
    499 
    500             //manage the relation packages of the other threads:
    501             manageRelationPackages(conf, max_relations);  //this method returns as soon as there are enough relations found
    502             if (userStopped)
    503                 return;
    504 
    505             //sieving is finished now, so give some informations and stop threads:
    506             ProgressChanged(0.9, 1.0);
    507             GuiLogMessage("Sieving finished", NotificationLevel.Info);
    508             quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
    509             {
    510                 quadraticSieveQuickWatchPresentation.timeLeft.Text = "";
    511                 quadraticSieveQuickWatchPresentation.endTime.Text = "";
    512                 quadraticSieveQuickWatchPresentation.factorInfo.Content = "Found enough relations! Please wait...";
    513             }, null);
    514            
    515             stopThreads();
    516             if (relationPackageQueue != null)
    517                 relationPackageQueue.Clear();
     469        /// <returns>true if enough relations found, false if not</returns>
     470        private bool prepareSieving(IntPtr conf, int update, IntPtr core_sieve_fcn, int max_relations)
     471        {
     472            try
     473            {
     474                int threads = Math.Min(settings.CoresUsed, Environment.ProcessorCount - 1);
     475                MethodInfo getObjFromConf = msieve.GetMethod("getObjFromConf");
     476                this.obj = (IntPtr)getObjFromConf.Invoke(null, new object[] { conf });
     477                relationPackageQueue = Queue.Synchronized(new Queue());
     478                conf_list = new ArrayList();
     479
     480                String message = "Start sieving using " + (threads + 1) + " cores!";
     481                GuiLogMessage(message, NotificationLevel.Info);
     482                quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
     483                {
     484                    quadraticSieveQuickWatchPresentation.logging.Text = message;
     485                    if (usePeer2Peer)
     486                        quadraticSieveQuickWatchPresentation.relationsInfo.Content = "";
     487                }
     488                , null);
     489
     490                ProgressChanged(0.1, 1.0);
     491
     492                running = true;
     493                //start helper threads:
     494                relationsPerMS = new double[threads + 1];
     495                for (int i = 0; i < threads + 1; i++)
     496                {
     497                    MethodInfo cloneSieveConf = msieve.GetMethod("cloneSieveConf");
     498                    IntPtr clone = (IntPtr)cloneSieveConf.Invoke(null, new object[] { conf });
     499                    conf_list.Add(clone);
     500                    WaitCallback worker = new WaitCallback(MSieveJob);
     501                    ThreadPool.QueueUserWorkItem(worker, new object[] { clone, update, core_sieve_fcn, relationPackageQueue, i });
     502                }
     503
     504                //manage the relation packages of the other threads:
     505                manageRelationPackages(conf, max_relations);  //this method returns as soon as there are enough relations found
     506                if (userStopped)
     507                    return false;
     508
     509                //sieving is finished now, so give some informations and stop threads:
     510                ProgressChanged(0.9, 1.0);
     511                GuiLogMessage("Sieving finished", NotificationLevel.Info);
     512                quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
     513                {
     514                    quadraticSieveQuickWatchPresentation.timeLeft.Text = "";
     515                    quadraticSieveQuickWatchPresentation.endTime.Text = "";
     516                    quadraticSieveQuickWatchPresentation.factorInfo.Content = "Found enough relations! Please wait...";
     517                }, null);
     518
     519                if (relationPackageQueue != null)
     520                    relationPackageQueue.Clear();
     521            }
     522            catch (AlreadySievedException)
     523            {
     524                ProgressChanged(0.9, 1.0);
     525                GuiLogMessage("Another peer already finished factorization of composite factor. Sieving next one...", NotificationLevel.Info);
     526                quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
     527                {
     528                    quadraticSieveQuickWatchPresentation.timeLeft.Text = "";
     529                    quadraticSieveQuickWatchPresentation.endTime.Text = "";
     530                    quadraticSieveQuickWatchPresentation.factorInfo.Content = "Other peer finished sieving!";
     531                }, null);
     532                otherPeerFinished = true;
     533                return false;
     534            }
     535            finally
     536            {
     537                stopThreads();
     538            }
     539            return true;
    518540        }
    519541
     
    668690                        peerToPeer.SetFactor(compositeFactor);
    669691
    670                     try
    671                     {
    672                         //now start quadratic sieve on it:               
    673                         IntPtr resultList = (IntPtr)msieve_run_core.Invoke(null, new object[2] { obj, compositeFactor.ToString() });
    674                         if (resultList == IntPtr.Zero)
    675                             throw new NotSievableException();
    676 
    677                         if (userStopped)
    678                             return;
    679 
    680                         factorManager.ReplaceCompositeByFactors(compositeFactor, resultList);   //add the result list to factorManager
    681 
    682                         if (usePeer2Peer)
    683                             peerToPeer.SyncFactorManager(factorManager);
    684                     }
    685                     catch (AlreadySievedException)
    686                     {
    687                         GuiLogMessage("Another peer already finished factorization of composite factor" + compositeFactor + ". Sieving next one...", NotificationLevel.Info);
    688                     }
     692                    //now start quadratic sieve on it:               
     693                    IntPtr resultList = (IntPtr)msieve_run_core.Invoke(null, new object[2] { obj, compositeFactor.ToString() });
     694                    if (otherPeerFinished)
     695                    {
     696                        otherPeerFinished = false;
     697                        continue;
     698                    }
     699
     700                    if (resultList == IntPtr.Zero)
     701                        throw new NotSievableException();
     702
     703                    if (userStopped)
     704                        return;
     705
     706                    factorManager.ReplaceCompositeByFactors(compositeFactor, resultList);   //add the result list to factorManager
     707
     708                    if (usePeer2Peer)
     709                        peerToPeer.SyncFactorManager(factorManager);
    689710                }
    690711            }
  • trunk/CrypPlugins/QuadraticSieve/msieve/build.vc9/WrapperTester/Program.cs

    r1692 r1798  
    1414        private static bool running;
    1515
    16         static void prepareSieving(IntPtr conf, int update, IntPtr core_sieve_fcn, int max_relations)
     16        static bool prepareSieving(IntPtr conf, int update, IntPtr core_sieve_fcn, int max_relations)
    1717        {
    1818            Console.WriteLine("Update: " + update);
     
    2525            running = true;
    2626            ThreadPool.QueueUserWorkItem(worker, new object[] { clone, update, core_sieve_fcn, relationpackagequeue });
     27            return true;
    2728        }
    2829
  • trunk/CrypPlugins/QuadraticSieve/msieve/build.vc9/msieve.dll/wrapper.cpp

    r1781 r1798  
    9696namespace Msieve
    9797{
    98         public delegate void prepareSievingDelegate(IntPtr conf, int update, IntPtr core_sieve_fcn, int max_relations);
     98        public delegate bool prepareSievingDelegate(IntPtr conf, int update, IntPtr core_sieve_fcn, int max_relations);
    9999        public delegate void putTrivialFactorlistDelegate(IntPtr list, IntPtr obj);
    100100
     
    397397}
    398398
    399 extern "C" void prepare_sieving(void* conf, int update, void* core_sieve_fcn, int max_relations)
     399extern "C" int prepare_sieving(void* conf, int update, void* core_sieve_fcn, int max_relations)
    400400{
    401         Msieve::msieve::callbacks->prepareSieving(IntPtr(conf), update, IntPtr(core_sieve_fcn), max_relations);
     401        return Msieve::msieve::callbacks->prepareSieving(IntPtr(conf), update, IntPtr(core_sieve_fcn), max_relations) ? 1 : 0;
    402402}
    403403
  • trunk/CrypPlugins/QuadraticSieve/msieve/build.vc9/msieve.dll/wrapper.h

    r1692 r1798  
    22
    33void showProgress(void* conf, int num_relations, int max_relations);
    4 void prepare_sieving(void* conf, int update, void* core_sieve_fcn, int max_relations);
     4int prepare_sieving(void* conf, int update, void* core_sieve_fcn, int max_relations);
    55void throwException(char* message);
    66
  • trunk/CrypPlugins/QuadraticSieve/msieve/mpqs/sieve.c

    r1692 r1798  
    653653
    654654        if (!(obj->flags & MSIEVE_FLAG_STOP_SIEVING) && num_relations < max_relations)
    655                 prepare_sieving(conf, update, core_sieve_fcn, max_relations);
     655                if (!prepare_sieving(conf, update, core_sieve_fcn, max_relations))
     656                        return -1;
    656657
    657658        num_relations = conf->num_relations +
Note: See TracChangeset for help on using the changeset viewer.