HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Multithreaded Mandelbrot Generator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
mandelbrotmultithreadedgenerator

Problem

I built one of these long ago, and it got lost when I reformatted my SSD (sad day) so here is the new version.

It is very multithreaded, spawning numCores - 1 threads to calculate n chunks. When one thread finishes, another is spawned until all chunks have been assigned.

It's quite quick and accurate, though the imageSize must be a ratio of 2:1 for width:height.

``
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mandelbrot_Generator
{
class Program
{
// To avoid the need for unnecessary multiplication (and the appearance of magic numbers), we'll make a constant
twoSquared:
const float twoSquared = 2 * 2;

static void Main(string[] args)
{
// Wooo! Mandelbrots! I miss the old programme though. It was probably better.
// That said, we'll get this thing on CR so they can tell us how badly we fkd up.

// Currently an arbitrary number.
short maxIterations = 1000;

// Let us consider a
width and height of the generated image.
Size imageSize = new Size(4096, 2048);

// Next, consider
xCenter and yCenter which represent what pixel is (0,0)` in the specified image size.
Point center = new Point(imageSize.Width / 2, imageSize.Height / 2);

// And we'll scale the size so the brot sits within [-2,2],
SizeF scaleSize = new SizeF(center.X / 2, center.Y);

// Setup the number of chunks to break into.
int numberOfChunks = 256;

int numberOfColours = 32;
int numberOfCores = Environment.ProcessorCount - 1;

Console.WriteLine("Creating Mandelbrot image of size ({0},{1}) and max iteration count of {2}, splitting the image into {3} sections across {4} cores.", imageSize.Width, imageSize.Height, maxIterations, numberOfChu

Solution

I really don't get why people are so afraid to create extra classes. 200+ lines in one Program.cs file isn't something I consider good style. Sure, this is just a project for fun, but even then...

When I write a small program for myself, often they are console applications. And the first thing I do is create a class that will contain all of the logic etc. that I need, unless there's so much that I need to move it to separate classes. The Main(string[] args) of my Program.cs usually contains nothing more than a call to this class.

The Main(string[] args) of this console application is 100+ lines. I know that it is hard to determine a maximum length, but a quick glance already tells me that much of this code should be moved to smaller methods, e.g. when you see comments like // Create and assign tasks (as we can). or // Make sure we finish our tasks and add them to our results., each of which is followed by a 10+ lines long while.

That whole GenerateSection method should be a class of its own, IMHO. I mean, it starts with nearly twenty (20!) lines of variable assignments!

Again: I know this is just a small thing you've done "for fun". But I'd sincerely urge you to apply good coding practices to those as well. After all, practice makes perfect.

Context

StackExchange Code Review Q#104171, answer score: 16

Revisions (0)

No revisions yet.