Changeset 1468


Ignore:
Timestamp:
May 24, 2010, 8:14:16 PM (12 years ago)
Author:
Sven Rech
Message:

some changes to quadratic sieve, to prepare it for use in peer2peer systems

Location:
trunk
Files:
1 added
8 edited

Legend:

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

    r1467 r1468  
    5555        private bool running;
    5656        private Queue yieldqueue;
     57        private AutoResetEvent yieldEvent = new AutoResetEvent(false);
    5758        private IntPtr obj = IntPtr.Zero;
    5859        private volatile int threadcount = 0;
    59         private DateTime start_sieving_time;
    60         private bool sieving_started;
    61         private int start_relations;
    6260        private ArrayList conf_list;
    6361        private bool userStopped = false;
    6462        private FactorManager factorManager;
     63        private PeerToPeer peerToPeer = new PeerToPeer();
     64        private BigInteger sumSize = 0;
    6565
    6666        private static Assembly msieveDLL = null;
     
    7575        public event PluginProgressChangedEventHandler OnPluginProgressChanged;
    7676        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    77         public event PluginProgressChangedEventHandler OnPluginProcessChanged;
     77        public event PluginProgressChangedEventHandler OnPluginProcessChanged;       
    7878
    7979        #endregion
     
    123123        public void Execute()
    124124        {
     125            sumSize = 0;
    125126            userStopped = false;
    126127
     
    339340                res += ts.Seconds + " seconds";
    340341            return res;
    341         }   
    342 
    343         /// <summary>
    344         /// Actualize the progress of the current sieving process
    345         /// </summary>
    346         /// <param name="conf"></param>
    347         /// <param name="num_relations"></param>
    348         /// <param name="max_relations"></param>
    349         private void showProgress(IntPtr conf, int num_relations, int max_relations)
    350         {
    351             if (num_relations == -1)    //sieving finished
    352             {
    353                 ProgressChanged(0.9, 1.0);
    354                 GuiLogMessage("Sieving finished", NotificationLevel.Info);
    355                 quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
    356                 {                   
    357                     quadraticSieveQuickWatchPresentation.factorInfo.Content = "Found enough relations! Please wait...";
    358                 }, null);
    359                 stopThreads();
    360                 if (yieldqueue != null)
    361                     yieldqueue.Clear();
    362             }
    363             else
    364             {
    365                 if (sieving_started)
    366                 {
    367                     TimeSpan diff = DateTime.Now - start_sieving_time;
    368                     double msleft = (diff.TotalMilliseconds / (num_relations - start_relations)) * (max_relations - num_relations);
    369                     if (msleft > 0)
    370                     {
    371                         TimeSpan ts = new TimeSpan(0, 0, 0, 0, (int)msleft);
    372                         String logging_message = "Found " + num_relations + " of " + max_relations + " relations!";
    373                         String timeLeft_message = showTimeSpan(ts) + " left";
    374                         String endtime_message = "" + DateTime.Now.AddMilliseconds((long)msleft);
    375                        
    376                         GuiLogMessage(logging_message + " " + timeLeft_message + ".", NotificationLevel.Debug);
    377                         quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
    378                         {
    379                             quadraticSieveQuickWatchPresentation.logging.Text = logging_message;
    380                             quadraticSieveQuickWatchPresentation.timeLeft.Text = timeLeft_message;
    381                             quadraticSieveQuickWatchPresentation.endTime.Text = endtime_message;
    382                         }
    383                         , null);
    384 
    385                     }
    386                 }
    387 
    388                 if (!sieving_started)
    389                 {
    390                     MethodInfo getCurrentFactor = msieve.GetMethod("getCurrentFactor");
    391                     //GuiLogMessage((String)(getCurrentFactor.Invoke(null, new object[] { conf })), NotificationLevel.Debug);
    392 
    393                     start_relations = num_relations;
    394                     start_sieving_time = DateTime.Now;
    395                     sieving_started = true;
    396                 }
    397 
    398                 ProgressChanged((double)num_relations / max_relations * 0.8 + 0.1, 1.0);               
    399 
    400                 while (yieldqueue.Count != 0)       //get all the results from the helper threads, and store them
    401                 {
    402                     MethodInfo saveYield = msieve.GetMethod("saveYield");
    403                     saveYield.Invoke(null, new object[] { conf, (IntPtr)yieldqueue.Dequeue() });                   
    404                 }               
    405             }
    406342        }
    407343
     
    414350        /// <param name="update">number of relations found</param>
    415351        /// <param name="core_sieve_fcn">pointer to internal sieve function of msieve</param>
    416         private void prepareSieving (IntPtr conf, int update, IntPtr core_sieve_fcn)
     352        private void prepareSieving(IntPtr conf, int update, IntPtr core_sieve_fcn, int max_relations)
    417353        {
    418354            int threads = Math.Min(settings.CoresUsed, Environment.ProcessorCount-1);
     
    420356            this.obj = (IntPtr)getObjFromConf.Invoke(null, new object[] { conf });           
    421357            yieldqueue = Queue.Synchronized(new Queue());
    422             sieving_started = false;
    423358            conf_list = new ArrayList();
    424359
     
    435370            running = true;
    436371            //start helper threads:
    437             for (int i = 0; i < threads; i++)
     372            for (int i = 0; i < threads+1; i++)
    438373            {
    439374                MethodInfo cloneSieveConf = msieve.GetMethod("cloneSieveConf");
     
    442377                WaitCallback worker = new WaitCallback(MSieveJob);
    443378                ThreadPool.QueueUserWorkItem(worker, new object[] { clone, update, core_sieve_fcn, yieldqueue });
     379            }
     380
     381            //manage the yields of the other threads:
     382            manageYields(conf, max_relations);  //this method returns as soon as there are enough relations found
     383            if (userStopped)
     384                return;
     385
     386            //sieving is finished now, so give some informations and stop threads:
     387            GuiLogMessage("Data size: " + (sumSize / 1024) / 1024 + " MB!", NotificationLevel.Debug);
     388            ProgressChanged(0.9, 1.0);
     389            GuiLogMessage("Sieving finished", NotificationLevel.Info);
     390            quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
     391            {
     392                quadraticSieveQuickWatchPresentation.factorInfo.Content = "Found enough relations! Please wait...";
     393            }, null);
     394            stopThreads();
     395            if (yieldqueue != null)
     396                yieldqueue.Clear();
     397        }
     398
     399        private void manageYields(IntPtr conf, int max_relations)
     400        {
     401            MethodInfo serializeYield = msieve.GetMethod("serializeYield");
     402            MethodInfo getNumRelations = msieve.GetMethod("getNumRelations");
     403            int num_relations = (int)getNumRelations.Invoke(null, new object[] { conf });
     404            int start_relations = num_relations;
     405            DateTime start_sieving_time = DateTime.Now;
     406            MethodInfo saveYield = msieve.GetMethod("saveYield");
     407
     408            while (num_relations < max_relations)
     409            {
     410                ProgressChanged((double)num_relations / max_relations * 0.8 + 0.1, 1.0);
     411               
     412                yieldEvent.WaitOne();               //wait until queue is not empty
     413                if (userStopped)
     414                    return;
     415                while (yieldqueue.Count != 0)       //get all the results from the helper threads, and store them
     416                {
     417                    IntPtr yield = (IntPtr)yieldqueue.Dequeue();
     418                    byte[] serializedYield = (byte[])serializeYield.Invoke(null, new object[] { yield });                   
     419
     420                    int compressedSize = peerToPeer.put(serializedYield);
     421                    sumSize += compressedSize;
     422
     423                    saveYield.Invoke(null, new object[] { conf, yield });
     424                }
     425                num_relations = (int)getNumRelations.Invoke(null, new object[] { conf });
     426                showProgressPresentation(max_relations, num_relations, start_relations, start_sieving_time);
     427            }           
     428        }
     429
     430        private void showProgressPresentation(int max_relations, int num_relations, int start_relations, DateTime start_sieving_time)
     431        {
     432            TimeSpan diff = DateTime.Now - start_sieving_time;
     433            double msleft = (diff.TotalMilliseconds / (num_relations - start_relations)) * (max_relations - num_relations);
     434            if (msleft > 0)
     435            {
     436                TimeSpan ts = new TimeSpan(0, 0, 0, 0, (int)msleft);
     437                String logging_message = "Found " + num_relations + " of " + max_relations + " relations!";
     438                String timeLeft_message = showTimeSpan(ts) + " left";
     439                String endtime_message = "" + DateTime.Now.AddMilliseconds((long)msleft);
     440
     441                GuiLogMessage(logging_message + " " + timeLeft_message + ".", NotificationLevel.Debug);
     442                quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback)delegate
     443                {
     444                    quadraticSieveQuickWatchPresentation.logging.Text = logging_message;
     445                    quadraticSieveQuickWatchPresentation.timeLeft.Text = timeLeft_message;
     446                    quadraticSieveQuickWatchPresentation.endTime.Text = endtime_message;
     447                }
     448                , null);
    444449            }
    445450        }
     
    497502                    IntPtr yield = (IntPtr)getYield.Invoke(null, new object[] { clone });
    498503
    499                     //Just for testing the serialize mechanism:
    500                     MethodInfo serializeYield = msieve.GetMethod("serializeYield");
    501                     byte[] serializedYield = (byte[])serializeYield.Invoke(null, new object[] { yield });
    502                     MethodInfo deserializeYield = msieve.GetMethod("deserializeYield");
    503                     yield = (IntPtr)deserializeYield.Invoke(null, new object[] { serializedYield });
    504 
    505504                    yieldqueue.Enqueue(yield);
     505                    yieldEvent.Set();
    506506                }
    507507                catch (Exception ex)
     
    512512                }               
    513513            }
     514
    514515            MethodInfo freeSieveConf = msieve.GetMethod("freeSieveConf");
    515516            freeSieveConf.Invoke(null, new object[] { clone });           
     
    601602            //init msieve with callbacks:
    602603            MethodInfo initMsieve = msieve.GetMethod("initMsieve");
    603             Object callback_struct = Activator.CreateInstance(msieveDLL.GetType("Msieve.callback_struct"));
    604             FieldInfo showProgressField = msieveDLL.GetType("Msieve.callback_struct").GetField("showProgress");
     604            Object callback_struct = Activator.CreateInstance(msieveDLL.GetType("Msieve.callback_struct"));           
    605605            FieldInfo prepareSievingField = msieveDLL.GetType("Msieve.callback_struct").GetField("prepareSieving");
    606             FieldInfo getTrivialFactorlistField = msieveDLL.GetType("Msieve.callback_struct").GetField("getTrivialFactorlist");
    607             Delegate showProgressDel = MulticastDelegate.CreateDelegate(msieveDLL.GetType("Msieve.showProgressDelegate"), this, "showProgress");
     606            FieldInfo getTrivialFactorlistField = msieveDLL.GetType("Msieve.callback_struct").GetField("getTrivialFactorlist");           
    608607            Delegate prepareSievingDel = MulticastDelegate.CreateDelegate(msieveDLL.GetType("Msieve.prepareSievingDelegate"), this, "prepareSieving");
    609             Delegate getTrivialFactorlistDel = MulticastDelegate.CreateDelegate(msieveDLL.GetType("Msieve.getTrivialFactorlistDelegate"), this, "getTrivialFactorlist");
    610             showProgressField.SetValue(callback_struct, showProgressDel);
     608            Delegate getTrivialFactorlistDel = MulticastDelegate.CreateDelegate(msieveDLL.GetType("Msieve.getTrivialFactorlistDelegate"), this, "getTrivialFactorlist");           
    611609            prepareSievingField.SetValue(callback_struct, prepareSievingDel);
    612610            getTrivialFactorlistField.SetValue(callback_struct, getTrivialFactorlistDel);
  • trunk/CrypPlugins/QuadraticSieve/QuadraticSieve.csproj

    r1462 r1468  
    133133  <ItemGroup>
    134134    <Compile Include="FactorManager.cs" />
     135    <Compile Include="PeerToPeer.cs" />
    135136    <Compile Include="Properties\AssemblyInfo.cs" />
    136137    <Compile Include="QuadraticSieve.cs" />
  • trunk/CrypPlugins/QuadraticSieve/msieve/build.vc9/WrapperTester/Program.cs

    r1463 r1468  
    1414        private static bool running;
    1515
    16         static void prepareSieving(IntPtr conf, int update, IntPtr core_sieve_fcn)
     16        static void prepareSieving(IntPtr conf, int update, IntPtr core_sieve_fcn, int max_relations)
    1717        {
    1818            Console.WriteLine("Update: " + update);
     
    4848        {           
    4949            Msieve.callback_struct callbacks = new Msieve.callback_struct();
    50             callbacks.showProgress = delegate(IntPtr conf, int num_relations, int max_relations)
    51             {               
    52                 System.Console.WriteLine("" + num_relations + " of " + max_relations + " relations!");
    53                 if (num_relations != -1)
    54                     while (yieldqueue != null && yieldqueue.Count != 0)
    55                     {
    56                         Msieve.msieve.saveYield(conf, (IntPtr)yieldqueue.Dequeue());
    57                         Console.WriteLine("Get yield from queue.");
    58                     }
    59                 else
    60                     running = false;               
    61             };
    6250            callbacks.prepareSieving = prepareSieving;
    6351            callbacks.getTrivialFactorlist = delegate(IntPtr list, IntPtr obj)
  • trunk/CrypPlugins/QuadraticSieve/msieve/build.vc9/msieve.dll/wrapper.cpp

    r1466 r1468  
    9696namespace Msieve
    9797{
    98         public delegate void showProgressDelegate(IntPtr conf, int num_relations, int max_relations);
    99         public delegate void prepareSievingDelegate(IntPtr conf, int update, IntPtr core_sieve_fcn);
     98        public delegate void prepareSievingDelegate(IntPtr conf, int update, IntPtr core_sieve_fcn, int max_relations);
    10099        public delegate void getTrivialFactorlistDelegate(IntPtr list, IntPtr obj);
    101100
     
    103102        {
    104103        public:
    105                 showProgressDelegate^ showProgress;
    106104                prepareSievingDelegate^ prepareSieving;
    107105                getTrivialFactorlistDelegate^ getTrivialFactorlist;
     
    363361                }
    364362
     363                static int getNumRelations(IntPtr conf)
     364                {
     365                        sieve_conf_t* c = (sieve_conf_t*)conf.ToPointer();
     366                        return c->num_relations +
     367                                c->num_cycles +
     368                                c->components - c->vertices;
     369                }
     370
    365371                static IntPtr msieve_run_core(IntPtr obj, String^ n)
    366372                {
     
    378384}
    379385
    380 extern "C" void showProgress(void* conf, int num_relations, int max_relations)
    381 {       
    382         Msieve::msieve::callbacks->showProgress(IntPtr(conf), num_relations, max_relations);
    383 }
    384 
    385 extern "C" void prepare_sieving(void* conf, int update, void* core_sieve_fcn)
     386extern "C" void prepare_sieving(void* conf, int update, void* core_sieve_fcn, int max_relations)
    386387{
    387         Msieve::msieve::callbacks->prepareSieving(IntPtr(conf), update, IntPtr(core_sieve_fcn));
     388        Msieve::msieve::callbacks->prepareSieving(IntPtr(conf), update, IntPtr(core_sieve_fcn), max_relations);
    388389}
    389390
  • trunk/CrypPlugins/QuadraticSieve/msieve/build.vc9/msieve.dll/wrapper.h

    r1373 r1468  
    22
    33void showProgress(void* conf, int num_relations, int max_relations);
    4 void prepare_sieving(void* conf, int update, void* core_sieve_fcn);
     4void 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

    r655 r1468  
    653653
    654654        if (!(obj->flags & MSIEVE_FLAG_STOP_SIEVING) && num_relations < max_relations)
    655                 prepare_sieving(conf, update, core_sieve_fcn);
     655                prepare_sieving(conf, update, core_sieve_fcn, max_relations);
     656
     657        num_relations = conf->num_relations +
     658                                conf->num_cycles +
     659                                conf->components - conf->vertices;
    656660
    657661        /* sieve until at least that many relations have
     
    659663           partials. This way we can declare sieving to be
    660664           finished the moment enough relations are available */
    661 
     665        /*
    662666        while (!(obj->flags & MSIEVE_FLAG_STOP_SIEVING) &&
    663667                num_relations < max_relations) {
     
    668672                                conf->num_cycles +
    669673                                conf->components - conf->vertices;
    670 
    671                 showProgress(conf, num_relations, max_relations);
    672674
    673675            if (obj->flags & (MSIEVE_FLAG_USE_LOGFILE |
     
    684686                }
    685687        }
    686 
    687         if (!(obj->flags & MSIEVE_FLAG_STOP_SIEVING))
    688                 showProgress(conf, -1, -1);     //We finnished sieving :D
     688        */
    689689
    690690        if (obj->flags & (MSIEVE_FLAG_USE_LOGFILE |
Note: See TracChangeset for help on using the changeset viewer.