forked from decred/gominer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.go
425 lines (343 loc) · 10.4 KB
/
program.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package cl
/*
#cgo CFLAGS: -I CL
#cgo !darwin LDFLAGS: -lOpenCL
#cgo darwin LDFLAGS: -framework OpenCL
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#ifdef __APPLE__
#include "OpenCL/opencl.h"
#else
#include "CL/opencl.h"
#endif
#include <string.h>
#include <stdlib.h>
extern void go_prg_notify(cl_program program, void *user_data);
static void CL_CALLBACK c_prg_build_notify(cl_program program, void *user_data) {
go_prg_notify(program, user_data);
}
static cl_int CLBuildProgram(cl_program program,
cl_uint num_devices,
const cl_device_id *devices,
const char *options,
void *user_data){
return clBuildProgram(program, num_devices, devices, options, c_prg_build_notify, user_data);
}
*/
import "C"
import (
"unsafe"
)
type CL_prg_notify func(program CL_program, user_data unsafe.Pointer)
var prg_notify map[C.cl_program]CL_prg_notify
func init() {
prg_notify = make(map[C.cl_program]CL_prg_notify)
}
//export go_prg_notify
func go_prg_notify(program C.cl_program, user_data unsafe.Pointer) {
prg_notify[program](CL_program{program}, user_data)
}
func CLCreateProgramWithSource(context CL_context,
count CL_uint,
strings [][]byte,
lengths []CL_size_t,
errcode_ret *CL_int) CL_program {
if count == 0 || len(strings) != int(count) || len(lengths) != int(count) {
if errcode_ret != nil {
*errcode_ret = CL_INVALID_VALUE
}
return CL_program{nil}
}
for i := 0; i < int(count); i++ {
if strings[i] == nil || lengths[i] == 0 {
if errcode_ret != nil {
*errcode_ret = CL_INVALID_VALUE
}
return CL_program{nil}
}
}
var c_program C.cl_program
var c_lengths []C.size_t
var c_strings []*C.char
var c_errcode_ret C.cl_int
c_lengths = make([]C.size_t, count)
c_strings = make([]*C.char, count)
for i := 0; i < int(count); i++ {
c_lengths[i] = C.size_t(lengths[i])
c_strings[i] = C.CString(string(strings[i]))
defer C.free(unsafe.Pointer(c_strings[i]))
}
c_program = C.clCreateProgramWithSource(context.cl_context,
C.cl_uint(count),
&c_strings[0],
&c_lengths[0],
&c_errcode_ret)
if errcode_ret != nil {
*errcode_ret = CL_int(c_errcode_ret)
}
return CL_program{c_program}
}
func CLCreateProgramWithBinary(context CL_context,
num_devices CL_uint,
devices []CL_device_id,
lengths []CL_size_t,
binaries [][]byte,
binary_status []CL_int,
errcode_ret *CL_int) CL_program {
if num_devices == 0 ||
len(devices) != int(num_devices) ||
len(lengths) != int(num_devices) ||
len(binaries) != int(num_devices) ||
len(binary_status) != int(num_devices) {
if errcode_ret != nil {
*errcode_ret = CL_INVALID_VALUE
}
return CL_program{nil}
}
for i := 0; i < int(num_devices); i++ {
if binaries[i] == nil || lengths[i] == 0 {
if errcode_ret != nil {
*errcode_ret = CL_INVALID_VALUE
}
return CL_program{nil}
}
}
var c_program C.cl_program
var c_devices []C.cl_device_id
var c_lengths []C.size_t
var c_binaries []*C.uchar
var c_status []C.cl_int
var c_errcode_ret C.cl_int
c_devices = make([]C.cl_device_id, num_devices)
c_lengths = make([]C.size_t, num_devices)
c_binaries = make([]*C.uchar, num_devices)
c_status = make([]C.cl_int, num_devices)
for i := CL_uint(0); i < num_devices; i++ {
c_devices[i] = devices[i].cl_device_id
c_lengths[i] = C.size_t(lengths[i])
c_binaries[i] = (*C.uchar)(C.malloc(c_lengths[i]))
C.memcpy(unsafe.Pointer(c_binaries[i]), unsafe.Pointer(&binaries[i][0]), c_lengths[i])
defer C.free(unsafe.Pointer(c_binaries[i]))
}
c_program = C.clCreateProgramWithBinary(context.cl_context,
C.cl_uint(num_devices),
&c_devices[0],
&c_lengths[0],
&c_binaries[0],
&c_status[0],
&c_errcode_ret)
for i := CL_uint(0); i < num_devices; i++ {
binary_status[i] = CL_int(c_status[i])
}
if errcode_ret != nil {
*errcode_ret = CL_int(c_errcode_ret)
}
return CL_program{c_program}
}
func CLRetainProgram(program CL_program) CL_int {
return CL_int(C.clRetainProgram(program.cl_program))
}
func CLReleaseProgram(program CL_program) CL_int {
return CL_int(C.clReleaseProgram(program.cl_program))
}
func CLBuildProgram(program CL_program,
num_devices CL_uint,
devices []CL_device_id,
options []byte,
pfn_notify CL_prg_notify,
user_data unsafe.Pointer) CL_int {
if (num_devices == 0 && devices != nil) ||
(num_devices != 0 && devices == nil) ||
(pfn_notify == nil && user_data != nil) {
return CL_INVALID_VALUE
}
var c_devices []C.cl_device_id
var c_options *C.char
var c_errcode_ret C.cl_int
c_devices = make([]C.cl_device_id, len(devices))
for i := 0; i < len(devices); i++ {
c_devices[i] = C.cl_device_id(devices[i].cl_device_id)
}
if options != nil {
c_options = C.CString(string(options))
defer C.free(unsafe.Pointer(c_options))
} else {
c_options = nil
}
if pfn_notify != nil {
prg_notify[program.cl_program] = pfn_notify
c_errcode_ret = C.CLBuildProgram(program.cl_program,
C.cl_uint(num_devices),
&c_devices[0],
c_options,
user_data)
} else {
c_errcode_ret = C.clBuildProgram(program.cl_program,
C.cl_uint(num_devices),
&c_devices[0],
c_options,
nil,
nil)
}
return CL_int(c_errcode_ret)
}
func CLGetProgramInfo(program CL_program,
param_name CL_program_info,
param_value_size CL_size_t,
param_value *interface{},
param_value_size_ret *CL_size_t) CL_int {
if (param_value_size == 0 || param_value == nil) && param_value_size_ret == nil {
return CL_INVALID_VALUE
} else {
var c_param_value_size_ret C.size_t
var c_errcode_ret C.cl_int
if param_value_size == 0 || param_value == nil {
c_errcode_ret = C.clGetProgramInfo(program.cl_program,
C.cl_program_info(param_name),
C.size_t(param_value_size),
nil,
&c_param_value_size_ret)
} else {
switch param_name {
case CL_PROGRAM_SOURCE:
value := make([]C.char, param_value_size)
c_errcode_ret = C.clGetProgramInfo(program.cl_program,
C.cl_program_info(param_name),
C.size_t(param_value_size),
unsafe.Pointer(&value[0]),
&c_param_value_size_ret)
*param_value = C.GoStringN(&value[0], C.int(c_param_value_size_ret-1))
case CL_PROGRAM_REFERENCE_COUNT,
CL_PROGRAM_NUM_DEVICES:
var value C.cl_uint
c_errcode_ret = C.clGetProgramInfo(program.cl_program,
C.cl_program_info(param_name),
C.size_t(param_value_size),
unsafe.Pointer(&value),
&c_param_value_size_ret)
case CL_PROGRAM_CONTEXT:
var value C.cl_context
c_errcode_ret = C.clGetProgramInfo(program.cl_program,
C.cl_program_info(param_name),
C.size_t(param_value_size),
unsafe.Pointer(&value),
&c_param_value_size_ret)
case CL_PROGRAM_DEVICES:
var param C.cl_device_id
length := int(C.size_t(param_value_size) / C.size_t(unsafe.Sizeof(param)))
value1 := make([]C.cl_device_id, length)
value2 := make([]CL_device_id, length)
c_errcode_ret = C.clGetProgramInfo(program.cl_program,
C.cl_program_info(param_name),
C.size_t(param_value_size),
unsafe.Pointer(&value1[0]),
&c_param_value_size_ret)
for i := 0; i < length; i++ {
value2[i].cl_device_id = value1[i]
}
*param_value = value2
case CL_PROGRAM_BINARY_SIZES:
var param C.size_t
length := int(C.size_t(param_value_size) / C.size_t(unsafe.Sizeof(param)))
value1 := make([]C.size_t, length)
value2 := make([]CL_size_t, length)
c_errcode_ret = C.clGetProgramInfo(program.cl_program,
C.cl_program_info(param_name),
C.size_t(param_value_size),
unsafe.Pointer(&value1[0]),
&c_param_value_size_ret)
for i := 0; i < length; i++ {
value2[i] = CL_size_t(value1[i])
}
*param_value = value2
case CL_PROGRAM_BINARIES:
var param *C.uchar
length := int(C.size_t(param_value_size) / C.size_t(unsafe.Sizeof(param)))
value1 := make([]*C.uchar, length)
value2 := make([]*CL_uchar, length)
c_errcode_ret = C.clGetProgramInfo(program.cl_program,
C.cl_program_info(param_name),
C.size_t(param_value_size),
unsafe.Pointer(&value1[0]),
&c_param_value_size_ret)
for i := 0; i < length; i++ {
value2[i] = (*CL_uchar)(value1[i])
}
*param_value = value2
default:
return CL_INVALID_VALUE
}
}
if param_value_size_ret != nil {
*param_value_size_ret = CL_size_t(c_param_value_size_ret)
}
return CL_int(c_errcode_ret)
}
}
func CLGetProgramBuildInfo(program CL_program,
device CL_device_id,
param_name CL_program_build_info,
param_value_size CL_size_t,
param_value *interface{},
param_value_size_ret *CL_size_t) CL_int {
if (param_value_size == 0 || param_value == nil) && param_value_size_ret == nil {
return CL_INVALID_VALUE
} else {
var c_param_value_size_ret C.size_t
var c_errcode_ret C.cl_int
if param_value_size == 0 || param_value == nil {
c_errcode_ret = C.clGetProgramBuildInfo(program.cl_program,
device.cl_device_id,
C.cl_program_build_info(param_name),
C.size_t(param_value_size),
nil,
&c_param_value_size_ret)
} else {
switch param_name {
case CL_PROGRAM_BUILD_STATUS:
var value C.cl_build_status
c_errcode_ret = C.clGetProgramBuildInfo(program.cl_program,
device.cl_device_id,
C.cl_program_build_info(param_name),
C.size_t(param_value_size),
unsafe.Pointer(&value),
&c_param_value_size_ret)
*param_value = CL_build_status(value)
case CL_PROGRAM_BUILD_OPTIONS,
CL_PROGRAM_BUILD_LOG:
value := make([]C.char, param_value_size)
c_errcode_ret = C.clGetProgramBuildInfo(program.cl_program,
device.cl_device_id,
C.cl_program_build_info(param_name),
C.size_t(param_value_size),
unsafe.Pointer(&value[0]),
&c_param_value_size_ret)
*param_value = C.GoStringN(&value[0], C.int(c_param_value_size_ret-1))
case CL_PROGRAM_BINARY_TYPE:
var value C.cl_program_binary_type
c_errcode_ret = C.clGetProgramBuildInfo(program.cl_program,
device.cl_device_id,
C.cl_program_build_info(param_name),
C.size_t(param_value_size),
unsafe.Pointer(&value),
&c_param_value_size_ret)
*param_value = CL_program_binary_type(value)
case CL_PROGRAM_BUILD_GLOBAL_VARIABLE_TOTAL_SIZE:
var value C.size_t
c_errcode_ret = C.clGetProgramBuildInfo(program.cl_program,
device.cl_device_id,
C.cl_program_build_info(param_name),
C.size_t(param_value_size),
unsafe.Pointer(&value),
&c_param_value_size_ret)
*param_value = CL_size_t(value)
default:
return CL_INVALID_VALUE
}
}
if param_value_size_ret != nil {
*param_value_size_ret = CL_size_t(c_param_value_size_ret)
}
return CL_int(c_errcode_ret)
}
}