-
Notifications
You must be signed in to change notification settings - Fork 7
/
oclengine.cpp
1923 lines (1660 loc) · 45.8 KB
/
oclengine.cpp
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#include "oclengine.h"
#include "winglue.h"
#include <cassert>
OCLEngine::OCLEngine(int platform_id, int device_id, const char* program, uint32_t ncols,
uint32_t nrows, uint32_t invsize, bool is_unlim_round, int32_t addr_mode, const char* pkey_base,
const char* filename, bool& should_exit) :
_is_unlim_round(is_unlim_round), _addr_mode(addr_mode), _pkey_base(pkey_base)
{
READY = false;
struct timeval before {}, after{};
uint8_t buf[20];
FILE* wfd;
uint64_t N = 0;
gettimeofday(&before, nullptr);
wfd = fopen(filename, "rb");
if (!wfd) {
printf("%s can not open\n", filename);
exit2("bloom init", 1);
}
_fseeki64(wfd, 0, SEEK_END);
N = _ftelli64(wfd);
N = N / 20;
BLOOM_N = 2 * N;
rewind(wfd);
auto* heap = (uint8_t*)malloc(N * 20);
memset(heap, 0, N * 20);
_bloom = new Bloom(BLOOM_N, 0.00001);
uint64_t percent = (N - 1) / 100;
uint64_t i = 0;
while (i < N && !should_exit) {
memset(buf, 0, 20);
memset(heap + (i * 20), 0, 20);
if (fread(buf, 1, 20, wfd) == 20) {
_bloom->add(buf, 20);
memcpy(heap + (i * 20), buf, 20);
if (i % percent == 0) {
printf("\rLoading addresses: %llu %% " , (i / percent));
fflush(stdout);
}
}
i++;
}
if (should_exit)
exit2("", 0);
printf("\n");
fclose(wfd);
BLOOM_N = _bloom->get_bytes();
DATA = heap;
DATA_SIZE = N * 20;
gettimeofday(&after, nullptr);
printf("Loaded addresses : %llu in %01.6f sec\n", i, (double)(time_diff(before, after) / 1000000));
printf("\n");
_bloom->print();
printf("\n");
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
/* get available platforms */
if ((_platform_id = ocl_platform_get(platform_id)) == nullptr) {
exit2("ocl_platform_get", 1);
}
/* get available devices */
if ((_device_id = ocl_device_get(_platform_id, device_id)) == nullptr) {
exit2("ocl_device_get", 1);
}
cl_int ret;
/* create context */
_context = clCreateContext(nullptr, 1, &(_device_id), nullptr, nullptr, &ret);
if (!_context) {
ocl_error(ret, "clCreateContext");
exit2("clCreateContext", 1);
}
/* create a command */
_command = clCreateCommandQueue(_context, _device_id, 0, &ret);
if (!_command) {
ocl_error(ret, "clCreateCommandQueue");
exit2("clCreateCommandQueue", 1);
}
/* get compiler options */
char optbuf[256];
_quirks = ocl_get_quirks(_device_id, optbuf);
/*Loading and compiling a CL program*/
if (!ocl_load_program(program, optbuf)) {
exit2("ocl_load_program", 1);
}
/*Calculating Matrix Settings*/
/*Number of simultaneously executed threads per GPU */
size_t nthreads = ocl_device_getsizet(_device_id, CL_DEVICE_MAX_WORK_GROUP_SIZE);
size_t full_threads = ocl_device_getsizet(_device_id, CL_DEVICE_MAX_COMPUTE_UNITS);
full_threads *= nthreads;
cl_ulong memsize = ocl_device_getulong(_device_id, CL_DEVICE_GLOBAL_MEM_SIZE);
cl_ulong allocsize = ocl_device_getulong(_device_id, CL_DEVICE_MAX_MEM_ALLOC_SIZE);
memsize /= 2;
if (!ncols || !nrows) {
ncols = full_threads;
nrows = 2;
while ((ncols > nrows) && !(ncols & 1)) {
ncols /= 2;
nrows *= 2;
}
int worksize = 2048; //defult is 2048
int wsmult = 1;
while ((!worksize || ((wsmult * 2) <= worksize)) &&
((ncols * nrows * 2 * 128) < memsize) &&
((ncols * nrows * 2 * 64) < allocsize)) {
if (ncols > nrows)
nrows *= 2;
else
ncols *= 2;
wsmult *= 2;
}
}
uint32_t round = nrows * ncols;
if (!invsize) {
invsize = 2;
while (!(round % (invsize << 1)) && ((round / invsize) > full_threads))
invsize <<= 1;
}
if ((round % invsize) || !is_pow2(invsize) || (invsize < 2)) {
fprintf(stderr, "Grid size: %dx%d\n", ncols, nrows);
fprintf(stderr, "Modular inverse thread size: %d\n", invsize);
if (round % invsize)
fprintf(stderr, "Modular inverse work size must evenly divide points\n");
else
fprintf(stderr, "Modular inverse work per task (%d) must be a power of 2\n", invsize);
exit2("Grid size settings", 1);
}
_ncols = ncols;
_nrows = nrows;
_round = round;
_invsize = invsize;
printf("\n\n");
printf("MATRIX:\n");
printf("\tGrid size : %dx%d\n", ncols, nrows);
printf("\tTotal : %d\n", round);
printf("\tMod inverse: %d threads [%d ops/thread]\n", round / invsize, invsize);
ocl_kernel_init();
ocl_print_info();
READY = true;
}
OCLEngine::~OCLEngine()
{
int i, arg;
for (arg = 0; arg < MAX_ARG; arg++) {
if (_arguments[arg]) {
clReleaseMemObject(_arguments[arg]);
_arguments[arg] = nullptr;
_argument_size[arg] = 0;
}
}
for (i = 0; i < MAX_KERNEL; i++) {
if (_kernel[i]) {
clReleaseKernel(_kernel[i]);
_kernel[i] = nullptr;
}
}
if (_program) {
clReleaseProgram(_program);
}
if (_command) {
clReleaseCommandQueue(_command);
}
if (_context) {
clReleaseContext(_context);
}
if (DATA)
free(DATA);
delete _bloom;
}
void OCLEngine::exit2(const char* err, int ret)
{
fprintf(stderr, "\nERROR: (%d) : %s\n", ret, err);
exit(ret);
}
bool OCLEngine::is_ready() const
{
return READY;
}
void OCLEngine::loop(bool& should_exit)
{
int i, n;
BIGNUM* bn_tmp = BN_new();
BN_CTX* bn_ctx = BN_CTX_new();
EC_KEY* pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
EC_KEY_precompute_mult(pkey, bn_ctx);
BIGNUM* N = BN_new();
BN_hex2bn(&N, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");
const BIGNUM* bn_key = NULL;
const EC_GROUP* pgroup = EC_KEY_get0_group(pkey);
const EC_POINT* pgen = EC_GROUP_get0_generator(pgroup);
EC_POINT** pprows = NULL;
EC_POINT** ppcols = NULL;
EC_POINT* pbatchinc = NULL;
EC_POINT* poffset = NULL;
//Allocating memory for matrix base points
ppcols = (EC_POINT**)malloc(_ncols * sizeof(EC_POINT*));
pprows = (EC_POINT**)malloc(_nrows * sizeof(EC_POINT*));
for (i = 0; i < (int)_ncols; i++) {
ppcols[i] = EC_POINT_new(pgroup);
}
for (i = 0; i < (int)_nrows; i++) {
pprows[i] = EC_POINT_new(pgroup);
}
pbatchinc = EC_POINT_new(pgroup);
poffset = EC_POINT_new(pgroup);
BN_set_word(bn_tmp, _ncols);
EC_POINT_mul(pgroup, pbatchinc, bn_tmp, NULL, NULL, bn_ctx);
EC_POINT_make_affine(pgroup, pbatchinc, bn_ctx);
//The point to shift the initial increments by the total number of elements in the matrix
BN_set_word(bn_tmp, _round);
EC_POINT_mul(pgroup, poffset, bn_tmp, NULL, NULL, bn_ctx);
EC_POINT_make_affine(pgroup, poffset, bn_ctx);
uint64_t total = 0;
uint32_t iterations = 0; //Number of private key change iterations
uint32_t rounds = 0; //Number of rounds of work with GPU
time_t now = 0;
uint32_t found_deltau = 0;
uint32_t found_deltac = 0;
uint32_t found_pos = 0;
uint8_t* points_in = NULL;
uint8_t* strides_in = NULL;
uint32_t* uint32_ptr = NULL;
uint8_t* uint8_ptr = NULL;
uint8_t* found_hashu = NULL;
uint8_t* found_hashc = NULL;
KeyInfo* info = NULL;
FILE* ffd = NULL;
uint8_t pkey_bin[32];
uint8_t pkey_s[65];
char buffer[4096];
char time_buf[128];
uint8_t hash_buf[128];
char tmp[1024];
HashRate round_hr;
HashRate total_hr;
uint32_t round_max = 3; //(_is_unlim_round == false ? (uint32_t)(0xFFFFFFFF / _round) + 1 : 0);
gettimeofday(&(total_hr.time_start), NULL);
while (!should_exit) {
/******************************************************************/
iterations++;
//Setting the result buffer to its default position
uint32_ptr = (uint32_t*)ocl_map_arg_buffer(0, 1);
if (!uint32_ptr) {
fprintf(stderr, "ERROR: Could not map result buffer\n");
return;
}
uint32_ptr[0] = 0xffffffff;
//uint32_ptr[6] = 0xffffffff;
ocl_unmap_arg_buffer(0, uint32_ptr);
//Generating a random private key
EC_KEY_generate_key(pkey);
//If the starting private key is set, set it
if (iterations == 1 && strlen(_pkey_base) != 0) {
BN_hex2bn(&bn_tmp, _pkey_base);
Utils::set_pkey(bn_tmp, pkey);
}
//Displaying the key on the screen
bn_key = EC_KEY_get0_private_key(pkey);
n = BN_num_bytes(bn_key);
if (n < 32) {
memset(pkey_bin, 0, 32 - n);
}
BN_bn2bin(bn_key, &pkey_bin[32 - n]);
Utils::bin2hex(pkey_s, pkey_bin, 32);
now = time(NULL);
strftime(buffer, 1023, "%Y-%m-%d %H:%M:%S", localtime(&now));
//printf("\nIteration %u at [%s] from: %s\n", iterations, buffer, pkey_s);
//Preparing initial values for the matrix
EC_POINT_copy(ppcols[0], EC_KEY_get0_public_key(pkey));
//Preparing initial values for the matrix
for (i = 1; i < (int)_ncols; i++) {
EC_POINT_add(pgroup, ppcols[i], ppcols[i - 1], pgen, bn_ctx);
}
EC_POINTs_make_affine(pgroup, _ncols, ppcols, bn_ctx);
//Fill in the obtained base points the variables of the OpenCL function
points_in = (uint8_t*)ocl_map_arg_buffer(3, 1);
if (!points_in) {
fprintf(stderr, "ERROR: Could not map column buffer\n"); return;
}
for (i = 0; i < (int)_ncols; i++) {
ocl_put_point_tpa(points_in, i, ppcols[i]);
}
ocl_unmap_arg_buffer(3, points_in);
//Calculating incremental base points
EC_POINT_copy(pprows[0], pgen);
for (i = 1; i < (int)_nrows; i++) {
EC_POINT_add(pgroup, pprows[i], pprows[i - 1], pbatchinc, bn_ctx);
}
EC_POINTs_make_affine(pgroup, _nrows, pprows, bn_ctx);
rounds = 1;
while ((rounds < round_max || round_max == 0) && !should_exit) {
gettimeofday(&(round_hr.time_start), NULL);
bn_key = EC_KEY_get0_private_key(pkey);
n = BN_num_bytes(bn_key);
if (n < 32) {
memset(pkey_bin, 0, 32 - n);
}
BN_bn2bin(bn_key, &pkey_bin[32 - n]);
Utils::bin2hex(pkey_s, pkey_bin, 32);
//printf("\nround %u from: %s\n",rounds, pkey_s);
if (rounds > 1) {
//Shift the increment by poffset points forward
for (i = 0; i < (int)_nrows; i++) {
EC_POINT_add(pgroup, pprows[i], pprows[i], poffset, bn_ctx);
}
EC_POINTs_make_affine(pgroup, _nrows, pprows, bn_ctx);
}
//Copying Incremental Base Points to a Device
strides_in = (uint8_t*)ocl_map_arg_buffer(4, 1);
if (!strides_in) {
fprintf(stderr, "ERROR: Could not map row buffer\n"); return;
}
memset(strides_in, 0, 64 * _nrows);
for (i = 0; i < (int)_nrows; i++) {
ocl_put_point(strides_in + (64 * i), pprows[i]);
}
ocl_unmap_arg_buffer(4, strides_in);
if (ocl_kernel_start()) {
//Getting the value of the attribute of finding a match
uint8_ptr = (uint8_t*)ocl_map_arg_buffer(0, 2);
if (!uint8_ptr) {
fprintf(stderr, "ERROR: Could not map result buffer");
return;
}
if (_addr_mode == 0) {
found_deltau = ((uint32_t*)uint8_ptr)[0];
if (found_deltau != 0xffffffff) {
found_hashu = &uint8_ptr[0 + 4];
if (check_hash_binary(found_hashu) > 0) {
report(bn_tmp, bn_key, info, found_deltau, found_hashu, hash_buf,
&now, time_buf, buffer, tmp, pkey_s, ffd, UNCOMPRESSED);
}
memset(uint8_ptr, 0, ARG_FOUND_SIZE / 2);
memset(uint8_ptr, 0xFF, 4);
}
}
else if (_addr_mode == 1) {
found_deltac = ((uint32_t*)uint8_ptr)[6];
if (found_deltac != 0xffffffff) {
found_hashc = &uint8_ptr[24 + 4];
if (check_hash_binary(found_hashc) > 0) {
report(bn_tmp, bn_key, info, found_deltac, found_hashc, hash_buf,
&now, time_buf, buffer, tmp, pkey_s, ffd, COMPRESSED);
}
memset(uint8_ptr + 24, 0, ARG_FOUND_SIZE / 2);
memset(uint8_ptr + 24, 0xFF, 4);
}
}
else {
found_deltau = ((uint32_t*)uint8_ptr)[0];
found_deltac = ((uint32_t*)uint8_ptr)[6];
if (found_deltau != 0xffffffff) {
found_hashu = &uint8_ptr[0 + 4];
if (check_hash_binary(found_hashu) > 0) {
report(bn_tmp, bn_key, info, found_deltau, found_hashu, hash_buf,
&now, time_buf, buffer, tmp, pkey_s, ffd, UNCOMPRESSED);
}
memset(uint8_ptr, 0, ARG_FOUND_SIZE / 2);
memset(uint8_ptr, 0xFF, 4);
}
if (found_deltac != 0xffffffff) {
found_hashc = &uint8_ptr[24 + 4];
if (check_hash_binary(found_hashc) > 0) {
report(bn_tmp, bn_key, info, found_deltac, found_hashc, hash_buf,
&now, time_buf, buffer, tmp, pkey_s, ffd, COMPRESSED);
}
memset(uint8_ptr + 24, 0, ARG_FOUND_SIZE / 2);
memset(uint8_ptr + 24, 0xFF, 4);
}
}
ocl_unmap_arg_buffer(0, uint8_ptr);
//private key increment
BN_copy(bn_tmp, bn_key);
BN_add_word(bn_tmp, _round);
Utils::set_pkey(bn_tmp, pkey);
}
else {
return;
}
total += _round;
Utils::hashrate_update(&round_hr, _round);
Utils::hashrate_update(&total_hr, total);
printf("\r[%s] (%01.2f %s) [total: %s] ",
pkey_s, round_hr.hashrate, round_hr.unit, formatThousands(total).c_str(), total_hr.hashrate);
fflush(stdout);
rounds++;
}
}
return;
}
std::string OCLEngine::formatThousands(uint64_t x)
{
char buf[32] = "";
sprintf(buf, "%lld", x);
std::string s(buf);
int len = (int)s.length();
int numCommas = (len - 1) / 3;
if (numCommas == 0) {
return s;
}
std::string result = "";
int count = ((len % 3) == 0) ? 0 : (3 - (len % 3));
for (int i = 0; i < len; i++) {
result += s[i];
if (count++ == 2 && i < len - 1) {
result += ",";
count = 0;
}
}
return result;
}
void OCLEngine::report(BIGNUM* bn_tmp, const BIGNUM* bn_key, KeyInfo* info, uint32_t found_delta, const uint8_t* found_hash,
uint8_t* hash_buf, time_t* now, char* time_buf, char* buffer, char* tmp, uint8_t* pkey_s, FILE* ffd, PubType pubtype)
{
BN_copy(bn_tmp, bn_key);
BN_add_word(bn_tmp, found_delta + 1);
info = Utils::get_key_info(bn_tmp, pubtype);
Utils::bin2hex(hash_buf, found_hash, 20);
*now = time(NULL);
strftime(time_buf, 127, "%Y-%m-%d %H:%M:%S", localtime(now));
int n = sprintf(buffer,
"======================================================================================\n"\
"TIME: %s\n"\
"PRIV: %s\n"\
"PUBK: %s\n"\
"HASH: %s\n"\
"ADDR: %s\n"\
"SALT: %s\n"\
"OFST: %i\n"\
"GPUH: %s"\
"\n",
time_buf,
info->private_hex,
pubtype == COMPRESSED ? info->publicc_hex : info->publicu_hex,
info->public_ripemd160_hex,
info->address_hex,
pkey_s,
found_delta,
hash_buf
);
ffd = fopen("BASE.txt", "a");
if (ffd) {
fwrite(buffer, n, 1, ffd);
fclose(ffd);
}
int bub = sprintf(buffer,
"%s\n",
info->address_hex
);
ffd = fopen("Found.txt", "a");
if (ffd) {
fwrite(buffer, bub, 1, ffd);
fclose(ffd);
}
free(info);
}
/***********************************************************************
* OpenCL debugging and support
***********************************************************************/
const char* OCLEngine::ocl_strerror(cl_int ret)
{
#define OCL_STATUS(st) case st: return #st;
switch (ret) {
OCL_STATUS(CL_SUCCESS);
OCL_STATUS(CL_DEVICE_NOT_FOUND);
OCL_STATUS(CL_DEVICE_NOT_AVAILABLE);
OCL_STATUS(CL_COMPILER_NOT_AVAILABLE);
OCL_STATUS(CL_MEM_OBJECT_ALLOCATION_FAILURE);
OCL_STATUS(CL_OUT_OF_RESOURCES);
OCL_STATUS(CL_OUT_OF_HOST_MEMORY);
OCL_STATUS(CL_PROFILING_INFO_NOT_AVAILABLE);
OCL_STATUS(CL_MEM_COPY_OVERLAP);
OCL_STATUS(CL_IMAGE_FORMAT_MISMATCH);
OCL_STATUS(CL_IMAGE_FORMAT_NOT_SUPPORTED);
OCL_STATUS(CL_BUILD_PROGRAM_FAILURE);
OCL_STATUS(CL_MAP_FAILURE);
#if defined(CL_MISALIGNED_SUB_BUFFER_OFFSET)
OCL_STATUS(CL_MISALIGNED_SUB_BUFFER_OFFSET);
#endif /* defined(CL_MISALIGNED_SUB_BUFFER_OFFSET) */
#if defined(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST)
OCL_STATUS(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
#endif /* defined(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST) */
OCL_STATUS(CL_INVALID_VALUE);
OCL_STATUS(CL_INVALID_DEVICE_TYPE);
OCL_STATUS(CL_INVALID_PLATFORM);
OCL_STATUS(CL_INVALID_DEVICE);
OCL_STATUS(CL_INVALID_CONTEXT);
OCL_STATUS(CL_INVALID_QUEUE_PROPERTIES);
OCL_STATUS(CL_INVALID_COMMAND_QUEUE);
OCL_STATUS(CL_INVALID_HOST_PTR);
OCL_STATUS(CL_INVALID_MEM_OBJECT);
OCL_STATUS(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR);
OCL_STATUS(CL_INVALID_IMAGE_SIZE);
OCL_STATUS(CL_INVALID_SAMPLER);
OCL_STATUS(CL_INVALID_BINARY);
OCL_STATUS(CL_INVALID_BUILD_OPTIONS);
OCL_STATUS(CL_INVALID_PROGRAM);
OCL_STATUS(CL_INVALID_PROGRAM_EXECUTABLE);
OCL_STATUS(CL_INVALID_KERNEL_NAME);
OCL_STATUS(CL_INVALID_KERNEL_DEFINITION);
OCL_STATUS(CL_INVALID_KERNEL);
OCL_STATUS(CL_INVALID_ARG_INDEX);
OCL_STATUS(CL_INVALID_ARG_VALUE);
OCL_STATUS(CL_INVALID_ARG_SIZE);
OCL_STATUS(CL_INVALID_KERNEL_ARGS);
OCL_STATUS(CL_INVALID_WORK_DIMENSION);
OCL_STATUS(CL_INVALID_WORK_GROUP_SIZE);
OCL_STATUS(CL_INVALID_WORK_ITEM_SIZE);
OCL_STATUS(CL_INVALID_GLOBAL_OFFSET);
OCL_STATUS(CL_INVALID_EVENT_WAIT_LIST);
OCL_STATUS(CL_INVALID_EVENT);
OCL_STATUS(CL_INVALID_OPERATION);
OCL_STATUS(CL_INVALID_GL_OBJECT);
OCL_STATUS(CL_INVALID_BUFFER_SIZE);
OCL_STATUS(CL_INVALID_MIP_LEVEL);
OCL_STATUS(CL_INVALID_GLOBAL_WORK_SIZE);
#if defined(CL_INVALID_PROPERTY)
OCL_STATUS(CL_INVALID_PROPERTY);
#endif /* defined(CL_INVALID_PROPERTY) */
#undef OCL_STATUS
default: {
static char tmp[64];
snprintf(tmp, sizeof(tmp), "Unknown code %d", ret);
return tmp;
}
}
}
void OCLEngine::ocl_error(int code, const char* desc)
{
const char* err = ocl_strerror(code);
if (desc) {
fprintf(stderr, "%s: %s\n", desc, err);
}
else {
fprintf(stderr, "%s\n", err);
}
}
/*Displaying information about the current device and platform*/
void OCLEngine::ocl_print_info()
{
cl_device_id did = _device_id;
printf("\nDEVICE INFO:\n");
printf("\tDevice : %s\n", ocl_device_getstr(did, CL_DEVICE_NAME));
printf("\tVendor : %s (%04x)\n", ocl_device_getstr(did, CL_DEVICE_VENDOR), ocl_device_getuint(did, CL_DEVICE_VENDOR_ID));
printf("\tDriver : %s\n", ocl_device_getstr(did, CL_DRIVER_VERSION));
printf("\tProfile : %s\n", ocl_device_getstr(did, CL_DEVICE_PROFILE));
printf("\tVersion : %s\n", ocl_device_getstr(did, CL_DEVICE_VERSION));
printf("\tMax compute units : %zd\n", ocl_device_getsizet(did, CL_DEVICE_MAX_COMPUTE_UNITS));
printf("\tMax workgroup size : %zd\n", ocl_device_getsizet(did, CL_DEVICE_MAX_WORK_GROUP_SIZE));
printf("\tGlobal memory : %llu\n", ocl_device_getulong(did, CL_DEVICE_GLOBAL_MEM_SIZE));
printf("\tMax allocation : %llu\n\n", ocl_device_getulong(did, CL_DEVICE_MAX_MEM_ALLOC_SIZE));
}
/***********************************************************************
* PLATFORM
***********************************************************************/
/*Getting the platform*/
cl_platform_id OCLEngine::ocl_platform_get(int num)
{
int np;
cl_platform_id id, * ids;
np = ocl_platform_list(&ids);
if (np < 0)
return nullptr;
if (!np) {
fprintf(stderr, "No OpenCL platforms available\n");
return nullptr;
}
if (num < 0) {
if (np == 1)
num = 0;
else
num = np;
}
if (num < np) {
id = ids[num];
free(ids);
return id;
}
free(ids);
return nullptr;
}
/*Getting a list of available platforms*/
int OCLEngine::ocl_platform_list(cl_platform_id** list_out)
{
cl_uint np;
cl_int res;
cl_platform_id* ids;
res = clGetPlatformIDs(0, nullptr, &np);
if (res != CL_SUCCESS) {
ocl_error(res, "clGetPlatformIDs(0)");
*list_out = nullptr;
return -1;
}
if (np) {
ids = (cl_platform_id*)malloc(np * sizeof(cl_platform_id));
if (ids == nullptr) {
fprintf(stderr,
"Could not allocate platform ID list\n");
*list_out = nullptr;
return -1;
}
res = clGetPlatformIDs(np, ids, nullptr);
if (res != CL_SUCCESS) {
ocl_error(res, "clGetPlatformIDs(n)");
free(ids);
*list_out = nullptr;
return -1;
}
*list_out = ids;
}
return np;
}
/*Displaying information about available platforms*/
void OCLEngine::ocl_platforms_info(cl_platform_id* ids, int np, int base)
{
int i;
char nbuf[128];
char vbuf[128];
size_t len;
cl_int res;
for (i = 0; i < np; i++) {
res = clGetPlatformInfo(ids[i], CL_PLATFORM_NAME, sizeof(nbuf), nbuf, &len);
if (res != CL_SUCCESS) {
ocl_error(res, "clGetPlatformInfo(NAME)");
continue;
}
if (len >= sizeof(nbuf))
len = sizeof(nbuf) - 1;
nbuf[len] = '\0';
res = clGetPlatformInfo(ids[i], CL_PLATFORM_VENDOR, sizeof(vbuf), vbuf, &len);
if (res != CL_SUCCESS) {
ocl_error(res, "clGetPlatformInfo(VENDOR)");
continue;
}
if (len >= sizeof(vbuf))
len = sizeof(vbuf) - 1;
vbuf[len] = '\0';
fprintf(stderr, "%d: [%s] %s\n", i + base, vbuf, nbuf);
}
}
const char* OCLEngine::ocl_platform_getstr(cl_platform_id pid, cl_platform_info param)
{
static char platform_str[1024];
cl_int ret;
size_t size_ret;
ret = clGetPlatformInfo(pid, param,
sizeof(platform_str), platform_str,
&size_ret);
if (ret != CL_SUCCESS) {
snprintf(platform_str, sizeof(platform_str),
"clGetPlatformInfo(%d): %s",
param, ocl_strerror(ret));
}
return platform_str;
}
/***********************************************************************
* DEVICE
***********************************************************************/
/*Getting the specified device on the specified platform*/
cl_device_id OCLEngine::ocl_device_manual(int platformidx, int deviceidx)
{
cl_platform_id pid;
cl_device_id did = nullptr;
pid = ocl_platform_get(platformidx);
if (pid) {
did = ocl_device_get(pid, deviceidx);
if (did)
return did;
}
return nullptr;
}
/*Receiving the device*/
cl_device_id OCLEngine::ocl_device_get(cl_platform_id pid, int num)
{
int nd;
cl_device_id id, * ids;
nd = ocl_devices_list(pid, &ids);
if (nd < 0)
return nullptr;
if (!nd) {
fprintf(stderr, "No OpenCL devices found\n");
return nullptr;
}
if (num < 0) {
if (nd == 1)
num = 0;
else
num = nd;
}
if (num < nd) {
id = ids[num];
free(ids);
return id;
}
free(ids);
return nullptr;
}
/*Platform device list*/
int OCLEngine::ocl_devices_list(cl_platform_id pid, cl_device_id** list_out)
{
cl_uint nd;
cl_int res;
cl_device_id* ids;
res = clGetDeviceIDs(pid, CL_DEVICE_TYPE_GPU, 0, nullptr, &nd);
if (res != CL_SUCCESS) {
ocl_error(res, "clGetDeviceIDs(0)");
*list_out = nullptr;
return -1;
}
if (nd) {
ids = (cl_device_id*)malloc(nd * sizeof(cl_device_id));
if (ids == nullptr) {
fprintf(stderr, "Could not allocate device ID list\n");
*list_out = nullptr;
return -1;
}
res = clGetDeviceIDs(pid, CL_DEVICE_TYPE_GPU, nd, ids, nullptr);
if (res != CL_SUCCESS) {
ocl_error(res, "clGetDeviceIDs(n)");
free(ids);
*list_out = nullptr;
return -1;
}
*list_out = ids;
}
return nd;
}
/*Platform device information*/
void OCLEngine::ocl_devices_info(cl_platform_id pid, cl_device_id* ids, int nd, int base)
{
int i;
char nbuf[128];
char vbuf[128];
size_t len;
cl_int res;
(void)pid;
for (i = 0; i < nd; i++) {
res = clGetDeviceInfo(ids[i], CL_DEVICE_NAME, sizeof(nbuf), nbuf, &len);
if (res != CL_SUCCESS)
continue;
if (len >= sizeof(nbuf))
len = sizeof(nbuf) - 1;
nbuf[len] = '\0';
res = clGetDeviceInfo(ids[i], CL_DEVICE_VENDOR, sizeof(vbuf), vbuf, &len);
if (res != CL_SUCCESS)
continue;
if (len >= sizeof(vbuf))
len = sizeof(vbuf) - 1;
vbuf[len] = '\0';
fprintf(stderr, " %d: [%s] %s\n", i + base, vbuf, nbuf);
}
}
/*Returns the platform of the given device*/
cl_platform_id OCLEngine::ocl_device_getplatform(cl_device_id did)
{
cl_int ret;
cl_platform_id val;
size_t size_ret;
ret = clGetDeviceInfo(did, CL_DEVICE_PLATFORM,
sizeof(cl_platform_id), &val, &size_ret);
if (ret != CL_SUCCESS) {
fprintf(stderr, "clGetDeviceInfo(CL_DEVICE_PLATFORM): %s",
ocl_strerror(ret));
}
return val;
}
/*Returns the type of device*/
cl_device_type OCLEngine::ocl_device_gettype(cl_device_id did)
{
cl_int ret;
cl_device_type val;
size_t size_ret;
ret = clGetDeviceInfo(did, CL_DEVICE_TYPE,
sizeof(val), &val, &size_ret);
if (ret != CL_SUCCESS) {
fprintf(stderr, "clGetDeviceInfo(CL_DEVICE_TYPE): %s",
ocl_strerror(ret));
}
return val;
}
/*Returns the text parameter of the device*/
const char* OCLEngine::ocl_device_getstr(cl_device_id did, cl_device_info param)
{
static char device_str[1024];
cl_int ret;
size_t size_ret;
ret = clGetDeviceInfo(did, param,
sizeof(device_str), device_str,
&size_ret);
if (ret != CL_SUCCESS) {
snprintf(device_str, sizeof(device_str),
"clGetDeviceInfo(%d): %s",
param, ocl_strerror(ret));
}
return device_str;
}
/*Returns the size_t device parameter*/
size_t OCLEngine::ocl_device_getsizet(cl_device_id did, cl_device_info param)
{
cl_int ret;
size_t val = 0;
size_t size_ret;
ret = clGetDeviceInfo(did, param, sizeof(val), &val, &size_ret);
if (ret != CL_SUCCESS) {
fprintf(stderr,
"clGetDeviceInfo(%d): %s", param, ocl_strerror(ret));
}
return val;
}
/*Returns the cl_ulong device parameter*/
cl_ulong OCLEngine::ocl_device_getulong(cl_device_id did, cl_device_info param)
{
cl_int ret;
cl_ulong val;
size_t size_ret;
ret = clGetDeviceInfo(did, param, sizeof(val), &val, &size_ret);
if (ret != CL_SUCCESS) {
fprintf(stderr,
"clGetDeviceInfo(%d): %s", param, ocl_strerror(ret));
}
return val;
}
/*Returns the cl_uint device parameter*/
cl_uint OCLEngine::ocl_device_getuint(cl_device_id did, cl_device_info param)
{
cl_int ret;
cl_uint val;
size_t size_ret;
ret = clGetDeviceInfo(did, param, sizeof(val), &val, &size_ret);
if (ret != CL_SUCCESS) {
fprintf(stderr,
"clGetDeviceInfo(%d): %s", param, ocl_strerror(ret));
}
return val;
}
enum {
VG_OCL_DEEP_PREPROC_UNROLL = (1 << 0),
VG_OCL_PRAGMA_UNROLL = (1 << 1),
VG_OCL_EXPENSIVE_BRANCHES = (1 << 2),
VG_OCL_DEEP_VLIW = (1 << 3),
VG_OCL_AMD_BFI_INT = (1 << 4),
VG_OCL_NV_VERBOSE = (1 << 5),
VG_OCL_BROKEN = (1 << 6),
VG_OCL_NO_BINARIES = (1 << 7),
VG_OCL_OPTIMIZATIONS = (VG_OCL_DEEP_PREPROC_UNROLL |
VG_OCL_PRAGMA_UNROLL |
VG_OCL_EXPENSIVE_BRANCHES |
VG_OCL_DEEP_VLIW |
VG_OCL_AMD_BFI_INT),
};
/***********************************************************************
* PROGRAM