Ejemplo n.º 1
0
    public static void Render(string pointFilePath, Size?resolution)
    {
        using var points = PointStream.Load(pointFilePath);
        var viewPort         = points.ViewPort;
        var actualResolution = resolution ?? points.ViewPort.Resolution;

        if (actualResolution != points.ViewPort.Resolution)
        {
            viewPort = new ViewPort(viewPort.Area, actualResolution);
        }

        var imageFilePath = pointFilePath + $".{actualResolution.Width}x{actualResolution.Height}.png";

        using var timer = TimedOperation.Start("points", totalWork: actualResolution.Area());
        var image = new FastImage(actualResolution);

        image.Fill(Color.White);

        Parallel.ForEach(points, point =>
        {
            image.SetPixel(viewPort.GetPosition(point), Color.Black);
            timer.AddWorkDone(1);
        });

        image.Save(imageFilePath);
    }
Ejemplo n.º 2
0
        public void TestLoggingToAzureTableStorageTimedOperationObject()
        {
            //Arrange
            var messageToLog  = "Test Timing Message";
            var caller        = "LoggerTests.TestLoggingToAzureTableStorageTimedOperationObject";
            var correlationId = Guid.NewGuid().ToString();

            //Act
            using (var timedOperation = new TimedOperation(messageToLog, caller, correlationId))
            {
                //Wrap any piece of code in a timing block to log it's start time, end time and running duration
                Thread.Sleep(2000); /* This represents the code being timed */
            }

            //Assert
            var queryDictionary = new Dictionary <string, string> {
                { "CorrelationId", correlationId }
            };
            var retrievedEntries = Logger.RetrieveLogMessagesFromTableStorage(queryDictionary);

            Assert.IsTrue(retrievedEntries.Count() == 2);

            //Verify the logged time was greater than the time we slept for above
            var runningTime = Logger.RetrieveRunningTimeOfOperation(correlationId);

            Assert.IsTrue(runningTime > new TimeSpan(0, 0, 2));
        }
Ejemplo n.º 3
0
    public static void RenderAllSpans(string edgeSpansPath, string imageFilePath, bool showDirections)
    {
        Log.Info($"Output image: {imageFilePath}");
        if (showDirections)
        {
            Log.Info("Displaying span directions as well.");
        }

        using var spans = EdgeSpanStream.Load(edgeSpansPath);
        using var timer = TimedOperation.Start("edge spans", totalWork: showDirections?spans.Count * 2 : spans.Count);
        var scale = showDirections ? 3 : 1;

        var resolution = spans.ViewPort.Resolution.Scale(scale);

        var image = new FastImage(resolution);

        image.Fill(Color.White);

        Parallel.ForEach(spans, logicalSpan =>
        {
            if (showDirections)
            {
                var scaledLocation = logicalSpan.Location.Scale(scale);
                for (int yDelta = 0; yDelta < 3; yDelta++)
                {
                    for (int xDelta = 0; xDelta < 3; xDelta++)
                    {
                        image.SetPixel(
                            scaledLocation.OffsetBy(xDelta, yDelta),
                            (logicalSpan.Location.X + logicalSpan.Location.Y) % 2 == 1 ? Color.Black : Color.Gray);
                    }
                }
            }
            else
            {
                image.SetPixel(
                    logicalSpan.Location,
                    Color.Black);
            }
            timer.AddWorkDone(1);
        });

        if (showDirections)
        {
            Parallel.ForEach(spans, locatedSpan =>
            {
                var scaledLocation = locatedSpan.Location.Scale(scale);
                var pointingTo     = scaledLocation.OffsetBy(1, 1).OffsetIn(locatedSpan.ToOutside);

                image.SetPixel(
                    pointingTo,
                    Color.Red);

                timer.AddWorkDone(1);
            });
        }

        image.Save(imageFilePath);
    }
