Exemplo n.º 1
0
        private async Task GenerateFractlAsync(int maxIterations)
        {
            Stopwatch sw = Stopwatch.StartNew();

            Mandelbrot mandelbrotGenerator = new Mandelbrot();

            double increment = scale / viewWidth;
            double left      = center.Real - (viewWidth / 2) * increment;
            double row       = center.Imaginary - (viewHeight / 2) * increment;

            var tasks = PartitionRows(Environment.ProcessorCount)
                        .Select(range => Task.Run(() =>
            {
                double startRow = row + (range.Item1 * increment);
                for (int y = range.Item1; y < range.Item2; y++)
                {
                    mandelbrotGenerator.RenderRow(left, startRow, increment,
                                                  maxIterations, colors, memoryBuffer, (int)(y * viewWidth), (int)viewWidth);
                    startRow += increment;
                }
            })).ToArray();

            await Task.WhenAll(tasks);

            RefreshScreen();

            sw.Stop();
            timer.Text = sw.Elapsed.ToString();
        }
Exemplo n.º 2
0
        private async Task GenerateFractlAsync(int maxIterations, CancellationToken cancelToken)
        {
            Stopwatch sw = Stopwatch.StartNew();

            Mandelbrot mandelbrotGenerator = new Mandelbrot();

            double increment = scale / viewWidth;
            double left      = center.Real - (viewWidth / 2) * increment;
            double row       = center.Imaginary - (viewHeight / 2) * increment;

            var tasks = PartitionRows(Environment.ProcessorCount)
                        .Select(range => Task.Run(() =>
            {
                double startRow = row + (range.Item1 * increment);
                for (int y = range.Item1; y < range.Item2; y++)
                {
                    cancelToken.ThrowIfCancellationRequested();
                    mandelbrotGenerator.RenderRow(left, startRow, increment,
                                                  maxIterations, colors, memoryBuffer, (int)(y * viewWidth), (int)viewWidth);
                    startRow += increment;
                }
            }, cancelToken)).ToArray();

            Task renderTask = null;

            try
            {
                renderTask = Task.WhenAll(tasks);
                await renderTask;
            }
            catch (OperationCanceledException)
            {
                // Canceled -return
                return;
            }
            catch // All exceptions
            {
                // Pull out all exceptions
                AggregateException aex = renderTask.Exception;
                if (aex != null)
                {
                    string message = string.Join(Environment.NewLine,
                                                 aex.Flatten().InnerExceptions.Select(x => x.Message));
                    await DisplayAlert("Render Data", message, "OK");
                }
            }

            RefreshScreen();

            sw.Stop();
            timer.Text = sw.Elapsed.ToString();
        }
Exemplo n.º 3
0
        private async Task GenerateFractlAsync(int maxIterations)
        {
            Stopwatch sw = Stopwatch.StartNew();

            Mandelbrot mandelbrotGenerator = new Mandelbrot();

            double increment = scale / viewWidth;
            double left      = center.Real - (viewWidth / 2) * increment;
            double row       = center.Imaginary - (viewHeight / 2) * increment;

            var tasks = PartitionRows(Environment.ProcessorCount)
                        .Select(range => Task.Run(() =>
            {
                double startRow = row + (range.Item1 * increment);
                for (int y = range.Item1; y < range.Item2; y++)
                {
                    mandelbrotGenerator.RenderRow(left, startRow, increment,
                                                  maxIterations, colors, memoryBuffer, (int)(y * viewWidth), (int)viewWidth);
                    startRow += increment;
                }

                // Throw an exception to pass data back out to task owner
                throw new Exception(string.Format("{0}: rows {1} to {2}",
                                                  Environment.CurrentManagedThreadId, range.Item1, range.Item2));
            })).ToArray();

            Task renderTask = null;

            try
            {
                renderTask = Task.WhenAll(tasks);
                await renderTask;
            }
            catch
            {
                // Pull out exceptions from all tasks
                AggregateException aex = renderTask.Exception;

                if (aex != null)
                {
                    string message = string.Join(Environment.NewLine,
                                                 aex.Flatten().InnerExceptions.Select(x => x.Message));
                    await DisplayAlert("Render Data", message, "OK");
                }
            }

            RefreshScreen();

            sw.Stop();
            timer.Text = sw.Elapsed.ToString();
        }
Exemplo n.º 4
0
        private void GenerateFractl(int maxIterations)
        {
            Stopwatch sw = Stopwatch.StartNew();

            Mandelbrot mandelbrotGenerator = new Mandelbrot();

            double increment = scale / viewWidth;
            double left      = center.Real - (viewWidth / 2) * increment;
            double row       = center.Imaginary - (viewHeight / 2) * increment;

            for (int y = 0; y < viewHeight; y++)
            {
                mandelbrotGenerator.RenderRow(left, row, increment,
                                              maxIterations, colors, memoryBuffer, (int)(y * viewWidth), (int)viewWidth);
                row += increment;
            }

            RefreshScreen();

            sw.Stop();
            timer.Text = sw.Elapsed.ToString();
        }