-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
327 lines (290 loc) · 8.05 KB
/
slice.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package data
// Slice stores N-component GPU or host data.
import (
"bytes"
"fmt"
"github.com/mumax/3/util"
"log"
"reflect"
"unsafe"
)
// Slice is like a [][]float32, but may be stored in GPU or host memory.
type Slice struct {
ptr_ [MAX_COMP]unsafe.Pointer // keeps data local // TODO: rm (premature optimization)
ptrs []unsafe.Pointer // points into ptr_, limited to NComp()
size [3]int
memType int8
}
// this package must not depend on CUDA. If CUDA is
// loaded, these functions are set to cu.MemFree, ...
// NOTE: cpyDtoH and cpuHtoD are only needed to support 32-bit builds,
// otherwise, it could be removed in favor of memCpy only.
var (
memFree, memFreeHost func(unsafe.Pointer)
memCpy, memCpyDtoH, memCpyHtoD func(dst, src unsafe.Pointer, bytes int64)
)
// Internal: enables slices on GPU. Called upon cuda init.
func EnableGPU(free, freeHost func(unsafe.Pointer),
cpy, cpyDtoH, cpyHtoD func(dst, src unsafe.Pointer, bytes int64)) {
memFree = free
memFreeHost = freeHost
memCpy = cpy
memCpyDtoH = cpyDtoH
memCpyHtoD = cpyHtoD
}
// Make a CPU Slice with nComp components of size length.
func NewSlice(nComp int, size [3]int) *Slice {
length := prod(size)
ptrs := make([]unsafe.Pointer, nComp)
for i := range ptrs {
ptrs[i] = unsafe.Pointer(&(make([]float32, length)[0]))
}
return SliceFromPtrs(size, CPUMemory, ptrs)
}
func SliceFromArray(data [][]float32, size [3]int) *Slice {
nComp := len(data)
length := prod(size)
ptrs := make([]unsafe.Pointer, nComp)
for i := range ptrs {
if len(data[i]) != length {
panic("size mismatch")
}
ptrs[i] = unsafe.Pointer(&data[i][0])
}
return SliceFromPtrs(size, CPUMemory, ptrs)
}
// Return a slice without underlying storage. Used to represent a mask containing all 1's.
func NilSlice(nComp int, size [3]int) *Slice {
return SliceFromPtrs(size, GPUMemory, make([]unsafe.Pointer, nComp))
}
// Internal: construct a Slice using bare memory pointers.
func SliceFromPtrs(size [3]int, memType int8, ptrs []unsafe.Pointer) *Slice {
length := prod(size)
nComp := len(ptrs)
util.Argument(nComp > 0 && length > 0 && nComp <= MAX_COMP)
s := new(Slice)
s.ptrs = s.ptr_[:nComp]
s.size = size
for c := range ptrs {
s.ptrs[c] = ptrs[c]
}
s.memType = memType
return s
}
const MAX_COMP = 3 // Maximum supported number of Slice components
// Frees the underlying storage and zeros the Slice header to avoid accidental use.
// Slices sharing storage will be invalid after Free. Double free is OK.
func (s *Slice) Free() {
if s == nil {
return
}
// free storage
switch s.memType {
case 0:
return // already freed
case GPUMemory:
for _, ptr := range s.ptrs {
memFree(ptr)
}
//case UnifiedMemory:
// for _, ptr := range s.ptrs {
// memFreeHost(ptr)
// }
case CPUMemory:
// nothing to do
default:
panic("invalid memory type")
}
s.Disable()
}
// INTERNAL. Overwrite struct fields with zeros to avoid
// accidental use after Free.
func (s *Slice) Disable() {
s.ptr_ = [MAX_COMP]unsafe.Pointer{}
s.ptrs = s.ptrs[:0]
s.size = [3]int{0, 0, 0}
s.memType = 0
}
// value for Slice.memType
const (
CPUMemory = 1 << 0
GPUMemory = 1 << 1
//UnifiedMemory = CPUMemory | GPUMemory
)
// MemType returns the memory type of the underlying storage:
// CPUMemory, GPUMemory or UnifiedMemory
func (s *Slice) MemType() int {
return int(s.memType)
}
// GPUAccess returns whether the Slice is accessible by the GPU.
// true means it is either stored on GPU or in unified host memory.
func (s *Slice) GPUAccess() bool {
return s.memType&GPUMemory != 0
}
// CPUAccess returns whether the Slice is accessible by the CPU.
// true means it is stored in host memory.
func (s *Slice) CPUAccess() bool {
return s.memType&CPUMemory != 0
}
// NComp returns the number of components.
func (s *Slice) NComp() int {
return len(s.ptrs)
}
// Len returns the number of elements per component.
func (s *Slice) Len() int {
return prod(s.size)
}
func (s *Slice) Size() [3]int {
if s == nil {
return [3]int{0, 0, 0}
}
return s.size
}
// Comp returns a single component of the Slice.
func (s *Slice) Comp(i int) *Slice {
sl := new(Slice)
sl.ptr_[0] = s.ptrs[i]
sl.ptrs = sl.ptr_[:1]
sl.size = s.size
sl.memType = s.memType
return sl
}
// DevPtr returns a CUDA device pointer to a component.
// Slice must have GPUAccess.
// It is safe to call on a nil slice, returns NULL.
func (s *Slice) DevPtr(component int) unsafe.Pointer {
if s == nil {
return nil
}
if !s.GPUAccess() {
panic("slice not accessible by GPU")
}
return s.ptrs[component]
}
const SIZEOF_FLOAT32 = 4
// Host returns the Slice as a [][]float32 indexed by component, cell number.
// It should have CPUAccess() == true.
func (s *Slice) Host() [][]float32 {
if !s.CPUAccess() {
log.Panic("slice not accessible by CPU")
}
list := make([][]float32, s.NComp())
for c := range list {
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&list[c]))
hdr.Data = uintptr(s.ptrs[c])
hdr.Len = s.Len()
hdr.Cap = hdr.Len
}
return list
}
// Returns a copy of the Slice, allocated on CPU.
func (s *Slice) HostCopy() *Slice {
cpy := NewSlice(s.NComp(), s.Size())
Copy(cpy, s)
return cpy
}
func Copy(dst, src *Slice) {
if dst.NComp() != src.NComp() || dst.Len() != src.Len() {
panic(fmt.Sprintf("slice copy: illegal sizes: dst: %vx%v, src: %vx%v", dst.NComp(), dst.Len(), src.NComp(), src.Len()))
}
d, s := dst.GPUAccess(), src.GPUAccess()
bytes := SIZEOF_FLOAT32 * int64(dst.Len())
switch {
default:
panic("bug")
case d && s:
for c := 0; c < dst.NComp(); c++ {
memCpy(dst.DevPtr(c), src.DevPtr(c), bytes)
}
case s && !d:
for c := 0; c < dst.NComp(); c++ {
memCpyDtoH(dst.ptr_[c], src.DevPtr(c), bytes)
}
case !s && d:
for c := 0; c < dst.NComp(); c++ {
memCpyHtoD(dst.DevPtr(c), src.ptr_[c], bytes)
}
case !d && !s:
dst, src := dst.Host(), src.Host()
for c := range dst {
copy(dst[c], src[c])
}
}
}
// Floats returns the data as 3D array,
// indexed by cell position. Data should be
// scalar (1 component) and have CPUAccess() == true.
func (f *Slice) Scalars() [][][]float32 {
x := f.Tensors()
if len(x) != 1 {
panic(fmt.Sprintf("expecting 1 component, got %v", f.NComp()))
}
return x[0]
}
// Vectors returns the data as 4D array,
// indexed by component, cell position. Data should have
// 3 components and have CPUAccess() == true.
func (f *Slice) Vectors() [3][][][]float32 {
x := f.Tensors()
if len(x) != 3 {
panic(fmt.Sprintf("expecting 3 components, got %v", f.NComp()))
}
return [3][][][]float32{x[0], x[1], x[2]}
}
// Tensors returns the data as 4D array,
// indexed by component, cell position.
// Requires CPUAccess() == true.
func (f *Slice) Tensors() [][][][]float32 {
tensors := make([][][][]float32, f.NComp())
host := f.Host()
for i := range tensors {
tensors[i] = reshape(host[i], f.Size())
}
return tensors
}
// IsNil returns true if either s is nil or s.pointer[0] == nil
func (s *Slice) IsNil() bool {
if s == nil {
return true
}
return s.ptr_[0] == nil
}
func (s *Slice) String() string {
if s == nil {
return "nil"
}
var buf bytes.Buffer
util.Fprint(&buf, s.Tensors())
return buf.String()
}
func (s *Slice) Set(comp, ix, iy, iz int, value float64) {
s.checkComp(comp)
s.Host()[comp][s.Index(ix, iy, iz)] = float32(value)
}
func (s *Slice) SetVector(ix, iy, iz int, v Vector) {
i := s.Index(ix, iy, iz)
for c := range v {
s.Host()[c][i] = float32(v[c])
}
}
func (s *Slice) SetScalar(ix, iy, iz int, v float64) {
s.Host()[0][s.Index(ix, iy, iz)] = float32(v)
}
func (s *Slice) Get(comp, ix, iy, iz int) float64 {
s.checkComp(comp)
return float64(s.Host()[comp][s.Index(ix, iy, iz)])
}
func (s *Slice) checkComp(comp int) {
if comp < 0 || comp >= s.NComp() {
panic(fmt.Sprintf("slice: invalid component index: %v (number of components=%v)\n", comp, s.NComp()))
}
}
func (s *Slice) Index(ix, iy, iz int) int {
return Index(s.Size(), ix, iy, iz)
}
func Index(size [3]int, ix, iy, iz int) int {
if ix < 0 || ix >= size[X] || iy < 0 || iy >= size[Y] || iz < 0 || iz >= size[Z] {
panic(fmt.Sprintf("Slice index out of bounds: %v,%v,%v (bounds=%v)\n", ix, iy, iz, size))
}
return (iz*size[Y]+iy)*size[X] + ix
}