-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocca_memory.go
More file actions
279 lines (240 loc) · 7.34 KB
/
occa_memory.go
File metadata and controls
279 lines (240 loc) · 7.34 KB
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
package gocca
/*
#cgo CFLAGS: -I/usr/local/include
#cgo LDFLAGS: -L/usr/local/lib -locca
#include <occa.h>
#include <stdlib.h>
void freeMemory(occaMemory m) {
occaFree(&m);
}
*/
import "C"
import (
"unsafe"
)
type OCCAMemory struct {
memory C.occaMemory
}
// IsInitialized checks if the memory is initialized
func (m *OCCAMemory) IsInitialized() bool {
return bool(C.occaMemoryIsInitialized(m.memory))
}
// Ptr returns the underlying memory pointer
func (m *OCCAMemory) Ptr() unsafe.Pointer {
return C.occaMemoryPtr(m.memory)
}
// GetDevice returns the device associated with the memory
func (m *OCCAMemory) GetDevice() *OCCADevice {
return &OCCADevice{device: C.occaMemoryGetDevice(m.memory)}
}
// GetProperties returns memory properties
func (m *OCCAMemory) GetProperties() *OCCAJson {
return &OCCAJson{json: C.occaMemoryGetProperties(m.memory)}
}
// Size returns the size of the memory in bytes
func (m *OCCAMemory) Size() uint64 {
return uint64(C.occaMemorySize(m.memory))
}
// Slice creates a slice of the memory
func (m *OCCAMemory) Slice(offset, bytes int64) *OCCAMemory {
sliceMem := C.occaMemorySlice(m.memory, C.occaDim_t(offset), C.occaDim_t(bytes))
return &OCCAMemory{memory: sliceMem}
}
// Clone creates a copy of the memory
func (m *OCCAMemory) Clone() *OCCAMemory {
cloneMem := C.occaMemoryClone(m.memory)
return &OCCAMemory{memory: cloneMem}
}
// Detach detaches the memory
func (m *OCCAMemory) Detach() {
C.occaMemoryDetach(m.memory)
}
// CopyDeviceToDevice performs an efficient device-to-device memory copy
func (dst *OCCAMemory) CopyDeviceToDevice(dstOffset int64, src *OCCAMemory, srcOffset int64, bytes int64) {
C.occaCopyMemToMem(
dst.memory,
src.memory,
C.occaUDim_t(bytes),
C.occaUDim_t(dstOffset),
C.occaUDim_t(srcOffset),
C.occaDefault,
)
}
// CopyDeviceToDeviceWithProps performs device-to-device copy with properties
func (dst *OCCAMemory) CopyDeviceToDeviceWithProps(dstOffset int64, src *OCCAMemory, srcOffset int64, bytes int64, props *OCCAJson) {
var propsArg C.occaJson
if props != nil {
propsArg = props.json
} else {
propsArg = C.occaDefault
}
C.occaCopyMemToMem(
dst.memory,
src.memory,
C.occaUDim_t(bytes),
C.occaUDim_t(dstOffset),
C.occaUDim_t(srcOffset),
propsArg,
)
}
// CopyTo copies data from device memory to host memory
func (m *OCCAMemory) CopyTo(dst unsafe.Pointer, bytes int64) {
C.occaCopyMemToPtr(dst, m.memory, C.occaUDim_t(bytes), C.occaUDim_t(0), C.occaDefault)
}
// CopyToWithOffset copies data from device memory to host memory with offset
func (m *OCCAMemory) CopyToWithOffset(dst unsafe.Pointer, bytes int64, offset int64) {
C.occaCopyMemToPtr(dst, m.memory, C.occaUDim_t(bytes), C.occaUDim_t(offset), C.occaDefault)
}
// CopyToWithProps copies data from device memory to host memory with properties
func (m *OCCAMemory) CopyToWithProps(dst unsafe.Pointer, bytes int64, offset int64, props *OCCAJson) {
var propsArg C.occaJson
if props != nil {
propsArg = props.json
} else {
propsArg = C.occaDefault
}
C.occaCopyMemToPtr(dst, m.memory, C.occaUDim_t(bytes), C.occaUDim_t(offset), propsArg)
}
// CopyFrom copies data from host memory to device memory
func (m *OCCAMemory) CopyFrom(src unsafe.Pointer, bytes int64) {
C.occaCopyPtrToMem(m.memory, src, C.occaUDim_t(bytes), C.occaUDim_t(0), C.occaDefault)
}
// CopyFromWithOffset copies data from host memory to device memory with offset
func (m *OCCAMemory) CopyFromWithOffset(src unsafe.Pointer, bytes int64, offset int64) {
C.occaCopyPtrToMem(m.memory, src, C.occaUDim_t(bytes), C.occaUDim_t(offset), C.occaDefault)
}
// CopyFromWithProps copies data from host memory to device memory with properties
func (m *OCCAMemory) CopyFromWithProps(src unsafe.Pointer, bytes int64, offset int64, props *OCCAJson) {
var propsArg C.occaJson
if props != nil {
propsArg = props.json
} else {
propsArg = C.occaDefault
}
C.occaCopyPtrToMem(m.memory, src, C.occaUDim_t(bytes), C.occaUDim_t(offset), propsArg)
}
// Helper methods for Go slices
// CopyToFloat32 copies memory to float32 slice
func (m *OCCAMemory) CopyToFloat32(data []float32) {
if len(data) == 0 {
return
}
bytes := int64(len(data) * 4)
m.CopyTo(unsafe.Pointer(&data[0]), bytes)
}
// CopyToFloat64 copies memory to float64 slice
func (m *OCCAMemory) CopyToFloat64(data []float64) {
if len(data) == 0 {
return
}
bytes := int64(len(data) * 8)
m.CopyTo(unsafe.Pointer(&data[0]), bytes)
}
// CopyToInt32 copies memory to int32 slice
func (m *OCCAMemory) CopyToInt32(data []int32) {
if len(data) == 0 {
return
}
bytes := int64(len(data) * 4)
m.CopyTo(unsafe.Pointer(&data[0]), bytes)
}
// CopyToInt64 copies memory to int64 slice
func (m *OCCAMemory) CopyToInt64(data []int64) {
if len(data) == 0 {
return
}
bytes := int64(len(data) * 8)
m.CopyTo(unsafe.Pointer(&data[0]), bytes)
}
// Memory copy functions
// CopyMemToMem copies memory from one device memory to another
func CopyMemToMem(dest, src *OCCAMemory, bytes int64, destOffset, srcOffset int64, props *OCCAJson) {
var propsArg C.occaJson
if props != nil {
propsArg = props.json
} else {
propsArg = C.occaDefault
}
C.occaCopyMemToMem(dest.memory, src.memory,
C.occaUDim_t(bytes),
C.occaUDim_t(destOffset),
C.occaUDim_t(srcOffset),
propsArg)
}
// CopyPtrToMem copies from host pointer to device memory
func CopyPtrToMem(dest *OCCAMemory, src unsafe.Pointer, bytes int64, offset int64, props *OCCAJson) {
var propsArg C.occaJson
if props != nil {
propsArg = props.json
} else {
propsArg = C.occaDefault
}
C.occaCopyPtrToMem(dest.memory, src,
C.occaUDim_t(bytes),
C.occaUDim_t(offset),
propsArg)
}
// CopyMemToPtr copies from device memory to host pointer
func CopyMemToPtr(dest unsafe.Pointer, src *OCCAMemory, bytes int64, offset int64, props *OCCAJson) {
var propsArg C.occaJson
if props != nil {
propsArg = props.json
} else {
propsArg = C.occaDefault
}
C.occaCopyMemToPtr(dest, src.memory,
C.occaUDim_t(bytes),
C.occaUDim_t(offset),
propsArg)
}
// CopyFromFloat32 copies float32 slice to device memory
func (m *OCCAMemory) CopyFromFloat32(data []float32) {
if len(data) == 0 {
return
}
bytes := int64(len(data) * 4)
m.CopyFrom(unsafe.Pointer(&data[0]), bytes)
}
// CopyFromFloat64 copies float64 slice to device memory
func (m *OCCAMemory) CopyFromFloat64(data []float64) {
if len(data) == 0 {
return
}
bytes := int64(len(data) * 8)
m.CopyFrom(unsafe.Pointer(&data[0]), bytes)
}
// CopyFromInt32 copies int32 slice to device memory
func (m *OCCAMemory) CopyFromInt32(data []int32) {
if len(data) == 0 {
return
}
bytes := int64(len(data) * 4)
m.CopyFrom(unsafe.Pointer(&data[0]), bytes)
}
// CopyFromInt64 copies int64 slice to device memory
func (m *OCCAMemory) CopyFromInt64(data []int64) {
if len(data) == 0 {
return
}
bytes := int64(len(data) * 8)
m.CopyFrom(unsafe.Pointer(&data[0]), bytes)
}
// Free frees the device memory
func (m *OCCAMemory) Free() {
C.freeMemory(m.memory)
}
// WrapCpuMemory wraps CPU memory for a specific device
// Note: This function might not be available in all OCCA versions
// If you get a linker error, comment out this function
/*
func WrapCpuMemory(device *OCCADevice, ptr unsafe.Pointer, bytes int64, props *OCCAJson) *OCCAMemory {
var propsArg C.occaJson
if props != nil {
propsArg = props.json
} else {
propsArg = C.occaDefault
}
memory := C.occaWrapCpuMemory(device.device, ptr, C.occaUDim_t(bytes), propsArg)
return &OCCAMemory{memory: memory}
}
*/