-
Notifications
You must be signed in to change notification settings - Fork 64
/
array.go
284 lines (263 loc) · 9.64 KB
/
array.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
package cu
// #include <cuda.h>
import "C"
import "unsafe"
// Array is the pointer to a CUDA array. The name is a bit of a misnomer,
// as it would lead one to imply that it's rangeable. It's not.
type Array struct {
arr *C.CUarray
}
func goArray(arr *C.CUarray) Array {
return Array{arr}
}
func (arr Array) c() C.CUarray {
return *arr.arr
}
// Array3Desc is the descriptor for CUDA 3D arrays, which is used to determine what to allocate.
//
// From the docs:
// Width, Height, and Depth are the width, height, and depth of the CUDA array (in elements); the following types of CUDA arrays can be allocated:
// - A 1D array is allocated if Height and Depth extents are both zero.
// - A 2D array is allocated if only Depth extent is zero.
// - A 3D array is allocated if all three extents are non-zero.
// - A 1D layered CUDA array is allocated if only Height is zero and the CUDA_ARRAY3D_LAYERED flag is set. Each layer is a 1D array. The number of layers is determined by the depth extent.
// - A 2D layered CUDA array is allocated if all three extents are non-zero and the CUDA_ARRAY3D_LAYERED flag is set. Each layer is a 2D array. The number of layers is determined by the depth extent.
// - A cubemap CUDA array is allocated if all three extents are non-zero and the CUDA_ARRAY3D_CUBEMAP flag is set. Width must be equal to Height, and Depth must be six. A cubemap is a special type of 2D layered CUDA array, where the six layers represent the six faces of a cube. The order of the six layers in memory is the same as that listed in CUarray_cubemap_face.
// - A cubemap layered CUDA array is allocated if all three extents are non-zero, and both, CUDA_ARRAY3D_CUBEMAP and CUDA_ARRAY3D_LAYERED flags are set. Width must be equal to Height, and Depth must be a multiple of six. A cubemap layered CUDA array is a special type of 2D layered CUDA array that consists of a collection of cubemaps. The first six layers represent the first cubemap, the next six layers form the second cubemap, and so on.
type Array3Desc struct {
Width, Height, Depth uint
Format Format
NumChannels uint
Flags uint
}
func (desc Array3Desc) c() *C.CUDA_ARRAY3D_DESCRIPTOR {
return &C.CUDA_ARRAY3D_DESCRIPTOR{
Width: C.size_t(desc.Width),
Height: C.size_t(desc.Height),
Depth: C.size_t(desc.Depth),
Format: C.CUarray_format(desc.Format),
NumChannels: C.uint(desc.NumChannels),
Flags: C.uint(desc.Flags),
}
}
func goArray3Desc(desc *C.CUDA_ARRAY3D_DESCRIPTOR) Array3Desc {
return Array3Desc{
Width: uint(desc.Width),
Height: uint(desc.Height),
Depth: uint(desc.Depth),
Format: Format(desc.Format),
NumChannels: uint(desc.NumChannels),
Flags: uint(desc.Flags),
}
}
// ArrayDesc is the descriptor for CUDA arrays, which is used to determine what to allocate.
//
// From the docs:
// Width, and Height are the width, and height of the CUDA array (in elements); the CUDA array is one-dimensional if height is 0, two-dimensional otherwise;
type ArrayDesc struct {
Width, Height uint
Format Format
NumChannels uint
}
func (desc ArrayDesc) c() *C.CUDA_ARRAY_DESCRIPTOR {
return &C.CUDA_ARRAY_DESCRIPTOR{
Width: C.size_t(desc.Width),
Height: C.size_t(desc.Height),
Format: C.CUarray_format(desc.Format),
NumChannels: C.uint(desc.NumChannels),
}
}
func goArrayDesc(desc *C.CUDA_ARRAY_DESCRIPTOR) ArrayDesc {
return ArrayDesc{
Width: uint(desc.Width),
Height: uint(desc.Height),
Format: Format(desc.Format),
NumChannels: uint(desc.NumChannels),
}
}
// // Descriptor3 get a 3D CUDA array descriptor
// func (arr Array) Descriptor3() (Array3Desc, error) {
// hArray := arr.c()
// var desc C.CUDA_ARRAY3D_DESCRIPTOR
// if err := result(C.cuArray3DGetDescriptor(&desc, hArray)); err != nil {
// return Array3Desc{}, errors.Wrapf(err, "Array3DGetDescriptor")
// }
// return goArray3Desc(&desc), nil
// }
// // Descriptor gets a 1D or 2D CUDA array descriptor
// func (arr Array) Descriptor() (ArrayDesc, error) {
// hArray := arr.c()
// var desc C.CUDA_ARRAY_DESCRIPTOR
// if err := result(C.cuArrayGetDescriptor(&desc, hArray)); err != nil {
// return ArrayDesc{}, errors.Wrapf(err, "ArrayGetDescriptor")
// }
// return goArrayDesc(&desc), nil
// }
// Memcpy2dParam is a struct representing the params of a 2D memory copy instruction.
// To aid usability, the fields are ordered as per the documentation (the actual struct is laid out differently).
type Memcpy2dParam struct {
Height int64
WidthInBytes int64
DstArray Array
DstDevice DevicePtr
DstHost unsafe.Pointer
DstMemoryType MemoryType
DstPitch int64
DstXInBytes int64
DstY int64
SrcArray Array
SrcDevice DevicePtr
SrcHost unsafe.Pointer
SrcMemoryType MemoryType
SrcPitch int64
SrcXInBytes int64
SrcY int64
}
func (cpy Memcpy2dParam) c() *C.CUDA_MEMCPY2D {
return &C.CUDA_MEMCPY2D{
srcXInBytes: C.size_t(cpy.SrcXInBytes),
srcY: C.size_t(cpy.SrcY),
srcMemoryType: C.CUmemorytype(cpy.SrcMemoryType),
srcHost: cpy.SrcHost,
srcDevice: C.CUdeviceptr(cpy.SrcDevice),
srcArray: cpy.SrcArray.c(),
srcPitch: C.size_t(cpy.SrcPitch),
dstXInBytes: C.size_t(cpy.DstXInBytes),
dstY: C.size_t(cpy.DstY),
dstMemoryType: C.CUmemorytype(cpy.DstMemoryType),
dstHost: cpy.DstHost,
dstDevice: C.CUdeviceptr(cpy.DstDevice),
dstArray: cpy.DstArray.c(),
dstPitch: C.size_t(cpy.DstPitch),
WidthInBytes: C.size_t(cpy.WidthInBytes),
Height: C.size_t(cpy.Height),
}
}
// Memcpy3dParam is a struct representing the params of a 3D memory copy instruction.
// To aid usability, the fields are ordered as per the documentation (the actual struct is laid out differently).
type Memcpy3dParam struct {
Depth int64
Height int64
WidthInBytes int64
DstArray Array
DstDevice DevicePtr
DstHeight int64
DstHost unsafe.Pointer
DstLOD int64
DstMemoryType MemoryType
DstPitch int64
DstXInBytes int64
DstY int64
DstZ int64
SrcArray Array
SrcDevice DevicePtr
SrcHeight int64
SrcHost unsafe.Pointer
SrcLOD int64
SrcMemoryType MemoryType
SrcPitch int64
SrcXInBytes int64
SrcY int64
SrcZ int64
}
func (cpy Memcpy3dParam) c() *C.CUDA_MEMCPY3D {
return &C.CUDA_MEMCPY3D{
srcXInBytes: C.size_t(cpy.SrcXInBytes),
srcY: C.size_t(cpy.SrcY),
srcZ: C.size_t(cpy.SrcZ),
srcLOD: C.size_t(cpy.SrcLOD),
srcMemoryType: C.CUmemorytype(cpy.SrcMemoryType),
srcHost: cpy.SrcHost,
srcDevice: C.CUdeviceptr(cpy.SrcDevice),
srcArray: cpy.SrcArray.c(),
reserved0: nil,
srcPitch: C.size_t(cpy.SrcPitch),
srcHeight: C.size_t(cpy.SrcHeight),
dstXInBytes: C.size_t(cpy.DstXInBytes),
dstY: C.size_t(cpy.DstY),
dstZ: C.size_t(cpy.DstZ),
dstLOD: C.size_t(cpy.DstLOD),
dstMemoryType: C.CUmemorytype(cpy.DstMemoryType),
dstHost: cpy.DstHost,
dstDevice: C.CUdeviceptr(cpy.DstDevice),
dstArray: cpy.DstArray.c(),
reserved1: nil,
dstPitch: C.size_t(cpy.DstPitch),
dstHeight: C.size_t(cpy.DstHeight),
WidthInBytes: C.size_t(cpy.WidthInBytes),
Height: C.size_t(cpy.Height),
Depth: C.size_t(cpy.Depth),
}
}
// Memcpy3dParam is a struct representing the params of a 3D memory copy instruction across contexts.
// To aid usability, the fields are ordered as per the documentation (the actual struct is laid out differently).
type Memcpy3dPeerParam struct {
Depth int64
Height int64
WidthInBytes int64
DstArray Array
DstContext CUContext
DstDevice DevicePtr
DstHeight int64
DstHost unsafe.Pointer
DstLOD int64
DstMemoryType MemoryType
DstPitch int64
DstXInBytes int64
DstY int64
DstZ int64
SrcArray Array
SrcContext CUContext
SrcDevice DevicePtr
SrcHeight int64
SrcHost unsafe.Pointer
SrcLOD int64
SrcMemoryType MemoryType
SrcPitch int64
SrcXInBytes int64
SrcY int64
SrcZ int64
}
func (cpy *Memcpy3dPeerParam) c() *C.CUDA_MEMCPY3D_PEER {
return &C.CUDA_MEMCPY3D_PEER{
srcXInBytes: C.size_t(cpy.SrcXInBytes),
srcY: C.size_t(cpy.SrcY),
srcZ: C.size_t(cpy.SrcZ),
srcLOD: C.size_t(cpy.SrcLOD),
srcMemoryType: C.CUmemorytype(cpy.SrcMemoryType),
srcHost: cpy.SrcHost,
srcDevice: C.CUdeviceptr(cpy.SrcDevice),
srcArray: cpy.SrcArray.c(),
srcContext: cpy.SrcContext.c(),
srcPitch: C.size_t(cpy.SrcPitch),
srcHeight: C.size_t(cpy.SrcHeight),
dstXInBytes: C.size_t(cpy.DstXInBytes),
dstY: C.size_t(cpy.DstY),
dstZ: C.size_t(cpy.DstZ),
dstLOD: C.size_t(cpy.DstLOD),
dstMemoryType: C.CUmemorytype(cpy.DstMemoryType),
dstHost: cpy.DstHost,
dstDevice: C.CUdeviceptr(cpy.DstDevice),
dstArray: cpy.DstArray.c(),
dstContext: cpy.DstContext.c(),
dstPitch: C.size_t(cpy.DstPitch),
dstHeight: C.size_t(cpy.DstHeight),
WidthInBytes: C.size_t(cpy.WidthInBytes),
Height: C.size_t(cpy.Height),
Depth: C.size_t(cpy.Depth),
}
}
func MakeArray(pAllocateArray ArrayDesc) (pHandle Array, err error) {
var CpHandle C.CUarray
CpAllocateArray := pAllocateArray.c()
err = result(C.cuArrayCreate(&CpHandle, CpAllocateArray))
pHandle = Array{&CpHandle}
return
}
func Make3DArray(pAllocateArray Array3Desc) (pHandle Array, err error) {
var CpHandle C.CUarray
CpAllocateArray := pAllocateArray.c()
err = result(C.cuArray3DCreate(&CpHandle, CpAllocateArray))
pHandle = Array{&CpHandle}
return
}