The Computer Language
24.11 Benchmarks Game

mandelbrot Free Pascal #2 program

source code

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

  contributed by Ales Katona
  modified by Vincent Snijders
  optimized and multithreaded by Jean de La Taille
  fixed by Peter Blackman
}

program mandelbrot;

uses
  {$ifdef unix}cthreads,{$endif}
  sysUtils, dateUtils, math;

const
  Limit = 4;

var
  n, n_1, dimx, dimy : longint;
  TextBuf: array of byte;
  start, finish : TDateTime;

type
  mem = record
    from_y, to_y : longint;
  end;
  pmem = ^mem;

function subThread(p: pointer) : ptrint;
var
  x, y, from_y, to_y, buf_index, i: Longint;
  Zr, Zi, Ti, Tr : Double;
  Cr, Ci : Double;
  bits: Longint;
  bit: Longint;
begin
  from_y := pmem(p)^.from_y;
  to_y := pmem(p)^.to_y;
  buf_index := from_y * dimx;
  for y := from_y to to_y do
  begin
    bit := 128; // 1000 0000
    bits := 0;
    Ci := ((y + y) / n) - 1.0;
    for x := 0 to n_1 do
    begin
      //---------------------------
      Zr := 0;
      Zi := 0;
      Tr := 0;
      Ti := 0;
      Cr := ((x + x) / n) - 1.5;
      for i := 1 to 50 do
      begin
        Zi := 2 * Zr * Zi + Ci;
        Zr := Tr - Ti + Cr;
        Ti := Zi * Zi;
        Tr := Zr * Zr;
        if ((Tr + Ti) > limit) then
        begin
          bits := bits or bit;
          break;
        end;
      end;
      //---------------------------
      bit := bit >> 1;
      if (bit = 0) then
      begin
        TextBuf[buf_index] := not bits;
        inc(buf_index);
        bits := 0;
        bit := 128;
      end;
    end;
  end;
  subThread := 0;
end;

procedure run;
var
  i, l, x, y, buf_index: Longint;
  tt : array[0..3] of TThreadID;
  m : array[0..3] of mem;
  stepL : longint;
begin
  n_1 := n - 1;
  l := 0;
  stepL := floor(n / 4);

  start := now;
  for i := 0 to 2 do
  begin
    m[i].from_y := l;
    m[i].to_y := l + stepL - 1;
    tt[i] := BeginThread(@subThread, @m[i]);
    l := l + stepL;
  end;
  m[3].from_y := l;
  m[3].to_y := n_1;
  tt[3] := BeginThread(@subThread, @m[3]);
  for i := 0 to 3 do
    WaitForThreadTerminate(tt[i], 0);
  finish := now;
  //WriteLn('Time : ', MilliSecondsBetween(start, finish) / 1000 : 0 : 4);

  buf_index := 0;
  for y := 0 to n_1 do
    for x := 0 to dimx - 1 do
    begin
      write(chr(TextBuf[buf_index]));;
      inc(buf_index);
    end;
end;


begin
  Val(ParamStr(1), n);
  write('P4', chr(10), n, ' ', n, chr(10));
  //write('P5', chr(10), n, ' ', n, chr(10), 255, chr(10));

  dimx := Ceil(n / 8);
  dimy := n;
  SetLength(TextBuf, (dimx * dimy) + 1);

  start := now;

  run;

  finish := now;
end.
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Free Pascal Compiler
version 3.2.2 [2021/05/16]


 Wed, 22 May 2024 21:06:06 GMT

MAKE:
mv mandelbrot.fpascal-2.fpascal mandelbrot.fpascal-2.pas
/opt/src/fpc-3.2.2/bin/fpc -FuInclude/fpascal -XXs -O3 -Ci- -Cr- -g- -CpCOREAVX -CfAVX -Tlinux  -oFPASCAL_RUN mandelbrot.fpascal-2.pas
Free Pascal Compiler version 3.2.2 [2021/05/16] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling mandelbrot.fpascal-2.pas
mandelbrot.fpascal-2.pas(22,3) Note: Local variable "start" is assigned but never used
mandelbrot.fpascal-2.pas(22,10) Note: Local variable "finish" is assigned but never used
Linking FPASCAL_RUN
Warning: "crtbegin.o" not found, this will probably cause a linking failure
Warning: "crtend.o" not found, this will probably cause a linking failure
131 lines compiled, 1.1 sec
2 warning(s) issued
2 note(s) issued
mv FPASCAL_RUN mandelbrot.fpascal-2.fpascal_run
rm mandelbrot.fpascal-2.pas

1.41s to complete and log all make actions

COMMAND LINE:
 ./mandelbrot.fpascal-2.fpascal_run 16000

(BINARY) PROGRAM OUTPUT NOT SHOWN