-
Notifications
You must be signed in to change notification settings - Fork 5
/
co.go
169 lines (145 loc) · 4.57 KB
/
co.go
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
package co
import (
"fmt"
"reflect"
"runtime"
"sync"
)
// ParBegin multiple functions onto background goroutines. This function blocks
// until all goroutines have terminated.
func ParBegin(fs ...func()) {
var wg sync.WaitGroup
for _, f := range fs {
wg.Add(1)
go func(f func()) {
defer wg.Done()
f()
}(f)
}
wg.Wait()
}
// ParForAll uses an iterator to execute an iterand function on each value
// returned by the iterator, using a background goroutine for each iteration.
// An iterator can be an array, a slice, a map, or an int. For arrays, and
// slices, the iterand function must accept an int index as the only argument.
// For maps, the iterand function must accept a key as the only argument. For
// ints, the iterand function must accept an int, in the range [0, n), as the
// only argument. This function blocks until all goroutines have terminated.
func ParForAll(iter interface{}, f interface{}) {
funTy := reflect.TypeOf(f)
if funTy.Kind() != reflect.Func {
panic(fmt.Sprintf("parforall error: expected iterator got %T", iter))
}
fun := reflect.ValueOf(f)
switch reflect.TypeOf(iter).Kind() {
case reflect.Array, reflect.Slice:
it := reflect.ValueOf(iter)
numGoroutines := it.Len()
var wg sync.WaitGroup
wg.Add(numGoroutines)
for i := 0; i < numGoroutines; i++ {
go func(i int) {
defer wg.Done()
fun.Call([]reflect.Value{reflect.ValueOf(i)})
}(i)
}
wg.Wait()
case reflect.Map:
it := reflect.ValueOf(iter)
keys := it.MapKeys()
numGoroutines := len(keys)
var wg sync.WaitGroup
wg.Add(numGoroutines)
for i := 0; i < numGoroutines; i++ {
go func(i int) {
defer wg.Done()
fun.Call([]reflect.Value{keys[i]})
}(i)
}
wg.Wait()
case reflect.Int:
numGoroutines := int(reflect.ValueOf(iter).Int())
var wg sync.WaitGroup
wg.Add(numGoroutines)
for i := 0; i < numGoroutines; i++ {
go func(i int) {
defer wg.Done()
fun.Call([]reflect.Value{reflect.ValueOf(i)})
}(i)
}
wg.Wait()
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
panic(fmt.Sprintf("parforall error: expected int got %T", iter))
default:
panic(fmt.Sprintf("parforall error: expected iterator got %T", iter))
}
}
// ForAll uses an iterator to execute an iterand function on each value
// returned by the iterator, using a background goroutine for CPU and
// distributing the iterands evenly across each goroutine. An iterator can be
// an array, a slice, a map, or an int. For arrays, and slices, the iterand
// function must accept an int index as the only argument. For maps, the
// iterand function must accept a key as the only argument. For ints, the
// iterand function must accept an int, in the range [0, n), as the only
// argument. This function blocks until all goroutines have terminated.
func ForAll(iter interface{}, f interface{}) {
funTy := reflect.TypeOf(f)
if funTy.Kind() != reflect.Func {
panic(fmt.Sprintf("forall error: expected iterator got %T", iter))
}
fun := reflect.ValueOf(f)
switch reflect.TypeOf(iter).Kind() {
case reflect.Array, reflect.Slice:
it := reflect.ValueOf(iter)
num := it.Len()
numGoroutines := runtime.NumCPU()
numPerGoroutine := num/numGoroutines + 1
var wg sync.WaitGroup
for i := 0; i < num; i += numPerGoroutine {
wg.Add(1)
go func(i int) {
defer wg.Done()
for j := i; j < i+numPerGoroutine && j < num; j++ {
fun.Call([]reflect.Value{reflect.ValueOf(j)})
}
}(i)
}
wg.Wait()
case reflect.Map:
it := reflect.ValueOf(iter)
keys := it.MapKeys()
num := len(keys)
numGoroutines := runtime.NumCPU()
numPerGoroutine := num/numGoroutines + 1
var wg sync.WaitGroup
for i := 0; i < num; i += numPerGoroutine {
wg.Add(1)
go func(i int) {
defer wg.Done()
for j := i; j < i+numPerGoroutine && j < num; j++ {
fun.Call([]reflect.Value{keys[j]})
}
}(i)
}
wg.Wait()
case reflect.Int:
num := int(reflect.ValueOf(iter).Int())
numGoroutines := runtime.NumCPU()
numPerGoroutine := num/numGoroutines + 1
var wg sync.WaitGroup
for i := 0; i < num; i += numPerGoroutine {
wg.Add(1)
go func(i int) {
defer wg.Done()
for j := i; j < i+numPerGoroutine && j < num; j++ {
fun.Call([]reflect.Value{reflect.ValueOf(j)})
}
}(i)
}
wg.Wait()
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
panic(fmt.Sprintf("forall error: expected int got %T", iter))
default:
panic(fmt.Sprintf("forall error: expected iterator got %T", iter))
}
}