-
Notifications
You must be signed in to change notification settings - Fork 1
/
bytes.go
73 lines (62 loc) · 1.78 KB
/
bytes.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
package cuda
// This file provides GPU byte slices, used to store regions.
import (
"log"
"unsafe"
"github.com/kuchkin/mumax3-gneb/cuda/cu"
"github.com/kuchkin/mumax3-gneb/util"
)
// 3D byte slice, used for region lookup.
type Bytes struct {
Ptr unsafe.Pointer
Len int
}
// Construct new byte slice with given length,
// initialised to zeros.
func NewBytes(Len int) *Bytes {
ptr := cu.MemAlloc(int64(Len))
cu.MemsetD8(cu.DevicePtr(ptr), 0, int64(Len))
return &Bytes{unsafe.Pointer(uintptr(ptr)), Len}
}
// Upload src (host) to dst (gpu).
func (dst *Bytes) Upload(src []byte) {
util.Argument(dst.Len == len(src))
MemCpyHtoD(dst.Ptr, unsafe.Pointer(&src[0]), int64(dst.Len))
}
// Copy on device: dst = src.
func (dst *Bytes) Copy(src *Bytes) {
util.Argument(dst.Len == src.Len)
MemCpy(dst.Ptr, src.Ptr, int64(dst.Len))
}
// Copy to host: dst = src.
func (src *Bytes) Download(dst []byte) {
util.Argument(src.Len == len(dst))
MemCpyDtoH(unsafe.Pointer(&dst[0]), src.Ptr, int64(src.Len))
}
// Set one element to value.
// data.Index can be used to find the index for x,y,z.
func (dst *Bytes) Set(index int, value byte) {
if index < 0 || index >= dst.Len {
log.Panic("Bytes.Set: index out of range:", index)
}
src := value
MemCpyHtoD(unsafe.Pointer(uintptr(dst.Ptr)+uintptr(index)), unsafe.Pointer(&src), 1)
}
// Get one element.
// data.Index can be used to find the index for x,y,z.
func (src *Bytes) Get(index int) byte {
if index < 0 || index >= src.Len {
log.Panic("Bytes.Set: index out of range:", index)
}
var dst byte
MemCpyDtoH(unsafe.Pointer(&dst), unsafe.Pointer(uintptr(src.Ptr)+uintptr(index)), 1)
return dst
}
// Frees the GPU memory and disables the slice.
func (b *Bytes) Free() {
if b.Ptr != nil {
cu.MemFree(cu.DevicePtr(uintptr(b.Ptr)))
}
b.Ptr = nil
b.Len = 0
}