-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathslice.go
275 lines (227 loc) · 6.73 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
package core
import (
"unsafe"
cr "github.com/ingonyama-zk/icicle/v2/wrappers/golang/cuda_runtime"
)
type HostOrDeviceSlice interface {
Len() int
Cap() int
IsEmpty() bool
IsOnDevice() bool
AsUnsafePointer() unsafe.Pointer
}
type DevicePointer = unsafe.Pointer
type DeviceSlice struct {
inner unsafe.Pointer
// capacity is the number of bytes that have been allocated
capacity int
// length is the number of elements that have been written
length int
}
func (d DeviceSlice) Len() int {
return d.length
}
func (d DeviceSlice) Cap() int {
return d.capacity
}
func (d DeviceSlice) IsEmpty() bool {
return d.length == 0
}
func (d DeviceSlice) AsUnsafePointer() unsafe.Pointer {
return d.inner
}
func (d DeviceSlice) IsOnDevice() bool {
return true
}
func (d DeviceSlice) GetDeviceId() int {
return cr.GetDeviceFromPointer(d.inner)
}
// CheckDevice is used to ensure that the DeviceSlice about to be used resides on the currently set device
func (d DeviceSlice) CheckDevice() {
if currentDeviceId, err := cr.GetDevice(); err != cr.CudaSuccess || d.GetDeviceId() != currentDeviceId {
panic("Attempt to use DeviceSlice on a different device")
}
}
func (d *DeviceSlice) Range(start, end int, endInclusive bool) DeviceSlice {
if end <= start {
panic("Cannot have negative or zero size slices")
}
if end >= d.length {
panic("Cannot increase slice size from Range")
}
var newSlice DeviceSlice
switch {
case start < 0:
panic("Negative value for start is not supported")
case start == 0:
newSlice = d.RangeTo(end, endInclusive)
case start > 0:
tempSlice := d.RangeFrom(start)
newSlice = tempSlice.RangeTo(end-start, endInclusive)
}
return newSlice
}
func (d *DeviceSlice) RangeTo(end int, inclusive bool) DeviceSlice {
if end <= 0 {
panic("Cannot have negative or zero size slices")
}
if end >= d.length {
panic("Cannot increase slice size from Range")
}
var newSlice DeviceSlice
sizeOfElement := d.capacity / d.length
newSlice.length = end
if inclusive {
newSlice.length += 1
}
newSlice.capacity = newSlice.length * sizeOfElement
newSlice.inner = d.inner
return newSlice
}
func (d *DeviceSlice) RangeFrom(start int) DeviceSlice {
if start >= d.length {
panic("Cannot have negative or zero size slices")
}
if start < 0 {
panic("Negative value for start is not supported")
}
var newSlice DeviceSlice
sizeOfElement := d.capacity / d.length
newSlice.inner = unsafe.Pointer(uintptr(d.inner) + uintptr(start)*uintptr(sizeOfElement))
newSlice.length = d.length - start
newSlice.capacity = d.capacity - start*sizeOfElement
return newSlice
}
// TODO: change signature to be Malloc(element, numElements)
// calc size internally
func (d *DeviceSlice) Malloc(size, sizeOfElement int) (DeviceSlice, cr.CudaError) {
dp, err := cr.Malloc(uint(size))
d.inner = dp
d.capacity = size
d.length = size / sizeOfElement
return *d, err
}
func (d *DeviceSlice) MallocAsync(size, sizeOfElement int, stream cr.CudaStream) (DeviceSlice, cr.CudaError) {
dp, err := cr.MallocAsync(uint(size), stream)
d.inner = dp
d.capacity = size
d.length = size / sizeOfElement
return *d, err
}
func (d *DeviceSlice) Free() cr.CudaError {
d.CheckDevice()
err := cr.Free(d.inner)
if err == cr.CudaSuccess {
d.length, d.capacity = 0, 0
d.inner = nil
}
return err
}
func (d *DeviceSlice) FreeAsync(stream cr.Stream) cr.CudaError {
d.CheckDevice()
err := cr.FreeAsync(d.inner, stream)
if err == cr.CudaSuccess {
d.length, d.capacity = 0, 0
d.inner = nil
}
return err
}
type HostSlice[T any] []T
func HostSliceFromElements[T any](elements []T) HostSlice[T] {
return elements
}
func HostSliceWithValue[T any](underlyingValue T, size int) HostSlice[T] {
slice := make(HostSlice[T], size)
for i := range slice {
slice[i] = underlyingValue
}
return slice
}
func (h HostSlice[T]) Len() int {
return len(h)
}
func (h HostSlice[T]) Cap() int {
return cap(h)
}
func (h HostSlice[T]) IsEmpty() bool {
return len(h) == 0
}
func (h HostSlice[T]) IsOnDevice() bool {
return false
}
func (h HostSlice[T]) SizeOfElement() int {
return int(unsafe.Sizeof(h[0]))
}
func (h HostSlice[T]) AsPointer() *T {
return &h[0]
}
func (h HostSlice[T]) AsUnsafePointer() unsafe.Pointer {
return unsafe.Pointer(&h[0])
}
// Registers host memory as pinned, allowing the GPU to read data from the host quicker and save GPU memory space.
// Memory pinned using this function should be unpinned using [Unpin]
func (h HostSlice[T]) Pin(flags cr.RegisterPinnedFlags) cr.CudaError {
_, err := cr.RegisterPinned(h.AsUnsafePointer(), h.SizeOfElement()*h.Len(), flags)
return err
}
// Unregisters host memory as pinned
func (h HostSlice[T]) Unpin() cr.CudaError {
return cr.FreeRegisteredPinned(h.AsUnsafePointer())
}
// Allocates new host memory as pinned and copies the HostSlice data to the newly allocated area
// Memory pinned using this function should be unpinned using [FreePinned]
func (h HostSlice[T]) AllocPinned(flags cr.AllocPinnedFlags) (HostSlice[T], cr.CudaError) {
pinnedMemPointer, err := cr.AllocPinned(h.SizeOfElement()*h.Len(), flags)
if err != cr.CudaSuccess {
return nil, err
}
pinnedMem := unsafe.Slice((*T)(pinnedMemPointer), h.Len())
copy(pinnedMem, h)
return pinnedMem, cr.CudaSuccess
}
// Unpins host memory that was pinned using [AllocPinned]
func (h HostSlice[T]) FreePinned() cr.CudaError {
return cr.FreeAllocPinned(h.AsUnsafePointer())
}
func (h HostSlice[T]) CopyToDevice(dst *DeviceSlice, shouldAllocate bool) *DeviceSlice {
size := h.Len() * h.SizeOfElement()
if shouldAllocate {
dst.Malloc(size, h.SizeOfElement())
}
dst.CheckDevice()
if size > dst.Cap() {
panic("Number of bytes to copy is too large for destination")
}
cr.CopyToDevice(dst.inner, h.AsUnsafePointer(), uint(size))
dst.length = h.Len()
return dst
}
func (h HostSlice[T]) CopyToDeviceAsync(dst *DeviceSlice, stream cr.CudaStream, shouldAllocate bool) *DeviceSlice {
size := h.Len() * h.SizeOfElement()
if shouldAllocate {
dst.MallocAsync(size, h.SizeOfElement(), stream)
}
dst.CheckDevice()
if size > dst.Cap() {
panic("Number of bytes to copy is too large for destination")
}
cr.CopyToDeviceAsync(dst.inner, h.AsUnsafePointer(), uint(size), stream)
dst.length = h.Len()
return dst
}
func (h HostSlice[T]) CopyFromDevice(src *DeviceSlice) {
src.CheckDevice()
if h.Len() != src.Len() {
panic("destination and source slices have different lengths")
}
bytesSize := src.Len() * h.SizeOfElement()
cr.CopyFromDevice(h.AsUnsafePointer(), src.inner, uint(bytesSize))
}
func (h HostSlice[T]) CopyFromDeviceAsync(src *DeviceSlice, stream cr.Stream) {
src.CheckDevice()
if h.Len() != src.Len() {
panic("destination and source slices have different lengths")
}
bytesSize := src.Len() * h.SizeOfElement()
cr.CopyFromDeviceAsync(h.AsUnsafePointer(), src.inner, uint(bytesSize), stream)
}