deviceapi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. * Copyright 2011-2013 Luke Dashjr
  3. * Copyright 2011-2012 Con Kolivas
  4. * Copyright 2012-2013 Andrew Smith
  5. * Copyright 2010 Jeff Garzik
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #include "config.h"
  13. #ifdef WIN32
  14. #include <winsock2.h>
  15. #else
  16. #include <sys/select.h>
  17. #endif
  18. #include <stdbool.h>
  19. #include <stdint.h>
  20. #include <sys/time.h>
  21. #include <sys/types.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. #include "compat.h"
  25. #include "deviceapi.h"
  26. #include "logging.h"
  27. #include "miner.h"
  28. #include "util.h"
  29. bool hashes_done(struct thr_info *thr, int64_t hashes, struct timeval *tvp_hashes, uint32_t *max_nonce)
  30. {
  31. struct cgpu_info *cgpu = thr->cgpu;
  32. const long cycle = opt_log_interval / 5 ? : 1;
  33. if (unlikely(hashes == -1)) {
  34. time_t now = time(NULL);
  35. if (difftime(now, cgpu->device_last_not_well) > 1.)
  36. dev_error(cgpu, REASON_THREAD_ZERO_HASH);
  37. if (thr->scanhash_working && opt_restart) {
  38. applog(LOG_ERR, "%"PRIpreprv" failure, attempting to reinitialize", cgpu->proc_repr);
  39. thr->scanhash_working = false;
  40. cgpu->reinit_backoff = 5.2734375;
  41. hashes = 0;
  42. } else {
  43. applog(LOG_ERR, "%"PRIpreprv" failure, disabling!", cgpu->proc_repr);
  44. cgpu->deven = DEV_RECOVER_ERR;
  45. return false;
  46. }
  47. }
  48. else
  49. thr->scanhash_working = true;
  50. thr->hashes_done += hashes;
  51. if (hashes > cgpu->max_hashes)
  52. cgpu->max_hashes = hashes;
  53. timeradd(&thr->tv_hashes_done, tvp_hashes, &thr->tv_hashes_done);
  54. // max_nonce management (optional)
  55. if (unlikely((long)thr->tv_hashes_done.tv_sec < cycle)) {
  56. int mult;
  57. if (likely(!max_nonce || *max_nonce == 0xffffffff))
  58. return true;
  59. mult = 1000000 / ((thr->tv_hashes_done.tv_usec + 0x400) / 0x400) + 0x10;
  60. mult *= cycle;
  61. if (*max_nonce > (0xffffffff * 0x400) / mult)
  62. *max_nonce = 0xffffffff;
  63. else
  64. *max_nonce = (*max_nonce * mult) / 0x400;
  65. } else if (unlikely(thr->tv_hashes_done.tv_sec > cycle) && max_nonce)
  66. *max_nonce = *max_nonce * cycle / thr->tv_hashes_done.tv_sec;
  67. else if (unlikely(thr->tv_hashes_done.tv_usec > 100000) && max_nonce)
  68. *max_nonce = *max_nonce * 0x400 / (((cycle * 1000000) + thr->tv_hashes_done.tv_usec) / (cycle * 1000000 / 0x400));
  69. hashmeter2(thr);
  70. return true;
  71. }
  72. /* A generic wait function for threads that poll that will wait a specified
  73. * time tdiff waiting on a work restart request. Returns zero if the condition
  74. * was met (work restart requested) or ETIMEDOUT if not.
  75. */
  76. int restart_wait(struct thr_info *thr, unsigned int mstime)
  77. {
  78. struct timeval tv_timer, tv_now, tv_timeout;
  79. fd_set rfds;
  80. SOCKETTYPE wrn = thr->work_restart_notifier[0];
  81. int rv;
  82. if (unlikely(thr->work_restart_notifier[1] == INVSOCK))
  83. {
  84. // This is a bug!
  85. applog(LOG_ERR, "%"PRIpreprv": restart_wait called without a work_restart_notifier", thr->cgpu->proc_repr);
  86. nmsleep(mstime);
  87. return (thr->work_restart ? 0 : ETIMEDOUT);
  88. }
  89. gettimeofday(&tv_now, NULL);
  90. timer_set_delay(&tv_timer, &tv_now, mstime * 1000);
  91. while (true)
  92. {
  93. FD_ZERO(&rfds);
  94. FD_SET(wrn, &rfds);
  95. tv_timeout = tv_timer;
  96. rv = select(wrn + 1, &rfds, NULL, NULL, select_timeout(&tv_timeout, &tv_now));
  97. if (rv == 0)
  98. return ETIMEDOUT;
  99. if (rv > 0)
  100. {
  101. if (thr->work_restart)
  102. return 0;
  103. notifier_read(thr->work_restart_notifier);
  104. }
  105. gettimeofday(&tv_now, NULL);
  106. }
  107. }
  108. static
  109. struct work *get_and_prepare_work(struct thr_info *thr)
  110. {
  111. struct cgpu_info *proc = thr->cgpu;
  112. struct device_drv *api = proc->drv;
  113. struct work *work;
  114. work = get_work(thr);
  115. if (!work)
  116. return NULL;
  117. if (api->prepare_work && !api->prepare_work(thr, work)) {
  118. free_work(work);
  119. applog(LOG_ERR, "%"PRIpreprv": Work prepare failed, disabling!", proc->proc_repr);
  120. proc->deven = DEV_RECOVER_ERR;
  121. return NULL;
  122. }
  123. return work;
  124. }
  125. // Miner loop to manage a single processor (with possibly multiple threads per processor)
  126. void minerloop_scanhash(struct thr_info *mythr)
  127. {
  128. struct cgpu_info *cgpu = mythr->cgpu;
  129. struct device_drv *api = cgpu->drv;
  130. struct timeval tv_start, tv_end;
  131. struct timeval tv_hashes, tv_worktime;
  132. uint32_t max_nonce = api->can_limit_work ? api->can_limit_work(mythr) : 0xffffffff;
  133. int64_t hashes;
  134. struct work *work;
  135. const bool primary = (!mythr->device_thread) || mythr->primary_thread;
  136. #ifdef HAVE_PTHREAD_CANCEL
  137. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
  138. #endif
  139. while (likely(!cgpu->shutdown)) {
  140. mythr->work_restart = false;
  141. request_work(mythr);
  142. work = get_and_prepare_work(mythr);
  143. if (!work)
  144. break;
  145. gettimeofday(&(work->tv_work_start), NULL);
  146. do {
  147. thread_reportin(mythr);
  148. /* Only allow the mining thread to be cancelled when
  149. * it is not in the driver code. */
  150. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  151. gettimeofday(&tv_start, NULL);
  152. hashes = api->scanhash(mythr, work, work->blk.nonce + max_nonce);
  153. gettimeofday(&tv_end, NULL);
  154. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  155. pthread_testcancel();
  156. thread_reportin(mythr);
  157. timersub(&tv_end, &tv_start, &tv_hashes);
  158. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &max_nonce : NULL))
  159. goto disabled;
  160. if (unlikely(mythr->work_restart)) {
  161. /* Apart from device_thread 0, we stagger the
  162. * starting of every next thread to try and get
  163. * all devices busy before worrying about
  164. * getting work for their extra threads */
  165. if (!primary) {
  166. struct timespec rgtp;
  167. rgtp.tv_sec = 0;
  168. rgtp.tv_nsec = 250 * mythr->device_thread * 1000000;
  169. nanosleep(&rgtp, NULL);
  170. }
  171. break;
  172. }
  173. if (unlikely(mythr->pause || cgpu->deven != DEV_ENABLED))
  174. disabled:
  175. mt_disable(mythr);
  176. timersub(&tv_end, &work->tv_work_start, &tv_worktime);
  177. } while (!abandon_work(work, &tv_worktime, cgpu->max_hashes));
  178. free_work(work);
  179. }
  180. }
  181. bool do_job_prepare(struct thr_info *mythr, struct timeval *tvp_now)
  182. {
  183. struct cgpu_info *proc = mythr->cgpu;
  184. struct device_drv *api = proc->drv;
  185. struct timeval tv_worktime;
  186. mythr->tv_morework.tv_sec = -1;
  187. mythr->_job_transition_in_progress = true;
  188. if (mythr->work)
  189. timersub(tvp_now, &mythr->work->tv_work_start, &tv_worktime);
  190. if ((!mythr->work) || abandon_work(mythr->work, &tv_worktime, proc->max_hashes))
  191. {
  192. mythr->work_restart = false;
  193. request_work(mythr);
  194. // FIXME: Allow get_work to return NULL to retry on notification
  195. if (mythr->next_work)
  196. free_work(mythr->next_work);
  197. mythr->next_work = get_and_prepare_work(mythr);
  198. if (!mythr->next_work)
  199. return false;
  200. mythr->starting_next_work = true;
  201. api->job_prepare(mythr, mythr->next_work, mythr->_max_nonce);
  202. }
  203. else
  204. {
  205. mythr->starting_next_work = false;
  206. api->job_prepare(mythr, mythr->work, mythr->_max_nonce);
  207. }
  208. job_prepare_complete(mythr);
  209. return true;
  210. }
  211. void job_prepare_complete(struct thr_info *mythr)
  212. {
  213. if (unlikely(mythr->busy_state == TBS_GETTING_RESULTS))
  214. return;
  215. if (mythr->work)
  216. {
  217. if (true /* TODO: job is near complete */ || unlikely(mythr->work_restart))
  218. do_get_results(mythr, true);
  219. else
  220. {} // TODO: Set a timer to call do_get_results when job is near complete
  221. }
  222. else // no job currently running
  223. do_job_start(mythr);
  224. }
  225. void do_get_results(struct thr_info *mythr, bool proceed_with_new_job)
  226. {
  227. struct cgpu_info *proc = mythr->cgpu;
  228. struct device_drv *api = proc->drv;
  229. struct work *work = mythr->work;
  230. mythr->_job_transition_in_progress = true;
  231. mythr->tv_results_jobstart = mythr->tv_jobstart;
  232. mythr->_proceed_with_new_job = proceed_with_new_job;
  233. if (api->job_get_results)
  234. api->job_get_results(mythr, work);
  235. else
  236. job_results_fetched(mythr);
  237. }
  238. void job_results_fetched(struct thr_info *mythr)
  239. {
  240. if (mythr->_proceed_with_new_job)
  241. do_job_start(mythr);
  242. else
  243. {
  244. struct timeval tv_now;
  245. gettimeofday(&tv_now, NULL);
  246. do_process_results(mythr, &tv_now, mythr->prev_work, true);
  247. }
  248. }
  249. void do_job_start(struct thr_info *mythr)
  250. {
  251. struct cgpu_info *proc = mythr->cgpu;
  252. struct device_drv *api = proc->drv;
  253. thread_reportin(mythr);
  254. api->job_start(mythr);
  255. }
  256. void mt_job_transition(struct thr_info *mythr)
  257. {
  258. struct timeval tv_now;
  259. gettimeofday(&tv_now, NULL);
  260. if (mythr->starting_next_work)
  261. {
  262. mythr->next_work->tv_work_start = tv_now;
  263. if (mythr->prev_work)
  264. free_work(mythr->prev_work);
  265. mythr->prev_work = mythr->work;
  266. mythr->work = mythr->next_work;
  267. mythr->next_work = NULL;
  268. }
  269. mythr->tv_jobstart = tv_now;
  270. mythr->_job_transition_in_progress = false;
  271. }
  272. void job_start_complete(struct thr_info *mythr)
  273. {
  274. struct timeval tv_now;
  275. gettimeofday(&tv_now, NULL);
  276. do_process_results(mythr, &tv_now, mythr->prev_work, false);
  277. }
  278. void job_start_abort(struct thr_info *mythr, bool failure)
  279. {
  280. struct cgpu_info *proc = mythr->cgpu;
  281. if (failure)
  282. proc->deven = DEV_RECOVER_ERR;
  283. mythr->work = NULL;
  284. mythr->_job_transition_in_progress = false;
  285. }
  286. bool do_process_results(struct thr_info *mythr, struct timeval *tvp_now, struct work *work, bool stopping)
  287. {
  288. struct cgpu_info *proc = mythr->cgpu;
  289. struct device_drv *api = proc->drv;
  290. struct timeval tv_hashes;
  291. int64_t hashes = 0;
  292. if (api->job_process_results)
  293. hashes = api->job_process_results(mythr, work, stopping);
  294. thread_reportin(mythr);
  295. if (hashes)
  296. {
  297. timersub(tvp_now, &mythr->tv_results_jobstart, &tv_hashes);
  298. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &mythr->_max_nonce : NULL))
  299. return false;
  300. }
  301. return true;
  302. }
  303. static
  304. void do_notifier_select(struct thr_info *thr, struct timeval *tvp_timeout)
  305. {
  306. struct cgpu_info *cgpu = thr->cgpu;
  307. struct timeval tv_now;
  308. int maxfd;
  309. fd_set rfds;
  310. gettimeofday(&tv_now, NULL);
  311. FD_ZERO(&rfds);
  312. FD_SET(thr->notifier[0], &rfds);
  313. maxfd = thr->notifier[0];
  314. FD_SET(thr->work_restart_notifier[0], &rfds);
  315. set_maxfd(&maxfd, thr->work_restart_notifier[0]);
  316. if (thr->mutex_request[1] != INVSOCK)
  317. {
  318. FD_SET(thr->mutex_request[0], &rfds);
  319. set_maxfd(&maxfd, thr->mutex_request[0]);
  320. }
  321. if (select(maxfd + 1, &rfds, NULL, NULL, select_timeout(tvp_timeout, &tv_now)) < 0)
  322. return;
  323. if (thr->mutex_request[1] != INVSOCK && FD_ISSET(thr->mutex_request[0], &rfds))
  324. {
  325. // FIXME: This can only handle one request at a time!
  326. pthread_mutex_t *mutexp = &cgpu->device_mutex;
  327. notifier_read(thr->mutex_request);
  328. mutex_lock(mutexp);
  329. pthread_cond_signal(&cgpu->device_cond);
  330. pthread_cond_wait(&cgpu->device_cond, mutexp);
  331. mutex_unlock(mutexp);
  332. }
  333. if (FD_ISSET(thr->notifier[0], &rfds)) {
  334. notifier_read(thr->notifier);
  335. }
  336. if (FD_ISSET(thr->work_restart_notifier[0], &rfds))
  337. notifier_read(thr->work_restart_notifier);
  338. }
  339. void minerloop_async(struct thr_info *mythr)
  340. {
  341. struct thr_info *thr = mythr;
  342. struct cgpu_info *cgpu = mythr->cgpu;
  343. struct device_drv *api = cgpu->drv;
  344. struct timeval tv_now;
  345. struct timeval tv_timeout;
  346. struct cgpu_info *proc;
  347. bool is_running, should_be_running;
  348. if (mythr->work_restart_notifier[1] == -1)
  349. notifier_init(mythr->work_restart_notifier);
  350. while (likely(!cgpu->shutdown)) {
  351. tv_timeout.tv_sec = -1;
  352. gettimeofday(&tv_now, NULL);
  353. for (proc = cgpu; proc; proc = proc->next_proc)
  354. {
  355. mythr = proc->thr[0];
  356. // Nothing should happen while we're starting a job
  357. if (unlikely(mythr->busy_state == TBS_STARTING_JOB))
  358. goto defer_events;
  359. is_running = mythr->work;
  360. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  361. if (should_be_running)
  362. {
  363. if (unlikely(!(is_running || mythr->_job_transition_in_progress)))
  364. {
  365. mt_disable_finish(mythr);
  366. goto djp;
  367. }
  368. if (unlikely(mythr->work_restart))
  369. goto djp;
  370. }
  371. else // ! should_be_running
  372. {
  373. if (unlikely(is_running && !mythr->_job_transition_in_progress))
  374. {
  375. disabled: ;
  376. mythr->tv_morework.tv_sec = -1;
  377. if (mythr->busy_state != TBS_GETTING_RESULTS)
  378. do_get_results(mythr, false);
  379. else
  380. // Avoid starting job when pending result fetch completes
  381. mythr->_proceed_with_new_job = false;
  382. }
  383. }
  384. if (timer_passed(&mythr->tv_morework, &tv_now))
  385. {
  386. djp: ;
  387. if (!do_job_prepare(mythr, &tv_now))
  388. goto disabled;
  389. }
  390. defer_events:
  391. if (timer_passed(&mythr->tv_poll, &tv_now))
  392. api->poll(mythr);
  393. reduce_timeout_to(&tv_timeout, &mythr->tv_morework);
  394. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  395. }
  396. do_notifier_select(thr, &tv_timeout);
  397. }
  398. }
  399. static
  400. void do_queue_flush(struct thr_info *mythr)
  401. {
  402. struct cgpu_info *proc = mythr->cgpu;
  403. struct device_drv *api = proc->drv;
  404. api->queue_flush(mythr);
  405. if (mythr->next_work)
  406. {
  407. free_work(mythr->next_work);
  408. mythr->next_work = NULL;
  409. }
  410. }
  411. void minerloop_queue(struct thr_info *thr)
  412. {
  413. struct thr_info *mythr;
  414. struct cgpu_info *cgpu = thr->cgpu;
  415. struct device_drv *api = cgpu->drv;
  416. struct timeval tv_now;
  417. struct timeval tv_timeout;
  418. struct cgpu_info *proc;
  419. bool should_be_running;
  420. struct work *work;
  421. if (thr->work_restart_notifier[1] == -1)
  422. notifier_init(thr->work_restart_notifier);
  423. while (likely(!cgpu->shutdown)) {
  424. tv_timeout.tv_sec = -1;
  425. gettimeofday(&tv_now, NULL);
  426. for (proc = cgpu; proc; proc = proc->next_proc)
  427. {
  428. mythr = proc->thr[0];
  429. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  430. redo:
  431. if (should_be_running)
  432. {
  433. if (unlikely(!mythr->_last_sbr_state))
  434. {
  435. mt_disable_finish(mythr);
  436. mythr->_last_sbr_state = should_be_running;
  437. }
  438. if (unlikely(mythr->work_restart))
  439. {
  440. mythr->work_restart = false;
  441. do_queue_flush(mythr);
  442. }
  443. while (!mythr->queue_full)
  444. {
  445. if (mythr->next_work)
  446. {
  447. work = mythr->next_work;
  448. mythr->next_work = NULL;
  449. }
  450. else
  451. {
  452. request_work(mythr);
  453. // FIXME: Allow get_work to return NULL to retry on notification
  454. work = get_and_prepare_work(mythr);
  455. }
  456. if (!work)
  457. break;
  458. if (!api->queue_append(mythr, work))
  459. mythr->next_work = work;
  460. }
  461. }
  462. else
  463. if (unlikely(mythr->_last_sbr_state))
  464. {
  465. mythr->_last_sbr_state = should_be_running;
  466. do_queue_flush(mythr);
  467. }
  468. if (timer_passed(&mythr->tv_poll, &tv_now))
  469. api->poll(mythr);
  470. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  471. if (should_be_running && !mythr->queue_full)
  472. goto redo;
  473. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  474. }
  475. do_notifier_select(thr, &tv_timeout);
  476. }
  477. }
  478. void *miner_thread(void *userdata)
  479. {
  480. struct thr_info *mythr = userdata;
  481. struct cgpu_info *cgpu = mythr->cgpu;
  482. struct device_drv *drv = cgpu->drv;
  483. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  484. char threadname[20];
  485. snprintf(threadname, 20, "miner_%s", cgpu->proc_repr_ns);
  486. RenameThread(threadname);
  487. if (drv->thread_init && !drv->thread_init(mythr)) {
  488. dev_error(cgpu, REASON_THREAD_FAIL_INIT);
  489. for (struct cgpu_info *slave = cgpu->next_proc; slave && !slave->threads; slave = slave->next_proc)
  490. dev_error(slave, REASON_THREAD_FAIL_INIT);
  491. __thr_being_msg(LOG_ERR, mythr, "failure, exiting");
  492. goto out;
  493. }
  494. thread_reportout(mythr);
  495. applog(LOG_DEBUG, "Popping ping in miner thread");
  496. notifier_read(mythr->notifier); // Wait for a notification to start
  497. cgtime(&cgpu->cgminer_stats.start_tv);
  498. if (drv->minerloop)
  499. drv->minerloop(mythr);
  500. else
  501. minerloop_scanhash(mythr);
  502. __thr_being_msg(LOG_NOTICE, mythr, "shutting down");
  503. out:
  504. cgpu->deven = DEV_DISABLED;
  505. if (drv->thread_shutdown)
  506. drv->thread_shutdown(mythr);
  507. thread_reportin(mythr);
  508. notifier_destroy(mythr->notifier);
  509. return NULL;
  510. }
  511. bool add_cgpu(struct cgpu_info *cgpu)
  512. {
  513. int lpcount;
  514. renumber_cgpu(cgpu);
  515. if (!cgpu->procs)
  516. cgpu->procs = 1;
  517. lpcount = cgpu->procs;
  518. cgpu->device = cgpu;
  519. cgpu->dev_repr = malloc(6);
  520. sprintf(cgpu->dev_repr, "%s%2u", cgpu->drv->name, cgpu->device_id % 100);
  521. cgpu->dev_repr_ns = malloc(6);
  522. sprintf(cgpu->dev_repr_ns, "%s%u", cgpu->drv->name, cgpu->device_id % 100);
  523. strcpy(cgpu->proc_repr, cgpu->dev_repr);
  524. sprintf(cgpu->proc_repr_ns, "%s%u", cgpu->drv->name, cgpu->device_id);
  525. wr_lock(&devices_lock);
  526. devices = realloc(devices, sizeof(struct cgpu_info *) * (total_devices + lpcount + 1));
  527. devices[total_devices++] = cgpu;
  528. if (lpcount > 1)
  529. {
  530. int ns;
  531. int tpp = cgpu->threads / lpcount;
  532. struct cgpu_info **nlp_p, *slave;
  533. const bool manylp = (lpcount > 26);
  534. const char *as = (manylp ? "aa" : "a");
  535. // Note, strcpy instead of assigning a byte to get the \0 too
  536. strcpy(&cgpu->proc_repr[5], as);
  537. ns = strlen(cgpu->proc_repr_ns);
  538. strcpy(&cgpu->proc_repr_ns[ns], as);
  539. nlp_p = &cgpu->next_proc;
  540. for (int i = 1; i < lpcount; ++i)
  541. {
  542. slave = malloc(sizeof(*slave));
  543. *slave = *cgpu;
  544. slave->proc_id = i;
  545. if (manylp)
  546. {
  547. slave->proc_repr[5] += i / 26;
  548. slave->proc_repr[6] += i % 26;
  549. slave->proc_repr_ns[ns ] += i / 26;
  550. slave->proc_repr_ns[ns + 1] += i % 26;
  551. }
  552. else
  553. {
  554. slave->proc_repr[5] += i;
  555. slave->proc_repr_ns[ns] += i;
  556. }
  557. slave->threads = tpp;
  558. devices[total_devices++] = slave;
  559. *nlp_p = slave;
  560. nlp_p = &slave->next_proc;
  561. }
  562. *nlp_p = NULL;
  563. cgpu->proc_id = 0;
  564. cgpu->threads -= (tpp * (lpcount - 1));
  565. }
  566. wr_unlock(&devices_lock);
  567. mutex_lock(&stats_lock);
  568. cgpu->last_device_valid_work = time(NULL);
  569. mutex_unlock(&stats_lock);
  570. return true;
  571. }