This repository has been archived by the owner on May 17, 2020. It is now read-only.
forked from monkins1010/nodecore-pow-AMD-cuda-miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.cc
1164 lines (1001 loc) · 32.4 KB
/
kernel.cc
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 NOMINMAX
#include <cstdint>
//#include "cuda_runtime.h"
//#include "device_launch_parameters.h"
#include <CL/cl.h>
#include <chrono>
#include <ctime>
//#include <fcntl.h>
//#include <unistd.h>
//#include <getopt.h>
//#include <errno.h>
//#include <time.h>
//#include <stdio.h>
//#include <unistd.h>
#include <stdlib.h>
#include "UCPClient.h"
#include <fcntl.h>
//#include <stddef.h>
#ifdef _WIN32
#include <Windows.h>
#include <VersionHelpers.h>
#include <io.h>
#include <BaseTsd.h>
#endif
#ifdef __linux__
#include <sys/socket.h>
#include <netdb.h>
#include "_kernel.h"
#endif
#include <ctime>
#include "Log.h"
#include <sstream>
#include "Constants.h"
#ifndef O_BINARY
#define O_BINARY 0
#endif
#define DEFAULT_BLOCKSIZE 0x1000
#define DEFAULT_THREADS_PER_BLOCK 128
int blocksize = DEFAULT_BLOCKSIZE;
int threadsPerBlock = DEFAULT_THREADS_PER_BLOCK;
bool verboseOutput = false;
int amd_flag = 0;
//typedef SSIZE_T ssize_t;
#define open _open
const char *source = NULL;
size_t source_len;
char *binary = NULL;
size_t binary_len;
uint32_t lastNonceStart = 0;
char outputBuffer[100 * 1024];
string selectedDeviceName;
/*
* Kernel function to search a range of nonces for a solution falling under the macro-configured difficulty (CPU=2^24, GPU=2^32).
*/
void promptExit(int exitCode)
{
cout << "Exiting in 10 seconds..." << endl;
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
exit(exitCode);
}
/**
* Takes the provided timestamp and places it in the header
*/
void embedTimestampInHeader(uint8_t *header, uint32_t timestamp)
{
header[55] = (timestamp & 0x000000FF);
header[54] = (timestamp & 0x0000FF00) >> 8;
header[53] = (timestamp & 0x00FF0000) >> 16;
header[52] = (timestamp & 0xFF000000) >> 24;
}
/**
* Returns a 64-byte header to attempt to mine with.
*/
uint64_t* getWork(UCPClient& ucpClient, uint32_t timestamp)
{
uint64_t *header = new uint64_t[8];
ucpClient.copyHeaderToHash((byte *)header);
embedTimestampInHeader((uint8_t*)header, timestamp);
return header;
}
int deviceToUse = 0;
#if NVML
nvmlDevice_t device;
void readyNVML(int deviceIndex) {
nvmlInit();
nvmlDeviceGetHandleByIndex(deviceIndex, &device);
}
int getTemperature() {
unsigned int temperature;
nvmlDeviceGetTemperature(device, NVML_TEMPERATURE_GPU, &temperature);
return temperature;
}
int getCoreClock() {
unsigned int clock;
nvmlDeviceGetClock(device, NVML_CLOCK_GRAPHICS, NVML_CLOCK_ID_CURRENT, &clock);
return clock;
}
int getMemoryClock() {
unsigned int memClock;
nvmlDeviceGetClock(device, NVML_CLOCK_MEM, NVML_CLOCK_ID_CURRENT, &memClock);
return memClock;
}
#else
void readyNVML(int deviceIndex) {
// Do Nothing
}
int getTemperature() {
return -1;
}
int getCoreClock() {
return -1;
}
int getMemoryClock() {
return -1;
}
#endif
#define SHARE_SUBMISSION_NO_RESPONSE_WARN_THRESHOLD 50
void vprintf(char* toprint) {
if (verboseOutput) {
printf(toprint);
}
}
void printHelpAndExit() {
printf("VeriBlock vBlake OpenCL Miner v1.0\n");
printf("Required Arguments:\n");
printf("-o <poolAddress> The pool address to mine to in the format host:port\n");
printf("-u <username> The username (often an address) used at the pool\n");
printf("Optional Arguments:\n");
printf("-p <password> The miner/worker password to use on the pool\n");
printf("-d <deviceNum> The ordinal of the device to use (default 0)\n");
printf("-tpb <threadPerBlock> The threads per block to use with the Blake kernel (default %d)\n", DEFAULT_THREADS_PER_BLOCK);
printf("-bs <blockSize> The blocksize to use with the vBlake kernel (default %d)\n", DEFAULT_BLOCKSIZE);
printf("-l <enableLogging> Whether to log to a file (default true)\n");
printf("-v <enableVerboseOutput> Whether to enable verbose output for debugging (default false)\n");
printf("\n");
printf("Example command line:\n");
printf("VeriBlock-NodeCore-PoW-CUDA -u VHT36jJyoVFN7ap5Gu77Crua2BMv5j -o testnet-pool-gpu.veriblock.org:8501 -l false\n");
promptExit(0);
}
#ifdef _WIN32
static WSADATA g_wsa_data;
#endif
char net_init(void)
{
#ifdef _WIN32
return (WSAStartup(MAKEWORD(2, 2), &g_wsa_data) == NO_ERROR);
#elif __linux__
return 1;
#endif
}
void net_deinit(void)
{
#ifdef _WIN32
WSACleanup();
#endif
}
string net_dns_resolve(const char* hostname)
{
struct addrinfo hints, *results, *item;
int status;
char ipstr[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; /* AF_INET6 to force version */
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(hostname, NULL, &hints, &results)) != 0)
{
fprintf(stderr, "failed to resolve hostname \"%s\": %s", hostname, gai_strerror(status));
return "invalid hostname";
}
printf("IP addresses for %s:\n\n", hostname);
string ret;
for (item = results; item != NULL; item = item->ai_next)
{
void* addr;
char* ipver;
/* get pointer to the address itself */
/* different fields in IPv4 and IPv6 */
if (item->ai_family == AF_INET) /* address is IPv4 */
{
struct sockaddr_in* ipv4 = (struct sockaddr_in*)item->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
}
else /* address is IPv6 */
{
struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)item->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
/* convert IP to a string and print it */
inet_ntop(item->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr);
ret = ipstr;
}
freeaddrinfo(results);
return ret;
}
cl_mem check_clCreateBuffer(cl_context ctx, cl_mem_flags flags, size_t size,
void *host_ptr)
{
cl_int status;
cl_mem ret;
ret = clCreateBuffer(ctx, flags, size, host_ptr, &status);
if (status != CL_SUCCESS || !ret) {
sprintf(outputBuffer, "clCreateBuffer (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
//fatal("clCreateBuffer (%d)\n", status);
return ret;
}
#ifdef _WIN32
void dump(const char *fname, void *data, size_t len)
{
int fd;
ssize_t ret;
if (-1 == (fd = open(fname, O_BINARY | O_WRONLY | O_CREAT | O_TRUNC, 0666))) {
sprintf(outputBuffer, "%s: %s", fname, strerror(errno));
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
ret = write(fd, data, len);
if (ret == -1) {
sprintf(outputBuffer, "write: %s: %s", fname, strerror(errno));
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
if ((size_t)ret != len) {
sprintf(outputBuffer, "%s: partial write", fname, strerror(errno));
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
if (-1 == close(fd)) {
sprintf(outputBuffer, "close: %s: %s", fname, strerror(errno));
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
}
#endif
void get_program_bins(cl_program program)
{
cl_int status;
size_t sizes;
unsigned char *p;
size_t ret = 0;
status = clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES,
sizeof(sizes), // size_t param_value_size
&sizes, // void *param_value
&ret); // size_t *param_value_size_ret
p = (unsigned char *)malloc(sizes);
status = clGetProgramInfo(program, CL_PROGRAM_BINARIES,
sizeof(p), // size_t param_value_size
&p, // void *param_value
&ret); // size_t *param_value_size_ret
#ifdef _WIN32
dump("dump.co", p, sizes);
#endif
}
void print_platform_info(cl_platform_id plat)
{
char name[1024];
size_t len = 0;
int status;
status = clGetPlatformInfo(plat, CL_PLATFORM_NAME, sizeof(name), &name,
&len);
if (status != CL_SUCCESS)
{
sprintf(outputBuffer, "clGetDeviceInfo (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
selectedDeviceName = name;
sprintf(outputBuffer, "Devices on platform \"%s\":", name);
cout << outputBuffer << endl;
Log::info(outputBuffer);
//printf("Devices on platform \"%s\":\n", name);
//fflush(stdout);
}
void print_device_info(unsigned i, cl_device_id d)
{
char name[1024];
size_t len = 0;
int status;
status = clGetDeviceInfo(d, CL_DEVICE_NAME, sizeof(name), &name, &len);
if (status != CL_SUCCESS) {
sprintf(outputBuffer, "clGetDeviceInfo (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
//fatal("malloc: %s\n", strerror(errno));
}
selectedDeviceName = name;
sprintf(outputBuffer, " ID %d: %s", i, name);
cout << outputBuffer << endl;
Log::info(outputBuffer);
//fatal("clGetDeviceInfo (%d)\n", status);
//printf(" ID %d: %s\n", i, name);
}
void check_clEnqueueReadBuffer(cl_command_queue queue, cl_mem buffer, cl_bool
blocking_read, size_t offset, size_t size, void *ptr, cl_uint
num_events_in_wait_list, const cl_event *event_wait_list, cl_event
*event)
{
cl_int status;
status = clEnqueueReadBuffer(queue, buffer, blocking_read, offset,
size, ptr, num_events_in_wait_list, event_wait_list, event);
if (status != CL_SUCCESS) {
sprintf(outputBuffer, "clEnqueueReadBuffer (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
}
void check_clSetKernelArg(cl_kernel k, cl_uint a_pos, cl_mem *a)
{
cl_int status;
status = clSetKernelArg(k, a_pos, sizeof(*a), a);
if (status != CL_SUCCESS) {
sprintf(outputBuffer, "clSetKernelArg (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
}
void check_clEnqueueNDRangeKernel(cl_command_queue queue, cl_kernel k, cl_uint
work_dim, const size_t *global_work_offset, const size_t
*global_work_size, const size_t *local_work_size, cl_uint
num_events_in_wait_list, const cl_event *event_wait_list, cl_event
*event)
{
cl_uint status;
status = clEnqueueNDRangeKernel(queue, k, work_dim, global_work_offset,
global_work_size, local_work_size, num_events_in_wait_list,
event_wait_list, event);
if (status != CL_SUCCESS) {
sprintf(outputBuffer, "clEnqueueNDRangeKernel (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
}
unsigned scan_platform(cl_platform_id plat, cl_uint *nr_devs_total,
cl_platform_id *plat_id, cl_device_id *dev_id)
{
cl_device_type typ = CL_DEVICE_TYPE_ALL;
cl_uint nr_devs = 0;
cl_device_id *devices;
cl_int status;
unsigned found = 0;
unsigned i;
print_platform_info(plat);
status = clGetDeviceIDs(plat, typ, 0, NULL, &nr_devs);
if (status != CL_SUCCESS)
{
sprintf(outputBuffer, "clGetDeviceInfo (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
if (nr_devs == 0)
return 0;
devices = (cl_device_id *)malloc(nr_devs * sizeof(*devices));
status = clGetDeviceIDs(plat, typ, nr_devs, devices, NULL);
if (status != CL_SUCCESS)
{
sprintf(outputBuffer, "clGetDeviceInfo (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
i = 0;
while (i < nr_devs)
{
print_device_info(*nr_devs_total, devices[i]);
if (*nr_devs_total == deviceToUse)
{
found = 1;
*plat_id = plat;
*dev_id = devices[i];
break;
}
(*nr_devs_total)++;
i++;
}
free(devices);
return found;
}
void get_program_build_log(cl_program program, cl_device_id device)
{
cl_int status2;
char val[100 * 1024];
size_t ret = 0;
status2 = clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(val), &val, &ret);
// if (status2 != CL_SUCCESS) {
sprintf(outputBuffer, "%s", val);
cout << outputBuffer << endl;
Log::info(outputBuffer);
//promptExit(-1);
//}
}
int is_platform_amd(cl_platform_id plat)
{
char name[1024];
size_t len = 0;
int status;
status = clGetPlatformInfo(plat, CL_PLATFORM_NAME, sizeof(name), &name,
&len);
if (status != CL_SUCCESS)
{
sprintf(outputBuffer, "clGetPlatformInfo (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
return strncmp(name, "AMD Accelerated Parallel Processing", len) == 0;
}
void scan_platforms(cl_platform_id *plat_id, cl_device_id *dev_id)
{
cl_uint nr_platforms;
cl_platform_id *platforms;
cl_uint i, nr_devs_total;
cl_int status;
status = clGetPlatformIDs(0, NULL, &nr_platforms);
if (status != CL_SUCCESS)
{
sprintf(outputBuffer, "Cannot get OpenCL platforms (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
if (1)
{
sprintf(outputBuffer, "Found %d OpenCL platform(s)", nr_platforms);
cout << outputBuffer << endl;
Log::info(outputBuffer);
}
platforms = (cl_platform_id *)malloc(nr_platforms * sizeof(*platforms));
if (!platforms)
{
sprintf(outputBuffer, "malloc: %s", strerror(errno));
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
status = clGetPlatformIDs(nr_platforms, platforms, NULL);
if (status != CL_SUCCESS)
{
sprintf(outputBuffer, "clGetPlatformIDs (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
i = nr_devs_total = 0;
while (i < nr_platforms)
{
if (scan_platform(platforms[i], &nr_devs_total, plat_id, dev_id))
break;
i++;
}
sprintf(outputBuffer, "Using GPU device ID %d", deviceToUse);
cout << outputBuffer << endl;
Log::error(outputBuffer);
amd_flag = is_platform_amd(*plat_id);
free(platforms);
}
#ifdef _WIN32
void load_file(const char *fname, char **dat, size_t *dat_len, int ignore_error)
{
struct stat st;
int fd;
ssize_t ret;
if (-1 == (fd = open(fname, O_RDONLY | O_BINARY))) {
if (ignore_error)
return;
//fatal("%s: %s\n", fname, strerror(errno));
sprintf(outputBuffer, "%s: %s", fname, strerror(errno));
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
if (fstat(fd, &st)) {
sprintf(outputBuffer, "fstat: %s: %s", fname, strerror(errno));
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
*dat_len = st.st_size;
if (!(*dat = (char *)malloc(*dat_len + 1))) {
sprintf(outputBuffer, "malloc: %s", strerror(errno));
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
ret = read(fd, *dat, *dat_len);
if (ret < 0) {
sprintf(outputBuffer, "read: %s: %s", fname, strerror(errno));
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
if ((size_t)ret != *dat_len) {
sprintf(outputBuffer, "%s: partial read", fname);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
if (close(fd)) {
sprintf(outputBuffer, "close: %s: %s", fname, strerror(errno));
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
(*dat)[*dat_len] = 0;
}
#endif
int main(int argc, char *argv[])
{
// Check for help argument (only -h)
for (int i = 1; i < argc; i++) {
char* argument = argv[i];
if (!strcmp(argument, "-h"))
{
printHelpAndExit();
}
}
if (argc % 2 != 1) {
sprintf(outputBuffer, "GPU miner must be provided valid argument pairs!");
cerr << outputBuffer << endl;
printHelpAndExit();
}
string hostAndPort = ""; // "94.130.64.18:8501";
string username = ""; // "VGX71bcRsEh4HZzhbA9Nj7GQNH5jGw";
string password = "";
if (argc > 1)
{
for (int i = 1; i < argc; i += 2)
{
char* argument = argv[i];
printf("%s\n", argument);
if (argument[0] == '-' && argument[1] == 'd')
{
if (strlen(argv[i + 1]) == 2) {
// device num >= 10
deviceToUse = (argv[i + 1][0] - 48) * 10 + (argv[i + 1][1] - 48);
}
else {
deviceToUse = argv[i + 1][0] - 48;
}
}
else if (!strcmp(argument, "-o"))
{
hostAndPort = string(argv[i + 1]);
}
else if (!strcmp(argument, "-u"))
{
username = string(argv[i + 1]);
}
else if (!strcmp(argument, "-p"))
{
password = string(argv[i + 1]);
}
else if (!strcmp(argument, "-tpb"))
{
threadsPerBlock = stoi(argv[i + 1]);
}
else if (!strcmp(argument, "-bs"))
{
blocksize = stoi(argv[i + 1]);
}
else if (!strcmp(argument, "-l"))
{
// to lower case conversion
for (int j = 0; j < strlen(argv[i + 1]); j++)
{
argv[i + 1][j] = tolower(argv[i + 1][j]);
}
if (!strcmp(argv[i + 1], "true") || !strcmp(argv[i + 1], "t"))
{
Log::setEnabled(true);
}
else
{
Log::setEnabled(false);
}
}
else if (!strcmp(argument, "-v"))
{
// to lower case conversion
for (int j = 0; j < strlen(argv[i + 1]); j++)
{
argv[i + 1][j] = tolower(argv[i + 1][j]);
}
if (!strcmp(argv[i + 1], "true") || !strcmp(argv[i + 1], "t"))
{
verboseOutput = true;
}
else
{
verboseOutput = false;
}
}
}
}
else {
printHelpAndExit();
}
if (HIGH_RESOURCE) {
sprintf(outputBuffer, "Resource Utilization: HIGH");
cerr << outputBuffer << endl;
Log::info(outputBuffer);
}
else {
sprintf(outputBuffer, "Resource Utilization: LOW");
cerr << outputBuffer << endl;
Log::info(outputBuffer);
}
#ifdef _WIN32
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
#else
#endif
if (hostAndPort.compare("") == 0) {
string error = "You must specify a host in the command line arguments! Example: \n-o 127.0.0.1:8501 or localhost:8501";
cerr << error << endl;
Log::error(error);
promptExit(-1);
}
if (username.compare("") == 0) {
string error = "You must specify a username in the command line arguments! Example: \n-u V5bLSbCqj9VzQR3MNANqL13YC2tUep";
cerr << error << endl;
Log::error(error);
promptExit(-1);
}
string host = hostAndPort.substr(0, hostAndPort.find(":"));
//GetHostByName
net_init();
host = net_dns_resolve(host.c_str());
net_deinit();
string portString = hostAndPort.substr(hostAndPort.find(":") + 1);
// Ensure that port is numeric
if (portString.find_first_not_of("1234567890") != string::npos) {
string error = "You must specify a host in the command line arguments! Example: \n-o 127.0.0.1:8501 or localhost:8501";
cerr << error << endl;
Log::error(error);
promptExit(-1);
}
int port = stoi(portString);
sprintf(outputBuffer, "Attempting to mine to pool %s:%d with username %s and password %s...", host.c_str(), port, username.c_str(), password.c_str());
cout << outputBuffer << endl;
Log::info(outputBuffer);
UCPClient ucpClient(host, port, username, password);
byte target[24];
ucpClient.copyMiningTarget(target);
sprintf(outputBuffer, "Using Device: %d\n\n", deviceToUse);
cout << outputBuffer << endl;
Log::info(outputBuffer);
int version, ret;
////////////////////////////////////////////
cl_platform_id plat_id = 0;
cl_device_id dev_id = 0;
cl_kernel k_vblake;
cl_int status;
scan_platforms(&plat_id, &dev_id);
if (!plat_id || !dev_id) {
sprintf(outputBuffer, "Selected device (ID %d) not found", deviceToUse);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
/* Create context.*/
cl_context context = clCreateContext(NULL, 1, &dev_id,
NULL, NULL, &status);
if (status != CL_SUCCESS || !context) {
sprintf(outputBuffer, "clCreateContext (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
/* Creating command queue associate with the context.*/
cl_command_queue queue = clCreateCommandQueue(context, dev_id,
0, &status);
if (status != CL_SUCCESS || !queue) {
sprintf(outputBuffer, "clCreateCommandQueue (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
/* Create program object */
#ifdef WIN32
load_file("input.cl", (char **)&source, &source_len, 0);
load_file("input.bin", &binary, &binary_len, 1);
#else
source = ocl_code;
#endif
source_len = strlen(source);
cl_program program;
program = clCreateProgramWithSource(context, 1, (const char **)&source,
&source_len, &status);
if (status != CL_SUCCESS || !program) {
sprintf(outputBuffer, "clCreateProgramWithSource (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
/* Build program. */
sprintf(outputBuffer, "Building program");
cout << outputBuffer << endl;
Log::info(outputBuffer);
status = clBuildProgram(program, 1, &dev_id,
(amd_flag) ? ("-I .. -I .") : ("-I .. -I ."), // compile options
NULL, NULL);
if (status != CL_SUCCESS) {
sprintf(outputBuffer, "OpenCL build failed (%d). Build log follows:", status);
get_program_build_log(program, dev_id);
cout << outputBuffer << endl;
Log::error(outputBuffer);
promptExit(-1);
}
get_program_bins(program);
// Create kernel objects
k_vblake = clCreateKernel(program, "kernel_vblake", &status);
if (status != CL_SUCCESS || !k_vblake) {
sprintf(outputBuffer, "clCreateKernel (%d)", status);
cout << outputBuffer << endl;
Log::error(outputBuffer);
get_program_build_log(program, dev_id);
promptExit(-1);
}
/////////////////////////////////
// Print out information about all available CUDA devices on system
sprintf(outputBuffer, "Mining on device #%d...", deviceToUse);
cout << outputBuffer << endl;
Log::info(outputBuffer);
// Run initialization of device before beginning timer
uint64_t* header = getWork(ucpClient, (uint32_t)std::time(0));
unsigned long long startTime = std::time(0);
uint32_t nonceResult[1] = { 0 };
uint64_t hashStart[1] = { 0 };
size_t global_ws;
unsigned long long hashes = 0;
size_t local_work_size = (unsigned int)threadsPerBlock;
uint64_t *phashstartout, *pheaderin;
uint32_t *pnoncestart, *pnonceout;
cl_mem pnoncestart_d, pnonceout_d, phashstartout_d, pheaderin_d;
//cudaError_t cudaStatus; replaced with status
pnoncestart = (uint32_t*)malloc(sizeof(uint32_t) * 2);
pnonceout = (uint32_t *)malloc(sizeof(uint32_t) * 2);
phashstartout = (uint64_t *)malloc(sizeof(uint64_t) * 2);
pheaderin = (uint64_t *)malloc(sizeof(uint64_t) * 8);
pnoncestart_d = check_clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(uint32_t) * 1, NULL);
pnonceout_d = check_clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(uint32_t) * 1, NULL);
phashstartout_d = check_clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(uint64_t) * 1, NULL);
pheaderin_d = check_clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(uint64_t) * 8, NULL);
uint32_t count = 0;
int numLines = 0;
// Mining loop
while (true) {
vprintf("top of mining loop\n");
count++;
long timestamp = (long)std::time(0);
delete[] header;
vprintf("Getting work...\n");
header = getWork(ucpClient, timestamp);
vprintf("Getting job id...\n");
int jobId = ucpClient.getJobId();
count++;
vprintf("Running kernel...\n");
for (int i = 0; i<8; i++)
pheaderin[i] = header[i];
pnonceout[0] = 0;
clEnqueueWriteBuffer(queue, pnonceout_d, CL_TRUE, 0, sizeof(uint32_t) * 1, pnonceout, 0, NULL, NULL);
phashstartout[0] = 0;
clEnqueueWriteBuffer(queue, pheaderin_d, CL_TRUE, 0, sizeof(uint64_t) * 8, pheaderin, 0, NULL, NULL);
uint32_t nonceStart = (uint64_t)lastNonceStart + (blocksize * threadsPerBlock * 128);
lastNonceStart = nonceStart;
pnoncestart[0] = nonceStart;
clEnqueueWriteBuffer(queue, pnoncestart_d, CL_TRUE, 0, sizeof(uint32_t) * 1, pnoncestart, 0, NULL, NULL);
clEnqueueWriteBuffer(queue, phashstartout_d, CL_TRUE, 0, sizeof(uint64_t) * 1, phashstartout, 0, NULL, NULL);
check_clSetKernelArg(k_vblake, 0, &pnoncestart_d);
check_clSetKernelArg(k_vblake, 1, &pnonceout_d);
check_clSetKernelArg(k_vblake, 2, &phashstartout_d);
check_clSetKernelArg(k_vblake, 3, &pheaderin_d);
global_ws = (unsigned int)(blocksize * threadsPerBlock * 128);
//start Opencl kernel
check_clEnqueueNDRangeKernel(queue, k_vblake, 1, NULL,
&global_ws, &local_work_size, 0, NULL, NULL);
clFinish(queue);
check_clEnqueueReadBuffer(queue, pnonceout_d,
CL_TRUE, // cl_bool blocking_read
0, // size_t offset
sizeof(uint32_t) * 1, // size_t size
pnonceout, // void *ptr
0, // cl_uint num_events_in_wait_list
NULL, // cl_event *event_wait_list
NULL); // cl_event *event
check_clEnqueueReadBuffer(queue, phashstartout_d,
CL_TRUE, // cl_bool blocking_read
0, // size_t offset
sizeof(uint64_t) * 1, // size_t size
phashstartout, // void *ptr
0, // cl_uint num_events_in_wait_list
NULL, // cl_event *event_wait_list
NULL); // cl_event *event
// cl_int openclStatus = grindNonces(nonceResult, hashStart, header, k_vblake, queue, context, dev_id, program);
nonceResult[0] = pnonceout[0];
hashStart[0] = phashstartout[0];
unsigned long long totalTime = std::time(0) - startTime;
hashes += (threadsPerBlock * blocksize * 128);
double hashSpeed = (double)hashes;
hashSpeed /= (totalTime * 1024 * 1024);
if (count % 10 == 0) {
int validShares = ucpClient.getValidShares();
int invalidShares = ucpClient.getInvalidShares();
int totalAccountedForShares = invalidShares + validShares;
int totalSubmittedShares = ucpClient.getSentShares();
int unaccountedForShares = totalSubmittedShares - totalAccountedForShares;
double percentage = ((double)validShares) / totalAccountedForShares;
percentage *= 100;
// printf("[GPU #%d (%s)] : %f MH/second valid shares: %d/%d/%d (%.3f%%)\n", deviceToUse, selectedDeviceName.c_str(), hashSpeed, validShares, totalAccountedForShares, totalSubmittedShares, percentage);
printf("[GPU #%d (%s)] : %0.2f MH/s shares: %d/%d/%d (%.3f%%)\n", deviceToUse, selectedDeviceName.c_str(), hashSpeed, validShares, totalAccountedForShares, totalSubmittedShares, percentage);
}
if (nonceResult[0] != 0x01000000 && nonceResult[0] != 0) {
uint32_t nonce = *nonceResult;
nonce = (((nonce & 0xFF000000) >> 24) | ((nonce & 0x00FF0000) >> 8) | ((nonce & 0x0000FF00) << 8) | ((nonce & 0x000000FF) << 24));
ucpClient.submitWork(jobId, timestamp, nonce);
nonceResult[0] = 0;
char line[100];
// Hash coming from GPU is reversed
uint64_t hashFlipped = 0;
hashFlipped |= (hashStart[0] & 0x00000000000000FF) << 56;
hashFlipped |= (hashStart[0] & 0x000000000000FF00) << 40;
hashFlipped |= (hashStart[0] & 0x0000000000FF0000) << 24;
hashFlipped |= (hashStart[0] & 0x00000000FF000000) << 8;
hashFlipped |= (hashStart[0] & 0x000000FF00000000) >> 8;
hashFlipped |= (hashStart[0] & 0x0000FF0000000000) >> 24;
hashFlipped |= (hashStart[0] & 0x00FF000000000000) >> 40;
hashFlipped |= (hashStart[0] & 0xFF00000000000000) >> 56;
#if CPU_SHARES
sprintf(line, "\t Share Found @ 2^24! {%#018llx} [nonce: %#08lx]", hashFlipped, nonce);
#else
sprintf(line, "\t Share Found @ 2^32! {%#018llx} [nonce: %#08lx]", hashFlipped, nonce);
#endif
cout << line << endl;
vprintf("Logging\n");
Log::info(line);
vprintf("Done logging\n");
vprintf("Made line\n");
numLines++;
// Uncomment these lines to get access to this data for display purposes
/*
long long extraNonce = ucpClient.getStartExtraNonce();
int jobId = ucpClient.getJobId();
int encodedDifficulty = ucpClient.getEncodedDifficulty();
string previousBlockHashHex = ucpClient.getPreviousBlockHash();
string merkleRoot = ucpClient.getMerkleRoot();
*/
}
vprintf("About to restart loop...\n");