The Computer Language
Benchmarks Game

mandelbrot Scala #5 program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/
   original contributed by Isaac Gouy
   made to use single array and parallelized by Stephen Marsh
   converted to Scala 2.8 by Rex Kerr
   made to use parallel collections and removed synchronized blocks by Steve Vickers
*/
import java.io.BufferedOutputStream

object mandelbrot {
	var size: Int = 0
	var bytesPerRow: Int = 0
	var nextRow = 0
	val limitSquared = 4.0
	val max = 50

	def main(args: Array[String]) {
		size = args(0).toInt
		bytesPerRow = (size+7)/8 // ceiling of (size / 8)

		println("P4\n" + size + " " + size)
		val w = new BufferedOutputStream(System.out)

		(0 until size).par.map (CalculateRow.apply).zipWithIndex.toVector.sortBy (_._2) foreach {
			case (bitmap, y) =>

			w.write(bitmap)
			}

		w.close
	}

	object CalculateRow {
		def apply (y : Int) : Array[Byte] = {
			val bitmap = new Array[Byte] (bytesPerRow);
			var bits = 0
			var bitnum = 0
			var x = 0
			var aindex = 0

			while (x < size) {

				val cr = 2.0 * x / size - 1.5
				val ci = 2.0 * y / size - 1.0

				var zr, tr, zi, ti = 0.0

				var j = max
				do {
					zi = 2.0 * zr * zi + ci
					zr = tr - ti + cr
					ti = zi*zi
					tr = zr*zr

					j = j - 1
				} while (!(tr + ti > limitSquared) && j > 0)

				bits = bits << 1
				if (!(tr + ti > limitSquared)) bits += 1
				bitnum += 1

				if (x == size - 1) {
					bits = bits << (8 - bitnum)
					bitnum = 8
				}

				if (bitnum == 8) {
					bitmap(aindex) = bits.toByte
					aindex += 1
					bits = 0
					bitnum = 0
				}

				x += 1
			}

			bitmap;
		}
	}
}

// vim: set ts=4 sw=4 noet:
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Scala compiler version 2.12.1 -- Copyright 2002-2016, LAMP/EPFL and Lightbend, Inc.
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)


Mon, 06 Feb 2017 05:38:16 GMT

MAKE:
mv mandelbrot.scala-5.scala mandelbrot.scala
/usr/local/src/scala-2.12.1/bin/scalac -optimise -target:jvm-1.8 mandelbrot.scala
warning: there was one deprecation warning; re-run with -deprecation for details
one warning found
6.71s to complete and log all make actions

COMMAND LINE:
 /usr/local/src/jdk1.8.0_121/bin/java  -Xbootclasspath/a:/usr/local/src/scala-2.12.1/lib/scala-library.jar:/usr/local/src/scala-2.12.1/lib/scala-actors-2.11.0.jar:/usr/local/src/scala-2.12.1/lib/akka-actor_2.11-2.3.4.jar:/usr/local/src/scala-2.12.1/lib/config-1.2.1.jar mandelbrot 16000

(BINARY) PROGRAM OUTPUT NOT SHOWN