CPU数によって並列度を変える #17

Merged
merged 3 commits into from May 1, 2017
Jump to file or symbol
Failed to load files and symbols.
+7 −0
Diff settings

Always

Just for now

View
@@ -2,6 +2,7 @@ package main
import (
"errors"
+ "runtime"
"sync"
)
@@ -36,11 +37,15 @@ func DependencyAccuracy(model *Model, sents []*Sentence) float64 {
predHeads := make([][]int, 0)
w := model.AveragedWeight()
+ cpus := runtime.NumCPU()
+ semaphore := make(chan int, cpus)
for _, sent := range sents {
wg.Add(1)
go func(sent *Sentence) {
defer wg.Done()
+ semaphore <- 1
Decode(&w, sent)
+ <-semaphore
}(sent)
}
wg.Wait()
View
@@ -5,6 +5,7 @@ import (
"io/ioutil"
"math/rand"
"os"
+ "runtime"
)
func shuffle(data []*Sentence) {
@@ -16,6 +17,7 @@ func shuffle(data []*Sentence) {
}
func main() {
+ runtime.GOMAXPROCS(runtime.NumCPU())
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)