func matrixMultiplyAdd(m, n, k int, mk, kn, mn []float32) { mkIn := func(kk int, east chan float32) { for mm := 0; mm < m; mm++ { east <- mk[mm*k+kk] } } var wg sync.WaitGroup mnOut := func(nn int, north chan float32) { for mm := 0; mm < m; mm++ { mn[mm*n+nn] += <-north } wg.Done() } dpu := func(kk, nn int, north, west, south, east chan float32) { for mm := 0; mm < m; mm++ { mn := float32(0) if north != nil { mn = <-north } mk := <-west mn += mk * kn[kk*n+nn] south <- mn if east != nil { east <- mk } } } south := make([]chan float32, n) for kk := 0; kk < k; kk++ { east := make(chan float32) go mkIn(kk, east) for nn := 0; nn < n; nn++ { var ( north = south[nn] west = east ) south[nn] = make(chan float32) east = nil if nn < n-1 { east = make(chan float32) } go dpu(kk, nn, north, west, south[nn], east) } } wg.Add(m) for nn, north := range south { go mnOut(nn, north) } wg.Wait() }
To receive a hint, submit unfixed code.