driver-opencl.c 47 KB

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