The Computer Language
24.04 Benchmarks Game

mandelbrot C# .NET #3 program

source code

/* The Computer Language Benchmarks Game
   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

   Adapted by Antti Lankila from the earlier Isaac Gouy's implementation
   Add multithread & tweaks from C++ by The Anh Tran
   Simplified bit logic and cleaned code by Robert F. Tobler
*/

using System;
using System.Threading;
using System.IO;

public class MandelBrot
{
    private static int n = 200;
    private static byte[][] data;
    private static int lineCount = -1;
    private static double[] xa;

    public static void Main (String[] args)
    {
        if (args.Length > 0) n = Int32.Parse(args[0]);
        Console.Out.WriteLine("P4\n{0} {0}", n);
        
        int lineLen = (n-1)/8 + 1;
        data = new byte[n][];
        for (int i = 0; i < n; i++) data[i] = new byte[lineLen];

        xa = new double[n];
        for (int x = 0; x < n; x++) xa[x] = x * 2.0/n - 1.5;

        var threads = new Thread[Environment.ProcessorCount];
        for (int i = 0; i < threads.Length; i++)
            (threads[i] = new Thread(MandelBrot.Calculate)).Start();

        foreach (var t in threads) t.Join();

        var s = Console.OpenStandardOutput();
        for (int y = 0; y < n; y++) s.Write(data[y], 0, lineLen);
    }

    private static void Calculate()
    {
        int y;
        while ((y = Interlocked.Increment(ref lineCount)) < n)
        {
            var line = data[y];
            int xbyte = 0, bits = 1;
            double ci = y * 2.0/n - 1.0;

            for (int x = 0; x < n; x++)
            {
                double cr = xa[x];
                if (bits > 0xff) { line[xbyte++] = (byte)bits; bits = 1; }
                double zr = cr, zi = ci, tr = cr * cr, ti = ci * ci;  
                int i = 49;
                do
                {
                    zi = zr * zi + zr * zi + ci; zr = tr - ti + cr;
                    tr = zr * zr; ti = zi * zi;
                }
                while ((tr + ti <= 4.0) && (--i > 0));
                bits = (bits << 1) | (i == 0 ? 1 : 0);
            }
            while (bits < 0x100) bits = (bits << 1);
            line[xbyte] = (byte)bits;
        }
    }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
.NET SDK 8.0.204
Host Version: 8.0.4
Commit: 2d7eea2529
<AllowUnsafeBlocks>true
<ServerGarbageCollection>true
<ConcurrentGarbageCollection>true
<PublishAot>false
<OptimizationPreference>Speed
<IlcInstructionSet>native


 Fri, 26 Apr 2024 00:50:20 GMT

MAKE:
cp mandelbrot.csharpcore-3.csharpcore Program.cs
cp Include/csharpcore/program.csproj .
mkdir obj
cp Include/csharpcore/project.assets.json ./obj
/usr/bin/dotnet build -c Release --use-current-runtime  	
MSBuild version 17.9.8+b34f75857 for .NET
  Determining projects to restore...
  Restored /home/dunham/all-benchmarksgame/benchmarksgame_i53330/mandelbrot/tmp/program.csproj (in 180 ms).
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/mandelbrot/tmp/Program.cs(16,29): warning CS8618: Non-nullable field 'data' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/mandelbrot/tmp/program.csproj]
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/mandelbrot/tmp/Program.cs(18,29): warning CS8618: Non-nullable field 'xa' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/mandelbrot/tmp/program.csproj]
  program -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/mandelbrot/tmp/bin/Release/net8.0/linux-x64/program.dll

Build succeeded.

/home/dunham/all-benchmarksgame/benchmarksgame_i53330/mandelbrot/tmp/Program.cs(16,29): warning CS8618: Non-nullable field 'data' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/mandelbrot/tmp/program.csproj]
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/mandelbrot/tmp/Program.cs(18,29): warning CS8618: Non-nullable field 'xa' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. [/home/dunham/all-benchmarksgame/benchmarksgame_i53330/mandelbrot/tmp/program.csproj]
    2 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.93

5.51s to complete and log all make actions

COMMAND LINE:
 ./bin/Release/net8.0/linux-x64/program 16000

(BINARY) PROGRAM OUTPUT NOT SHOWN