예제 #1
0
 /// <summary>
 ///  The main constructor.
 /// </summary>
 protected HostEnvironmentBase(IRandom rand, bool verbose, int conc,
                               string shortName = null, string parentFullName = null)
     : base(shortName, parentFullName, verbose)
 {
     Contracts.CheckValueOrNull(rand);
     _rand           = rand ?? RandomUtils.Create();
     _conc           = conc;
     ListenerDict    = new ConcurrentDictionary <Type, Dispatcher>();
     ProgressTracker = new ProgressReporting.ProgressTracker(this);
     _cancelLock     = new object();
     _tempLock       = new object();
     _tempFiles      = new List <IFileHandle>();
     Root            = this as TEnv;
 }
예제 #2
0
        /// <summary>
        /// This constructor is for forking.
        /// </summary>
        protected HostEnvironmentBase(HostEnvironmentBase <TEnv> source, IRandom rand, bool verbose,
                                      int?conc, string shortName = null, string parentFullName = null)
            : base(shortName, parentFullName, verbose)
        {
            Contracts.CheckValue(source, nameof(source));
            Contracts.CheckValueOrNull(rand);
            _rand       = rand ?? RandomUtils.Create();
            _conc       = conc;
            _cancelLock = new object();

            // This fork shares some stuff with the master.
            Master          = source;
            Root            = source.Root;
            ListenerDict    = source.ListenerDict;
            ProgressTracker = source.ProgressTracker;
        }
            /// <summary>
            /// Query all progress and:
            /// * If there's any checkpoint/start/stop event, print all of them.
            /// * If there's none, print a dot.
            /// * If there's <see cref="_maxDots"/> dots, print the current status for all running calculations.
            /// </summary>
            public void GetAndPrintAllProgress(ProgressReporting.ProgressTracker progressTracker)
            {
                Contracts.AssertValue(progressTracker);

                var entries = progressTracker.GetAllProgress();

                if (entries.Count == 0)
                {
                    // There's no calculation running. Don't even print a dot.
                    return;
                }

                var checkpoints = entries.Where(
                    x => x.Kind != ProgressReporting.ProgressEvent.EventKind.Progress || x.ProgressEntry.IsCheckpoint);

                lock (_lock)
                {
                    bool anyCheckpoint = false;
                    foreach (var ev in checkpoints)
                    {
                        anyCheckpoint = true;
                        EnsureNewLine();
                        // We assume that things like status counters, which contain only things
                        // like loss function values, counts of rows, counts of items, etc., are
                        // not sensitive.
                        WriteAndReturnLinePrefix(MessageSensitivity.None, _out);
                        switch (ev.Kind)
                        {
                        case ProgressReporting.ProgressEvent.EventKind.Start:
                            PrintOperationStart(_out, ev);
                            break;

                        case ProgressReporting.ProgressEvent.EventKind.Stop:
                            PrintOperationStop(_out, ev);
                            break;

                        case ProgressReporting.ProgressEvent.EventKind.Progress:
                            _out.Write("[{0}] ", ev.Index);
                            PrintProgressLine(_out, ev);
                            break;
                        }
                    }
                    if (anyCheckpoint)
                    {
                        // At least one checkpoint has been printed, so there's no need for dots.
                        return;
                    }

                    if (PrintDot())
                    {
                        // We need to print an extended status line. At this point, every event should be
                        // a non-checkpoint progress event.
                        bool needPrepend = entries.Count > 1;
                        foreach (var ev in entries)
                        {
                            Contracts.Assert(ev.Kind == ProgressReporting.ProgressEvent.EventKind.Progress);
                            Contracts.Assert(!ev.ProgressEntry.IsCheckpoint);
                            if (needPrepend)
                            {
                                EnsureNewLine();
                                WriteAndReturnLinePrefix(MessageSensitivity.None, _out);
                                _out.Write("[{0}] ", ev.Index);
                            }
                            else
                            {
                                // This is the only case we are printing something at the end of the line of dots.
                                // So, we need to reset the dots counter.
                                _dots = 0;
                            }
                            PrintProgressLine(_out, ev);
                        }
                    }
                }
            }