-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecution.go
46 lines (38 loc) · 1.32 KB
/
execution.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
package cu
// This file implements execution of CUDA kernels
//#include <cuda.h>
import "C"
import (
"unsafe"
)
const pointerSize = 8 // sorry, 64 bits only.
func LaunchKernel(f Function, gridDimX, gridDimY, gridDimZ int, blockDimX, blockDimY, blockDimZ int, sharedMemBytes int, stream Stream, kernelParams []unsafe.Pointer) {
// Since Go 1.6, a cgo argument cannot have a Go pointer to Go pointer,
// so we copy the argument values go C memory first.
argv := C.malloc(C.size_t(len(kernelParams) * pointerSize))
argp := C.malloc(C.size_t(len(kernelParams) * pointerSize))
defer C.free(argv)
defer C.free(argp)
for i := range kernelParams {
*((*unsafe.Pointer)(offset(argp, i))) = offset(argv, i) // argp[i] = &argv[i]
*((*uint64)(offset(argv, i))) = *((*uint64)(kernelParams[i])) // argv[i] = *kernelParams[i]
}
err := Result(C.cuLaunchKernel(
C.CUfunction(unsafe.Pointer(uintptr(f))),
C.uint(gridDimX),
C.uint(gridDimY),
C.uint(gridDimZ),
C.uint(blockDimX),
C.uint(blockDimY),
C.uint(blockDimZ),
C.uint(sharedMemBytes),
C.CUstream(unsafe.Pointer(uintptr(stream))),
(*unsafe.Pointer)(argp),
(*unsafe.Pointer)(unsafe.Pointer(uintptr(0)))))
if err != SUCCESS {
panic(err)
}
}
func offset(ptr unsafe.Pointer, i int) unsafe.Pointer {
return unsafe.Pointer(uintptr(ptr) + pointerSize*uintptr(i))
}