1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
package parallel
import (
"context"
"runtime"
"sync"
"sync/atomic"
"golang.org/x/sync/errgroup"
"github.com/bradenaw/juniper/container/xheap"
"github.com/bradenaw/juniper/iterator"
"github.com/bradenaw/juniper/stream"
)
// Do calls f from parallelism goroutines n times, providing each invocation a unique i in [0, n).
//
// If parallelism <= 0, uses GOMAXPROCS instead.
func Do(
parallelism int,
n int,
f func(i int),
) {
if parallelism <= 0 {
parallelism = runtime.GOMAXPROCS(-1)
}
if parallelism > n {
parallelism = n
}
if parallelism == 1 {
for i := 0; i < n; i++ {
f(i)
}
return
}
x := int32(-1)
var wg sync.WaitGroup
wg.Add(parallelism)
for j := 0; j < parallelism; j++ {
go func() {
defer wg.Done()
for {
i := int(atomic.AddInt32(&x, 1))
if i >= n {
return
}
f(i)
}
}()
}
wg.Wait()
return
}
// DoContext calls f from parallelism goroutines n times, providing each invocation a unique i in
// [0, n).
//
// If any call to f returns an error the context passed to invocations of f is cancelled, no further
// calls to f are made, and Do returns the first error encountered.
//
// If parallelism <= 0, uses GOMAXPROCS instead.
func DoContext(
ctx context.Context,
parallelism int,
n int,
f func(ctx context.Context, i int) error,
) error {
if parallelism <= 0 {
parallelism = runtime.GOMAXPROCS(-1)
}
if parallelism > n {
parallelism = n
}
if parallelism == 1 {
for i := 0; i < n; i++ {
err := f(ctx, i)
if err != nil {
return err
}
}
return nil
}
x := int32(-1)
eg, ctx := errgroup.WithContext(ctx)
for j := 0; j < parallelism; j++ {
eg.Go(func() error {
for {
i := int(atomic.AddInt32(&x, 1))
if i >= n {
return nil
}
if ctx.Err() != nil {
return ctx.Err()
}
err := f(ctx, i)
if err != nil {
return err
}
}
})
}
return eg.Wait()
}
// Map uses parallelism goroutines to call f once for each element of in. out[i] is the
// result of f for in[i].
//
// If parallelism <= 0, uses GOMAXPROCS instead.
func Map[T any, U any](
parallelism int,
in []T,
f func(in T) U,
) []U {
out := make([]U, len(in))
Do(parallelism, len(in), func(i int) {
out[i] = f(in[i])
})
return out
}
// MapContext uses parallelism goroutines to call f once for each element of in. out[i] is the
// result of f for in[i].
//
// If any call to f returns an error the context passed to invocations of f is cancelled, no further
// calls to f are made, and Map returns the first error encountered.
//
// If parallelism <= 0, uses GOMAXPROCS instead.
func MapContext[T any, U any](
ctx context.Context,
parallelism int,
in []T,
f func(ctx context.Context, in T) (U, error),
) ([]U, error) {
out := make([]U, len(in))
err := DoContext(ctx, parallelism, len(in), func(ctx context.Context, i int) error {
var err error
out[i], err = f(ctx, in[i])
return err
})
if err != nil {
return nil, err
}
return out, nil
}
// MapIterator uses parallelism goroutines to call f once for each element yielded by iter. The
// returned iterator returns these results in the same order that iter yielded them in.
//
// This iterator, in contrast with most, must be consumed completely or it will leak the goroutines.
//
// If parallelism <= 0, uses GOMAXPROCS instead.
//
// bufferSize is the size of the work buffer. A larger buffer uses more memory but gives better
// throughput in the face of larger variance in the processing time for f.
func MapIterator[T any, U any](
iter iterator.Iterator[T],
parallelism int,
bufferSize int,
f func(T) U,
) iterator.Iterator[U] {
if parallelism <= 0 {
parallelism = runtime.GOMAXPROCS(-1)
}
if bufferSize < parallelism {
bufferSize = parallelism
}
in := make(chan valueAndIndex[T])
mIter := &mapIterator[U]{
ch: make(chan valueAndIndex[U]),
h: xheap.New(func(a, b valueAndIndex[U]) bool {
return a.idx < b.idx
}, nil),
i: 0,
bufferSize: bufferSize,
inFlight: 0,
}
mIter.cond = sync.NewCond(&mIter.m)
go func() {
i := 0
for {
item, ok := iter.Next()
if !ok {
break
}
mIter.m.Lock()
for mIter.inFlight >= bufferSize {
mIter.cond.Wait()
}
mIter.inFlight++
mIter.m.Unlock()
in <- valueAndIndex[T]{
value: item,
idx: i,
}
i++
}
close(in)
}()
nDone := uint32(0)
for i := 0; i < parallelism; i++ {
go func() {
for item := range in {
u := f(item.value)
mIter.ch <- valueAndIndex[U]{value: u, idx: item.idx}
}
if atomic.AddUint32(&nDone, 1) == uint32(parallelism) {
close(mIter.ch)
}
}()
}
return mIter
}
type mapIterator[U any] struct {
ch chan valueAndIndex[U]
m sync.Mutex
cond *sync.Cond
bufferSize int
inFlight int
h xheap.Heap[valueAndIndex[U]]
i int
}
func (iter *mapIterator[U]) Next() (U, bool) {
for {
if iter.h.Len() > 0 && iter.h.Peek().idx == iter.i {
item := iter.h.Pop()
iter.i++
iter.m.Lock()
iter.inFlight--
if iter.inFlight == iter.bufferSize-1 {
iter.cond.Signal()
}
iter.m.Unlock()
return item.value, true
}
item, ok := <-iter.ch
if !ok {
var zero U
return zero, false
}
iter.h.Push(item)
}
}
type valueAndIndex[T any] struct {
value T
idx int
}
// MapStream uses parallelism goroutines to call f once for each element yielded by s. The returned
// stream returns these results in the same order that s yielded them in.
//
// If any call to f returns an error the context passed to invocations of f is cancelled, no further
// calls to f are made, and the returned stream's Next returns the first error encountered.
//
// If parallelism <= 0, uses GOMAXPROCS instead.
//
// bufferSize is the size of the work buffer. A larger buffer uses more memory but gives better
// throughput in the face of larger variance in the processing time for f.
func MapStream[T any, U any](
ctx context.Context,
s stream.Stream[T],
parallelism int,
bufferSize int,
f func(context.Context, T) (U, error),
) stream.Stream[U] {
if parallelism <= 0 {
parallelism = runtime.GOMAXPROCS(-1)
}
if bufferSize < parallelism {
bufferSize = parallelism
}
in := make(chan valueAndIndex[T])
ready := make(chan struct{}, bufferSize)
for i := 0; i < bufferSize; i++ {
ready <- struct{}{}
}
ctx, cancel := context.WithCancel(ctx)
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
defer s.Close()
defer close(in)
i := 0
for {
item, err := s.Next(ctx)
if err == stream.End {
break
} else if err != nil {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
case <-ready:
}
select {
case <-ctx.Done():
return ctx.Err()
case in <- valueAndIndex[T]{
value: item,
idx: i,
}:
}
i++
}
return nil
})
c := make(chan valueAndIndex[U], bufferSize)
nDone := uint32(0)
for i := 0; i < parallelism; i++ {
eg.Go(func() error {
defer func() {
if atomic.AddUint32(&nDone, 1) == uint32(parallelism) {
close(c)
}
}()
for item := range in {
u, err := f(ctx, item.value)
if err != nil {
return err
}
select {
case c <- valueAndIndex[U]{value: u, idx: item.idx}:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
})
}
return &mapStream[U]{
cancel: cancel,
eg: eg,
c: c,
ready: ready,
h: xheap.New(func(a, b valueAndIndex[U]) bool {
return a.idx < b.idx
}, nil),
i: 0,
}
}
type mapStream[U any] struct {
cancel context.CancelFunc
eg *errgroup.Group
c <-chan valueAndIndex[U]
ready chan struct{}
h xheap.Heap[valueAndIndex[U]]
i int
}
func (s *mapStream[U]) Next(ctx context.Context) (U, error) {
var zero U
for {
if s.h.Len() > 0 && s.h.Peek().idx == s.i {
item := s.h.Pop()
s.i++
s.ready <- struct{}{}
return item.value, nil
}
select {
case item, ok := <-s.c:
if !ok {
err := s.eg.Wait()
if err != nil {
return zero, err
}
return zero, stream.End
}
s.h.Push(item)
case <-ctx.Done():
return zero, ctx.Err()
}
}
}
func (s *mapStream[U]) Close() {
s.cancel()
_ = s.eg.Wait()
}
|