The Computer Language
Benchmarks Game

mandelbrot OCaml program

source code

(*
 * The Computer Language Benchmarks Game
 * http://benchmarksgame.alioth.debian.org/
 *
 * Contributed by Paolo Ribeca
 *
 * (Very loosely based on previous version Ocaml #3,
 *  which had been contributed by
 *   Christophe TROESTLER
 *  and enhanced by
 *   Christian Szegedy and Yaron Minsky)
 *)

let niter = 50
let limit = 4.
let workers = 64

let worker w h_lo h_hi =
  let buf =
    String.create ((w / 8 + (if w mod 8 > 0 then 1 else 0)) * (h_hi - h_lo))
  and ptr = ref 0 in
  let fw = float w /. 2. in
  let fh = fw in
  let red_w = w - 1 and red_h_hi = h_hi - 1 and byte = ref 0 in
  for y = h_lo to red_h_hi do
    let ci = float y /. fh -. 1. in
    for x = 0 to red_w do
      let cr = float x /. fw -. 1.5
      and zr = ref 0. and zi = ref 0. and trmti = ref 0. and n = ref 0 in
      begin try
	while true do
	  zi := 2. *. !zr *. !zi +. ci;
	  zr := !trmti +. cr;
	  let tr = !zr *. !zr and ti = !zi *. !zi in
	  if tr +. ti > limit then begin
	    byte := !byte lsl 1;
	    raise Exit
	  end else if incr n; !n = niter then begin
	    byte := (!byte lsl 1) lor 0x01;
	    raise Exit
	  end else
	    trmti := tr -. ti
	done
      with Exit -> ()
      end;
      if x mod 8 = 7 then begin
	buf.[!ptr] <- (Char.chr !byte);
	incr ptr;
	byte := 0
      end
    done;
    let rem = w mod 8 in
    if rem != 0 then begin
      buf.[!ptr] <- (Char.chr (!byte lsl (8 - rem)));
      incr ptr;
      byte := 0
    end
  done;
  buf

let _ =
  let w = int_of_string (Array.get Sys.argv 1) in
  let rows = w / workers and rem = w mod workers in
  Printf.printf "P4\n%i %i\n%!" w w;
  let rec spawn i =
    if i > 0 then
      let red_i = i - 1 in
      match Unix.fork () with
      | 0 -> spawn red_i
      | pid ->
	  let buf =
	    worker w (red_i * rows + min red_i rem) (i * rows + min i rem) in
	  match Unix.waitpid [] pid with
	  | _, Unix.WEXITED 0 ->
	      Printf.printf "%s%!" buf;
	      exit 0
	  | _ -> assert false
    else
      exit 0 in
  spawn workers
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
The OCaml native-code compiler, version 4.06.0


Sun, 05 Nov 2017 22:19:25 GMT

MAKE:
mv mandelbrot.ocaml mandelbrot.ml
/opt/src/ocaml-4.06.0/bin/ocamlopt -noassert -unsafe -fPIC -nodynlink -inline 100 -O3 unix.cmxa mandelbrot.ml -o mandelbrot.ocaml_run
File "mandelbrot.ml", line 20, characters 4-17:
Warning 3: deprecated: String.create
Use Bytes.create instead.
File "mandelbrot.ml", line 47, characters 1-31:
Warning 3: deprecated: String.unsafe_set
File "mandelbrot.ml", line 54, characters 6-52:
Warning 3: deprecated: String.unsafe_set
File "mandelbrot.ml", line 75, characters 28-31:
Error: This expression has type bytes but an expression was expected of type
         string
/home/dunham/benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:417: recipe for target 'mandelbrot.ocaml_run' failed
make: [mandelbrot.ocaml_run] Error 2 (ignored)
rm mandelbrot.ml

0.05s to complete and log all make actions

COMMAND LINE:
./mandelbrot.ocaml_run 1000

MAKE ERROR