forked from dereklstinson/gocudnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoHostThread.go
55 lines (46 loc) · 1.04 KB
/
goHostThread.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
package gocudnn
import (
"runtime"
)
//Device is a cuda device that can be set on the host thread
type Device interface {
Set() error
}
//Worker works on functions on a device
type Worker struct {
w chan (func() error)
errChan chan error
}
//NewWorker creates a worker that works on a single host thread
func NewWorker(d Device) (w *Worker) {
w = new(Worker)
w.errChan = make(chan error, 1)
w.w = make(chan (func() error), 1)
go w.start(d)
return w
}
func (w *Worker) start(d Device) {
runtime.LockOSThread()
if d != nil {
d.Set()
}
for x := range w.w {
w.errChan <- x()
}
runtime.UnlockOSThread()
return
}
//Work takes a call back function, sends it
//through a channel to a locked thread hosting a gpu.
//
//This function will block until work is done. You don't have to wait though.
//
// If not wanting to wait. I would recomend wrapping this around a go func(){}()
func (w *Worker) Work(fn func() error) error {
w.w <- fn
return <-w.errChan
}
//Close closes the worker channel
func (w *Worker) Close() {
close(w.w)
}