Skip to content

Commit

Permalink
Merge 47e892e into 879073f
Browse files Browse the repository at this point in the history
  • Loading branch information
marianrh committed May 20, 2020
2 parents 879073f + 47e892e commit 4058213
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions utils.go
Expand Up @@ -7,6 +7,10 @@ import (
"sync"
)

// MaxProcs limits the number of parallel processing subroutines to the set value.
// A value <= 0 is ignored.
var MaxProcs = 0

// parallel processes the data in separate goroutines.
func parallel(start, stop int, fn func(<-chan int)) {
count := stop - start
Expand All @@ -15,6 +19,9 @@ func parallel(start, stop int, fn func(<-chan int)) {
}

procs := runtime.GOMAXPROCS(0)
if procs > MaxProcs && MaxProcs > 0 {
procs = MaxProcs
}
if procs > count {
procs = count
}
Expand Down
27 changes: 27 additions & 0 deletions utils_test.go
Expand Up @@ -49,6 +49,33 @@ func testParallelN(n, procs int) bool {
return true
}

func TestParallelMaxProcs(t *testing.T) {
for _, n := range []int{0, 1, 10, 100, 1000} {
for _, p := range []int{1, 2, 4, 8, 16, 100} {
if !testParallelMaxProcsN(n, p) {
t.Fatalf("test [parallel %d %d] failed", n, p)
}
}
}
}

func testParallelMaxProcsN(n, procs int) bool {
data := make([]bool, n)
MaxProcs = procs
parallel(0, n, func(is <-chan int) {
for i := range is {
data[i] = true
}
})

for i := 0; i < n; i++ {
if !data[i] {
return false
}
}
return true
}

func TestClamp(t *testing.T) {
testCases := []struct {
f float64
Expand Down

0 comments on commit 4058213

Please sign in to comment.