-
Notifications
You must be signed in to change notification settings - Fork 869
/
cuda.go
271 lines (234 loc) · 8.41 KB
/
cuda.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
// Package cuda is the GoCV wrapper around OpenCV cuda.
//
// For further details, please see:
// https://github.com/opencv/opencv
//
// import "gocv.io/x/gocv/cuda"
package cuda
/*
#include <stdlib.h>
#include "cuda.h"
*/
import "C"
import "gocv.io/x/gocv"
type FeatureSet int
const (
FeatureSetCompute10 FeatureSet = 10
FeatureSetCompute11 FeatureSet = 11
FeatureSetCompute12 FeatureSet = 12
FeatureSetCompute13 FeatureSet = 13
FeatureSetCompute20 FeatureSet = 20
FeatureSetCompute21 FeatureSet = 21
FeatureSetCompute30 FeatureSet = 30
FeatureSetCompute32 FeatureSet = 32
FeatureSetCompute35 FeatureSet = 35
FeatureSetCompute50 FeatureSet = 50
FeatureSetGlobalAtomics = FeatureSetCompute11
FeatureSetSharedAtomics = FeatureSetCompute12
FeatureSetNativeDouble = FeatureSetCompute13
FeatureSetWarpShuffleFunctions = FeatureSetCompute30
FeatureSetDynamicParallelism = FeatureSetCompute35
)
// GpuMat is the GPU version of a Mat
//
// For further details, please see:
// https://docs.opencv.org/master/d0/d60/classcv_1_1cuda_1_1GpuMat.html
type GpuMat struct {
p C.GpuMat
}
// Upload performs data upload to GpuMat (Blocking call)
//
// For further details, please see:
// https://docs.opencv.org/master/d0/d60/classcv_1_1cuda_1_1GpuMat.html#a00ef5bfe18d14623dcf578a35e40a46b
func (g *GpuMat) Upload(data gocv.Mat) {
C.GpuMat_Upload(g.p, C.Mat(data.Ptr()), nil)
}
// UploadWithStream performs data upload to GpuMat (non-blocking call)
//
// For further details, please see:
// https://docs.opencv.org/master/d0/d60/classcv_1_1cuda_1_1GpuMat.html#a00ef5bfe18d14623dcf578a35e40a46b
func (g *GpuMat) UploadWithStream(data gocv.Mat, s Stream) {
C.GpuMat_Upload(g.p, C.Mat(data.Ptr()), s.p)
}
// Download performs data download from GpuMat (Blocking call)
//
// For further details, please see:
// https://docs.opencv.org/master/d0/d60/classcv_1_1cuda_1_1GpuMat.html#a027e74e4364ddfd9687b58aa5db8d4e8
func (g *GpuMat) Download(dst *gocv.Mat) {
C.GpuMat_Download(g.p, C.Mat(dst.Ptr()), nil)
}
// DownloadWithStream performs data download from GpuMat (non-blocking call)
//
// For further details, please see:
// https://docs.opencv.org/master/d0/d60/classcv_1_1cuda_1_1GpuMat.html#a027e74e4364ddfd9687b58aa5db8d4e8
func (g *GpuMat) DownloadWithStream(dst *gocv.Mat, s Stream) {
C.GpuMat_Download(g.p, C.Mat(dst.Ptr()), s.p)
}
// Empty returns true if GpuMat is empty
func (g *GpuMat) Empty() bool {
return C.GpuMat_Empty(g.p) != 0
}
// Close the GpuMat object
func (g *GpuMat) Close() error {
C.GpuMat_Close(g.p)
g.p = nil
return nil
}
// Closed determines if the GpuMat is closed or not.
func (g *GpuMat) Closed() bool {
return g.p == nil
}
// NewGpuMat returns a new empty GpuMat
func NewGpuMat() GpuMat {
return newGpuMat(C.GpuMat_New())
}
// NewGpuMatFromMat returns a new GpuMat based on a Mat
func NewGpuMatFromMat(mat gocv.Mat) GpuMat {
return newGpuMat(C.GpuMat_NewFromMat(C.Mat(mat.Ptr())))
}
// NewGpuMatWithSize returns a new GpuMat with a specific size and type.
func NewGpuMatWithSize(rows int, cols int, mt gocv.MatType) GpuMat {
return newGpuMat(C.GpuMat_NewWithSize(C.int(rows), C.int(cols), C.int(mt)))
}
func newGpuMat(p C.GpuMat) GpuMat {
return GpuMat{p: p}
}
// PrintCudaDeviceInfo prints extensive cuda device information
func PrintCudaDeviceInfo(device int) {
C.PrintCudaDeviceInfo(C.int(device))
}
// PrintShortCudaDeviceInfo prints a small amount of cuda device information
func PrintShortCudaDeviceInfo(device int) {
C.PrintShortCudaDeviceInfo(C.int(device))
}
// GetCudaEnabledDeviceCount returns the number of cuda enabled devices on the
// system
func GetCudaEnabledDeviceCount() int {
return int(C.GetCudaEnabledDeviceCount())
}
// GetDevice returns the current device index.
//
// For further details, please see:
// https://docs.opencv.org/master/d8/d40/group__cudacore__init.html#ga6ded4ed8e4fc483a9863d31f34ec9c0e
func GetDevice() int {
return int(C.GetCudaDevice())
}
// SetDevice sets a device and initializes it for the current thread.
//
// For further details, please see:
// https://docs.opencv.org/master/d8/d40/group__cudacore__init.html#gaefa34186b185de47851836dba537828b
func SetDevice(device int) {
C.SetCudaDevice(C.int(device))
}
// ResetDevice explicitly destroys and cleans up all resources associated
// with the current device in the current process.
//
// Any subsequent API call to this device will reinitialize the device.
//
// For further details, please see:
// https://docs.opencv.org/master/d8/d40/group__cudacore__init.html#ga6153b6f461101374e655a54fc77e725e
func ResetDevice() {
C.ResetCudaDevice()
}
// DeviceSupports checks whether current device supports the given feature.
//
// For further details, please see:
// https://docs.opencv.org/4.x/d8/d40/group__cudacore__init.html#ga170b10cc9af4aa8cce8c0afdb4b1d08c
func DeviceSupports(feature FeatureSet) bool {
return bool(C.CudaDeviceSupports(C.int(feature)))
}
// ConvertTo converts GpuMat into destination GpuMat.
//
// For further details, please see:
// https://docs.opencv.org/master/d0/d60/classcv_1_1cuda_1_1GpuMat.html#a3a1b076e54d8a8503014e27a5440d98a
func (m *GpuMat) ConvertTo(dst *GpuMat, mt gocv.MatType) {
C.GpuMat_ConvertTo(m.p, dst.p, C.int(mt), nil)
return
}
// ConvertToWithStream converts GpuMat into destination GpuMat.
//
// For further details, please see:
// https://docs.opencv.org/master/d0/d60/classcv_1_1cuda_1_1GpuMat.html#a3a1b076e54d8a8503014e27a5440d98a
func (m *GpuMat) ConvertToWithStream(dst *GpuMat, mt gocv.MatType, s Stream) {
C.GpuMat_ConvertTo(m.p, dst.p, C.int(mt), s.p)
return
}
// ConvertFp16 converts an array to half precision floating number.
//
// For further details, please see:
// https://docs.opencv.org/4.x/d8/d40/group__cudacore__init.html#gaa1c52258763197958eb9e6681917f723
func (m *GpuMat) ConvertFp16(dst *GpuMat) {
C.GpuMat_ConvertFp16(m.p, dst.p)
return
}
// CopyTo copies GpuMat into destination GpuMat.
//
// For further details, please see:
// https://docs.opencv.org/master/d0/d60/classcv_1_1cuda_1_1GpuMat.html#a948c562ee340c0678a44884bde1f5a3e
func (m *GpuMat) CopyTo(dst *GpuMat) {
C.GpuMat_CopyTo(m.p, dst.p, nil)
return
}
// CopyToWithStream copies GpuMat into destination GpuMat.
//
// For further details, please see:
// https://docs.opencv.org/master/d0/d60/classcv_1_1cuda_1_1GpuMat.html#a948c562ee340c0678a44884bde1f5a3e
func (m *GpuMat) CopyToWithStream(dst *GpuMat, s Stream) {
C.GpuMat_CopyTo(m.p, dst.p, s.p)
return
}
// Rows returns the number of rows for this GpuMat.
func (m *GpuMat) Rows() int {
return int(C.GpuMat_Rows(m.p))
}
// Cols returns the number of columns for this GpuMat.
func (m *GpuMat) Cols() int {
return int(C.GpuMat_Cols(m.p))
}
// Channels returns the number of channels for this GpuMat.
func (m *GpuMat) Channels() int {
return int(C.GpuMat_Channels(m.p))
}
// Type returns the type for this GpuMat.
func (m *GpuMat) Type() gocv.MatType {
return gocv.MatType(C.GpuMat_Type(m.p))
}
// Reshape creates a new GpuMat with the same data
// but with a different number of channels and/or different number of rows.
//
// For further details, please see:
// https://docs.opencv.org/master/d0/d60/classcv_1_1cuda_1_1GpuMat.html#a408e22ed824d1ddf59f58bda895017a8
func (m *GpuMat) Reshape(cn int, rows int) GpuMat {
return newGpuMat(C.GpuMat_Reshape(m.p, C.int(cn), C.int(rows)))
}
// Stream asynchronous stream used for CUDA operations.
//
// For further details, please see:
// https://docs.opencv.org/master/d9/df3/classcv_1_1cuda_1_1Stream.html#aa6434e2f5f29bd81406732b39951c246
type Stream struct {
p C.Stream
}
// NewStream returns a new empty Stream.
func NewStream() Stream {
return Stream{p: C.Stream_New()}
}
// Close the Stream.
func (s *Stream) Close() error {
C.Stream_Close(s.p)
s.p = nil
return nil
}
// QueryIfComplete returns true if the current stream queue is finished
//
// For further details, please see:
// https://docs.opencv.org/master/d9/df3/classcv_1_1cuda_1_1Stream.html#a9fab618395d42fa31987506e42fab1b4
func (s *Stream) QueryIfComplete() bool {
return bool(C.Stream_QueryIfComplete(s.p))
}
// WaitForCompletion blocks the current CPU thread until all operations in the stream are complete.
//
// For further details, please see:
// https://docs.opencv.org/master/d9/df3/classcv_1_1cuda_1_1Stream.html#a0e1d939503e8faad741ab584b720bca6
func (s *Stream) WaitForCompletion() {
C.Stream_WaitForCompletion(s.p)
}