The Computer Language
Benchmarks Game

mandelbrot Pascal Free Pascal #2 program

source code

{ The Computer Language Benchmarks Game
  http://benchmarksgame.alioth.debian.org

  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:
32-bit Ubuntu one core
Free Pascal Compiler version 3.0.0 [2015/11/24] for i386


Mon, 30 Nov 2015 00:27:22 GMT

MAKE:
mv mandelbrot.fpascal-2.fpascal mandelbrot.fpascal-2.pas
/usr/local/src/fpc-3.0.0.i386-linux/bin/fpc -FuInclude/fpascal -XXs -Oppentiumm -Cppentiumm -O3 -Cfsse2  -oFPASCAL_RUN mandelbrot.fpascal-2.pas
Free Pascal Compiler version 3.0.0 [2015/11/24] for i386
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Linux for i386
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: "crti.o" not found, this will probably cause a linking failure
Warning: "crtn.o" not found, this will probably cause a linking failure



























/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
131 lines compiled, 0.2 sec
2 warning(s) issued
2 note(s) issued
mv FPASCAL_RUN mandelbrot.fpascal-2.fpascal_run
rm mandelbrot.fpascal-2.pas
0.25s to complete and log all make actions

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

(BINARY) PROGRAM OUTPUT NOT SHOWN