forked from mumax/3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
107 lines (93 loc) · 3.02 KB
/
context.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
package cu
// This file implements CUDA driver context management
//#include <cuda.h>
import "C"
import "unsafe"
// CUDA context.
type Context uintptr
// Create a CUDA context.
func CtxCreate(flags uint, dev Device) Context {
var ctx C.CUcontext
err := Result(C.cuCtxCreate(&ctx, C.uint(flags), C.CUdevice(dev)))
if err != SUCCESS {
panic(err)
}
return Context(uintptr(unsafe.Pointer(ctx)))
}
//Destroys the CUDA context specified by ctx. If the context usage count is not equal to 1, or the context is current to any CPU thread other than the current one, this function fails. Floating contexts (detached from a CPU thread via cuCtxPopCurrent()) may be destroyed by this function.
func CtxDestroy(ctx *Context) {
err := Result(C.cuCtxDestroy(C.CUcontext(unsafe.Pointer(uintptr(*ctx)))))
*ctx = 0
if err != SUCCESS {
panic(err)
}
}
//Destroys the CUDA context.
func (ctx *Context) Destroy() {
CtxDestroy(ctx)
}
// Returns the API version to create the context.
func CtxGetApiVersion(ctx Context) (version int) {
var cversion C.uint
err := Result(C.cuCtxGetApiVersion(C.CUcontext(unsafe.Pointer(uintptr(ctx))), &cversion))
if err != SUCCESS {
panic(err)
}
version = int(cversion)
return
}
// Returns the API version to create the context.
func (ctx Context) ApiVersion() (version int) {
return CtxGetApiVersion(ctx)
}
// Gets the current active context.
func CtxGetCurrent() Context {
var ctx C.CUcontext
err := Result(C.cuCtxGetCurrent(&ctx))
if err != SUCCESS {
panic(err)
}
return Context(uintptr(unsafe.Pointer(ctx)))
}
// Returns the ordinal of the current context's device.
func CtxGetDevice() Device {
var dev C.CUdevice
err := Result(C.cuCtxGetDevice(&dev))
if err != SUCCESS {
panic(err)
}
return Device(dev)
}
// Sets the current active context.
func CtxSetCurrent(ctx Context) {
err := Result(C.cuCtxSetCurrent(C.CUcontext(unsafe.Pointer(uintptr(ctx)))))
if err != SUCCESS {
panic(err)
}
}
// Sets the current active context.
func (ctx Context) SetCurrent() {
CtxSetCurrent(ctx)
}
// Blocks until the device has completed all preceding requested tasks, if the context was created with the CU_CTX_SCHED_BLOCKING_SYNC flag.
func CtxSynchronize() {
err := Result(C.cuCtxSynchronize())
if err != SUCCESS {
panic(err)
}
}
// Flags for CtxCreate
const (
// If the number of contexts > number of CPUs, yield to other OS threads when waiting for the GPU, otherwise CUDA spin on the processor.
CTX_SCHED_AUTO = C.CU_CTX_SCHED_AUTO
// Spin when waiting for results from the GPU.
CTX_SCHED_SPIN = C.CU_CTX_SCHED_SPIN
// Yield its thread when waiting for results from the GPU.
CTX_SCHED_YIELD = C.CU_CTX_SCHED_YIELD
// Bock the CPU thread on a synchronization primitive when waiting for the GPU to finish work.
CTX_BLOCKING_SYNC
// Support mapped pinned allocations. This flag must be set in order to allocate pinned host memory that is accessible to the GPU.
CTX_MAP_HOST = C.CU_CTX_MAP_HOST
//Do not reduce local memory after resizing local memory for a kernel.
CTX_LMEM_RESIZE_TO_MAX = C.CU_CTX_LMEM_RESIZE_TO_MAX
)