main.mx raw

   1  // The slice: one domain per variable node of a repetition code's chain, a
   2  // synchronous round per belief-propagation step, and a manager that drives the
   3  // barrier. The graph is a fixed chain (a tree, so BP is exact); the answer is
   4  // the all-zero codeword, and the manager also runs the same BP serially as the
   5  // reference. The point is the number, not the demo: edges/s/domain, the frame
   6  // bytes per message, the allocation churn, and how a slow neighbour stalls a
   7  // round.
   8  //
   9  // Edges: node i owns the edge to i+1, and the same channel is both ends' view of
  10  // it. One round: every node sends its belief left and right, receives the
  11  // peer's, adds its prior, and reports to the manager. The manager's barrier is
  12  // what makes the rounds synchronous - it sends every node a go and waits for
  13  // every node's done.
  14  package main
  15  
  16  import (
  17  	"runtime"
  18  	"time"
  19  )
  20  
  21  type breaker = chan struct{}
  22  type ch32 = chan int32
  23  
  24  const (
  25  	nodes       = int32(32)
  26  	rounds      = int32(32)
  27  	slowNode    = int32(13)
  28  	slowMicros  = int32(200)
  29  	elementSize = int32(4)
  30  	// A ring frame is a 4-byte big-endian length and the payload, aligned to 8.
  31  	frameBytes = int32((4 + elementSize + 7) & 0xFFFFFFF8)
  32  )
  33  
  34  // priorAt is the LLR of the received word: mostly the all-zero codeword, with
  35  // three flipped bits. Both the serial reference and the nodes read it.
  36  func priorAt(i int32) (p int32) {
  37  	if i == 3 || i == 11 || i == 24 {
  38  		return -8
  39  	}
  40  	return 8
  41  }
  42  
  43  // node is one variable node. It sends its belief on both incident edges, takes
  44  // the peer's, adds its prior, and reports the result on done. The boundary
  45  // nodes simply skip the edge they do not have.
  46  func node(i int32, goCh chan int32, doneCh chan int32, left chan int32, right chan int32, stats chan int32, slow int32) (closed chan struct{}) {
  47  	var before runtime.MemStats
  48  	runtime.ReadMemStats(&before)
  49  	// mL/mR are the messages this node has received from its two checks; the
  50  	// posterior is prior + both, and what it SENDS is the extrinsic (prior plus
  51  	// the other side). Sending the posterior double-counts and diverges.
  52  	mL := int32(0)
  53  	mR := int32(0)
  54  	belief := priorAt(i)
  55  	for {
  56  		select {
  57  		case <-closed:
  58  			var after runtime.MemStats
  59  			runtime.ReadMemStats(&after)
  60  			stats <- int32(after.Allocs - before.Allocs)
  61  			return closed
  62  		case <-goCh:
  63  			{
  64  			eL := priorAt(i) + mR
  65  			eR := priorAt(i) + mL
  66  			if i > 0 {
  67  				left <- eL
  68  			}
  69  			if i < nodes-1 {
  70  				right <- eR
  71  			}
  72  			if i > 0 {
  73  				mL = <-left
  74  			}
  75  			if i < nodes-1 {
  76  				mR = <-right
  77  			}
  78  			belief = priorAt(i) + mL + mR
  79  			if slow > 0 && i == slowNode {
  80  				deadline := time.Now().Add(time.Duration(slow) * time.Microsecond)
  81  				for time.Now().Before(deadline) {
  82  				}
  83  			}
  84  			doneCh <- belief
  85  			}
  86  		}
  87  	}
  88  }
  89  
  90  // serial is the reference: the same BP, one thread, no channels.
  91  func serial(r int32) (bits []int32) {
  92  	mL := []int32{:nodes}
  93  	mR := []int32{:nodes}
  94  	nL := []int32{:nodes}
  95  	nR := []int32{:nodes}
  96  	for round := int32(0); round < r; round++ {
  97  		for i := int32(0); i < nodes; i++ {
  98  			nL[i] = priorAt(i) + mR[i]
  99  			nR[i] = priorAt(i) + mL[i]
 100  		}
 101  		for i := int32(0); i < nodes; i++ {
 102  			mL[i] = 0
 103  			mR[i] = 0
 104  			if i > 0 {
 105  				mL[i] = nR[i-1]
 106  			}
 107  			if i < nodes-1 {
 108  				mR[i] = nL[i+1]
 109  			}
 110  		}
 111  	}
 112  	bits = []int32{:nodes}
 113  	for i := int32(0); i < nodes; i++ {
 114  		belief := priorAt(i) + mL[i] + mR[i]
 115  		if belief < 0 {
 116  			bits[i] = 1
 117  		}
 118  	}
 119  	return bits
 120  }
 121  
 122  // run builds the bundle, drives r rounds, and returns the round time, the
 123  // decoded bits, the allocation count across root and nodes, and the belief
 124  // vector the manager saw last.
 125  func run(r int32, slow int32) (us int64, bits []int32, allocs int64) {
 126  	goCh := []ch32{:nodes}
 127  	doneCh := []ch32{:nodes}
 128  	statsCh := []ch32{:nodes}
 129  	for i := int32(0); i < nodes; i++ {
 130  		goCh[i] = chan int32{}
 131  		doneCh[i] = chan int32{}
 132  		statsCh[i] = chan int32{}
 133  	}
 134  	edges := []ch32{:(nodes - 1)}
 135  	for i := int32(0); i < nodes-1; i++ {
 136  		edges[i] = chan int32{}
 137  	}
 138  	dummyL := chan int32{}
 139  	dummyR := chan int32{}
 140  	breakers := []breaker{:nodes}
 141  	for i := int32(0); i < nodes; i++ {
 142  		left := dummyL
 143  		right := dummyR
 144  		if i > 0 {
 145  			left = edges[i-1]
 146  		}
 147  		if i < nodes-1 {
 148  			right = edges[i]
 149  		}
 150  		breakers[i] = spawn(node, i, goCh[i], doneCh[i], left, right, statsCh[i], slow)
 151  	}
 152  	var rootBefore runtime.MemStats
 153  	runtime.ReadMemStats(&rootBefore)
 154  	beliefs := []int32{:nodes}
 155  	start := time.Now()
 156  	for round := int32(0); round < r; round++ {
 157  		for i := int32(0); i < nodes; i++ {
 158  			goCh[i] <- 1
 159  		}
 160  		for i := int32(0); i < nodes; i++ {
 161  			beliefs[i] = <-doneCh[i]
 162  		}
 163  	}
 164  	elapsed := time.Since(start)
 165  	us = elapsed.Microseconds()
 166  	var rootAfter runtime.MemStats
 167  	runtime.ReadMemStats(&rootAfter)
 168  	allocs = int64(rootAfter.Allocs - rootBefore.Allocs)
 169  	for i := int32(0); i < nodes; i++ {
 170  		close(breakers[i])
 171  	}
 172  	for i := int32(0); i < nodes; i++ {
 173  		allocs += int64(<-statsCh[i])
 174  	}
 175  	bits = []int32{:nodes}
 176  	for i := int32(0); i < nodes; i++ {
 177  		if beliefs[i] < 0 {
 178  			bits[i] = 1
 179  		}
 180  	}
 181  	return us, bits, allocs
 182  }
 183  
 184  func main() {
 185  	select {
 186  	case <-shutdown:
 187  		return
 188  	default:
 189  	}
 190  
 191  	ref := serial(rounds)
 192  	us, bits, allocs := run(rounds, 0)
 193  	usSlow, _, _ := run(rounds, slowMicros)
 194  
 195  	edges := int64(2*(nodes-1)) * int64(rounds)
 196  	msgs := edges + int64(2*nodes)*int64(rounds)
 197  	perSec := int64(0)
 198  	if us > 0 {
 199  		perSec = edges * 1000000 / us
 200  	}
 201  	msgPerSec := int64(0)
 202  	if us > 0 {
 203  		msgPerSec = msgs * 1000000 / us
 204  	}
 205  	allocsPerRound := allocs / int64(rounds)
 206  	bytesPerRound := allocsPerRound * 64 // an arena allocation is at least a cache line
 207  	println("slice nodes=", nodes, "rounds=", rounds, "edges=", edges, "msgs=", msgs)
 208  	println("baseline_us=", us, "edges_per_sec=", perSec, "edges_per_sec_domain=", perSec/int64(nodes))
 209  	println("msgs_per_sec=", msgPerSec, "frame_bytes=", frameBytes, "element_bytes=", elementSize)
 210  	println("allocs_total=", allocs, "allocs_per_round=", allocsPerRound, "bytes_per_round_est=", bytesPerRound)
 211  	println("slow_node=", slowNode, "slow_us=", slowMicros, "slow_round_us=", usSlow, "stall_us=", usSlow-us)
 212  
 213  	// The reference and the distributed run must agree, and the answer must be
 214  	// the all-zero codeword: the graph is a tree, so BP is exact.
 215  	for i := int32(0); i < nodes; i++ {
 216  		if bits[i] != ref[i] {
 217  			println("mismatch at", i, "distributed=", bits[i], "reference=", ref[i])
 218  			panic("distributed BP disagrees with the serial reference")
 219  		}
 220  		if bits[i] != 0 {
 221  			println("nonzero at", i, "distributed=", bits[i], "reference=", ref[i])
 222  			panic("BP did not decode to the all-zero codeword")
 223  		}
 224  	}
 225  	println("decode ok: distributed == reference, all-zero codeword")
 226  }
 227