mandelbrot Swift #5 program
source code
/* The Computer Language Benchmarks Game
https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
direct transliteration of Greg Buchholz's C program
contributed by Isaac Gouy, fix by David Turnbull
dispatching to multiple cores by Patrick Stein
*/
#if os(Linux)
import Glibc
import Dispatch
#else
import Foundation
#endif
let width: Int = Int(CommandLine.arguments[1])!
let height = width
let iter = 50, limit = 2.0
print("P4\n\(width) \(height)")
let linesize = (width + (width % 8 != 0 ? 8 : 0 )) / 8
let dispatchGroup = DispatchGroup()
var outputbits:[UInt8] = [UInt8](repeating:0, count: linesize*height)
var counter = 0
var height_d = Double(height)
for y in 0..<height
{
var address = counter
counter += linesize
dispatchGroup.enter()
DispatchQueue.global().async
{
var Zr, Zi, Cr, Ci, Tr, Ti: Double
var byte_acc:UInt8 = 0
var bit_num = 0
Ci = 2.0*Double(y)/height_d - 1.0
let twodivwidth = 2.0 / Double(width)
for x in 0..<width
{
Zr = 0.0; Zi = 0.0; Tr = 0.0; Ti = 0.0
Cr = twodivwidth*Double(x) - 1.5;
var i = 0
while i < iter && (Tr+Ti <= limit*limit)
{
i += 1
Zi = 2.0*Zr*Zi + Ci
Zr = Tr - Ti + Cr
Tr = Zr * Zr
Ti = Zi * Zi
}
byte_acc <<= 1
if Tr+Ti <= limit*limit { byte_acc |= 0x01 }
bit_num += 1
if bit_num == 8 {
outputbits[address] = byte_acc
address += 1
byte_acc = 0
bit_num = 0
}
}
if bit_num != 0
{
byte_acc <<= (8-width%8)
outputbits[address] = byte_acc
}
dispatchGroup.leave()
}
}
dispatchGroup.wait()
counter = 0
for _ in 0..<height
{
for x in 0..<linesize
{
putc(Int32(outputbits[counter+x]),stdout)
}
counter += linesize
}
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
Swift version 6.0
(swift-6.0-RELEASE)
Target: x86_64-unknown-linux-gnu
Wed, 18 Sep 2024 23:22:35 GMT
MAKE:
/opt/src/swift-6.0-RELEASE/usr/bin/swiftc mandelbrot.swift-5.swift -Ounchecked -wmo -o mandelbrot.swift-5.swift_run
mandelbrot.swift-5.swift:66:28: warning: reference to captured var 'address' in concurrently-executing code; this is an error in the Swift 6 language mode
64 |
65 | if bit_num == 8 {
66 | outputbits[address] = byte_acc
| `- warning: reference to captured var 'address' in concurrently-executing code; this is an error in the Swift 6 language mode
67 | address += 1
68 | byte_acc = 0
mandelbrot.swift-5.swift:67:17: warning: mutation of captured var 'address' in concurrently-executing code; this is an error in the Swift 6 language mode
65 | if bit_num == 8 {
66 | outputbits[address] = byte_acc
67 | address += 1
| `- warning: mutation of captured var 'address' in concurrently-executing code; this is an error in the Swift 6 language mode
68 | byte_acc = 0
69 | bit_num = 0
mandelbrot.swift-5.swift:75:25: warning: reference to captured var 'address' in concurrently-executing code; this is an error in the Swift 6 language mode
73 | {
74 | byte_acc <<= (8-width%8)
75 | outputbits[address] = byte_acc
| `- warning: reference to captured var 'address' in concurrently-executing code; this is an error in the Swift 6 language mode
76 | }
77 |
13.92s to complete and log all make actions
COMMAND LINE:
./mandelbrot.swift-5.swift_run 16000
(BINARY) PROGRAM OUTPUT NOT SHOWN