Ejemplo n.º 4
0
        internal TimedTrigger(TimeSpan when, TimedOperation operation)
        {
            if (operation == TimedOperation.Between)
            {
                throw new ArgumentException("The Between operation can only be used when specifying two moments in time.", nameof(operation));
            }

            _when      = when;
            _operation = operation;
        }
Ejemplo n.º 5
0
 public static void FindEdgeSpans(
     string pointGridFilePath,
     string outputFilePath)
 {
     using var pointGrid = PointGrid.Load(pointGridFilePath);
     using var timer     = TimedOperation.Start("points", totalWork: pointGrid.ViewPort.Resolution.Height);
     EdgeSpanStream.Write(
         outputFilePath,
         pointGrid.ViewPort,
         pointGrid.ComputationType,
         GetEdgeSpans(pointGrid, timer));
 }
Ejemplo n.º 6
0
        public TimedTrigger(TimeSpan from, TimeSpan until)
        {
            if (from > until)
            {
                throw new ArgumentException("\"Until\" should be a moment after \"from\"", nameof(until));
            }

            _when = from;
            _end  = until;

            _operation = TimedOperation.Between;
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Times the operation in <paramref name="operation" />.
        /// </summary>
        /// <param name="startFresh">
        ///     If true, forces a GC in order to count just new garbage collections.
        /// </param>
        /// <param name="text">
        ///     The text to display along with the timing information.
        /// </param>
        /// <param name="iterations">
        ///     The number of times to execute <paramref name="operation" />.
        /// </param>
        /// <param name="operation">
        ///     The <see cref="TimedOperation" /> delegate to execute.
        /// </param>
        public static void Time(bool startFresh,
                                string text,
                                int iterations,
                                TimedOperation operation)
        {
            operation();

            using (new CodeTimer(startFresh, text))
            {
                while (iterations-- > 0)
                {
                    operation();
                }
            }
        }
Ejemplo n.º 8
0
    public static void CalculateToCsv(string edgeSpansPath, int pointsToCalculate)
    {
        using var edgeSpans      = EdgeSpanStream.Load(edgeSpansPath);
        using var timedOperation = TimedOperation.Start("edge spans", totalWork: pointsToCalculate > 0 ? pointsToCalculate : edgeSpans.Count);
        using var outputFile     = File.OpenWrite(edgeSpansPath + $".{timedOperation.TotalWork}.borderPoints.csv");
        using var textWriter     = new StreamWriter(outputFile);
        using var csv            = new CsvWriter(textWriter, CultureInfo.InvariantCulture);
        //var kernel = KernelBuilder.BuildScalarKernel(edgeSpans.ComputationType);

        var sequence =
            edgeSpans.Take((int)timedOperation.TotalWork).
            Select((logicalSpan, index) => new { LogicalSpan = logicalSpan, Index = index }).
            AsParallel().
            Select(result =>
        {
            var span        = result.LogicalSpan.ToConcreteDouble(edgeSpans.ViewPort);
            var borderPoint = span.FindBoundaryPoint(Constant.IterationRange.Max);
            var escapeTime  = ScalarDoubleKernel.FindEscapeTime(borderPoint, Constant.IterationRange.Max);
            timedOperation.AddWorkDone(1);
            return
            (
                Index: result.Index,
                Location: result.LogicalSpan.Location,
                Span: span,
                Point: borderPoint,
                EscapeTime: escapeTime
            );
        });

        csv.WriteField("Index");
        csv.WriteField("Location");
        csv.WriteField("Span");
        csv.WriteField("Border Point");
        csv.WriteField("Escape Time");
        csv.NextRecord();

        foreach (var result in sequence)
        {
            csv.WriteField(result.Index);
            csv.WriteField(result.Location);
            csv.WriteField(result.Span);
            csv.WriteField(result.Point);
            csv.WriteField(result.EscapeTime);
            csv.NextRecord();
        }
    }
 public void Done(AppOperation op)
 {
     if (op.State != OperationState.Running)
     {
         return;
     }
     lock (operations)
     {
         operations.Remove(op);
     }
     op.Stop(false);
     currentOperation.Value = null;
     Interlocked.Increment(ref successedOperations);
     //record long operations
     lock (prolongOperations)
     {
         var to = new TimedOperation(op);
         if (prolongOperations.Count > 0 && to.Duration <= prolongOperations[0].Duration)
         {
             return;
         }
         int i = prolongOperations.BinarySearch(to);
         if (i < 0)
         {
             i = ~i;
             if (i == prolongOperations.Count)
             {
                 prolongOperations.Add(to);
             }
             else
             {
                 prolongOperations.Insert(i, to);
             }
         }
         else
         {
             prolongOperations.Insert(i, to);
         }
         if (prolongOperations.Count > MaxProlongOperations)
         {
             prolongOperations.RemoveAt(0);
         }
     }
 }
Ejemplo n.º 10
0
    public static void CalculateWithDoubles(string edgeSpansPath, int pointsToCalculate)
    {
        using var edgeSpans      = EdgeSpanStream.Load(edgeSpansPath);
        using var timedOperation = TimedOperation.Start("edge spans", totalWork: pointsToCalculate > 0 ? pointsToCalculate : edgeSpans.Count);
        var points =
            edgeSpans.Take((int)timedOperation.TotalWork).
            AsParallel().
            Select(span => span.
                   ToConcreteDouble(edgeSpans.ViewPort).
                   FindBoundaryPoint(Constant.IterationRange.Max)).
            Where(point =>
        {
            var escapeTime = ScalarDoubleKernel.FindEscapeTime(point, Constant.IterationRange.Max);
            timedOperation.AddWorkDone(1);
            return(Constant.IterationRange.IsInside(escapeTime));
        });

        PointStream.Write(edgeSpansPath + $".{timedOperation.TotalWork}.doublePoints", edgeSpans.ViewPort, points);
    }
Ejemplo n.º 11
0
        private static void DisplayRows(TimedOperation <Dictionary <string, Dictionary <string, string> > > timedOperation)
        {
            var rows = timedOperation.Value;

            foreach (var row in rows)
            {
                Console.WriteLine(new String('-', 20));
                Console.WriteLine("Key: " + row.Key);
                Console.WriteLine(new String('-', 20));
                foreach (var column in row.Value)
                {
                    Console.WriteLine(column.Key + " = " + column.Value);
                }
                Console.WriteLine(new String('-', 20));
                Console.WriteLine();
            }

            Console.WriteLine("Operation time: {0}ms", timedOperation.ElapsedMilliseconds);
            Console.WriteLine();
        }
Ejemplo n.º 12
0
    public static void Render(string gridFilePath, string imageFilePath)
    {
        Log.Info($"Output image: {imageFilePath}");

        using var grid  = PointGrid.Load(gridFilePath);
        using var timer = TimedOperation.Start("points", totalWork: grid.ViewPort.Resolution.Area());
        var image = new FastImage(grid.ViewPort.Resolution);

        image.Fill(Color.White);

        Parallel.ForEach(grid, row =>
        {
            foreach (var setSegment in row.GetSegmentsInSet())
            {
                foreach (var x in Enumerable.Range(setSegment.StartCol, setSegment.Length))
                {
                    image.SetPixel(x, row.Y, Color.Black);
                }
            }
            timer.AddWorkDone(grid.ViewPort.Resolution.Width);
        });

        image.Save(imageFilePath);
    }
Ejemplo n.º 13
0
 private static void DisplayTables(TimedOperation <IEnumerable <string> > timedOperation)
 {
     Console.WriteLine("Available Tables: " + String.Join(", ", timedOperation.Value));
     Console.WriteLine();
     Console.WriteLine("Operation time: {0}ms", timedOperation.ElapsedMilliseconds);
 }
Ejemplo n.º 14
0
    private static IEnumerable <LogicalEdgeSpan> GetEdgeSpans(IEnumerable <PointRow> rows, TimedOperation timer)
    {
        IEnumerable <LogicalEdgeSpan> Process(PointRow?aboveRow, PointRow?currentRow, PointRow?belowRow)
        {
            var y    = currentRow.Y;
            int maxX = currentRow.Width - 1;

            foreach (var x in currentRow.GetXPositionsOfSet())
            {
                if (aboveRow != null)
                {
                    if (x > 0 && !aboveRow[x - 1])
                    {
                        yield return(new LogicalEdgeSpan(new Point(x, y), Direction.UpLeft));
                    }
                    if (!aboveRow[x])
                    {
                        yield return(new LogicalEdgeSpan(new Point(x, y), Direction.Up));
                    }
                    if (x < maxX && !aboveRow[x + 1])
                    {
                        yield return(new LogicalEdgeSpan(new Point(x, y), Direction.UpRight));
                    }
                }

                if (x > 0 && !currentRow[x - 1])
                {
                    yield return(new LogicalEdgeSpan(new Point(x, y), Direction.Left));
                }
                if (x < maxX && !currentRow[x + 1])
                {
                    yield return(new LogicalEdgeSpan(new Point(x, y), Direction.Right));
                }

                if (belowRow != null)
                {
                    if (x > 0 && !belowRow[x - 1])
                    {
                        yield return(new LogicalEdgeSpan(new Point(x, y), Direction.DownLeft));
                    }
                    if (!belowRow[x])
                    {
                        yield return(new LogicalEdgeSpan(new Point(x, y), Direction.Down));
                    }
                    if (x < maxX && !belowRow[x + 1])
                    {
                        yield return(new LogicalEdgeSpan(new Point(x, y), Direction.DownRight));
                    }
                }
            }
            timer.AddWorkDone(1);
        }

        return
            (GetRowBatches(rows).
             AsParallel().
             SelectMany(rowBatch => Process(rowBatch.above, rowBatch.current, rowBatch.below)));
    }
Ejemplo n.º 15
0
 /// <summary>
 /// Times the operation in <paramref name="operation"/>.
 /// </summary>
 /// <param name="text">
 /// The text to display along with the timing information.
 /// </param>
 /// <param name="iterations">
 /// The number of times to execute <paramref name="operation"/>.
 /// </param>
 /// <param name="operation">
 /// The <see cref="TimedOperation"/> delegate to execute.
 /// </param>
 public static void Time(String text,
                         Int32 iterations,
                         TimedOperation operation)
 {
     Time(false, text, iterations, operation);
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Times the operation in <paramref name="operation"/>.
 /// </summary>
 /// <param name="startFresh">
 /// If true, forces a GC in order to count just new garbage collections.
 /// </param>
 /// <param name="text">
 /// The text to display along with the timing information.
 /// </param>
 /// <param name="iterations">
 /// The number of times to execute <paramref name="operation"/>.
 /// </param>
 /// <param name="operation">
 /// The <see cref="TimedOperation"/> delegate to execute.
 /// </param>
 public static void Time(Boolean startFresh,
                         String text,
                         Int32 iterations,
                         TimedOperation operation)
 {
     operation();
     
     using (new CodeTimer(startFresh, text))
     {
         while (iterations-- > 0)
         {
             operation();
         }
     }
 }
Ejemplo n.º 17
0
 /// <summary>
 ///     Times the operation in <paramref name="operation" />.
 /// </summary>
 /// <param name="text">
 ///     The text to display along with the timing information.
 /// </param>
 /// <param name="iterations">
 ///     The number of times to execute <paramref name="operation" />.
 /// </param>
 /// <param name="operation">
 ///     The <see cref="TimedOperation" /> delegate to execute.
 /// </param>
 public static void Time(string text,
                         int iterations,
                         TimedOperation operation)
 {
     Time(false, text, iterations, operation);
 }
Ejemplo n.º 18
0
        /// <summary>
        /// called on each timer tick - implimented by the base class
        /// </summary>
        protected virtual void TimerTick()
        {
            try
            {
                sendDataReq("HB", DateTime.Now.Ticks.ToString());

                // are we stale - use the time of the last HB
                if ((DateTime.Now.Ticks - m_LastHBTicks) > m_MaxHBDelayTicks)
                {
                    // dont report stale if alread disconnected
                    if (ConnectionState != RTDConnectionState.disconnected)
                    {
                        SetConnectionState(RTDConnectionState.stale);
                    }
                }
                else
                {
                    SetConnectionState(RTDConnectionState.connected);
                }

                // each type of request defines primitive operations that need to
                // be done a sequence
                switch (this.Request)
                {
                    case ERequest.restart:
                        m_TimedOperation = m_Restart[m_RestartPos];
                        m_RestartPos++;
                        if (m_RestartPos >= m_Restart.Length)
                        {
                            // we are done
                            this.Request = ERequest.none;
                        }
                        break;
                    default:
                        m_TimedOperation = TimedOperation.none;
                        break;
                }

                // process any primitive operations - these are set above
                switch (m_TimedOperation)
                {
                    case TimedOperation.start:
                        this.Start();
                        break;

                    case TimedOperation.stop:
                        this.Stop();
                        break;

                    case TimedOperation.resubscribe:
                        //ReSubscribe("*", 0);
                        break;
                }

            }
            catch (Exception myE)
            {
                //m_Log.Error("TimerTick", myE);
            }
        }
Ejemplo n.º 19
0
    public static void RenderSingleSpan(string edgeSpansPath, int spanIndex, int sideResolution)
    {
        var resolution = new Size(sideResolution, sideResolution);

        using var spans = EdgeSpanStream.Load(edgeSpansPath);
        using var timer = TimedOperation.Start("points", totalWork: resolution.Area());
        var random = new Random();

        var index = spanIndex >= 0 ? spanIndex : random.Next(0, spans.Count);

        var imageFilePath = Path.Combine(
            Path.GetDirectoryName(edgeSpansPath) ?? throw new Exception($"Could not get directory name: {edgeSpansPath}"),
            Path.GetFileNameWithoutExtension(edgeSpansPath) + $"_{index}_{sideResolution}x{sideResolution}.png");

        Log.Info($"Using edge span index {index:N0}");
        Log.Info($"Output file: {imageFilePath}");

        var span       = spans.ElementAt(index).ToConcreteDouble(spans.ViewPort);
        var spanLength = span.Length();

        Log.Info($"Edge span: {span} (length: {spanLength})");

        var image = new FastImage(resolution);

        var viewPort = new ViewPort(GetArea(span), resolution);

        Log.Info($"View port: {viewPort}");


        var positionInSet    = viewPort.GetPosition(span.InSet);
        var positionNotInSet = viewPort.GetPosition(span.NotInSet);

        var highlightPixelRadius = resolution.Width / 100;

        var borderPoint = span.FindBoundaryPoint(Constant.IterationRange.Max);

        Log.Info($"Border point: {borderPoint} (escape time: {ScalarDoubleKernel.FindEscapeTime(borderPoint)})");
        var borderPointPosition = viewPort.GetPosition(borderPoint);

        // TODO: Why is the inner loop parallelized?
        for (int row = 0; row < resolution.Height; row++)
        {
            Parallel.For(0, resolution.Width,
                         col =>
            {
                var position = new Point(col, row);

                var c = viewPort.GetComplex(position);

                Color PickColor()
                {
                    if (position.DistanceSquaredFrom(positionInSet) <= highlightPixelRadius)
                    {
                        return(Color.Red);
                    }

                    if (position.DistanceSquaredFrom(positionNotInSet) <= highlightPixelRadius)
                    {
                        return(Color.ForestGreen);
                    }

                    if (position.DistanceSquaredFrom(borderPointPosition) <= highlightPixelRadius)
                    {
                        return(Color.Fuchsia);
                    }

                    var isInSet = ScalarDoubleKernel.FindEscapeTime(c, Constant.IterationRange.Max).IsInfinite;
                    return(isInSet ? Color.FromArgb(0x20, 0x20, 0x20) : Color.White);
                }


                image.SetPixel(position, PickColor());
            });

            timer.AddWorkDone(resolution.Width);
        }

        image.Save(imageFilePath);
    }
Ejemplo n.º 20
0
    public static void Compute(
        string filePath,
        ViewPort viewPort,
        ComputationType computationType,
        CancellationToken token)
    {
        Log.Info($"Outputting to: {filePath}");
        Log.Info($"Resolution: {viewPort.Resolution.Width:N0}x{viewPort.Resolution.Height:N0}");
        Log.Info($"Area: {viewPort.Area}");
        Log.Info($"Computation type: {computationType}");

        IEnumerable <bool> GetPointsInSetScalar()
        {
            var kernel = KernelBuilder.BuildScalarKernel(computationType);

            var rowPointsInSet = new bool[viewPort.Resolution.Width];

            using var progress = TimedOperation.Start("points", totalWork: viewPort.Resolution.Area());
            for (int row = 0; row < viewPort.Resolution.Height; row++)
            {
                Parallel.For(
                    0,
                    viewPort.Resolution.Width,
                    col => rowPointsInSet[col] = kernel.FindEscapeTime(viewPort.GetComplex(col, row), Constant.IterationRange.Max).IsInfinite);

                for (int x = 0; x < viewPort.Resolution.Width; x++)
                {
                    yield return(rowPointsInSet[x]);
                }

                progress.AddWorkDone(viewPort.Resolution.Width);
            }
        }

        IEnumerable <bool> GetPointsInSetVectorDoubles()
        {
            using var progress = TimedOperation.Start("points", totalWork: viewPort.Resolution.Area());
            var vWidth = VectorDoubleKernel.Capacity;

            var vectorBatches = viewPort.Resolution.Width / vWidth;
            var remainder     = viewPort.Resolution.Width % vWidth;

            if (remainder != 0)
            {
                vectorBatches++;
            }
            var lastIndex = vectorBatches - 1;

            var rowPointsInSet = new bool[viewPort.Resolution.Width];

            // TODO: Why is the Parallel.For inside a loop?
            for (int row = 0; row < viewPort.Resolution.Height; row++)
            {
                Parallel.For(
                    0,
                    vectorBatches,
                    batchIndex =>
                {
                    var realBatch = new double[vWidth];
                    var imagBatch = new double[vWidth];
                    var times     = new EscapeTime[vWidth];

                    var batchSize = (batchIndex == lastIndex) ? remainder : vWidth;

                    for (int i = 0; i < batchSize; i++)
                    {
                        var c        = viewPort.GetComplex(batchIndex * vWidth + i, row);
                        realBatch[i] = c.Real;
                        imagBatch[i] = c.Imaginary;
                    }

                    VectorDoubleKernel.FindEscapeTimes(
                        realBatch, imagBatch, Constant.IterationRange.Max, times);

                    for (int i = 0; i < batchSize; i++)
                    {
                        rowPointsInSet[batchIndex * vWidth + i] = times[i].Iterations == Constant.IterationRange.Max;
                    }
                });

                for (int x = 0; x < viewPort.Resolution.Width; x++)
                {
                    yield return(rowPointsInSet[x]);
                }

                progress.AddWorkDone(viewPort.Resolution.Width);
            }
        }

        IEnumerable <bool> ChooseEnumerator() =>
        computationType switch
        {
            ComputationType.ScalarDouble => GetPointsInSetScalar(),
            ComputationType.ScalarFloat => GetPointsInSetScalar(),
            ComputationType.VectorDouble => GetPointsInSetVectorDoubles(),
            _ => throw new ArgumentException("Unsupported computation type: " + computationType)
        };

        Write(filePath, viewPort, computationType, ChooseEnumerator());
    }
}