示例#1
0
        public void Save(XmlDocument xml, XmlNode parent)
        {
            var node = parent.AppendChild(xml.CreateElement("Input"));

            node.AppendChild(xml.CreateElement("VisibleName")).InnerText     = VisibleName;
            node.AppendChild(xml.CreateElement("Key")).InnerText             = Key.ToString();
            node.AppendChild(xml.CreateElement("InputNumber")).InnerText     = InputNumber.ToString();
            node.AppendChild(xml.CreateElement("ConnectedOutput")).InnerText = ConnectedOutput;
        }
示例#2
0
        public void Execute()
        {
            _statusNr     = 0;
            _stop         = false;
            OutputFactors = null;
            ProgressChanged(0, 100);

            try
            {
                if (InputNumber < 1)
                {
                    GuiLogMessage("Can't factorize number smaller than 1", NotificationLevel.Error);
                    return;
                }

                var    inputString = InputNumber.ToString();
                string name;
                if (inputString.Length > 10)
                {
                    name = inputString.Substring(0, 10) + "_" + inputString.GetHashCode();
                }
                else
                {
                    name = inputString;
                }
                var nFile = Path.Combine(_directoryName, name + ".n");
                if (!File.Exists(nFile))
                {
                    using (var numberFile = File.CreateText(nFile))
                    {
                        numberFile.WriteLine("n: " + inputString);
                    }
                }

                Directory.SetCurrentDirectory(_directoryName);
                var ggnfsDir = Path.Combine(_directoryName, "ggnfs") + Path.DirectorySeparatorChar;
                var engine   = IronPython.Hosting.Python.CreateEngine();

                var searchPaths = engine.GetSearchPaths();
                searchPaths.Add(Path.Combine(ggnfsDir, "pythonlib"));
                engine.SetSearchPaths(searchPaths);
                using (var outputStream = new GGNFSOutputStream(delegate(string buffer)
                {
                    _presentation.Append(buffer);
                    SetStatus(_scope.GetVariable <int>("status"),
                              _scope.GetVariable <double>(
                                  "sievingProgress"));
                }))
                    using (var errorOutputStream = new GGNFSOutputStream(buffer => GuiLogMessage(buffer, NotificationLevel.Error)))
                    {
                        engine.Runtime.IO.SetOutput(outputStream, Encoding.ASCII);
                        engine.Runtime.IO.SetErrorOutput(errorOutputStream, Encoding.ASCII);

                        var          scope  = engine.CreateScope();
                        ScriptSource source = engine.CreateScriptSourceFromFile(Path.Combine(ggnfsDir, "factmsieve.py"));

                        source.Execute(scope);
                        _scope = scope;
                        scope.SetVariable("NAME", name);
                        scope.SetVariable("GGNFS_PATH", ggnfsDir);
                        scope.SetVariable("MSIEVE_PATH", ggnfsDir);
                        scope.SetVariable("NUM_CORES", _settings.NumCores);
                        scope.SetVariable("THREADS_PER_CORE", _settings.NumThreadsPerCore);
                        scope.SetVariable("USE_CUDA", _settings.UseCUDA);
                        scope.SetVariable("GPU_NUM", 1);

                        _executingThread = Thread.CurrentThread;
                        _kill            = scope.GetVariable <Action>("Kill");
                        var main = scope.GetVariable <Func <List> >("Main");
                        var res  = main.Invoke();

                        if (_stop || res == null || res.Count == 0)
                        {
                            return;
                        }

                        //give out factors:
                        var factorList = new List <BigInteger>();
                        foreach (var factor in res)
                        {
                            if (factor is BigInteger)
                            {
                                factorList.Add((BigInteger)factor);
                            }
                            else if (factor is int)
                            {
                                factorList.Add((int)factor);
                            }
                            else if (factor is long)
                            {
                                factorList.Add((long)factor);
                            }
                        }
                        OutputFactors = factorList.ToArray();
                    }
            }
            catch (SystemExitException)
            {
                if (!_stop)
                {
                    ShowStopMessage();
                }
            }
            catch (ThreadAbortException)
            {
                ShowStopMessage();
                Thread.ResetAbort();
            }
            catch (Exception ex)
            {
                GuiLogMessage(Properties.Resources._Error_executing_number_field_sieve__ + ex.Message, NotificationLevel.Error);
            }
            finally
            {
                Status           = null;
                _executingThread = null;
                _kill            = null;
            }
        }
