forked from ewalker544/libsvm-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.go
More file actions
97 lines (82 loc) · 1.98 KB
/
Copy pathparallel.go
File metadata and controls
97 lines (82 loc) · 1.98 KB
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
/*
** Copyright 2014 Edward Walker
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http ://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** Description: Useful types/methods for running loops in parallel.
** @author: Ed Walker
*/
package libSvm
import (
//"fmt"
"runtime"
)
type parallelRunner struct {
N int
numCPU int
rem int
constRem int
block int
start int
done chan bool
}
/**
* Returns the start and end iterations for a CPU
*/
func (p *parallelRunner) next() (int, int) {
start := p.start
end := p.start + p.block
if p.rem > 0 {
end++
p.rem--
}
p.start = end
return start, end
}
func (p *parallelRunner) reset() {
p.rem = p.constRem
p.start = 0
}
func (p parallelRunner) run(f func(int, int, int)) {
funcWrap := func(tid, start, end int) {
f(tid, start, end)
p.done <- true
}
p.reset()
for i := 0; i < p.numCPU; i++ {
start, end := p.next()
go funcWrap(i, start, end)
}
}
func (p parallelRunner) waitAll() {
for i := 0; i < p.numCPU; i++ {
<-p.done
}
}
func newParallelRunner(n, nCPU int) parallelRunner {
var cpus int
if nCPU < 1 {
cpus = runtime.NumCPU() // query the number of available CPUs
runtime.GOMAXPROCS(cpus) // set this as the number of usable CPUs
} else {
runtime.GOMAXPROCS(nCPU)
}
cpus = runtime.GOMAXPROCS(0) // get the new max number of CPUs
p := parallelRunner{N: n, numCPU: cpus}
p.rem = p.N % p.numCPU
p.constRem = p.rem
p.block = p.N / p.numCPU
p.start = 0
p.done = make(chan bool, 1) // synchronization channel for run()/waitAll()
return p
}