public snapshot · system/tools/nib/bench/xlang/ · system@main · commit a92f56ab098e · shared 2026-11-22T22:43:57Z· frozen: this page does not change when the repository does
1 // mandelbrot on every core: mandelbrot's image and output, one goroutine per
2 // GOMAXPROCS, each taking the next row from a shared counter.
3 package main
4
5 import (
6 "fmt"
7 "runtime"
8 "sync"
9 "sync/atomic"
10 )
11
12 func row(y, size, iters int) (int64, int64) {
13 ci := 2.0*float64(y)/float64(size) - 1.0
14 bytes := make([]byte, (size+7)/8)
15 var count int64
16 for x := 0; x < size; x++ {
17 cr := 2.0*float64(x)/float64(size) - 1.5
18 zr, zi := 0.0, 0.0
19 inside := true
20 for i := 0; i < iters; i++ {
21 t := zr*zr - zi*zi + cr
22 zi = 2.0*zr*zi + ci
23 zr = t
24 if zr*zr+zi*zi > 4.0 {
25 inside = false
26 break
27 }
28 }
29 if inside {
30 bytes[x/8] |= 0x80 >> uint(x%8)
31 count++
32 }
33 }
34 var sum int64
35 for _, b := range bytes {
36 sum += int64(b)
37 }
38 return sum, count
39 }
40
41 func main() {
42 size, iters := 4000, 50
43 threads := runtime.GOMAXPROCS(0)
44 var next int64 = -1
45 sums := make([]int64, threads)
46 counts := make([]int64, threads)
47 var wg sync.WaitGroup
48 for t := 0; t < threads; t++ {
49 wg.Add(1)
50 go func(t int) {
51 defer wg.Done()
52 for {
53 y := int(atomic.AddInt64(&next, 1))
54 if y >= size {
55 return
56 }
57 s, c := row(y, size, iters)
58 sums[t] += s
59 counts[t] += c
60 }
61 }(t)
62 }
63 wg.Wait()
64 var byteSum, count int64
65 for t := 0; t < threads; t++ {
66 byteSum += sums[t]
67 count += counts[t]
68 }
69 fmt.Printf("%d\n%d\n", byteSum, count)
70 }
snapshot of a92f56ab098e · 2858 files · browse