static void DrawMandelbrotSet(int width, int height, Stream stream, MandelbrotScale scale) { if (!IsQuiet) { Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} started drawing Mandelbrot. \n"); } using (var image = new Image <Rgba32>(width, height)) { for (int i = 0; i < width; i++) { MandelbrotInnerLoop(i, width, height, image, scale); } image.SaveAsPng(stream); }; if (!IsQuiet) { Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} finished drawing Mandelbrot. \n"); } }
static void MandelbrotInnerLoop(int i, int width, int height, Image <Rgba32> image, MandelbrotScale scale) { for (int j = 0; j < height; j++) { var cx = ((double)i).Map(0, width, scale.Lx, scale.Hx); var cy = ((double)j).Map(0, height, scale.Ly, scale.Hy); var c = new Complex(cx, cy); var iterations = CalculatePixel(c); if (iterations == maxIterations) { image[i, j] = new Rgba32(0, 0, 0); } else { var factor = iterations / (double)maxIterations; var intensity = (byte)Math.Round(255 * factor * 10); image[i, j] = new Rgba32(intensity, intensity, intensity); } } }
static void DrawMandelbrotSetParallel2(int width, int height, Stream stream, int taskCnt, int chunkSize, MandelbrotScale scale) { using (var image = new Image <Rgba32>(width, height)) { Parallel.ForEach(Partitioner.Create(0, width, chunkSize), new ParallelOptions() { MaxDegreeOfParallelism = taskCnt }, range => { for (int i = range.Item1; i < range.Item2; i++) { MandelbrotInnerLoop(i, width, height, image, scale); } }); image.SaveAsPng(stream); }; }
static void DrawMandelbrotSetParallel(int width, int height, Stream stream, int taskCnt, int chunkSize, MandelbrotScale scale) { if (!IsQuiet) { Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} started drawing Mandelbrot. \n"); } using (var image = new Image <Rgba32>(width, height)) { CustomChunkPartitionParallelFor(0, width, (i) => { MandelbrotInnerLoop(i, width, height, image, scale); }, chunkSize, taskCnt); image.SaveAsPng(stream); }; if (!IsQuiet) { Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} finished drawing Mandelbrot. \n"); } }
static void DrawMandelbrotSetParallelNaive(int width, int height, Stream stream, int taskCnt, MandelbrotScale scale) { using (var image = new Image <Rgba32>(width, height)) { CustomPartitionParallelFor(0, width, (i) => { MandelbrotInnerLoop(i, width, height, image, scale); }, taskCnt); image.SaveAsPng(stream); }; }