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