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