The Computer Language
24.11 Benchmarks Game

mandelbrot Chapel program

source code

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

   contributed by Jacob Nelson, Lydia Duncan, Brad Chamberlain,
   and Ben Harshbarger
   derived from the GNU C version by Greg Buchholz
*/

use DynamicIters, IO, Math;

config const n = 200,              // image size in pixels (n x n)
             maxIter = 50,         // max # of iterations per pixel
             limit = 4.0,          // per-pixel convergence limit
             chunkSize = 1;        // dynamic iterator's chunk size

param bitsPerElt = 8;              // # of bits to store per array element
type eltType = uint(bitsPerElt);   // element type used to store the image


proc main() {
  const ydim = 0..#n,                          // the image's y dimension
        xdim = 0..#divCeilPos(n, bitsPerElt);  // the compacted x dimension

  var image: [ydim, xdim] eltType;             // the compacted bitmap image

  forall y in dynamic(ydim, chunkSize) {       // for all rows...
    for xelt in xdim {                         //   for each column element...

      var buff: eltType;                       // a single-element pixel buffer

      for off in 0..#bitsPerElt {              // for each bit in the buffer
        const x = xelt*bitsPerElt + off;       // compute its logical column

        const Cr = 2.0*x/n - 1.5;              // the (x,y) pixel as a complex
        const Ci = 2.0*y/n - 1.0;              //   (real, imag) value 'C'
        var Zr, Zi, Tr, Ti = 0.0;              // 'complex' helper values

        for 1..maxIter {                       // for the max # of iterations
          if (Tr + Ti > limit) then            // if we haven't converged
            break;
          
          Zi = 2.0*Zr*Zi + Ci;                 // update Z and T
          Zr = Tr - Ti + Cr;
          Tr = Zr**2;
          Ti = Zi**2;
        }

        buff <<= 1;                            // shift the pixel buffer
        if (Tr + Ti <= limit) then             // if 'C' is within the limit,
          buff |= 0x1;                         //   turn the low pixel on
      }

      image[y, xelt] = buff;                   // store the pixel buffer
    }
  }

  stdout.writef("P4\n");
  stdout.writef("%i %i\n", n, n);
  stdout.writeBinary(image);
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
chpl version  2.3.0
built with LLVM version 19.1.1
Copyright 2020-2024
Hewlett Packard
Enterprise Development LP
Copyright 2004-2019 Cray Inc.


 Thu, 19 Dec 2024 02:15:11 GMT

MAKE:
mv mandelbrot.chapel mandelbrot.chpl
/opt/src/chapel-2.3.0/bin/linux64-x86_64/chpl --fast mandelbrot.chpl -o mandelbrot.chapel_run
rm mandelbrot.chpl

22.02s to complete and log all make actions

COMMAND LINE:
 ./mandelbrot.chapel_run --n=16000

(BINARY) PROGRAM OUTPUT NOT SHOWN