示例#3
0
        /// <summary>
        /// Called by the environment to execute this plugin
        /// </summary>
        public void Execute()
        {
            if (checkInUse())
            {
                return;
            }

            try
            {
                Thread.CurrentThread.CurrentCulture   = _cultureInfo;
                Thread.CurrentThread.CurrentUICulture = _cultureUiInfo;

                if (useGnuplot)
                {
                    gnuplotFile = new StreamWriter(Path.Combine(directoryName, "gnuplot.dat"), false);
                }

                userStopped       = false;
                otherPeerFinished = false;

                if (InputNumber < 2)
                {
                    GuiLogMessage("Please enter a number >= 2.", NotificationLevel.Error);
                    return;
                }

                if (InputNumber.ToString().Length >= 275)
                {
                    GuiLogMessage(Resources.Input_too_big_, NotificationLevel.Error);
                    return;
                }

                String info_message = typeof(QuadraticSieve).GetPluginStringResource("Starting_quadratic_sieve");

                start_time = DateTime.Now;

                GuiLogMessage(info_message, NotificationLevel.Info);
                quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback) delegate
                {
                    quadraticSieveQuickWatchPresentation.ProgressRelationPackages.Clear();
                    quadraticSieveQuickWatchPresentation.information.Content = info_message;
                    quadraticSieveQuickWatchPresentation.endTime.Content     = "-";
                    quadraticSieveQuickWatchPresentation.timeLeft.Content    = "-";
                    quadraticSieveQuickWatchPresentation.elapsedTime.Content = "-";
                    quadraticSieveQuickWatchPresentation.startTime.Content   = "" + start_time;
                    quadraticSieveQuickWatchPresentation.factorList.Items.Clear();
                    quadraticSieveQuickWatchPresentation.factorInfo.Content = typeof(QuadraticSieve).GetPluginStringResource("Searching_trivial_factors");
                    if (usePeer2Peer)
                    {
                        quadraticSieveQuickWatchPresentation.localSieving.Visibility = Visibility.Hidden;
                    }
                    else
                    {
                        quadraticSieveQuickWatchPresentation.localSieving.Visibility = Visibility.Visible;
                    }
                }
                                                                       , null);

                initMsieveDLL();
                factorManager = new FactorManager(msieve.GetMethod("getPrimeFactors"), msieve.GetMethod("getCompositeFactors"), InputNumber);
                factorManager.FactorsChanged += this.FactorsChanged;

                //Now factorize:
                try
                {
                    string file = Path.Combine(directoryName, "" + InputNumber + ".dat");
                    if (settings.DeleteCache && File.Exists(file))
                    {
                        File.Delete(file);
                    }
                    MethodInfo start = msieve.GetMethod("start");
                    start.Invoke(null, new object[] { InputNumber.ToString(), file });
                    obj = IntPtr.Zero;
                }
                catch (Exception ex)
                {
                    GuiLogMessage("Error using msieve. " + ex.Message, NotificationLevel.Error);
                    stopThreads();
                    return;
                }

                if (!userStopped)
                {
                    Debug.Assert(factorManager.CalculateNumber() == InputNumber);
                    OutputFactors = factorManager.getPrimeFactors();

                    String timeLeft_message = Resources.NoneLeft;
                    String endtime_message  = DateTime.Now.ToString();

                    quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback) delegate
                    {
                        quadraticSieveQuickWatchPresentation.information.Content = string.Format(typeof(QuadraticSieve).GetPluginStringResource("Sieving_finished"), OutputFactors.Count());
                        quadraticSieveQuickWatchPresentation.endTime.Content     = endtime_message;
                        quadraticSieveQuickWatchPresentation.timeLeft.Content    = timeLeft_message;
                        quadraticSieveQuickWatchPresentation.factorInfo.Content  = "";
                    }
                                                                           , null);

                    ProgressChanged(1, 1);
                }
                else
                {
                    info_message = typeof(QuadraticSieve).GetPluginStringResource("Stopped_by_user");

                    GuiLogMessage(info_message, NotificationLevel.Info);
                    quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback) delegate
                    {
                        quadraticSieveQuickWatchPresentation.information.Content = typeof(QuadraticSieve).GetPluginStringResource("Stopped_by_user");
                        quadraticSieveQuickWatchPresentation.endTime.Content     = "-";
                        quadraticSieveQuickWatchPresentation.timeLeft.Content    = "-";
                        quadraticSieveQuickWatchPresentation.startTime.Content   = "-";
                        quadraticSieveQuickWatchPresentation.elapsedTime.Content = "-";
                        quadraticSieveQuickWatchPresentation.factorInfo.Content  = "";
                    }
                                                                           , null);
                }

                if (useGnuplot)
                {
                    gnuplotFile.Close();
                }
            }
            finally
            {
                alreadyInUse = false;
            }
        }