3 ms·
In which situations is parallel execution used?
by nzjrs 10y ago
In which situations is parallel execution used?
- amzans 10y agoBasically every time that we need to iterate over the Pix []uint8 containing the pixel data, as this can be run in parallel (or concurrently if running in a single logical CPU). You will usually see it like this: parallel.Line(height, func(start, end int) { for y := start; y < end; y++ { for x := 0; x < w; x++ { pos:= y*img.Stride + x*4 // ... } } }) parallel.Line is a function that takes a length int and a fn func(start, end int) and it splits the provided length into segments, then it dispatches the provided fn with each segment range to a new goroutine. The number of segments is defined by the number of available logical CPUs. So basically each dispatched fn is iterating over its assigned range of the Pix []uint8 and the image is split into chunks by height. Notice that only the y-axis loop is assigned a partial range and we iterate over the x-axis first, this is because we want to move sequentially in the slice as much as possible. During the optimization stage I would like to benchmark against a tiled segmentation instead of a line one, but the current version only implements the latter.