driver-opencl.c 44 KB

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