driver-opencl.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. /*
  2. * Copyright 2011-2012 Con Kolivas
  3. * Copyright 2011-2012 Luke Dashjr
  4. * Copyright 2010 Jeff Garzik
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #ifdef HAVE_CURSES
  13. #include <curses.h>
  14. #endif
  15. #ifndef WIN32
  16. #include <dlfcn.h>
  17. #else
  18. #include <windows.h>
  19. #endif
  20. #include <string.h>
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include <sys/types.h>
  24. #ifndef WIN32
  25. #include <sys/resource.h>
  26. #endif
  27. #include <ccan/opt/opt.h>
  28. #define OMIT_OPENCL_API
  29. #include "compat.h"
  30. #include "miner.h"
  31. #include "driver-opencl.h"
  32. #include "findnonce.h"
  33. #include "ocl.h"
  34. #include "adl.h"
  35. /* TODO: cleanup externals ********************/
  36. #ifdef HAVE_OPENCL
  37. /* Platform API */
  38. CL_API_ENTRY cl_int CL_API_CALL
  39. (*clGetPlatformIDs)(cl_uint /* num_entries */,
  40. cl_platform_id * /* platforms */,
  41. cl_uint * /* num_platforms */) CL_API_SUFFIX__VERSION_1_0;
  42. CL_API_ENTRY cl_int CL_API_CALL
  43. (*clGetPlatformInfo)(cl_platform_id /* platform */,
  44. cl_platform_info /* param_name */,
  45. size_t /* param_value_size */,
  46. void * /* param_value */,
  47. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  48. /* Device APIs */
  49. CL_API_ENTRY cl_int CL_API_CALL
  50. (*clGetDeviceIDs)(cl_platform_id /* platform */,
  51. cl_device_type /* device_type */,
  52. cl_uint /* num_entries */,
  53. cl_device_id * /* devices */,
  54. cl_uint * /* num_devices */) CL_API_SUFFIX__VERSION_1_0;
  55. CL_API_ENTRY cl_int CL_API_CALL
  56. (*clGetDeviceInfo)(cl_device_id /* device */,
  57. cl_device_info /* param_name */,
  58. size_t /* param_value_size */,
  59. void * /* param_value */,
  60. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  61. /* Context APIs */
  62. CL_API_ENTRY cl_context CL_API_CALL
  63. (*clCreateContextFromType)(const cl_context_properties * /* properties */,
  64. cl_device_type /* device_type */,
  65. void (CL_CALLBACK * /* pfn_notify*/ )(const char *, const void *, size_t, void *),
  66. void * /* user_data */,
  67. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  68. CL_API_ENTRY cl_int CL_API_CALL
  69. (*clReleaseContext)(cl_context /* context */) CL_API_SUFFIX__VERSION_1_0;
  70. /* Command Queue APIs */
  71. CL_API_ENTRY cl_command_queue CL_API_CALL
  72. (*clCreateCommandQueue)(cl_context /* context */,
  73. cl_device_id /* device */,
  74. cl_command_queue_properties /* properties */,
  75. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  76. CL_API_ENTRY cl_int CL_API_CALL
  77. (*clReleaseCommandQueue)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  78. /* Memory Object APIs */
  79. CL_API_ENTRY cl_mem CL_API_CALL
  80. (*clCreateBuffer)(cl_context /* context */,
  81. cl_mem_flags /* flags */,
  82. size_t /* size */,
  83. void * /* host_ptr */,
  84. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  85. /* Program Object APIs */
  86. CL_API_ENTRY cl_program CL_API_CALL
  87. (*clCreateProgramWithSource)(cl_context /* context */,
  88. cl_uint /* count */,
  89. const char ** /* strings */,
  90. const size_t * /* lengths */,
  91. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  92. CL_API_ENTRY cl_program CL_API_CALL
  93. (*clCreateProgramWithBinary)(cl_context /* context */,
  94. cl_uint /* num_devices */,
  95. const cl_device_id * /* device_list */,
  96. const size_t * /* lengths */,
  97. const unsigned char ** /* binaries */,
  98. cl_int * /* binary_status */,
  99. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  100. CL_API_ENTRY cl_int CL_API_CALL
  101. (*clReleaseProgram)(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
  102. CL_API_ENTRY cl_int CL_API_CALL
  103. (*clBuildProgram)(cl_program /* program */,
  104. cl_uint /* num_devices */,
  105. const cl_device_id * /* device_list */,
  106. const char * /* options */,
  107. void (CL_CALLBACK * /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
  108. void * /* user_data */) CL_API_SUFFIX__VERSION_1_0;
  109. CL_API_ENTRY cl_int CL_API_CALL
  110. (*clGetProgramInfo)(cl_program /* program */,
  111. cl_program_info /* param_name */,
  112. size_t /* param_value_size */,
  113. void * /* param_value */,
  114. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  115. CL_API_ENTRY cl_int CL_API_CALL
  116. (*clGetProgramBuildInfo)(cl_program /* program */,
  117. cl_device_id /* device */,
  118. cl_program_build_info /* param_name */,
  119. size_t /* param_value_size */,
  120. void * /* param_value */,
  121. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  122. /* Kernel Object APIs */
  123. CL_API_ENTRY cl_kernel CL_API_CALL
  124. (*clCreateKernel)(cl_program /* program */,
  125. const char * /* kernel_name */,
  126. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  127. CL_API_ENTRY cl_int CL_API_CALL
  128. (*clReleaseKernel)(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0;
  129. CL_API_ENTRY cl_int CL_API_CALL
  130. (*clSetKernelArg)(cl_kernel /* kernel */,
  131. cl_uint /* arg_index */,
  132. size_t /* arg_size */,
  133. const void * /* arg_value */) CL_API_SUFFIX__VERSION_1_0;
  134. /* Flush and Finish APIs */
  135. CL_API_ENTRY cl_int CL_API_CALL
  136. (*clFinish)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  137. /* Enqueued Commands APIs */
  138. CL_API_ENTRY cl_int CL_API_CALL
  139. (*clEnqueueReadBuffer)(cl_command_queue /* command_queue */,
  140. cl_mem /* buffer */,
  141. cl_bool /* blocking_read */,
  142. size_t /* offset */,
  143. size_t /* size */,
  144. void * /* ptr */,
  145. cl_uint /* num_events_in_wait_list */,
  146. const cl_event * /* event_wait_list */,
  147. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  148. CL_API_ENTRY cl_int CL_API_CALL
  149. (*clEnqueueWriteBuffer)(cl_command_queue /* command_queue */,
  150. cl_mem /* buffer */,
  151. cl_bool /* blocking_write */,
  152. size_t /* offset */,
  153. size_t /* size */,
  154. const void * /* ptr */,
  155. cl_uint /* num_events_in_wait_list */,
  156. const cl_event * /* event_wait_list */,
  157. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  158. CL_API_ENTRY cl_int CL_API_CALL
  159. (*clEnqueueNDRangeKernel)(cl_command_queue /* command_queue */,
  160. cl_kernel /* kernel */,
  161. cl_uint /* work_dim */,
  162. const size_t * /* global_work_offset */,
  163. const size_t * /* global_work_size */,
  164. const size_t * /* local_work_size */,
  165. cl_uint /* num_events_in_wait_list */,
  166. const cl_event * /* event_wait_list */,
  167. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  168. #ifdef WIN32
  169. #define dlsym (void*)GetProcAddress
  170. #define dlclose FreeLibrary
  171. #endif
  172. #define LOAD_OCL_SYM(sym) do { \
  173. if (!(sym = dlsym(cl, #sym))) { \
  174. applog(LOG_ERR, "Failed to load OpenCL symbol " #sym ", no GPUs usable"); \
  175. dlclose(cl); \
  176. return false; \
  177. } \
  178. } while(0)
  179. static bool
  180. load_opencl_symbols() {
  181. #ifndef WIN32
  182. void *cl = dlopen("libOpenCL.so", RTLD_LAZY);
  183. #else
  184. HMODULE cl = LoadLibrary("OpenCL.dll");
  185. #endif
  186. if (!cl)
  187. {
  188. applog(LOG_ERR, "Failed to load OpenCL library, no GPUs usable");
  189. return false;
  190. }
  191. LOAD_OCL_SYM(clGetPlatformIDs);
  192. LOAD_OCL_SYM(clGetPlatformInfo);
  193. LOAD_OCL_SYM(clGetDeviceIDs);
  194. LOAD_OCL_SYM(clGetDeviceInfo);
  195. LOAD_OCL_SYM(clCreateContextFromType);
  196. LOAD_OCL_SYM(clReleaseContext);
  197. LOAD_OCL_SYM(clCreateCommandQueue);
  198. LOAD_OCL_SYM(clReleaseCommandQueue);
  199. LOAD_OCL_SYM(clCreateBuffer);
  200. LOAD_OCL_SYM(clCreateProgramWithSource);
  201. LOAD_OCL_SYM(clCreateProgramWithBinary);
  202. LOAD_OCL_SYM(clReleaseProgram);
  203. LOAD_OCL_SYM(clBuildProgram);
  204. LOAD_OCL_SYM(clGetProgramInfo);
  205. LOAD_OCL_SYM(clGetProgramBuildInfo);
  206. LOAD_OCL_SYM(clCreateKernel);
  207. LOAD_OCL_SYM(clReleaseKernel);
  208. LOAD_OCL_SYM(clSetKernelArg);
  209. LOAD_OCL_SYM(clFinish);
  210. LOAD_OCL_SYM(clEnqueueReadBuffer);
  211. LOAD_OCL_SYM(clEnqueueWriteBuffer);
  212. LOAD_OCL_SYM(clEnqueueNDRangeKernel);
  213. return true;
  214. }
  215. #endif
  216. #ifdef HAVE_CURSES
  217. extern WINDOW *mainwin, *statuswin, *logwin;
  218. extern void enable_curses(void);
  219. #endif
  220. extern int mining_threads;
  221. extern double total_secs;
  222. extern int opt_g_threads;
  223. extern bool ping;
  224. extern bool opt_loginput;
  225. extern char *opt_kernel_path;
  226. extern int gpur_thr_id;
  227. extern bool opt_noadl;
  228. extern bool have_opencl;
  229. extern void *miner_thread(void *userdata);
  230. extern int dev_from_id(int thr_id);
  231. extern void tailsprintf(char *f, const char *fmt, ...);
  232. extern void wlog(const char *f, ...);
  233. extern void decay_time(double *f, double fadd);
  234. /**********************************************/
  235. #ifdef HAVE_ADL
  236. extern float gpu_temp(int gpu);
  237. extern int gpu_fanspeed(int gpu);
  238. extern int gpu_fanpercent(int gpu);
  239. #endif
  240. #ifdef HAVE_OPENCL
  241. char *set_vector(char *arg)
  242. {
  243. int i, val = 0, device = 0;
  244. char *nextptr;
  245. nextptr = strtok(arg, ",");
  246. if (nextptr == NULL)
  247. return "Invalid parameters for set vector";
  248. val = atoi(nextptr);
  249. if (val != 1 && val != 2 && val != 4)
  250. return "Invalid value passed to set_vector";
  251. gpus[device++].vwidth = val;
  252. while ((nextptr = strtok(NULL, ",")) != NULL) {
  253. val = atoi(nextptr);
  254. if (val != 1 && val != 2 && val != 4)
  255. return "Invalid value passed to set_vector";
  256. gpus[device++].vwidth = val;
  257. }
  258. if (device == 1) {
  259. for (i = device; i < MAX_GPUDEVICES; i++)
  260. gpus[i].vwidth = gpus[0].vwidth;
  261. }
  262. return NULL;
  263. }
  264. char *set_worksize(char *arg)
  265. {
  266. int i, val = 0, device = 0;
  267. char *nextptr;
  268. nextptr = strtok(arg, ",");
  269. if (nextptr == NULL)
  270. return "Invalid parameters for set work size";
  271. val = atoi(nextptr);
  272. if (val < 1 || val > 9999)
  273. return "Invalid value passed to set_worksize";
  274. gpus[device++].work_size = val;
  275. while ((nextptr = strtok(NULL, ",")) != NULL) {
  276. val = atoi(nextptr);
  277. if (val < 1 || val > 9999)
  278. return "Invalid value passed to set_worksize";
  279. gpus[device++].work_size = val;
  280. }
  281. if (device == 1) {
  282. for (i = device; i < MAX_GPUDEVICES; i++)
  283. gpus[i].work_size = gpus[0].work_size;
  284. }
  285. return NULL;
  286. }
  287. #ifdef USE_SCRYPT
  288. char *set_shaders(char *arg)
  289. {
  290. int i, val = 0, device = 0;
  291. char *nextptr;
  292. nextptr = strtok(arg, ",");
  293. if (nextptr == NULL)
  294. return "Invalid parameters for set lookup gap";
  295. val = atoi(nextptr);
  296. gpus[device++].shaders = val;
  297. while ((nextptr = strtok(NULL, ",")) != NULL) {
  298. val = atoi(nextptr);
  299. gpus[device++].shaders = val;
  300. }
  301. if (device == 1) {
  302. for (i = device; i < MAX_GPUDEVICES; i++)
  303. gpus[i].shaders = gpus[0].shaders;
  304. }
  305. return NULL;
  306. }
  307. char *set_lookup_gap(char *arg)
  308. {
  309. int i, val = 0, device = 0;
  310. char *nextptr;
  311. nextptr = strtok(arg, ",");
  312. if (nextptr == NULL)
  313. return "Invalid parameters for set lookup gap";
  314. val = atoi(nextptr);
  315. gpus[device++].opt_lg = val;
  316. while ((nextptr = strtok(NULL, ",")) != NULL) {
  317. val = atoi(nextptr);
  318. gpus[device++].opt_lg = val;
  319. }
  320. if (device == 1) {
  321. for (i = device; i < MAX_GPUDEVICES; i++)
  322. gpus[i].opt_lg = gpus[0].opt_lg;
  323. }
  324. return NULL;
  325. }
  326. char *set_thread_concurrency(char *arg)
  327. {
  328. int i, val = 0, device = 0;
  329. char *nextptr;
  330. nextptr = strtok(arg, ",");
  331. if (nextptr == NULL)
  332. return "Invalid parameters for set thread concurrency";
  333. val = atoi(nextptr);
  334. gpus[device++].opt_tc = val;
  335. while ((nextptr = strtok(NULL, ",")) != NULL) {
  336. val = atoi(nextptr);
  337. gpus[device++].opt_tc = val;
  338. }
  339. if (device == 1) {
  340. for (i = device; i < MAX_GPUDEVICES; i++)
  341. gpus[i].opt_tc = gpus[0].opt_tc;
  342. }
  343. return NULL;
  344. }
  345. #endif
  346. static enum cl_kernels select_kernel(char *arg)
  347. {
  348. if (!strcmp(arg, "diablo"))
  349. return KL_DIABLO;
  350. if (!strcmp(arg, "diakgcn"))
  351. return KL_DIAKGCN;
  352. if (!strcmp(arg, "poclbm"))
  353. return KL_POCLBM;
  354. if (!strcmp(arg, "phatk"))
  355. return KL_PHATK;
  356. #ifdef USE_SCRYPT
  357. if (!strcmp(arg, "scrypt"))
  358. return KL_SCRYPT;
  359. #endif
  360. return KL_NONE;
  361. }
  362. char *set_kernel(char *arg)
  363. {
  364. enum cl_kernels kern;
  365. int i, device = 0;
  366. char *nextptr;
  367. if (opt_scrypt)
  368. return "Cannot use sha256 kernel with scrypt";
  369. nextptr = strtok(arg, ",");
  370. if (nextptr == NULL)
  371. return "Invalid parameters for set kernel";
  372. kern = select_kernel(nextptr);
  373. if (kern == KL_NONE)
  374. return "Invalid parameter to set_kernel";
  375. gpus[device++].kernel = kern;
  376. while ((nextptr = strtok(NULL, ",")) != NULL) {
  377. kern = select_kernel(nextptr);
  378. if (kern == KL_NONE)
  379. return "Invalid parameter to set_kernel";
  380. gpus[device++].kernel = kern;
  381. }
  382. if (device == 1) {
  383. for (i = device; i < MAX_GPUDEVICES; i++)
  384. gpus[i].kernel = gpus[0].kernel;
  385. }
  386. return NULL;
  387. }
  388. #endif
  389. #ifdef HAVE_ADL
  390. /* This function allows us to map an adl device to an opencl device for when
  391. * simple enumeration has failed to match them. */
  392. char *set_gpu_map(char *arg)
  393. {
  394. int val1 = 0, val2 = 0;
  395. char *nextptr;
  396. nextptr = strtok(arg, ",");
  397. if (nextptr == NULL)
  398. return "Invalid parameters for set gpu map";
  399. if (sscanf(arg, "%d:%d", &val1, &val2) != 2)
  400. return "Invalid description for map pair";
  401. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  402. return "Invalid value passed to set_gpu_map";
  403. gpus[val1].virtual_adl = val2;
  404. gpus[val1].mapped = true;
  405. while ((nextptr = strtok(NULL, ",")) != NULL) {
  406. if (sscanf(nextptr, "%d:%d", &val1, &val2) != 2)
  407. return "Invalid description for map pair";
  408. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  409. return "Invalid value passed to set_gpu_map";
  410. gpus[val1].virtual_adl = val2;
  411. gpus[val1].mapped = true;
  412. }
  413. return NULL;
  414. }
  415. void get_intrange(char *arg, int *val1, int *val2)
  416. {
  417. if (sscanf(arg, "%d-%d", val1, val2) == 1) {
  418. *val2 = *val1;
  419. *val1 = 0;
  420. }
  421. }
  422. char *set_gpu_engine(char *arg)
  423. {
  424. int i, val1 = 0, val2 = 0, device = 0;
  425. char *nextptr;
  426. nextptr = strtok(arg, ",");
  427. if (nextptr == NULL)
  428. return "Invalid parameters for set gpu engine";
  429. get_intrange(nextptr, &val1, &val2);
  430. if (val1 < 0 || val1 > 9999 || val2 < 0 || val2 > 9999)
  431. return "Invalid value passed to set_gpu_engine";
  432. gpus[device].min_engine = val1;
  433. gpus[device].gpu_engine = val2;
  434. device++;
  435. while ((nextptr = strtok(NULL, ",")) != NULL) {
  436. get_intrange(nextptr, &val1, &val2);
  437. if (val1 < 0 || val1 > 9999 || val2 < 0 || val2 > 9999)
  438. return "Invalid value passed to set_gpu_engine";
  439. gpus[device].min_engine = val1;
  440. gpus[device].gpu_engine = val2;
  441. device++;
  442. }
  443. if (device == 1) {
  444. for (i = 1; i < MAX_GPUDEVICES; i++) {
  445. gpus[i].min_engine = gpus[0].min_engine;
  446. gpus[i].gpu_engine = gpus[0].gpu_engine;
  447. }
  448. }
  449. return NULL;
  450. }
  451. char *set_gpu_fan(char *arg)
  452. {
  453. int i, val1 = 0, val2 = 0, device = 0;
  454. char *nextptr;
  455. nextptr = strtok(arg, ",");
  456. if (nextptr == NULL)
  457. return "Invalid parameters for set gpu fan";
  458. get_intrange(nextptr, &val1, &val2);
  459. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100)
  460. return "Invalid value passed to set_gpu_fan";
  461. gpus[device].min_fan = val1;
  462. gpus[device].gpu_fan = val2;
  463. device++;
  464. while ((nextptr = strtok(NULL, ",")) != NULL) {
  465. get_intrange(nextptr, &val1, &val2);
  466. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100)
  467. return "Invalid value passed to set_gpu_fan";
  468. gpus[device].min_fan = val1;
  469. gpus[device].gpu_fan = val2;
  470. device++;
  471. }
  472. if (device == 1) {
  473. for (i = 1; i < MAX_GPUDEVICES; i++) {
  474. gpus[i].min_fan = gpus[0].min_fan;
  475. gpus[i].gpu_fan = gpus[0].gpu_fan;
  476. }
  477. }
  478. return NULL;
  479. }
  480. char *set_gpu_memclock(char *arg)
  481. {
  482. int i, val = 0, device = 0;
  483. char *nextptr;
  484. nextptr = strtok(arg, ",");
  485. if (nextptr == NULL)
  486. return "Invalid parameters for set gpu memclock";
  487. val = atoi(nextptr);
  488. if (val < 0 || val >= 9999)
  489. return "Invalid value passed to set_gpu_memclock";
  490. gpus[device++].gpu_memclock = val;
  491. while ((nextptr = strtok(NULL, ",")) != NULL) {
  492. val = atoi(nextptr);
  493. if (val < 0 || val >= 9999)
  494. return "Invalid value passed to set_gpu_memclock";
  495. gpus[device++].gpu_memclock = val;
  496. }
  497. if (device == 1) {
  498. for (i = device; i < MAX_GPUDEVICES; i++)
  499. gpus[i].gpu_memclock = gpus[0].gpu_memclock;
  500. }
  501. return NULL;
  502. }
  503. char *set_gpu_memdiff(char *arg)
  504. {
  505. int i, val = 0, device = 0;
  506. char *nextptr;
  507. nextptr = strtok(arg, ",");
  508. if (nextptr == NULL)
  509. return "Invalid parameters for set gpu memdiff";
  510. val = atoi(nextptr);
  511. if (val < -9999 || val > 9999)
  512. return "Invalid value passed to set_gpu_memdiff";
  513. gpus[device++].gpu_memdiff = val;
  514. while ((nextptr = strtok(NULL, ",")) != NULL) {
  515. val = atoi(nextptr);
  516. if (val < -9999 || val > 9999)
  517. return "Invalid value passed to set_gpu_memdiff";
  518. gpus[device++].gpu_memdiff = val;
  519. }
  520. if (device == 1) {
  521. for (i = device; i < MAX_GPUDEVICES; i++)
  522. gpus[i].gpu_memdiff = gpus[0].gpu_memdiff;
  523. }
  524. return NULL;
  525. }
  526. char *set_gpu_powertune(char *arg)
  527. {
  528. int i, val = 0, device = 0;
  529. char *nextptr;
  530. nextptr = strtok(arg, ",");
  531. if (nextptr == NULL)
  532. return "Invalid parameters for set gpu powertune";
  533. val = atoi(nextptr);
  534. if (val < -99 || val > 99)
  535. return "Invalid value passed to set_gpu_powertune";
  536. gpus[device++].gpu_powertune = val;
  537. while ((nextptr = strtok(NULL, ",")) != NULL) {
  538. val = atoi(nextptr);
  539. if (val < -99 || val > 99)
  540. return "Invalid value passed to set_gpu_powertune";
  541. gpus[device++].gpu_powertune = val;
  542. }
  543. if (device == 1) {
  544. for (i = device; i < MAX_GPUDEVICES; i++)
  545. gpus[i].gpu_powertune = gpus[0].gpu_powertune;
  546. }
  547. return NULL;
  548. }
  549. char *set_gpu_vddc(char *arg)
  550. {
  551. int i, device = 0;
  552. float val = 0;
  553. char *nextptr;
  554. nextptr = strtok(arg, ",");
  555. if (nextptr == NULL)
  556. return "Invalid parameters for set gpu vddc";
  557. val = atof(nextptr);
  558. if (val < 0 || val >= 9999)
  559. return "Invalid value passed to set_gpu_vddc";
  560. gpus[device++].gpu_vddc = val;
  561. while ((nextptr = strtok(NULL, ",")) != NULL) {
  562. val = atof(nextptr);
  563. if (val < 0 || val >= 9999)
  564. return "Invalid value passed to set_gpu_vddc";
  565. gpus[device++].gpu_vddc = val;
  566. }
  567. if (device == 1) {
  568. for (i = device; i < MAX_GPUDEVICES; i++)
  569. gpus[i].gpu_vddc = gpus[0].gpu_vddc;
  570. }
  571. return NULL;
  572. }
  573. char *set_temp_overheat(char *arg)
  574. {
  575. int i, val = 0, device = 0, *to;
  576. char *nextptr;
  577. nextptr = strtok(arg, ",");
  578. if (nextptr == NULL)
  579. return "Invalid parameters for set temp overheat";
  580. val = atoi(nextptr);
  581. if (val < 0 || val > 200)
  582. return "Invalid value passed to set temp overheat";
  583. to = &gpus[device++].adl.overtemp;
  584. *to = val;
  585. while ((nextptr = strtok(NULL, ",")) != NULL) {
  586. val = atoi(nextptr);
  587. if (val < 0 || val > 200)
  588. return "Invalid value passed to set temp overheat";
  589. to = &gpus[device++].adl.overtemp;
  590. *to = val;
  591. }
  592. if (device == 1) {
  593. for (i = device; i < MAX_GPUDEVICES; i++) {
  594. to = &gpus[i].adl.overtemp;
  595. *to = val;
  596. }
  597. }
  598. return NULL;
  599. }
  600. char *set_temp_target(char *arg)
  601. {
  602. int i, val = 0, device = 0, *tt;
  603. char *nextptr;
  604. nextptr = strtok(arg, ",");
  605. if (nextptr == NULL)
  606. return "Invalid parameters for set temp target";
  607. val = atoi(nextptr);
  608. if (val < 0 || val > 200)
  609. return "Invalid value passed to set temp target";
  610. tt = &gpus[device++].adl.targettemp;
  611. *tt = val;
  612. while ((nextptr = strtok(NULL, ",")) != NULL) {
  613. val = atoi(nextptr);
  614. if (val < 0 || val > 200)
  615. return "Invalid value passed to set temp target";
  616. tt = &gpus[device++].adl.targettemp;
  617. *tt = val;
  618. }
  619. if (device == 1) {
  620. for (i = device; i < MAX_GPUDEVICES; i++) {
  621. tt = &gpus[i].adl.targettemp;
  622. *tt = val;
  623. }
  624. }
  625. return NULL;
  626. }
  627. #endif
  628. #ifdef HAVE_OPENCL
  629. char *set_intensity(char *arg)
  630. {
  631. int i, device = 0, *tt;
  632. char *nextptr, val = 0;
  633. nextptr = strtok(arg, ",");
  634. if (nextptr == NULL)
  635. return "Invalid parameters for set intensity";
  636. if (!strncasecmp(nextptr, "d", 1))
  637. gpus[device].dynamic = true;
  638. else {
  639. gpus[device].dynamic = false;
  640. val = atoi(nextptr);
  641. if (val < MIN_INTENSITY || val > MAX_INTENSITY)
  642. return "Invalid value passed to set intensity";
  643. tt = &gpus[device].intensity;
  644. *tt = val;
  645. }
  646. device++;
  647. while ((nextptr = strtok(NULL, ",")) != NULL) {
  648. if (!strncasecmp(nextptr, "d", 1))
  649. gpus[device].dynamic = true;
  650. else {
  651. gpus[device].dynamic = false;
  652. val = atoi(nextptr);
  653. if (val < MIN_INTENSITY || val > MAX_INTENSITY)
  654. return "Invalid value passed to set intensity";
  655. tt = &gpus[device].intensity;
  656. *tt = val;
  657. }
  658. device++;
  659. }
  660. if (device == 1) {
  661. for (i = device; i < MAX_GPUDEVICES; i++) {
  662. gpus[i].dynamic = gpus[0].dynamic;
  663. gpus[i].intensity = gpus[0].intensity;
  664. }
  665. }
  666. return NULL;
  667. }
  668. #endif
  669. #ifdef HAVE_OPENCL
  670. struct device_api opencl_api;
  671. char *print_ndevs_and_exit(int *ndevs)
  672. {
  673. opt_log_output = true;
  674. opencl_api.api_detect();
  675. clear_adl(*ndevs);
  676. applog(LOG_INFO, "%i GPU devices max detected", *ndevs);
  677. exit(*ndevs);
  678. }
  679. #endif
  680. struct cgpu_info gpus[MAX_GPUDEVICES]; /* Maximum number apparently possible */
  681. struct cgpu_info *cpus;
  682. #ifdef HAVE_OPENCL
  683. #ifdef WIN32
  684. static UINT timeperiod_set;
  685. #endif
  686. void opencl_dynamic_cleanup() {
  687. #ifdef WIN32
  688. if (timeperiod_set) {
  689. timeEndPeriod(timeperiod_set);
  690. timeperiod_set = 0;
  691. }
  692. #endif
  693. }
  694. extern int opt_dynamic_interval;
  695. /* In dynamic mode, only the first thread of each device will be in use.
  696. * This potentially could start a thread that was stopped with the start-stop
  697. * options if one were to disable dynamic from the menu on a paused GPU */
  698. void pause_dynamic_threads(int gpu)
  699. {
  700. struct cgpu_info *cgpu = &gpus[gpu];
  701. int i;
  702. #ifdef WIN32
  703. bool any_dynamic = false;
  704. #endif
  705. for (i = 1; i < cgpu->threads; i++) {
  706. struct thr_info *thr = &thr_info[i];
  707. #ifdef WIN32
  708. if (cgpu->dynamic)
  709. any_dynamic = true;
  710. #endif
  711. if (!thr->pause && cgpu->dynamic) {
  712. applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
  713. applog(LOG_WARNING, "Tune dynamic intensity with --gpu-dyninterval");
  714. }
  715. thr->pause = cgpu->dynamic;
  716. if (!cgpu->dynamic && cgpu->deven != DEV_DISABLED)
  717. tq_push(thr->q, &ping);
  718. }
  719. #ifdef WIN32
  720. if (any_dynamic) {
  721. if (!timeperiod_set) {
  722. timeperiod_set = opt_dynamic_interval > 3 ? (opt_dynamic_interval / 2) : 1;
  723. if (TIMERR_NOERROR != timeBeginPeriod(timeperiod_set))
  724. timeperiod_set = 0;
  725. }
  726. } else {
  727. if (timeperiod_set) {
  728. opencl_dynamic_cleanup();
  729. }
  730. }
  731. #endif
  732. }
  733. struct device_api opencl_api;
  734. #endif /* HAVE_OPENCL */
  735. #if defined(HAVE_OPENCL) && defined(HAVE_CURSES)
  736. void manage_gpu(void)
  737. {
  738. struct thr_info *thr;
  739. int selected, gpu, i;
  740. char checkin[40];
  741. char input;
  742. if (!opt_g_threads)
  743. return;
  744. opt_loginput = true;
  745. immedok(logwin, true);
  746. clear_logwin();
  747. retry:
  748. for (gpu = 0; gpu < nDevs; gpu++) {
  749. struct cgpu_info *cgpu = &gpus[gpu];
  750. double displayed_rolling, displayed_total;
  751. bool mhash_base = true;
  752. displayed_rolling = cgpu->rolling;
  753. displayed_total = cgpu->total_mhashes / total_secs;
  754. if (displayed_rolling < 1) {
  755. displayed_rolling *= 1000;
  756. displayed_total *= 1000;
  757. mhash_base = false;
  758. }
  759. wlog("GPU %d: %.1f / %.1f %sh/s | A:%d R:%d HW:%d U:%.2f/m I:%d\n",
  760. gpu, displayed_rolling, displayed_total, mhash_base ? "M" : "K",
  761. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  762. cgpu->utility, cgpu->intensity);
  763. #ifdef HAVE_ADL
  764. if (gpus[gpu].has_adl) {
  765. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  766. float temp = 0, vddc = 0;
  767. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune)) {
  768. char logline[255];
  769. strcpy(logline, ""); // In case it has no data
  770. if (temp != -1)
  771. sprintf(logline, "%.1f C ", temp);
  772. if (fanspeed != -1 || fanpercent != -1) {
  773. tailsprintf(logline, "F: ");
  774. if (fanpercent != -1)
  775. tailsprintf(logline, "%d%% ", fanpercent);
  776. if (fanspeed != -1)
  777. tailsprintf(logline, "(%d RPM) ", fanspeed);
  778. tailsprintf(logline, " ");
  779. }
  780. if (engineclock != -1)
  781. tailsprintf(logline, "E: %d MHz ", engineclock);
  782. if (memclock != -1)
  783. tailsprintf(logline, "M: %d Mhz ", memclock);
  784. if (vddc != -1)
  785. tailsprintf(logline, "V: %.3fV ", vddc);
  786. if (activity != -1)
  787. tailsprintf(logline, "A: %d%% ", activity);
  788. if (powertune != -1)
  789. tailsprintf(logline, "P: %d%%", powertune);
  790. tailsprintf(logline, "\n");
  791. wlog(logline);
  792. }
  793. }
  794. #endif
  795. wlog("Last initialised: %s\n", cgpu->init);
  796. wlog("Intensity: ");
  797. if (gpus[gpu].dynamic)
  798. wlog("Dynamic (only one thread in use)\n");
  799. else
  800. wlog("%d\n", gpus[gpu].intensity);
  801. for (i = 0; i < mining_threads; i++) {
  802. thr = &thr_info[i];
  803. if (thr->cgpu != cgpu)
  804. continue;
  805. get_datestamp(checkin, &thr->last);
  806. displayed_rolling = thr->rolling;
  807. if (!mhash_base)
  808. displayed_rolling *= 1000;
  809. wlog("Thread %d: %.1f %sh/s %s ", i, displayed_rolling, mhash_base ? "M" : "K" , cgpu->deven != DEV_DISABLED ? "Enabled" : "Disabled");
  810. switch (cgpu->status) {
  811. default:
  812. case LIFE_WELL:
  813. wlog("ALIVE");
  814. break;
  815. case LIFE_SICK:
  816. wlog("SICK reported in %s", checkin);
  817. break;
  818. case LIFE_DEAD:
  819. wlog("DEAD reported in %s", checkin);
  820. break;
  821. case LIFE_INIT:
  822. case LIFE_NOSTART:
  823. wlog("Never started");
  824. break;
  825. }
  826. if (thr->pause)
  827. wlog(" paused");
  828. wlog("\n");
  829. }
  830. wlog("\n");
  831. }
  832. wlogprint("[E]nable [D]isable [I]ntensity [R]estart GPU %s\n",adl_active ? "[C]hange settings" : "");
  833. wlogprint("Or press any other key to continue\n");
  834. input = getch();
  835. if (nDevs == 1)
  836. selected = 0;
  837. else
  838. selected = -1;
  839. if (!strncasecmp(&input, "e", 1)) {
  840. struct cgpu_info *cgpu;
  841. if (selected)
  842. selected = curses_int("Select GPU to enable");
  843. if (selected < 0 || selected >= nDevs) {
  844. wlogprint("Invalid selection\n");
  845. goto retry;
  846. }
  847. if (gpus[selected].deven != DEV_DISABLED) {
  848. wlogprint("Device already enabled\n");
  849. goto retry;
  850. }
  851. gpus[selected].deven = DEV_ENABLED;
  852. for (i = 0; i < mining_threads; ++i) {
  853. thr = &thr_info[i];
  854. cgpu = thr->cgpu;
  855. if (cgpu->api != &opencl_api)
  856. continue;
  857. if (dev_from_id(i) != selected)
  858. continue;
  859. if (cgpu->status != LIFE_WELL) {
  860. wlogprint("Must restart device before enabling it");
  861. goto retry;
  862. }
  863. applog(LOG_DEBUG, "Pushing ping to thread %d", thr->id);
  864. tq_push(thr->q, &ping);
  865. }
  866. goto retry;
  867. } if (!strncasecmp(&input, "d", 1)) {
  868. if (selected)
  869. selected = curses_int("Select GPU to disable");
  870. if (selected < 0 || selected >= nDevs) {
  871. wlogprint("Invalid selection\n");
  872. goto retry;
  873. }
  874. if (gpus[selected].deven == DEV_DISABLED) {
  875. wlogprint("Device already disabled\n");
  876. goto retry;
  877. }
  878. gpus[selected].deven = DEV_DISABLED;
  879. goto retry;
  880. } else if (!strncasecmp(&input, "i", 1)) {
  881. int intensity;
  882. char *intvar;
  883. if (selected)
  884. selected = curses_int("Select GPU to change intensity on");
  885. if (selected < 0 || selected >= nDevs) {
  886. wlogprint("Invalid selection\n");
  887. goto retry;
  888. }
  889. intvar = curses_input("Set GPU scan intensity (d or " _MIN_INTENSITY_STR " -> " _MAX_INTENSITY_STR ")");
  890. if (!intvar) {
  891. wlogprint("Invalid input\n");
  892. goto retry;
  893. }
  894. if (!strncasecmp(intvar, "d", 1)) {
  895. wlogprint("Dynamic mode enabled on gpu %d\n", selected);
  896. gpus[selected].dynamic = true;
  897. pause_dynamic_threads(selected);
  898. free(intvar);
  899. goto retry;
  900. }
  901. intensity = atoi(intvar);
  902. free(intvar);
  903. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  904. wlogprint("Invalid selection\n");
  905. goto retry;
  906. }
  907. gpus[selected].dynamic = false;
  908. gpus[selected].intensity = intensity;
  909. wlogprint("Intensity on gpu %d set to %d\n", selected, intensity);
  910. pause_dynamic_threads(selected);
  911. goto retry;
  912. } else if (!strncasecmp(&input, "r", 1)) {
  913. if (selected)
  914. selected = curses_int("Select GPU to attempt to restart");
  915. if (selected < 0 || selected >= nDevs) {
  916. wlogprint("Invalid selection\n");
  917. goto retry;
  918. }
  919. wlogprint("Attempting to restart threads of GPU %d\n", selected);
  920. reinit_device(&gpus[selected]);
  921. goto retry;
  922. } else if (adl_active && (!strncasecmp(&input, "c", 1))) {
  923. if (selected)
  924. selected = curses_int("Select GPU to change settings on");
  925. if (selected < 0 || selected >= nDevs) {
  926. wlogprint("Invalid selection\n");
  927. goto retry;
  928. }
  929. change_gpusettings(selected);
  930. goto retry;
  931. } else
  932. clear_logwin();
  933. immedok(logwin, false);
  934. opt_loginput = false;
  935. }
  936. #else
  937. void manage_gpu(void)
  938. {
  939. }
  940. #endif
  941. #ifdef HAVE_OPENCL
  942. static _clState *clStates[MAX_GPUDEVICES];
  943. #define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
  944. #define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
  945. #define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
  946. static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  947. {
  948. cl_kernel *kernel = &clState->kernel;
  949. unsigned int num = 0;
  950. cl_int status = 0;
  951. CL_SET_BLKARG(ctx_a);
  952. CL_SET_BLKARG(ctx_b);
  953. CL_SET_BLKARG(ctx_c);
  954. CL_SET_BLKARG(ctx_d);
  955. CL_SET_BLKARG(ctx_e);
  956. CL_SET_BLKARG(ctx_f);
  957. CL_SET_BLKARG(ctx_g);
  958. CL_SET_BLKARG(ctx_h);
  959. CL_SET_BLKARG(cty_b);
  960. CL_SET_BLKARG(cty_c);
  961. CL_SET_BLKARG(cty_f);
  962. CL_SET_BLKARG(cty_g);
  963. CL_SET_BLKARG(cty_h);
  964. if (!clState->goffset) {
  965. cl_uint vwidth = clState->vwidth;
  966. uint *nonces = alloca(sizeof(uint) * vwidth);
  967. unsigned int i;
  968. for (i = 0; i < vwidth; i++)
  969. nonces[i] = blk->nonce + (i * threads);
  970. CL_SET_VARG(vwidth, nonces);
  971. }
  972. CL_SET_BLKARG(fW0);
  973. CL_SET_BLKARG(fW1);
  974. CL_SET_BLKARG(fW2);
  975. CL_SET_BLKARG(fW3);
  976. CL_SET_BLKARG(fW15);
  977. CL_SET_BLKARG(fW01r);
  978. CL_SET_BLKARG(D1A);
  979. CL_SET_BLKARG(C1addK5);
  980. CL_SET_BLKARG(B1addK6);
  981. CL_SET_BLKARG(W16addK16);
  982. CL_SET_BLKARG(W17addK17);
  983. CL_SET_BLKARG(PreVal4addT1);
  984. CL_SET_BLKARG(PreVal0);
  985. CL_SET_ARG(clState->outputBuffer);
  986. return status;
  987. }
  988. static cl_int queue_phatk_kernel(_clState *clState, dev_blk_ctx *blk,
  989. __maybe_unused cl_uint threads)
  990. {
  991. cl_kernel *kernel = &clState->kernel;
  992. cl_uint vwidth = clState->vwidth;
  993. unsigned int i, num = 0;
  994. cl_int status = 0;
  995. uint *nonces;
  996. CL_SET_BLKARG(ctx_a);
  997. CL_SET_BLKARG(ctx_b);
  998. CL_SET_BLKARG(ctx_c);
  999. CL_SET_BLKARG(ctx_d);
  1000. CL_SET_BLKARG(ctx_e);
  1001. CL_SET_BLKARG(ctx_f);
  1002. CL_SET_BLKARG(ctx_g);
  1003. CL_SET_BLKARG(ctx_h);
  1004. CL_SET_BLKARG(cty_b);
  1005. CL_SET_BLKARG(cty_c);
  1006. CL_SET_BLKARG(cty_d);
  1007. CL_SET_BLKARG(cty_f);
  1008. CL_SET_BLKARG(cty_g);
  1009. CL_SET_BLKARG(cty_h);
  1010. nonces = alloca(sizeof(uint) * vwidth);
  1011. for (i = 0; i < vwidth; i++)
  1012. nonces[i] = blk->nonce + i;
  1013. CL_SET_VARG(vwidth, nonces);
  1014. CL_SET_BLKARG(W16);
  1015. CL_SET_BLKARG(W17);
  1016. CL_SET_BLKARG(PreVal4_2);
  1017. CL_SET_BLKARG(PreVal0);
  1018. CL_SET_BLKARG(PreW18);
  1019. CL_SET_BLKARG(PreW19);
  1020. CL_SET_BLKARG(PreW31);
  1021. CL_SET_BLKARG(PreW32);
  1022. CL_SET_ARG(clState->outputBuffer);
  1023. return status;
  1024. }
  1025. static cl_int queue_diakgcn_kernel(_clState *clState, dev_blk_ctx *blk,
  1026. __maybe_unused cl_uint threads)
  1027. {
  1028. cl_kernel *kernel = &clState->kernel;
  1029. unsigned int num = 0;
  1030. cl_int status = 0;
  1031. if (!clState->goffset) {
  1032. cl_uint vwidth = clState->vwidth;
  1033. uint *nonces = alloca(sizeof(uint) * vwidth);
  1034. unsigned int i;
  1035. for (i = 0; i < vwidth; i++)
  1036. nonces[i] = blk->nonce + i;
  1037. CL_SET_VARG(vwidth, nonces);
  1038. }
  1039. CL_SET_BLKARG(PreVal0);
  1040. CL_SET_BLKARG(PreVal4_2);
  1041. CL_SET_BLKARG(cty_h);
  1042. CL_SET_BLKARG(D1A);
  1043. CL_SET_BLKARG(cty_b);
  1044. CL_SET_BLKARG(cty_c);
  1045. CL_SET_BLKARG(cty_f);
  1046. CL_SET_BLKARG(cty_g);
  1047. CL_SET_BLKARG(C1addK5);
  1048. CL_SET_BLKARG(B1addK6);
  1049. CL_SET_BLKARG(PreVal0addK7);
  1050. CL_SET_BLKARG(W16addK16);
  1051. CL_SET_BLKARG(W17addK17);
  1052. CL_SET_BLKARG(PreW18);
  1053. CL_SET_BLKARG(PreW19);
  1054. CL_SET_BLKARG(W16);
  1055. CL_SET_BLKARG(W17);
  1056. CL_SET_BLKARG(PreW31);
  1057. CL_SET_BLKARG(PreW32);
  1058. CL_SET_BLKARG(ctx_a);
  1059. CL_SET_BLKARG(ctx_b);
  1060. CL_SET_BLKARG(ctx_c);
  1061. CL_SET_BLKARG(ctx_d);
  1062. CL_SET_BLKARG(ctx_e);
  1063. CL_SET_BLKARG(ctx_f);
  1064. CL_SET_BLKARG(ctx_g);
  1065. CL_SET_BLKARG(ctx_h);
  1066. CL_SET_BLKARG(zeroA);
  1067. CL_SET_BLKARG(zeroB);
  1068. CL_SET_BLKARG(oneA);
  1069. CL_SET_BLKARG(twoA);
  1070. CL_SET_BLKARG(threeA);
  1071. CL_SET_BLKARG(fourA);
  1072. CL_SET_BLKARG(fiveA);
  1073. CL_SET_BLKARG(sixA);
  1074. CL_SET_BLKARG(sevenA);
  1075. CL_SET_ARG(clState->outputBuffer);
  1076. return status;
  1077. }
  1078. static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  1079. {
  1080. cl_kernel *kernel = &clState->kernel;
  1081. unsigned int num = 0;
  1082. cl_int status = 0;
  1083. if (!clState->goffset) {
  1084. cl_uint vwidth = clState->vwidth;
  1085. uint *nonces = alloca(sizeof(uint) * vwidth);
  1086. unsigned int i;
  1087. for (i = 0; i < vwidth; i++)
  1088. nonces[i] = blk->nonce + (i * threads);
  1089. CL_SET_VARG(vwidth, nonces);
  1090. }
  1091. CL_SET_BLKARG(PreVal0);
  1092. CL_SET_BLKARG(PreVal0addK7);
  1093. CL_SET_BLKARG(PreVal4addT1);
  1094. CL_SET_BLKARG(PreW18);
  1095. CL_SET_BLKARG(PreW19);
  1096. CL_SET_BLKARG(W16);
  1097. CL_SET_BLKARG(W17);
  1098. CL_SET_BLKARG(W16addK16);
  1099. CL_SET_BLKARG(W17addK17);
  1100. CL_SET_BLKARG(PreW31);
  1101. CL_SET_BLKARG(PreW32);
  1102. CL_SET_BLKARG(D1A);
  1103. CL_SET_BLKARG(cty_b);
  1104. CL_SET_BLKARG(cty_c);
  1105. CL_SET_BLKARG(cty_h);
  1106. CL_SET_BLKARG(cty_f);
  1107. CL_SET_BLKARG(cty_g);
  1108. CL_SET_BLKARG(C1addK5);
  1109. CL_SET_BLKARG(B1addK6);
  1110. CL_SET_BLKARG(ctx_a);
  1111. CL_SET_BLKARG(ctx_b);
  1112. CL_SET_BLKARG(ctx_c);
  1113. CL_SET_BLKARG(ctx_d);
  1114. CL_SET_BLKARG(ctx_e);
  1115. CL_SET_BLKARG(ctx_f);
  1116. CL_SET_BLKARG(ctx_g);
  1117. CL_SET_BLKARG(ctx_h);
  1118. CL_SET_ARG(clState->outputBuffer);
  1119. return status;
  1120. }
  1121. #ifdef USE_SCRYPT
  1122. static cl_int queue_scrypt_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unused cl_uint threads)
  1123. {
  1124. unsigned char *midstate = blk->work->midstate;
  1125. cl_kernel *kernel = &clState->kernel;
  1126. unsigned int num = 0;
  1127. cl_uint le_target;
  1128. cl_int status = 0;
  1129. le_target = *(cl_uint *)(blk->work->target + 28);
  1130. clState->cldata = blk->work->data;
  1131. status = clEnqueueWriteBuffer(clState->commandQueue, clState->CLbuffer0, true, 0, 80, clState->cldata, 0, NULL,NULL);
  1132. CL_SET_ARG(clState->CLbuffer0);
  1133. CL_SET_ARG(clState->outputBuffer);
  1134. CL_SET_ARG(clState->padbuffer8);
  1135. CL_SET_VARG(4, &midstate[0]);
  1136. CL_SET_VARG(4, &midstate[16]);
  1137. CL_SET_ARG(le_target);
  1138. return status;
  1139. }
  1140. #endif
  1141. static void set_threads_hashes(unsigned int vectors, unsigned int *threads,
  1142. int64_t *hashes, size_t *globalThreads,
  1143. unsigned int minthreads, int intensity)
  1144. {
  1145. if (opt_scrypt) {
  1146. if (intensity < 0)
  1147. intensity = 0;
  1148. *threads = 1 << intensity;
  1149. } else
  1150. *threads = 1 << (15 + intensity);
  1151. if (*threads < minthreads)
  1152. *threads = minthreads;
  1153. *globalThreads = *threads;
  1154. *hashes = *threads * vectors;
  1155. }
  1156. #endif /* HAVE_OPENCL */
  1157. #ifdef HAVE_OPENCL
  1158. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  1159. * init command fails due to a completely wedged GPU, the thread will never
  1160. * return, unable to harm other GPUs. If it does return, it means we only had
  1161. * a soft failure and then the reinit_gpu thread is ready to tackle another
  1162. * GPU */
  1163. void *reinit_gpu(void *userdata)
  1164. {
  1165. struct thr_info *mythr = userdata;
  1166. struct cgpu_info *cgpu;
  1167. struct thr_info *thr;
  1168. struct timeval now;
  1169. char name[256];
  1170. int thr_id;
  1171. int gpu;
  1172. pthread_detach(pthread_self());
  1173. rename_thr("bfg-reinit_gpu");
  1174. select_cgpu:
  1175. cgpu = tq_pop(mythr->q, NULL);
  1176. if (!cgpu)
  1177. goto out;
  1178. if (clDevicesNum() != nDevs) {
  1179. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  1180. goto out;
  1181. }
  1182. gpu = cgpu->device_id;
  1183. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  1184. thr = &thr_info[thr_id];
  1185. cgpu = thr->cgpu;
  1186. if (cgpu->api != &opencl_api)
  1187. continue;
  1188. if (dev_from_id(thr_id) != gpu)
  1189. continue;
  1190. thr = &thr_info[thr_id];
  1191. if (!thr) {
  1192. applog(LOG_WARNING, "No reference to thread %d exists", thr_id);
  1193. continue;
  1194. }
  1195. thr->rolling = thr->cgpu->rolling = 0;
  1196. /* Reports the last time we tried to revive a sick GPU */
  1197. gettimeofday(&thr->sick, NULL);
  1198. if (!pthread_cancel(thr->pth)) {
  1199. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  1200. } else
  1201. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  1202. }
  1203. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  1204. int virtual_gpu;
  1205. thr = &thr_info[thr_id];
  1206. cgpu = thr->cgpu;
  1207. if (cgpu->api != &opencl_api)
  1208. continue;
  1209. if (dev_from_id(thr_id) != gpu)
  1210. continue;
  1211. virtual_gpu = cgpu->virtual_gpu;
  1212. /* Lose this ram cause we may get stuck here! */
  1213. //tq_freeze(thr->q);
  1214. thr->q = tq_new();
  1215. if (!thr->q)
  1216. quit(1, "Failed to tq_new in reinit_gpu");
  1217. /* Lose this ram cause we may dereference in the dying thread! */
  1218. //free(clState);
  1219. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  1220. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  1221. if (!clStates[thr_id]) {
  1222. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  1223. goto select_cgpu;
  1224. }
  1225. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1226. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  1227. applog(LOG_ERR, "thread %d create failed", thr_id);
  1228. return NULL;
  1229. }
  1230. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  1231. }
  1232. gettimeofday(&now, NULL);
  1233. get_datestamp(cgpu->init, &now);
  1234. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  1235. thr = &thr_info[thr_id];
  1236. cgpu = thr->cgpu;
  1237. if (cgpu->api != &opencl_api)
  1238. continue;
  1239. if (dev_from_id(thr_id) != gpu)
  1240. continue;
  1241. tq_push(thr->q, &ping);
  1242. }
  1243. goto select_cgpu;
  1244. out:
  1245. return NULL;
  1246. }
  1247. #else
  1248. void *reinit_gpu(__maybe_unused void *userdata)
  1249. {
  1250. return NULL;
  1251. }
  1252. #endif
  1253. #ifdef HAVE_OPENCL
  1254. struct device_api opencl_api;
  1255. static void opencl_detect()
  1256. {
  1257. if (!load_opencl_symbols()) {
  1258. nDevs = 0;
  1259. return;
  1260. }
  1261. int i;
  1262. nDevs = clDevicesNum();
  1263. if (nDevs < 0) {
  1264. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  1265. nDevs = 0;
  1266. }
  1267. if (!nDevs)
  1268. return;
  1269. for (i = 0; i < nDevs; ++i) {
  1270. struct cgpu_info *cgpu;
  1271. cgpu = &gpus[i];
  1272. cgpu->devtype = "GPU";
  1273. cgpu->deven = DEV_ENABLED;
  1274. cgpu->api = &opencl_api;
  1275. cgpu->device_id = i;
  1276. cgpu->threads = opt_g_threads;
  1277. cgpu->virtual_gpu = i;
  1278. add_cgpu(cgpu);
  1279. }
  1280. if (!opt_noadl)
  1281. init_adl(nDevs);
  1282. }
  1283. static void reinit_opencl_device(struct cgpu_info *gpu)
  1284. {
  1285. tq_push(thr_info[gpur_thr_id].q, gpu);
  1286. }
  1287. #ifdef HAVE_ADL
  1288. static void get_opencl_statline_before(char *buf, struct cgpu_info *gpu)
  1289. {
  1290. if (gpu->has_adl) {
  1291. int gpuid = gpu->device_id;
  1292. float gt = gpu_temp(gpuid);
  1293. int gf = gpu_fanspeed(gpuid);
  1294. int gp;
  1295. if (gt != -1)
  1296. tailsprintf(buf, "%5.1fC ", gt);
  1297. else
  1298. tailsprintf(buf, " ", gt);
  1299. if (gf != -1)
  1300. tailsprintf(buf, "%4dRPM ", gf);
  1301. else if ((gp = gpu_fanpercent(gpuid)) != -1)
  1302. tailsprintf(buf, "%3d%% ", gp);
  1303. else
  1304. tailsprintf(buf, " ");
  1305. tailsprintf(buf, "| ");
  1306. }
  1307. else
  1308. tailsprintf(buf, " | ");
  1309. }
  1310. #endif
  1311. static struct api_data*
  1312. get_opencl_api_extra_device_status(struct cgpu_info *gpu)
  1313. {
  1314. struct api_data*root = NULL;
  1315. float gt, gv;
  1316. int ga, gf, gp, gc, gm, pt;
  1317. #ifdef HAVE_ADL
  1318. if (!gpu_stats(gpu->device_id, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  1319. #endif
  1320. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  1321. root = api_add_int(root, "Fan Speed", &gf, true);
  1322. root = api_add_int(root, "Fan Percent", &gp, true);
  1323. root = api_add_int(root, "GPU Clock", &gc, true);
  1324. root = api_add_int(root, "Memory Clock", &gm, true);
  1325. root = api_add_volts(root, "GPU Voltage", &gv, true);
  1326. root = api_add_int(root, "GPU Activity", &ga, true);
  1327. root = api_add_int(root, "Powertune", &pt, true);
  1328. char intensity[20];
  1329. if (gpu->dynamic)
  1330. strcpy(intensity, "D");
  1331. else
  1332. sprintf(intensity, "%d", gpu->intensity);
  1333. root = api_add_string(root, "Intensity", intensity, true);
  1334. return root;
  1335. }
  1336. struct opencl_thread_data {
  1337. cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *, cl_uint);
  1338. uint32_t *res;
  1339. struct work *last_work;
  1340. struct work _last_work;
  1341. };
  1342. static uint32_t *blank_res;
  1343. static bool opencl_thread_prepare(struct thr_info *thr)
  1344. {
  1345. char name[256];
  1346. struct timeval now;
  1347. struct cgpu_info *cgpu = thr->cgpu;
  1348. int gpu = cgpu->device_id;
  1349. int virtual_gpu = cgpu->virtual_gpu;
  1350. int i = thr->id;
  1351. static bool failmessage = false;
  1352. if (!blank_res)
  1353. blank_res = calloc(BUFFERSIZE, 1);
  1354. if (!blank_res) {
  1355. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1356. return false;
  1357. }
  1358. strcpy(name, "");
  1359. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  1360. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  1361. if (!clStates[i]) {
  1362. #ifdef HAVE_CURSES
  1363. if (use_curses)
  1364. enable_curses();
  1365. #endif
  1366. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  1367. if (!failmessage) {
  1368. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  1369. applog(LOG_ERR, "Try restarting BFGMiner.");
  1370. failmessage = true;
  1371. #ifdef HAVE_CURSES
  1372. char *buf;
  1373. if (use_curses) {
  1374. buf = curses_input("Press enter to continue");
  1375. if (buf)
  1376. free(buf);
  1377. }
  1378. #endif
  1379. }
  1380. cgpu->deven = DEV_DISABLED;
  1381. cgpu->status = LIFE_NOSTART;
  1382. cgpu->device_last_not_well = time(NULL);
  1383. cgpu->device_not_well_reason = REASON_DEV_NOSTART;
  1384. cgpu->dev_nostart_count++;
  1385. return false;
  1386. }
  1387. if (!cgpu->name)
  1388. cgpu->name = strdup(name);
  1389. if (!cgpu->kname)
  1390. {
  1391. switch (clStates[i]->chosen_kernel) {
  1392. case KL_DIABLO:
  1393. cgpu->kname = "diablo";
  1394. break;
  1395. case KL_DIAKGCN:
  1396. cgpu->kname = "diakgcn";
  1397. break;
  1398. case KL_PHATK:
  1399. cgpu->kname = "phatk";
  1400. break;
  1401. #ifdef USE_SCRYPT
  1402. case KL_SCRYPT:
  1403. cgpu->kname = "scrypt";
  1404. break;
  1405. #endif
  1406. case KL_POCLBM:
  1407. cgpu->kname = "poclbm";
  1408. break;
  1409. default:
  1410. break;
  1411. }
  1412. }
  1413. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1414. gettimeofday(&now, NULL);
  1415. get_datestamp(cgpu->init, &now);
  1416. have_opencl = true;
  1417. return true;
  1418. }
  1419. static bool opencl_thread_init(struct thr_info *thr)
  1420. {
  1421. const int thr_id = thr->id;
  1422. struct cgpu_info *gpu = thr->cgpu;
  1423. struct opencl_thread_data *thrdata;
  1424. _clState *clState = clStates[thr_id];
  1425. cl_int status = 0;
  1426. thrdata = calloc(1, sizeof(*thrdata));
  1427. thr->cgpu_data = thrdata;
  1428. if (!thrdata) {
  1429. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1430. return false;
  1431. }
  1432. switch (clState->chosen_kernel) {
  1433. case KL_POCLBM:
  1434. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  1435. break;
  1436. case KL_PHATK:
  1437. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  1438. break;
  1439. case KL_DIAKGCN:
  1440. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  1441. break;
  1442. #ifdef USE_SCRYPT
  1443. case KL_SCRYPT:
  1444. thrdata->queue_kernel_parameters = &queue_scrypt_kernel;
  1445. break;
  1446. #endif
  1447. default:
  1448. case KL_DIABLO:
  1449. thrdata->queue_kernel_parameters = &queue_diablo_kernel;
  1450. break;
  1451. }
  1452. thrdata->res = calloc(BUFFERSIZE, 1);
  1453. if (!thrdata->res) {
  1454. free(thrdata);
  1455. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1456. return false;
  1457. }
  1458. status |= clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1459. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1460. if (unlikely(status != CL_SUCCESS)) {
  1461. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1462. return false;
  1463. }
  1464. gpu->status = LIFE_WELL;
  1465. gpu->device_last_well = time(NULL);
  1466. return true;
  1467. }
  1468. static void opencl_free_work(struct thr_info *thr, struct work *work)
  1469. {
  1470. const int thr_id = thr->id;
  1471. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1472. _clState *clState = clStates[thr_id];
  1473. struct cgpu_info *gpu = thr->cgpu;
  1474. if (gpu->dynamic)
  1475. return;
  1476. clFinish(clState->commandQueue);
  1477. if (thrdata->res[FOUND]) {
  1478. thrdata->last_work = &thrdata->_last_work;
  1479. memcpy(thrdata->last_work, work, sizeof(*thrdata->last_work));
  1480. }
  1481. }
  1482. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  1483. {
  1484. #ifdef USE_SCRYPT
  1485. if (opt_scrypt)
  1486. work->blk.work = work;
  1487. else
  1488. #endif
  1489. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1490. return true;
  1491. }
  1492. extern int opt_dynamic_interval;
  1493. static int64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1494. int64_t __maybe_unused max_nonce)
  1495. {
  1496. const int thr_id = thr->id;
  1497. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1498. struct cgpu_info *gpu = thr->cgpu;
  1499. _clState *clState = clStates[thr_id];
  1500. const cl_kernel *kernel = &clState->kernel;
  1501. const int dynamic_us = opt_dynamic_interval * 1000;
  1502. cl_int status;
  1503. size_t globalThreads[1];
  1504. size_t localThreads[1] = { clState->wsize };
  1505. unsigned int threads;
  1506. int64_t hashes;
  1507. /* This finish flushes the readbuffer set with CL_FALSE later */
  1508. if (!gpu->dynamic)
  1509. clFinish(clState->commandQueue);
  1510. set_threads_hashes(clState->vwidth, &threads, &hashes, globalThreads,
  1511. localThreads[0], gpu->intensity);
  1512. if (hashes > gpu->max_hashes)
  1513. gpu->max_hashes = hashes;
  1514. /* FOUND entry is used as a counter to say how many nonces exist */
  1515. if (thrdata->res[FOUND]) {
  1516. /* Clear the buffer again */
  1517. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1518. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1519. if (unlikely(status != CL_SUCCESS)) {
  1520. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1521. return -1;
  1522. }
  1523. if (unlikely(thrdata->last_work)) {
  1524. applog(LOG_DEBUG, "GPU %d found something in last work?", gpu->device_id);
  1525. postcalc_hash_async(thr, thrdata->last_work, thrdata->res);
  1526. thrdata->last_work = NULL;
  1527. } else {
  1528. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1529. postcalc_hash_async(thr, work, thrdata->res);
  1530. }
  1531. memset(thrdata->res, 0, BUFFERSIZE);
  1532. clFinish(clState->commandQueue);
  1533. }
  1534. gettimeofday(&gpu->tv_gpustart, NULL);
  1535. status = thrdata->queue_kernel_parameters(clState, &work->blk, globalThreads[0]);
  1536. if (unlikely(status != CL_SUCCESS)) {
  1537. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1538. return -1;
  1539. }
  1540. if (clState->goffset) {
  1541. size_t global_work_offset[1];
  1542. global_work_offset[0] = work->blk.nonce;
  1543. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, global_work_offset,
  1544. globalThreads, localThreads, 0, NULL, NULL);
  1545. } else
  1546. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1547. globalThreads, localThreads, 0, NULL, NULL);
  1548. if (unlikely(status != CL_SUCCESS)) {
  1549. applog(LOG_ERR, "Error %d: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)", status);
  1550. return -1;
  1551. }
  1552. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1553. BUFFERSIZE, thrdata->res, 0, NULL, NULL);
  1554. if (unlikely(status != CL_SUCCESS)) {
  1555. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed error %d. (clEnqueueReadBuffer)", status);
  1556. return -1;
  1557. }
  1558. if (gpu->dynamic) {
  1559. struct timeval diff;
  1560. suseconds_t gpu_us;
  1561. clFinish(clState->commandQueue);
  1562. gettimeofday(&gpu->tv_gpuend, NULL);
  1563. timersub(&gpu->tv_gpuend, &gpu->tv_gpustart, &diff);
  1564. gpu_us = diff.tv_sec * 1000000 + diff.tv_usec;
  1565. if (likely(gpu_us >= 0)) {
  1566. gpu->gpu_us_average = (gpu->gpu_us_average + gpu_us * 0.63) / 1.63;
  1567. /* Try to not let the GPU be out for longer than
  1568. * opt_dynamic_interval in ms, but increase
  1569. * intensity when the system is idle in dynamic mode */
  1570. if (gpu->gpu_us_average > dynamic_us) {
  1571. if (gpu->intensity > MIN_INTENSITY)
  1572. --gpu->intensity;
  1573. } else if (gpu->gpu_us_average < dynamic_us / 2) {
  1574. if (gpu->intensity < MAX_INTENSITY)
  1575. ++gpu->intensity;
  1576. }
  1577. }
  1578. }
  1579. /* The amount of work scanned can fluctuate when intensity changes
  1580. * and since we do this one cycle behind, we increment the work more
  1581. * than enough to prevent repeating work */
  1582. work->blk.nonce += gpu->max_hashes;
  1583. return hashes;
  1584. }
  1585. static void opencl_thread_shutdown(struct thr_info *thr)
  1586. {
  1587. const int thr_id = thr->id;
  1588. _clState *clState = clStates[thr_id];
  1589. clReleaseCommandQueue(clState->commandQueue);
  1590. clReleaseKernel(clState->kernel);
  1591. clReleaseProgram(clState->program);
  1592. clReleaseContext(clState->context);
  1593. }
  1594. struct device_api opencl_api = {
  1595. .dname = "opencl",
  1596. .name = "OCL",
  1597. .api_detect = opencl_detect,
  1598. .reinit_device = reinit_opencl_device,
  1599. #ifdef HAVE_ADL
  1600. .get_statline_before = get_opencl_statline_before,
  1601. #endif
  1602. .get_api_extra_device_status = get_opencl_api_extra_device_status,
  1603. .thread_prepare = opencl_thread_prepare,
  1604. .thread_init = opencl_thread_init,
  1605. .free_work = opencl_free_work,
  1606. .prepare_work = opencl_prepare_work,
  1607. .scanhash = opencl_scanhash,
  1608. .thread_shutdown = opencl_thread_shutdown,
  1609. };
  1610. #endif