Refactor miner_thread to be common code for any kind of device This expands on the device API, by taking the actual main thread out of the device's code, and calling the API only for specific tasks. This brings various changes that were made to the GPU thread code into the CPU miner. It also fixes a bug where shares found on old GPU work were discarded when it switched to a new work.
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
diff --git a/main.c b/main.c
index 31c25ef..bfd4c15 100644
--- a/main.c
+++ b/main.c
@@ -4323,230 +4323,149 @@ bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
return submit_work_sync(thr, work);
}
+static inline bool abandon_work(int thr_id, struct work *work, struct timeval *wdiff, uint64_t hashes)
+{
+ if (wdiff->tv_sec > opt_scantime ||
+ work->blk.nonce >= MAXTHREADS - hashes ||
+ stale_work(work, false))
+ return true;
+ return false;
+}
+
static void *miner_thread(void *userdata)
{
- struct work *work = make_work();
struct thr_info *mythr = userdata;
const int thr_id = mythr->id;
- uint32_t max_nonce = 0xffffff, total_hashes = 0;
- unsigned long hashes_done = max_nonce;
- bool needs_work = true;
+ struct cgpu_info *cgpu = mythr->cgpu;
+ struct device_api *api = cgpu->api;
+
/* Try to cycle approximately 5 times before each log update */
- const unsigned long cycle = opt_log_interval / 5 ? : 1;
+ const unsigned long def_cycle = opt_log_interval / 5 ? : 1;
+ unsigned long cycle;
+ struct timeval tv_start, tv_end, tv_workstart, tv_lastupdate;
+ struct timeval diff, sdiff, wdiff;
+ uint32_t max_nonce = api->can_limit_work ? api->can_limit_work(mythr) : 0xffffffff;
+ uint32_t hashes_done = 0;
+ uint32_t hashes;
+ struct work *work = make_work();
unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
+ unsigned const long request_nonce = MAXTHREADS / 3 * 2;
bool requested = false;
- uint32_t nonce_inc = max_nonce, hash_div = 1;
- double hash_divfloat = 1.0;
-
+ uint32_t hash_div = 1;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
- /* Set worker threads to nice 19 and then preferentially to SCHED_IDLE
- * and if that fails, then SCHED_BATCH. No need for this to be an
- * error if it fails */
- setpriority(PRIO_PROCESS, 0, 19);
- drop_policy();
+ if (api->thread_init && !api->thread_init(mythr))
+ goto out;
- /* Cpu affinity only makes sense if the number of threads is a multiple
- * of the number of CPUs */
- if (!(opt_n_threads % num_processors))
- affine_to_cpu(dev_from_id(thr_id), dev_from_id(thr_id) % num_processors);
+ if (opt_debug)
+ applog(LOG_DEBUG, "Popping ping in miner thread");
+ tq_pop(mythr->q, NULL); /* Wait for a ping to start */
- /* Invalidate pool so it fails can_roll() test */
- work->pool = NULL;
+ sdiff.tv_sec = sdiff.tv_usec = 0;
+ gettimeofday(&tv_lastupdate, NULL);
while (1) {
- struct timeval tv_workstart, tv_start, tv_end, diff;
- uint64_t max64;
- bool rc;
-
- if (needs_work) {
- gettimeofday(&tv_workstart, NULL);
- /* obtain new work from internal workio thread */
- if (unlikely(!get_work(work, requested, mythr, thr_id, hash_div))) {
- applog(LOG_ERR, "work retrieval failed, exiting "
- "mining thread %d", thr_id);
- goto out;
- }
- needs_work = requested = false;
- total_hashes = 0;
- max_nonce = work->blk.nonce + hashes_done;
+ work_restart[thr_id].restart = 0;
+ if (api->free_work && likely(work->pool))
+ api->free_work(mythr, work);
+ if (unlikely(!get_work(work, requested, mythr, thr_id, hash_div))) {
+ applog(LOG_ERR, "work retrieval failed, exiting "
+ "mining thread %d", thr_id);
+ break;
}
- hashes_done = 0;
- gettimeofday(&tv_start, NULL);
-
- /* scan nonces for a proof-of-work hash */
- switch (opt_algo) {
- case ALGO_C:
- rc = scanhash_c(thr_id, work->midstate, work->data + 64,
- work->hash1, work->hash, work->target,
- max_nonce, &hashes_done,
- work->blk.nonce);
+ requested = false;
+ cycle = (can_roll(work) && should_roll(work)) ? 1 : def_cycle;
+ gettimeofday(&tv_workstart, NULL);
+ work->blk.nonce = 0;
+ if (api->prepare_work && !api->prepare_work(mythr, work)) {
+ applog(LOG_ERR, "work prepare failed, exiting "
+ "mining thread %d", thr_id);
break;
+ }
-#ifdef WANT_X8632_SSE2
- case ALGO_SSE2_32: {
- unsigned int rc5 =
- scanhash_sse2_32(thr_id, work->midstate, work->data + 64,
- work->hash1, work->hash,
- work->target,
- max_nonce, &hashes_done,
- work->blk.nonce);
- rc = (rc5 == -1) ? false : true;
- }
- break;
-#endif
+ do {
+ gettimeofday(&tv_start, NULL);
-#ifdef WANT_X8664_SSE2
- case ALGO_SSE2_64: {
- unsigned int rc5 =
- scanhash_sse2_64(thr_id, work->midstate, work->data + 64,
- work->hash1, work->hash,
- work->target,
- max_nonce, &hashes_done,
- work->blk.nonce);
- rc = (rc5 == -1) ? false : true;
+ hashes = api->scanhash(mythr, work, work->blk.nonce + max_nonce);
+ if (unlikely(work_restart[thr_id].restart))
+ break;
+ if (unlikely(!hashes))
+ goto out;
+ hashes_done += hashes;
+
+ gettimeofday(&tv_end, NULL);
+ timeval_subtract(&diff, &tv_end, &tv_start);
+ sdiff.tv_sec += diff.tv_sec;
+ sdiff.tv_usec += diff.tv_usec;
+ if (sdiff.tv_usec > 1000000) {
+ ++sdiff.tv_sec;
+ sdiff.tv_usec -= 1000000;
}
- break;
-#endif
-#ifdef WANT_X8664_SSE4
- case ALGO_SSE4_64: {
- unsigned int rc5 =
- scanhash_sse4_64(thr_id, work->midstate, work->data + 64,
- work->hash1, work->hash,
- work->target,
- max_nonce, &hashes_done,
- work->blk.nonce);
- rc = (rc5 == -1) ? false : true;
- }
- break;
+ timeval_subtract(&wdiff, &tv_end, &tv_workstart);
+ if (!requested) {
+#if 0
+ if (wdiff.tv_sec > request_interval)
+ hash_div = (MAXTHREADS / total_hashes) ? : 1;
#endif
-
-#ifdef WANT_SSE2_4WAY
- case ALGO_4WAY: {
- unsigned int rc4 =
- ScanHash_4WaySSE2(thr_id, work->midstate, work->data + 64,
- work->hash1, work->hash,
- work->target,
- max_nonce, &hashes_done,
- work->blk.nonce);
- rc = (rc4 == -1) ? false : true;
+ if (wdiff.tv_sec > request_interval || work->blk.nonce > request_nonce) {
+ thread_reportout(mythr);
+ if (unlikely(!queue_request(mythr, false))) {
+ applog(LOG_ERR, "Failed to queue_request in miner_thread %d", thr_id);
+ goto out;
+ }
+ thread_reportin(mythr);
+ requested = true;
+ }
}
- break;
-#endif
-
-#ifdef WANT_ALTIVEC_4WAY
- case ALGO_ALTIVEC_4WAY:
- {
- unsigned int rc4 = ScanHash_altivec_4way(thr_id, work->midstate, work->data + 64,
- work->hash1, work->hash,
- work->target,
- max_nonce, &hashes_done,
- work->blk.nonce);
- rc = (rc4 == -1) ? false : true;
- }
- break;
-#endif
-#ifdef WANT_VIA_PADLOCK
- case ALGO_VIA:
- rc = scanhash_via(thr_id, work->data, work->target,
- max_nonce, &hashes_done,
- work->blk.nonce);
- break;
-#endif
- case ALGO_CRYPTOPP:
- rc = scanhash_cryptopp(thr_id, work->midstate, work->data + 64,
- work->hash1, work->hash, work->target,
- max_nonce, &hashes_done,
- work->blk.nonce);
- break;
-
-#ifdef WANT_CRYPTOPP_ASM32
- case ALGO_CRYPTOPP_ASM32:
- rc = scanhash_asm32(thr_id, work->midstate, work->data + 64,
- work->hash1, work->hash, work->target,
- max_nonce, &hashes_done,
- work->blk.nonce);
- break;
-#endif
+ if (sdiff.tv_sec < cycle) {
+ if (likely(!api->can_limit_work || max_nonce == 0xffffffff))
+ continue;
- default:
- /* should never happen */
- goto out;
- }
+ {
+ int mult = 1000000 / ((sdiff.tv_usec + 0x400) / 0x400) + 0x10;
+ mult *= cycle;
+ if (max_nonce > (0xffffffff * 0x400) / mult)
+ max_nonce = 0xffffffff;
+ else
+ max_nonce = (max_nonce * mult) / 0x400;
+ }
+ } else if (unlikely(sdiff.tv_sec > cycle) && api->can_limit_work) {
+ max_nonce = max_nonce * cycle / sdiff.tv_sec;
+ } else if (unlikely(sdiff.tv_usec > 100000) && api->can_limit_work) {
+ max_nonce = max_nonce * 0x400 / (((cycle * 1000000) + sdiff.tv_usec) / (cycle * 1000000 / 0x400));
+ }
- /* record scanhash elapsed time */
- gettimeofday(&tv_end, NULL);
- timeval_subtract(&diff, &tv_end, &tv_start);
-
- hashes_done -= work->blk.nonce;
- hashmeter(thr_id, &diff, hashes_done);
- total_hashes += hashes_done;
- work->blk.nonce += hashes_done;
-
- /* adjust max_nonce to meet target cycle time */
- if (diff.tv_usec > 500000)
- diff.tv_sec++;
- if (diff.tv_sec && diff.tv_sec != cycle) {
- uint64_t next_inc = ((uint64_t)hashes_done * (uint64_t)cycle) / (uint64_t)diff.tv_sec;
-
- if (next_inc > (uint64_t)nonce_inc / 2 * 3)
- next_inc = nonce_inc / 2 * 3;
- nonce_inc = next_inc;
- } else if (!diff.tv_sec)
- nonce_inc = hashes_done * 2;
- if (nonce_inc < 4)
- nonce_inc = 0xffffff;
- max64 = work->blk.nonce + nonce_inc;
- if (max64 > 0xfffffffaULL)
- max64 = 0xfffffffaULL;
- max_nonce = max64;
-
- /* if nonce found, submit work */
- if (unlikely(rc)) {
- if (opt_debug)
- applog(LOG_DEBUG, "CPU %d found something?", dev_from_id(thr_id));
- if (unlikely(!submit_work_sync(mythr, work))) {
- applog(LOG_ERR, "Failed to submit_work_sync in miner_thread %d", thr_id);
- break;
+ timeval_subtract(&diff, &tv_end, &tv_lastupdate);
+ if (diff.tv_sec >= opt_log_interval) {
+ hashmeter(thr_id, &diff, hashes_done);
+ hashes_done = 0;
+ tv_lastupdate = tv_end;
}
- work->blk.nonce += 4;
- }
- timeval_subtract(&diff, &tv_end, &tv_workstart);
- if (!requested && (diff.tv_sec >= request_interval)) {
- thread_reportout(mythr);
- if (unlikely(!queue_request(mythr, false))) {
- applog(LOG_ERR, "Failed to queue_request in miner_thread %d", thr_id);
- goto out;
+ if (unlikely(mythr->pause || !cgpu->enabled)) {
+ applog(LOG_WARNING, "Thread %d being disabled", thr_id);
+ mythr->rolling = mythr->cgpu->rolling = 0;
+ if (opt_debug)
+ applog(LOG_DEBUG, "Popping wakeup ping in miner thread");
+ thread_reportout(mythr);
+ tq_pop(mythr->q, NULL); /* Ignore ping that's popped */
+ thread_reportin(mythr);
+ applog(LOG_WARNING, "Thread %d being re-enabled", thr_id);
}
- thread_reportin(mythr);
- requested = true;
- }
- if (diff.tv_sec > opt_scantime) {
- decay_time(&hash_divfloat , (double)((MAXTHREADS / total_hashes) ? : 1));
- hash_div = hash_divfloat;
- needs_work = true;
- } else if (work_restart[thr_id].restart || stale_work(work, false) ||
- work->blk.nonce >= MAXTHREADS - hashes_done)
- needs_work = true;
-
- if (unlikely(mythr->pause)) {
- applog(LOG_WARNING, "Thread %d being disabled", thr_id);
- mythr->rolling = mythr->cgpu->rolling = 0;
- if (opt_debug)
- applog(LOG_DEBUG, "Popping wakeup ping in miner thread");
+ sdiff.tv_sec = sdiff.tv_usec = 0;
- thread_reportout(mythr);
- tq_pop(mythr->q, NULL); /* Ignore ping that's popped */
- thread_reportin(mythr);
- applog(LOG_WARNING, "Thread %d being re-enabled", thr_id);
- }
+ if (can_roll(work) && should_roll(work))
+ roll_work(work);
+ } while (!abandon_work(thr_id, work, &wdiff, hashes));
}
out:
+ if (api->thread_shutdown)
+ api->thread_shutdown(mythr);
+
thread_reportin(mythr);
applog(LOG_ERR, "Thread %d failure, exiting", thr_id);
tq_freeze(mythr->q);
@@ -4654,224 +4573,6 @@ static void set_threads_hashes(unsigned int vectors, unsigned int *threads,
*globalThreads = *threads;
*hashes = *threads * vectors;
}
-
-static void *gpuminer_thread(void *userdata)
-{
- cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *);
-
- const unsigned long cycle = opt_log_interval / 5 ? : 1;
- struct timeval tv_start, tv_end, diff, tv_workstart;
- struct thr_info *mythr = userdata;
- const int thr_id = mythr->id;
- uint32_t *res, *blank_res;
- double gpu_ms_average = 7;
- int gpu = dev_from_id(thr_id);
-
- size_t globalThreads[1];
- size_t localThreads[1];
-
- cl_int status;
-
- _clState *clState = clStates[thr_id];
- const cl_kernel *kernel = &clState->kernel;
-
- struct work *work = make_work();
- unsigned int threads;
- unsigned const int vectors = clState->preferred_vwidth;
- unsigned int hashes;
- unsigned int hashes_done = 0;
-
- /* Request the next work item at 2/3 of the scantime */
- unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
- unsigned const long request_nonce = MAXTHREADS / 3 * 2;
- bool requested = false;
- uint32_t total_hashes = 0, hash_div = 1;
-
- switch (chosen_kernel) {
- case KL_POCLBM:
- queue_kernel_parameters = &queue_poclbm_kernel;
- break;
- case KL_PHATK:
- default:
- queue_kernel_parameters = &queue_phatk_kernel;
- break;
- }
-
- pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
-
- res = calloc(BUFFERSIZE, 1);
- blank_res = calloc(BUFFERSIZE, 1);
-
- if (!res || !blank_res) {
- applog(LOG_ERR, "Failed to calloc in gpuminer_thread");
- goto out;
- }
-
- gettimeofday(&tv_start, NULL);
- localThreads[0] = clState->work_size;
- set_threads_hashes(vectors, &threads, &hashes, &globalThreads[0],
- localThreads[0], gpus[gpu].intensity);
-
- diff.tv_sec = 0;
- gettimeofday(&tv_end, NULL);
-
- work->pool = NULL;
-
- status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
- BUFFERSIZE, blank_res, 0, NULL, NULL);
- if (unlikely(status != CL_SUCCESS))
- { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
-
- mythr->cgpu->status = LIFE_WELL;
- if (opt_debug)
- applog(LOG_DEBUG, "Popping ping in gpuminer thread");
-
- tq_pop(mythr->q, NULL); /* Wait for a ping to start */
- gettimeofday(&tv_workstart, NULL);
- /* obtain new work from internal workio thread */
- if (unlikely(!get_work(work, requested, mythr, thr_id, hash_div))) {
- applog(LOG_ERR, "work retrieval failed, exiting "
- "gpu mining thread %d", thr_id);
- goto out;
- }
- requested = false;
- precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
- work->blk.nonce = 0;
-
- while (1) {
- struct timeval tv_gpustart, tv_gpuend;
- suseconds_t gpu_us;
-
- gettimeofday(&tv_gpustart, NULL);
- timeval_subtract(&diff, &tv_gpustart, &tv_gpuend);
- /* This finish flushes the readbuffer set with CL_FALSE later */
- clFinish(clState->commandQueue);
- gettimeofday(&tv_gpuend, NULL);
- timeval_subtract(&diff, &tv_gpuend, &tv_gpustart);
- gpu_us = diff.tv_sec * 1000000 + diff.tv_usec;
- decay_time(&gpu_ms_average, gpu_us / 1000);
- if (gpus[gpu].dynamic) {
- /* Try to not let the GPU be out for longer than 6ms, but
- * increase intensity when the system is idle, unless
- * dynamic is disabled. */
- if (gpu_ms_average > 7) {
- if (gpus[gpu].intensity > -10)
- gpus[gpu].intensity--;
- } else if (gpu_ms_average < 3) {
- if (gpus[gpu].intensity < 10)
- gpus[gpu].intensity++;
- }
- }
- set_threads_hashes(vectors, &threads, &hashes, globalThreads,
- localThreads[0], gpus[gpu].intensity);
-
- if (diff.tv_sec > opt_scantime ||
- work->blk.nonce >= MAXTHREADS - hashes ||
- work_restart[thr_id].restart ||
- stale_work(work, false)) {
- /* Ignore any reads since we're getting new work and queue a clean buffer */
- status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
- BUFFERSIZE, blank_res, 0, NULL, NULL);
- if (unlikely(status != CL_SUCCESS))
- { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
- memset(res, 0, BUFFERSIZE);
-
- gettimeofday(&tv_workstart, NULL);
- if (opt_debug)
- applog(LOG_DEBUG, "getwork thread %d", thr_id);
- /* obtain new work from internal workio thread */
- if (unlikely(!get_work(work, requested, mythr, thr_id, hash_div))) {
- applog(LOG_ERR, "work retrieval failed, exiting "
- "gpu mining thread %d", thr_id);
- goto out;
- }
- requested = false;
-
- precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
- work_restart[thr_id].restart = 0;
-
- /* Flushes the writebuffer set with CL_FALSE above */
- clFinish(clState->commandQueue);
- }
- status = queue_kernel_parameters(clState, &work->blk);
- if (unlikely(status != CL_SUCCESS))
- { applog(LOG_ERR, "Error: clSetKernelArg of all params failed."); goto out; }
-
- /* MAXBUFFERS entry is used as a flag to say nonces exist */
- if (res[FOUND]) {
- /* Clear the buffer again */
- status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
- BUFFERSIZE, blank_res, 0, NULL, NULL);
- if (unlikely(status != CL_SUCCESS))
- { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
- if (opt_debug)
- applog(LOG_DEBUG, "GPU %d found something?", gpu);
- postcalc_hash_async(mythr, work, res);
- memset(res, 0, BUFFERSIZE);
- clFinish(clState->commandQueue);
- }
-
- status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
- globalThreads, localThreads, 0, NULL, NULL);
- if (unlikely(status != CL_SUCCESS))
- { applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)"); goto out; }
-
- status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
- BUFFERSIZE, res, 0, NULL, NULL);
- if (unlikely(status != CL_SUCCESS))
- { applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)"); goto out;}
-
- gettimeofday(&tv_end, NULL);
- timeval_subtract(&diff, &tv_end, &tv_start);
- hashes_done += hashes;
- total_hashes += hashes;
- work->blk.nonce += hashes;
- if (diff.tv_sec >= cycle) {
- hashmeter(thr_id, &diff, hashes_done);
- gettimeofday(&tv_start, NULL);
- hashes_done = 0;
- }
-
- timeval_subtract(&diff, &tv_end, &tv_workstart);
- if (!requested) {
-#if 0
- if (diff.tv_sec > request_interval)
- hash_div = (MAXTHREADS / total_hashes) ? : 1;
-#endif
- if (diff.tv_sec > request_interval || work->blk.nonce > request_nonce) {
- thread_reportout(mythr);
- if (unlikely(!queue_request(mythr, false))) {
- applog(LOG_ERR, "Failed to queue_request in gpuminer_thread %d", thr_id);
- goto out;
- }
- thread_reportin(mythr);
- requested = true;
- }
- }
- if (unlikely(!gpus[gpu].enabled || mythr->pause)) {
- applog(LOG_WARNING, "Thread %d being disabled", thr_id);
- mythr->rolling = mythr->cgpu->rolling = 0;
- if (opt_debug)
- applog(LOG_DEBUG, "Popping wakeup ping in gpuminer thread");
-
- thread_reportout(mythr);
- tq_pop(mythr->q, NULL); /* Ignore ping that's popped */
- thread_reportin(mythr);
- applog(LOG_WARNING, "Thread %d being re-enabled", thr_id);
- }
- }
-out:
- clReleaseCommandQueue(clState->commandQueue);
- clReleaseKernel(clState->kernel);
- clReleaseProgram(clState->program);
- clReleaseContext(clState->context);
-
- thread_reportin(mythr);
- applog(LOG_ERR, "Thread %d failure, exiting", thr_id);
- tq_freeze(mythr->q);
-
- return NULL;
-}
#endif /* HAVE_OPENCL */
/* Stage another work item from the work returned in a longpoll */
@@ -5153,7 +4854,7 @@ select_cgpu:
}
applog(LOG_INFO, "initCl() finished. Found %s", name);
- if (unlikely(thr_info_create(thr, NULL, gpuminer_thread, thr))) {
+ if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
applog(LOG_ERR, "thread %d create failed", thr_id);
return NULL;
}
@@ -5732,19 +5433,157 @@ static void reinit_cpu_device(struct cgpu_info *cpu)
tq_push(thr_info[cpur_thr_id].q, cpu);
}
-static void cpu_thread_start(struct thr_info *thr)
+static bool cpu_thread_prepare(struct thr_info *thr)
{
thread_reportin(thr);
- if (unlikely(thr_info_create(thr, NULL, miner_thread, thr)))
- quit(1, "thread %d create failed", thr->id);
+ return true;
+}
+
+static uint64_t cpu_can_limit_work(struct thr_info *thr)
+{
+ return 0xfffff;
+}
+
+static bool cpu_thread_init(struct thr_info *thr)
+{
+ const int thr_id = thr->id;
+
+ /* Set worker threads to nice 19 and then preferentially to SCHED_IDLE
+ * and if that fails, then SCHED_BATCH. No need for this to be an
+ * error if it fails */
+ setpriority(PRIO_PROCESS, 0, 19);
+ drop_policy();
+ /* Cpu affinity only makes sense if the number of threads is a multiple
+ * of the number of CPUs */
+ if (!(opt_n_threads % num_processors))
+ affine_to_cpu(dev_from_id(thr_id), dev_from_id(thr_id) % num_processors);
+ return true;
+}
+
+static uint64_t cpu_scanhash(struct thr_info *thr, struct work *work, uint64_t max_nonce)
+{
+ const int thr_id = thr->id;
+
+ long unsigned int hashes_done = 0;
+ uint32_t first_nonce = work->blk.nonce;
+ bool rc = false;
+
+ /* scan nonces for a proof-of-work hash */
+ switch (opt_algo) {
+ case ALGO_C:
+ rc = scanhash_c(thr_id, work->midstate, work->data + 64,
+ work->hash1, work->hash, work->target,
+ max_nonce, &hashes_done,
+ work->blk.nonce);
+ break;
+#ifdef WANT_X8632_SSE2
+ case ALGO_SSE2_32: {
+ unsigned int rc5 =
+ scanhash_sse2_32(thr_id, work->midstate, work->data + 64,
+ work->hash1, work->hash,
+ work->target,
+ max_nonce, &hashes_done,
+ work->blk.nonce);
+ rc = (rc5 == -1) ? false : true;
+ }
+ break;
+#endif
+#ifdef WANT_X8664_SSE2
+ case ALGO_SSE2_64: {
+ unsigned int rc5 =
+ scanhash_sse2_64(thr_id, work->midstate, work->data + 64,
+ work->hash1, work->hash,
+ work->target,
+ max_nonce, &hashes_done,
+ work->blk.nonce);
+ rc = (rc5 == -1) ? false : true;
+ }
+ break;
+#endif
+#ifdef WANT_X8664_SSE4
+ case ALGO_SSE4_64: {
+ unsigned int rc5 =
+ scanhash_sse4_64(thr_id, work->midstate, work->data + 64,
+ work->hash1, work->hash,
+ work->target,
+ max_nonce, &hashes_done,
+ work->blk.nonce);
+ rc = (rc5 == -1) ? false : true;
+ }
+ break;
+#endif
+#ifdef WANT_SSE2_4WAY
+ case ALGO_4WAY: {
+ unsigned int rc4 =
+ ScanHash_4WaySSE2(thr_id, work->midstate, work->data + 64,
+ work->hash1, work->hash,
+ work->target,
+ max_nonce, &hashes_done,
+ work->blk.nonce);
+ rc = (rc4 == -1) ? false : true;
+ }
+ break;
+#endif
+#ifdef WANT_ALTIVEC_4WAY
+ case ALGO_ALTIVEC_4WAY:
+ {
+ unsigned int rc4 = ScanHash_altivec_4way(thr_id, work->midstate, work->data + 64,
+ work->hash1, work->hash,
+ work->target,
+ max_nonce, &hashes_done,
+ work->blk.nonce);
+ rc = (rc4 == -1) ? false : true;
+ }
+ break;
+#endif
+#ifdef WANT_VIA_PADLOCK
+ case ALGO_VIA:
+ rc = scanhash_via(thr_id, work->data, work->target,
+ max_nonce, &hashes_done,
+ work->blk.nonce);
+ break;
+#endif
+ case ALGO_CRYPTOPP:
+ rc = scanhash_cryptopp(thr_id, work->midstate, work->data + 64,
+ work->hash1, work->hash, work->target,
+ max_nonce, &hashes_done,
+ work->blk.nonce);
+ break;
+#ifdef WANT_CRYPTOPP_ASM32
+ case ALGO_CRYPTOPP_ASM32:
+ rc = scanhash_asm32(thr_id, work->midstate, work->data + 64,
+ work->hash1, work->hash, work->target,
+ max_nonce, &hashes_done,
+ work->blk.nonce);
+ break;
+#endif
+ default:
+ /* should never happen */
+ applog(LOG_ERR, "Unrecognized hash algorithm! This should be impossible!");
+ }
+
+ /* if nonce found, submit work */
+ if (unlikely(rc)) {
+ if (opt_debug)
+ applog(LOG_DEBUG, "CPU %d found something?", dev_from_id(thr_id));
+ if (unlikely(!submit_work_sync(thr, work))) {
+ applog(LOG_ERR, "Failed to submit_work_sync in miner_thread %d", thr_id);
+ }
+ }
+
+ work->blk.nonce = hashes_done;
+ return (uint64_t)hashes_done - first_nonce;
}
struct device_api cpu_api = {
.name = "CPU",
.api_detect = cpu_detect,
.reinit_device = reinit_cpu_device,
- .thread_start = cpu_thread_start,
+ .thread_prepare = cpu_thread_prepare,
+ .can_limit_work = cpu_can_limit_work,
+ .thread_init = cpu_thread_init,
+ .scanhash = cpu_scanhash,
};
@@ -5815,7 +5654,16 @@ static void get_opencl_statline(char *buf, struct cgpu_info *gpu)
#endif
}
-static void opencl_thread_start(struct thr_info *thr)
+struct opencl_thread_data {
+ cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *);
+ uint32_t *res;
+ struct work *last_work;
+ struct work _last_work;
+};
+
+static uint32_t *blank_res;
+
+static bool opencl_thread_prepare(struct thr_info *thr)
{
char name[256];
struct timeval now;
@@ -5824,13 +5672,11 @@ static void opencl_thread_start(struct thr_info *thr)
int i = thr->id;
static bool failmessage = false;
- /* Enable threads for devices set not to mine but disable
- * their queue in case we wish to enable them later*/
- if (cgpu->enabled) {
- if (opt_debug)
- applog(LOG_DEBUG, "Pushing ping to thread %d", thr->id);
-
- tq_push(thr->q, &ping);
+ if (!blank_res)
+ blank_res = calloc(BUFFERSIZE, 1);
+ if (!blank_res) {
+ applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
+ return false;
}
applog(LOG_INFO, "Init GPU thread %i", i);
@@ -5851,7 +5697,7 @@ static void opencl_thread_start(struct thr_info *thr)
}
cgpu->enabled = false;
cgpu->status = LIFE_NOSTART;
- return;
+ return false;
}
applog(LOG_INFO, "initCl() finished. Found %s", name);
gettimeofday(&now, NULL);
@@ -5859,8 +5705,174 @@ static void opencl_thread_start(struct thr_info *thr)
have_opencl = true;
- if (unlikely(thr_info_create(thr, NULL, gpuminer_thread, thr)))
- quit(1, "thread %d create failed", i);
+ return true;
+}
+
+static bool opencl_thread_init(struct thr_info *thr)
+{
+ const int thr_id = thr->id;
+ struct cgpu_info *gpu = thr->cgpu;
+
+ struct opencl_thread_data *thrdata;
+ thrdata = calloc(1, sizeof(*thrdata));
+ thr->cgpu_data = thrdata;
+
+ if (!thrdata) {
+ applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
+ return false;
+ }
+
+ switch (chosen_kernel) {
+ case KL_POCLBM:
+ thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
+ break;
+ case KL_PHATK:
+ default:
+ thrdata->queue_kernel_parameters = &queue_phatk_kernel;
+ break;
+ }
+
+ thrdata->res = calloc(BUFFERSIZE, 1);
+
+ if (!thrdata->res) {
+ free(thrdata);
+ applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
+ return false;
+ }
+
+ _clState *clState = clStates[thr_id];
+ cl_int status;
+
+ status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
+ BUFFERSIZE, blank_res, 0, NULL, NULL);
+ if (unlikely(status != CL_SUCCESS)) {
+ applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
+ return false;
+ }
+
+ gpu->status = LIFE_WELL;
+
+ return true;
+}
+
+static void opencl_free_work(struct thr_info *thr, struct work *work)
+{
+ const int thr_id = thr->id;
+ struct opencl_thread_data *thrdata = thr->cgpu_data;
+ _clState *clState = clStates[thr_id];
+
+ clFinish(clState->commandQueue);
+ if (thrdata->res[FOUND]) {
+ thrdata->last_work = &thrdata->_last_work;
+ memcpy(thrdata->last_work, work, sizeof(*thrdata->last_work));
+ }
+}
+
+static bool opencl_prepare_work(struct thr_info *thr, struct work *work)
+{
+ precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
+ return true;
+}
+
+static uint64_t opencl_scanhash(struct thr_info *thr, struct work *work, uint64_t max_nonce)
+{
+ const int thr_id = thr->id;
+ struct opencl_thread_data *thrdata = thr->cgpu_data;
+ struct cgpu_info *gpu = thr->cgpu;
+ _clState *clState = clStates[thr_id];
+ const cl_kernel *kernel = &clState->kernel;
+
+ double gpu_ms_average = 7;
+ cl_int status;
+
+ size_t globalThreads[1];
+ size_t localThreads[1] = { clState->work_size };
+ unsigned int threads;
+ unsigned int hashes;
+
+
+ struct timeval tv_gpustart, tv_gpuend, diff;
+ suseconds_t gpu_us;
+
+ gettimeofday(&tv_gpustart, NULL);
+ timeval_subtract(&diff, &tv_gpustart, &tv_gpuend);
+ /* This finish flushes the readbuffer set with CL_FALSE later */
+ clFinish(clState->commandQueue);
+ gettimeofday(&tv_gpuend, NULL);
+ timeval_subtract(&diff, &tv_gpuend, &tv_gpustart);
+ gpu_us = diff.tv_sec * 1000000 + diff.tv_usec;
+ decay_time(&gpu_ms_average, gpu_us / 1000);
+ if (gpu->dynamic) {
+ /* Try to not let the GPU be out for longer than 6ms, but
+ * increase intensity when the system is idle, unless
+ * dynamic is disabled. */
+ if (gpu_ms_average > 7) {
+ if (gpu->intensity > -10)
+ --gpu->intensity;
+ } else if (gpu_ms_average < 3) {
+ if (gpu->intensity < 10)
+ ++gpu->intensity;
+ }
+ }
+ set_threads_hashes(clState->preferred_vwidth, &threads, &hashes, globalThreads,
+ localThreads[0], gpu->intensity);
+
+ status = thrdata->queue_kernel_parameters(clState, &work->blk);
+ if (unlikely(status != CL_SUCCESS)) {
+ applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
+ return 0;
+ }
+
+ /* MAXBUFFERS entry is used as a flag to say nonces exist */
+ if (thrdata->res[FOUND]) {
+ /* Clear the buffer again */
+ status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
+ BUFFERSIZE, blank_res, 0, NULL, NULL);
+ if (unlikely(status != CL_SUCCESS)) {
+ applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
+ return 0;
+ }
+ if (unlikely(thrdata->last_work)) {
+ if (opt_debug)
+ applog(LOG_DEBUG, "GPU %d found something in last work?", gpu->device_id);
+ postcalc_hash_async(thr, thrdata->last_work, thrdata->res);
+ thrdata->last_work = NULL;
+ } else {
+ if (opt_debug)
+ applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
+ postcalc_hash_async(thr, work, thrdata->res);
+ }
+ memset(thrdata->res, 0, BUFFERSIZE);
+ clFinish(clState->commandQueue);
+ }
+ status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
+ globalThreads, localThreads, 0, NULL, NULL);
+ if (unlikely(status != CL_SUCCESS)) {
+ applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)");
+ return 0;
+ }
+
+ status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
+ BUFFERSIZE, thrdata->res, 0, NULL, NULL);
+ if (unlikely(status != CL_SUCCESS)) {
+ applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)");
+ return 0;
+ }
+
+ work->blk.nonce += hashes;
+
+ return hashes;
+}
+
+static void opencl_thread_shutdown(struct thr_info *thr)
+{
+ const int thr_id = thr->id;
+ _clState *clState = clStates[thr_id];
+
+ clReleaseCommandQueue(clState->commandQueue);
+ clReleaseKernel(clState->kernel);
+ clReleaseProgram(clState->program);
+ clReleaseContext(clState->context);
}
struct device_api opencl_api = {
@@ -5868,7 +5880,12 @@ struct device_api opencl_api = {
.api_detect = opencl_detect,
.reinit_device = reinit_opencl_device,
.get_statline = get_opencl_statline,
- .thread_start = opencl_thread_start,
+ .thread_prepare = opencl_thread_prepare,
+ .thread_init = opencl_thread_init,
+ .free_work = opencl_free_work,
+ .prepare_work = opencl_prepare_work,
+ .scanhash = opencl_scanhash,
+ .thread_shutdown = opencl_thread_shutdown,
};
#endif
@@ -6224,7 +6241,20 @@ retry_pools:
if (!thr->q)
quit(1, "tq_new failed in starting %s%d mining thread (#%d)", cgpu->api->name, cgpu->device_id, i);
- cgpu->api->thread_start(thr);
+ /* Enable threads for devices set not to mine but disable
+ * their queue in case we wish to enable them later */
+ if (cgpu->enabled) {
+ if (opt_debug)
+ applog(LOG_DEBUG, "Pushing ping to thread %d", thr->id);
+
+ tq_push(thr->q, &ping);
+ }
+
+ if (cgpu->api->thread_prepare && !cgpu->api->thread_prepare(thr))
+ continue;
+
+ if (unlikely(thr_info_create(thr, NULL, miner_thread, thr)))
+ quit(1, "thread %d create failed", thr->id);
}
}
diff --git a/miner.h b/miner.h
index cb92d99..33518b1 100644
--- a/miner.h
+++ b/miner.h
@@ -210,6 +210,7 @@ struct gpu_adl {
struct cgpu_info;
struct thr_info;
+struct work;
struct device_api {
char*name;
@@ -222,7 +223,13 @@ struct device_api {
void (*get_statline)(char*, struct cgpu_info*);
// Thread-specific functions
- void (*thread_start)(struct thr_info*);
+ bool (*thread_prepare)(struct thr_info*);
+ uint64_t (*can_limit_work)(struct thr_info*);
+ bool (*thread_init)(struct thr_info*);
+ void (*free_work)(struct thr_info*, struct work*);
+ bool (*prepare_work)(struct thr_info*, struct work*);
+ uint64_t (*scanhash)(struct thr_info*, struct work*, uint64_t);
+ void (*thread_shutdown)(struct thr_info*);
};
struct cgpu_info {
@@ -278,6 +285,7 @@ struct thr_info {
pthread_t pth;
struct thread_q *q;
struct cgpu_info *cgpu;
+ void *cgpu_data;
struct timeval last;
struct timeval sick;