driver-opencl.c 46 KB

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