driver-opencl.c 45 KB

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