driver-opencl.c 51 KB

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