driver-opencl.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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. /* Platform API */
  37. CL_API_ENTRY cl_int CL_API_CALL
  38. (*clGetPlatformIDs)(cl_uint /* num_entries */,
  39. cl_platform_id * /* platforms */,
  40. cl_uint * /* num_platforms */) CL_API_SUFFIX__VERSION_1_0;
  41. CL_API_ENTRY cl_int CL_API_CALL
  42. (*clGetPlatformInfo)(cl_platform_id /* platform */,
  43. cl_platform_info /* param_name */,
  44. size_t /* param_value_size */,
  45. void * /* param_value */,
  46. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  47. /* Device APIs */
  48. CL_API_ENTRY cl_int CL_API_CALL
  49. (*clGetDeviceIDs)(cl_platform_id /* platform */,
  50. cl_device_type /* device_type */,
  51. cl_uint /* num_entries */,
  52. cl_device_id * /* devices */,
  53. cl_uint * /* num_devices */) CL_API_SUFFIX__VERSION_1_0;
  54. CL_API_ENTRY cl_int CL_API_CALL
  55. (*clGetDeviceInfo)(cl_device_id /* device */,
  56. cl_device_info /* param_name */,
  57. size_t /* param_value_size */,
  58. void * /* param_value */,
  59. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  60. /* Context APIs */
  61. CL_API_ENTRY cl_context CL_API_CALL
  62. (*clCreateContextFromType)(const cl_context_properties * /* properties */,
  63. cl_device_type /* device_type */,
  64. void (CL_CALLBACK * /* pfn_notify*/ )(const char *, const void *, size_t, void *),
  65. void * /* user_data */,
  66. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  67. CL_API_ENTRY cl_int CL_API_CALL
  68. (*clReleaseContext)(cl_context /* context */) CL_API_SUFFIX__VERSION_1_0;
  69. /* Command Queue APIs */
  70. CL_API_ENTRY cl_command_queue CL_API_CALL
  71. (*clCreateCommandQueue)(cl_context /* context */,
  72. cl_device_id /* device */,
  73. cl_command_queue_properties /* properties */,
  74. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  75. CL_API_ENTRY cl_int CL_API_CALL
  76. (*clReleaseCommandQueue)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  77. /* Memory Object APIs */
  78. CL_API_ENTRY cl_mem CL_API_CALL
  79. (*clCreateBuffer)(cl_context /* context */,
  80. cl_mem_flags /* flags */,
  81. size_t /* size */,
  82. void * /* host_ptr */,
  83. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  84. /* Program Object APIs */
  85. CL_API_ENTRY cl_program CL_API_CALL
  86. (*clCreateProgramWithSource)(cl_context /* context */,
  87. cl_uint /* count */,
  88. const char ** /* strings */,
  89. const size_t * /* lengths */,
  90. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  91. CL_API_ENTRY cl_program CL_API_CALL
  92. (*clCreateProgramWithBinary)(cl_context /* context */,
  93. cl_uint /* num_devices */,
  94. const cl_device_id * /* device_list */,
  95. const size_t * /* lengths */,
  96. const unsigned char ** /* binaries */,
  97. cl_int * /* binary_status */,
  98. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  99. CL_API_ENTRY cl_int CL_API_CALL
  100. (*clReleaseProgram)(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
  101. CL_API_ENTRY cl_int CL_API_CALL
  102. (*clBuildProgram)(cl_program /* program */,
  103. cl_uint /* num_devices */,
  104. const cl_device_id * /* device_list */,
  105. const char * /* options */,
  106. void (CL_CALLBACK * /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
  107. void * /* user_data */) CL_API_SUFFIX__VERSION_1_0;
  108. CL_API_ENTRY cl_int CL_API_CALL
  109. (*clGetProgramInfo)(cl_program /* program */,
  110. cl_program_info /* param_name */,
  111. size_t /* param_value_size */,
  112. void * /* param_value */,
  113. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  114. CL_API_ENTRY cl_int CL_API_CALL
  115. (*clGetProgramBuildInfo)(cl_program /* program */,
  116. cl_device_id /* device */,
  117. cl_program_build_info /* param_name */,
  118. size_t /* param_value_size */,
  119. void * /* param_value */,
  120. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  121. /* Kernel Object APIs */
  122. CL_API_ENTRY cl_kernel CL_API_CALL
  123. (*clCreateKernel)(cl_program /* program */,
  124. const char * /* kernel_name */,
  125. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  126. CL_API_ENTRY cl_int CL_API_CALL
  127. (*clReleaseKernel)(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0;
  128. CL_API_ENTRY cl_int CL_API_CALL
  129. (*clSetKernelArg)(cl_kernel /* kernel */,
  130. cl_uint /* arg_index */,
  131. size_t /* arg_size */,
  132. const void * /* arg_value */) CL_API_SUFFIX__VERSION_1_0;
  133. /* Flush and Finish APIs */
  134. CL_API_ENTRY cl_int CL_API_CALL
  135. (*clFinish)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  136. /* Enqueued Commands APIs */
  137. CL_API_ENTRY cl_int CL_API_CALL
  138. (*clEnqueueReadBuffer)(cl_command_queue /* command_queue */,
  139. cl_mem /* buffer */,
  140. cl_bool /* blocking_read */,
  141. size_t /* offset */,
  142. size_t /* size */,
  143. void * /* ptr */,
  144. cl_uint /* num_events_in_wait_list */,
  145. const cl_event * /* event_wait_list */,
  146. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  147. CL_API_ENTRY cl_int CL_API_CALL
  148. (*clEnqueueWriteBuffer)(cl_command_queue /* command_queue */,
  149. cl_mem /* buffer */,
  150. cl_bool /* blocking_write */,
  151. size_t /* offset */,
  152. size_t /* size */,
  153. const void * /* ptr */,
  154. cl_uint /* num_events_in_wait_list */,
  155. const cl_event * /* event_wait_list */,
  156. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  157. CL_API_ENTRY cl_int CL_API_CALL
  158. (*clEnqueueNDRangeKernel)(cl_command_queue /* command_queue */,
  159. cl_kernel /* kernel */,
  160. cl_uint /* work_dim */,
  161. const size_t * /* global_work_offset */,
  162. const size_t * /* global_work_size */,
  163. const size_t * /* local_work_size */,
  164. cl_uint /* num_events_in_wait_list */,
  165. const cl_event * /* event_wait_list */,
  166. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  167. #ifdef WIN32
  168. #define dlsym (void*)GetProcAddress
  169. #define dlclose FreeLibrary
  170. #endif
  171. #define LOAD_OCL_SYM(sym) do { \
  172. if (!(sym = dlsym(cl, #sym))) { \
  173. applog(LOG_ERR, "Failed to load OpenCL symbol " #sym ", no GPUs usable"); \
  174. dlclose(cl); \
  175. return false; \
  176. } \
  177. } while(0)
  178. static bool
  179. load_opencl_symbols() {
  180. #if defined(__APPLE__)
  181. void *cl = dlopen("/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL", RTLD_LAZY);
  182. #elif !defined(WIN32)
  183. void *cl = dlopen("libOpenCL.so", RTLD_LAZY);
  184. #else
  185. HMODULE cl = LoadLibrary("OpenCL.dll");
  186. #endif
  187. if (!cl)
  188. {
  189. applog(LOG_ERR, "Failed to load OpenCL library, no GPUs usable");
  190. return false;
  191. }
  192. LOAD_OCL_SYM(clGetPlatformIDs);
  193. LOAD_OCL_SYM(clGetPlatformInfo);
  194. LOAD_OCL_SYM(clGetDeviceIDs);
  195. LOAD_OCL_SYM(clGetDeviceInfo);
  196. LOAD_OCL_SYM(clCreateContextFromType);
  197. LOAD_OCL_SYM(clReleaseContext);
  198. LOAD_OCL_SYM(clCreateCommandQueue);
  199. LOAD_OCL_SYM(clReleaseCommandQueue);
  200. LOAD_OCL_SYM(clCreateBuffer);
  201. LOAD_OCL_SYM(clCreateProgramWithSource);
  202. LOAD_OCL_SYM(clCreateProgramWithBinary);
  203. LOAD_OCL_SYM(clReleaseProgram);
  204. LOAD_OCL_SYM(clBuildProgram);
  205. LOAD_OCL_SYM(clGetProgramInfo);
  206. LOAD_OCL_SYM(clGetProgramBuildInfo);
  207. LOAD_OCL_SYM(clCreateKernel);
  208. LOAD_OCL_SYM(clReleaseKernel);
  209. LOAD_OCL_SYM(clSetKernelArg);
  210. LOAD_OCL_SYM(clFinish);
  211. LOAD_OCL_SYM(clEnqueueReadBuffer);
  212. LOAD_OCL_SYM(clEnqueueWriteBuffer);
  213. LOAD_OCL_SYM(clEnqueueNDRangeKernel);
  214. return true;
  215. }
  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, thread_no = 0;
  625. for (i = 0; i < mining_threads; i++) {
  626. struct thr_info *thr = &thr_info[i];
  627. if (thr->cgpu != cgpu)
  628. continue;
  629. if (!thread_no++)
  630. continue;
  631. if (!thr->pause && cgpu->dynamic) {
  632. applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
  633. applog(LOG_WARNING, "Tune dynamic intensity with --gpu-dyninterval");
  634. }
  635. thr->pause = cgpu->dynamic;
  636. if (!cgpu->dynamic && cgpu->deven != DEV_DISABLED)
  637. tq_push(thr->q, &ping);
  638. }
  639. }
  640. struct device_api opencl_api;
  641. #endif /* HAVE_OPENCL */
  642. #if defined(HAVE_OPENCL) && defined(HAVE_CURSES)
  643. void manage_gpu(void)
  644. {
  645. struct thr_info *thr;
  646. int selected, gpu, i;
  647. char checkin[40];
  648. char input;
  649. if (!opt_g_threads)
  650. return;
  651. opt_loginput = true;
  652. immedok(logwin, true);
  653. clear_logwin();
  654. retry:
  655. for (gpu = 0; gpu < nDevs; gpu++) {
  656. struct cgpu_info *cgpu = &gpus[gpu];
  657. wlog("GPU %d: %.1f / %.1f Mh/s | A:%d R:%d HW:%d U:%.2f/m I:%d\n",
  658. gpu, cgpu->rolling, cgpu->total_mhashes / total_secs,
  659. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  660. cgpu->utility, cgpu->intensity);
  661. #ifdef HAVE_ADL
  662. if (gpus[gpu].has_adl) {
  663. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  664. float temp = 0, vddc = 0;
  665. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune)) {
  666. char logline[255];
  667. strcpy(logline, ""); // In case it has no data
  668. if (temp != -1)
  669. sprintf(logline, "%.1f C ", temp);
  670. if (fanspeed != -1 || fanpercent != -1) {
  671. tailsprintf(logline, "F: ");
  672. if (fanpercent != -1)
  673. tailsprintf(logline, "%d%% ", fanpercent);
  674. if (fanspeed != -1)
  675. tailsprintf(logline, "(%d RPM) ", fanspeed);
  676. tailsprintf(logline, " ");
  677. }
  678. if (engineclock != -1)
  679. tailsprintf(logline, "E: %d MHz ", engineclock);
  680. if (memclock != -1)
  681. tailsprintf(logline, "M: %d Mhz ", memclock);
  682. if (vddc != -1)
  683. tailsprintf(logline, "V: %.3fV ", vddc);
  684. if (activity != -1)
  685. tailsprintf(logline, "A: %d%% ", activity);
  686. if (powertune != -1)
  687. tailsprintf(logline, "P: %d%%", powertune);
  688. tailsprintf(logline, "\n");
  689. wlog(logline);
  690. }
  691. }
  692. #endif
  693. wlog("Last initialised: %s\n", cgpu->init);
  694. wlog("Intensity: ");
  695. if (gpus[gpu].dynamic)
  696. wlog("Dynamic (only one thread in use)\n");
  697. else
  698. wlog("%d\n", gpus[gpu].intensity);
  699. for (i = 0; i < mining_threads; i++) {
  700. thr = &thr_info[i];
  701. if (thr->cgpu != cgpu)
  702. continue;
  703. get_datestamp(checkin, &thr->last);
  704. wlog("Thread %d: %.1f Mh/s %s ", i, thr->rolling, cgpu->deven != DEV_DISABLED ? "Enabled" : "Disabled");
  705. switch (cgpu->status) {
  706. default:
  707. case LIFE_WELL:
  708. wlog("ALIVE");
  709. break;
  710. case LIFE_SICK:
  711. wlog("SICK reported in %s", checkin);
  712. break;
  713. case LIFE_DEAD:
  714. wlog("DEAD reported in %s", checkin);
  715. break;
  716. case LIFE_NOSTART:
  717. wlog("Never started");
  718. break;
  719. }
  720. if (thr->pause)
  721. wlog(" paused");
  722. wlog("\n");
  723. }
  724. wlog("\n");
  725. }
  726. wlogprint("[E]nable [D]isable [I]ntensity [R]estart GPU %s\n",adl_active ? "[C]hange settings" : "");
  727. wlogprint("Or press any other key to continue\n");
  728. input = getch();
  729. if (nDevs == 1)
  730. selected = 0;
  731. else
  732. selected = -1;
  733. if (!strncasecmp(&input, "e", 1)) {
  734. struct cgpu_info *cgpu;
  735. if (selected)
  736. selected = curses_int("Select GPU to enable");
  737. if (selected < 0 || selected >= nDevs) {
  738. wlogprint("Invalid selection\n");
  739. goto retry;
  740. }
  741. if (gpus[selected].deven != DEV_DISABLED) {
  742. wlogprint("Device already enabled\n");
  743. goto retry;
  744. }
  745. gpus[selected].deven = DEV_ENABLED;
  746. for (i = 0; i < mining_threads; ++i) {
  747. thr = &thr_info[i];
  748. cgpu = thr->cgpu;
  749. if (cgpu->api != &opencl_api)
  750. continue;
  751. if (dev_from_id(i) != selected)
  752. continue;
  753. if (cgpu->status != LIFE_WELL) {
  754. wlogprint("Must restart device before enabling it");
  755. goto retry;
  756. }
  757. applog(LOG_DEBUG, "Pushing ping to thread %d", thr->id);
  758. tq_push(thr->q, &ping);
  759. }
  760. goto retry;
  761. } if (!strncasecmp(&input, "d", 1)) {
  762. if (selected)
  763. selected = curses_int("Select GPU to disable");
  764. if (selected < 0 || selected >= nDevs) {
  765. wlogprint("Invalid selection\n");
  766. goto retry;
  767. }
  768. if (gpus[selected].deven == DEV_DISABLED) {
  769. wlogprint("Device already disabled\n");
  770. goto retry;
  771. }
  772. gpus[selected].deven = DEV_DISABLED;
  773. goto retry;
  774. } else if (!strncasecmp(&input, "i", 1)) {
  775. int intensity;
  776. char *intvar;
  777. if (selected)
  778. selected = curses_int("Select GPU to change intensity on");
  779. if (selected < 0 || selected >= nDevs) {
  780. wlogprint("Invalid selection\n");
  781. goto retry;
  782. }
  783. intvar = curses_input("Set GPU scan intensity (d or " _MIN_INTENSITY_STR " -> " _MAX_INTENSITY_STR ")");
  784. if (!intvar) {
  785. wlogprint("Invalid input\n");
  786. goto retry;
  787. }
  788. if (!strncasecmp(intvar, "d", 1)) {
  789. wlogprint("Dynamic mode enabled on gpu %d\n", selected);
  790. gpus[selected].dynamic = true;
  791. pause_dynamic_threads(selected);
  792. free(intvar);
  793. goto retry;
  794. }
  795. intensity = atoi(intvar);
  796. free(intvar);
  797. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  798. wlogprint("Invalid selection\n");
  799. goto retry;
  800. }
  801. gpus[selected].dynamic = false;
  802. gpus[selected].intensity = intensity;
  803. wlogprint("Intensity on gpu %d set to %d\n", selected, intensity);
  804. pause_dynamic_threads(selected);
  805. goto retry;
  806. } else if (!strncasecmp(&input, "r", 1)) {
  807. if (selected)
  808. selected = curses_int("Select GPU to attempt to restart");
  809. if (selected < 0 || selected >= nDevs) {
  810. wlogprint("Invalid selection\n");
  811. goto retry;
  812. }
  813. wlogprint("Attempting to restart threads of GPU %d\n", selected);
  814. reinit_device(&gpus[selected]);
  815. goto retry;
  816. } else if (adl_active && (!strncasecmp(&input, "c", 1))) {
  817. if (selected)
  818. selected = curses_int("Select GPU to change settings on");
  819. if (selected < 0 || selected >= nDevs) {
  820. wlogprint("Invalid selection\n");
  821. goto retry;
  822. }
  823. change_gpusettings(selected);
  824. goto retry;
  825. } else
  826. clear_logwin();
  827. immedok(logwin, false);
  828. opt_loginput = false;
  829. }
  830. #else
  831. void manage_gpu(void)
  832. {
  833. }
  834. #endif
  835. #ifdef HAVE_OPENCL
  836. static _clState *clStates[MAX_GPUDEVICES];
  837. #define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
  838. #define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
  839. #define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
  840. static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  841. {
  842. cl_kernel *kernel = &clState->kernel;
  843. unsigned int num = 0;
  844. cl_int status = 0;
  845. CL_SET_BLKARG(ctx_a);
  846. CL_SET_BLKARG(ctx_b);
  847. CL_SET_BLKARG(ctx_c);
  848. CL_SET_BLKARG(ctx_d);
  849. CL_SET_BLKARG(ctx_e);
  850. CL_SET_BLKARG(ctx_f);
  851. CL_SET_BLKARG(ctx_g);
  852. CL_SET_BLKARG(ctx_h);
  853. CL_SET_BLKARG(cty_b);
  854. CL_SET_BLKARG(cty_c);
  855. CL_SET_BLKARG(cty_f);
  856. CL_SET_BLKARG(cty_g);
  857. CL_SET_BLKARG(cty_h);
  858. if (!clState->goffset) {
  859. cl_uint vwidth = clState->vwidth;
  860. uint *nonces = alloca(sizeof(uint) * vwidth);
  861. unsigned int i;
  862. for (i = 0; i < vwidth; i++)
  863. nonces[i] = blk->nonce + (i * threads);
  864. CL_SET_VARG(vwidth, nonces);
  865. }
  866. CL_SET_BLKARG(fW0);
  867. CL_SET_BLKARG(fW1);
  868. CL_SET_BLKARG(fW2);
  869. CL_SET_BLKARG(fW3);
  870. CL_SET_BLKARG(fW15);
  871. CL_SET_BLKARG(fW01r);
  872. CL_SET_BLKARG(D1A);
  873. CL_SET_BLKARG(C1addK5);
  874. CL_SET_BLKARG(B1addK6);
  875. CL_SET_BLKARG(W16addK16);
  876. CL_SET_BLKARG(W17addK17);
  877. CL_SET_BLKARG(PreVal4addT1);
  878. CL_SET_BLKARG(PreVal0);
  879. CL_SET_ARG(clState->outputBuffer);
  880. return status;
  881. }
  882. static cl_int queue_phatk_kernel(_clState *clState, dev_blk_ctx *blk,
  883. __maybe_unused cl_uint threads)
  884. {
  885. cl_kernel *kernel = &clState->kernel;
  886. cl_uint vwidth = clState->vwidth;
  887. unsigned int i, num = 0;
  888. cl_int status = 0;
  889. uint *nonces;
  890. CL_SET_BLKARG(ctx_a);
  891. CL_SET_BLKARG(ctx_b);
  892. CL_SET_BLKARG(ctx_c);
  893. CL_SET_BLKARG(ctx_d);
  894. CL_SET_BLKARG(ctx_e);
  895. CL_SET_BLKARG(ctx_f);
  896. CL_SET_BLKARG(ctx_g);
  897. CL_SET_BLKARG(ctx_h);
  898. CL_SET_BLKARG(cty_b);
  899. CL_SET_BLKARG(cty_c);
  900. CL_SET_BLKARG(cty_d);
  901. CL_SET_BLKARG(cty_f);
  902. CL_SET_BLKARG(cty_g);
  903. CL_SET_BLKARG(cty_h);
  904. nonces = alloca(sizeof(uint) * vwidth);
  905. for (i = 0; i < vwidth; i++)
  906. nonces[i] = blk->nonce + i;
  907. CL_SET_VARG(vwidth, nonces);
  908. CL_SET_BLKARG(W16);
  909. CL_SET_BLKARG(W17);
  910. CL_SET_BLKARG(PreVal4_2);
  911. CL_SET_BLKARG(PreVal0);
  912. CL_SET_BLKARG(PreW18);
  913. CL_SET_BLKARG(PreW19);
  914. CL_SET_BLKARG(PreW31);
  915. CL_SET_BLKARG(PreW32);
  916. CL_SET_ARG(clState->outputBuffer);
  917. return status;
  918. }
  919. static cl_int queue_diakgcn_kernel(_clState *clState, dev_blk_ctx *blk,
  920. __maybe_unused cl_uint threads)
  921. {
  922. cl_kernel *kernel = &clState->kernel;
  923. cl_uint vwidth = clState->vwidth;
  924. unsigned int i, num = 0;
  925. cl_int status = 0;
  926. uint *nonces;
  927. nonces = alloca(sizeof(uint) * vwidth);
  928. for (i = 0; i < vwidth; i++)
  929. nonces[i] = blk->nonce + i;
  930. CL_SET_VARG(vwidth, nonces);
  931. CL_SET_BLKARG(PreVal0);
  932. CL_SET_BLKARG(PreVal4_2);
  933. CL_SET_BLKARG(cty_h);
  934. CL_SET_BLKARG(D1A);
  935. CL_SET_BLKARG(cty_b);
  936. CL_SET_BLKARG(cty_c);
  937. CL_SET_BLKARG(cty_f);
  938. CL_SET_BLKARG(cty_g);
  939. CL_SET_BLKARG(C1addK5);
  940. CL_SET_BLKARG(B1addK6);
  941. CL_SET_BLKARG(PreVal0addK7);
  942. CL_SET_BLKARG(W16addK16);
  943. CL_SET_BLKARG(W17addK17);
  944. CL_SET_BLKARG(PreW18);
  945. CL_SET_BLKARG(PreW19);
  946. CL_SET_BLKARG(W16);
  947. CL_SET_BLKARG(W17);
  948. CL_SET_BLKARG(PreW31);
  949. CL_SET_BLKARG(PreW32);
  950. CL_SET_BLKARG(ctx_a);
  951. CL_SET_BLKARG(ctx_b);
  952. CL_SET_BLKARG(ctx_c);
  953. CL_SET_BLKARG(ctx_d);
  954. CL_SET_BLKARG(ctx_e);
  955. CL_SET_BLKARG(ctx_f);
  956. CL_SET_BLKARG(ctx_g);
  957. CL_SET_BLKARG(ctx_h);
  958. CL_SET_BLKARG(zeroA);
  959. CL_SET_BLKARG(zeroB);
  960. CL_SET_BLKARG(oneA);
  961. CL_SET_BLKARG(twoA);
  962. CL_SET_BLKARG(threeA);
  963. CL_SET_BLKARG(fourA);
  964. CL_SET_BLKARG(fiveA);
  965. CL_SET_BLKARG(sixA);
  966. CL_SET_BLKARG(sevenA);
  967. CL_SET_ARG(clState->outputBuffer);
  968. return status;
  969. }
  970. static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  971. {
  972. cl_kernel *kernel = &clState->kernel;
  973. unsigned int num = 0;
  974. cl_int status = 0;
  975. if (!clState->goffset) {
  976. cl_uint vwidth = clState->vwidth;
  977. uint *nonces = alloca(sizeof(uint) * vwidth);
  978. unsigned int i;
  979. for (i = 0; i < vwidth; i++)
  980. nonces[i] = blk->nonce + (i * threads);
  981. CL_SET_VARG(vwidth, nonces);
  982. }
  983. CL_SET_BLKARG(PreVal0);
  984. CL_SET_BLKARG(PreVal0addK7);
  985. CL_SET_BLKARG(PreVal4addT1);
  986. CL_SET_BLKARG(PreW18);
  987. CL_SET_BLKARG(PreW19);
  988. CL_SET_BLKARG(W16);
  989. CL_SET_BLKARG(W17);
  990. CL_SET_BLKARG(W16addK16);
  991. CL_SET_BLKARG(W17addK17);
  992. CL_SET_BLKARG(PreW31);
  993. CL_SET_BLKARG(PreW32);
  994. CL_SET_BLKARG(D1A);
  995. CL_SET_BLKARG(cty_b);
  996. CL_SET_BLKARG(cty_c);
  997. CL_SET_BLKARG(cty_h);
  998. CL_SET_BLKARG(cty_f);
  999. CL_SET_BLKARG(cty_g);
  1000. CL_SET_BLKARG(C1addK5);
  1001. CL_SET_BLKARG(B1addK6);
  1002. CL_SET_BLKARG(ctx_a);
  1003. CL_SET_BLKARG(ctx_b);
  1004. CL_SET_BLKARG(ctx_c);
  1005. CL_SET_BLKARG(ctx_d);
  1006. CL_SET_BLKARG(ctx_e);
  1007. CL_SET_BLKARG(ctx_f);
  1008. CL_SET_BLKARG(ctx_g);
  1009. CL_SET_BLKARG(ctx_h);
  1010. CL_SET_ARG(clState->outputBuffer);
  1011. return status;
  1012. }
  1013. static void set_threads_hashes(unsigned int vectors, unsigned int *threads,
  1014. unsigned int *hashes, size_t *globalThreads,
  1015. unsigned int minthreads, int intensity)
  1016. {
  1017. *threads = 1 << (15 + intensity);
  1018. if (*threads < minthreads)
  1019. *threads = minthreads;
  1020. *globalThreads = *threads;
  1021. *hashes = *threads * vectors;
  1022. }
  1023. #endif /* HAVE_OPENCL */
  1024. #ifdef HAVE_OPENCL
  1025. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  1026. * init command fails due to a completely wedged GPU, the thread will never
  1027. * return, unable to harm other GPUs. If it does return, it means we only had
  1028. * a soft failure and then the reinit_gpu thread is ready to tackle another
  1029. * GPU */
  1030. void *reinit_gpu(void *userdata)
  1031. {
  1032. struct thr_info *mythr = userdata;
  1033. struct cgpu_info *cgpu;
  1034. struct thr_info *thr;
  1035. struct timeval now;
  1036. char name[256];
  1037. int thr_id;
  1038. int gpu;
  1039. pthread_detach(pthread_self());
  1040. select_cgpu:
  1041. cgpu = tq_pop(mythr->q, NULL);
  1042. if (!cgpu)
  1043. goto out;
  1044. if (clDevicesNum() != nDevs) {
  1045. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  1046. goto out;
  1047. }
  1048. gpu = cgpu->device_id;
  1049. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  1050. thr = &thr_info[thr_id];
  1051. cgpu = thr->cgpu;
  1052. if (cgpu->api != &opencl_api)
  1053. continue;
  1054. if (dev_from_id(thr_id) != gpu)
  1055. continue;
  1056. thr = &thr_info[thr_id];
  1057. if (!thr) {
  1058. applog(LOG_WARNING, "No reference to thread %d exists", thr_id);
  1059. continue;
  1060. }
  1061. thr->rolling = thr->cgpu->rolling = 0;
  1062. /* Reports the last time we tried to revive a sick GPU */
  1063. gettimeofday(&thr->sick, NULL);
  1064. if (!pthread_cancel(thr->pth)) {
  1065. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  1066. } else
  1067. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  1068. }
  1069. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  1070. int virtual_gpu;
  1071. thr = &thr_info[thr_id];
  1072. cgpu = thr->cgpu;
  1073. if (cgpu->api != &opencl_api)
  1074. continue;
  1075. if (dev_from_id(thr_id) != gpu)
  1076. continue;
  1077. virtual_gpu = cgpu->virtual_gpu;
  1078. /* Lose this ram cause we may get stuck here! */
  1079. //tq_freeze(thr->q);
  1080. thr->q = tq_new();
  1081. if (!thr->q)
  1082. quit(1, "Failed to tq_new in reinit_gpu");
  1083. /* Lose this ram cause we may dereference in the dying thread! */
  1084. //free(clState);
  1085. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  1086. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  1087. if (!clStates[thr_id]) {
  1088. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  1089. goto select_cgpu;
  1090. }
  1091. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1092. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  1093. applog(LOG_ERR, "thread %d create failed", thr_id);
  1094. return NULL;
  1095. }
  1096. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  1097. }
  1098. gettimeofday(&now, NULL);
  1099. get_datestamp(cgpu->init, &now);
  1100. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  1101. thr = &thr_info[thr_id];
  1102. cgpu = thr->cgpu;
  1103. if (cgpu->api != &opencl_api)
  1104. continue;
  1105. if (dev_from_id(thr_id) != gpu)
  1106. continue;
  1107. tq_push(thr->q, &ping);
  1108. }
  1109. goto select_cgpu;
  1110. out:
  1111. return NULL;
  1112. }
  1113. #else
  1114. void *reinit_gpu(void *userdata)
  1115. {
  1116. return NULL;
  1117. }
  1118. #endif
  1119. #ifdef HAVE_OPENCL
  1120. struct device_api opencl_api;
  1121. static void opencl_detect()
  1122. {
  1123. if (!load_opencl_symbols()) {
  1124. nDevs = 0;
  1125. return;
  1126. }
  1127. int i;
  1128. nDevs = clDevicesNum();
  1129. if (nDevs < 0) {
  1130. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  1131. nDevs = 0;
  1132. }
  1133. if (MAX_DEVICES - total_devices < nDevs)
  1134. nDevs = MAX_DEVICES - total_devices;
  1135. if (!nDevs)
  1136. return;
  1137. for (i = 0; i < nDevs; ++i) {
  1138. struct cgpu_info *cgpu;
  1139. cgpu = &gpus[i];
  1140. cgpu->deven = DEV_ENABLED;
  1141. cgpu->api = &opencl_api;
  1142. cgpu->device_id = i;
  1143. cgpu->threads = opt_g_threads;
  1144. cgpu->virtual_gpu = i;
  1145. add_cgpu(cgpu);
  1146. }
  1147. if (!opt_noadl)
  1148. init_adl(nDevs);
  1149. }
  1150. static void reinit_opencl_device(struct cgpu_info *gpu)
  1151. {
  1152. tq_push(thr_info[gpur_thr_id].q, gpu);
  1153. }
  1154. #ifdef HAVE_ADL
  1155. static void get_opencl_statline_before(char *buf, struct cgpu_info *gpu)
  1156. {
  1157. if (gpu->has_adl) {
  1158. int gpuid = gpu->device_id;
  1159. float gt = gpu_temp(gpuid);
  1160. int gf = gpu_fanspeed(gpuid);
  1161. int gp;
  1162. if (gt != -1)
  1163. tailsprintf(buf, "%5.1fC ", gt);
  1164. else
  1165. tailsprintf(buf, " ", gt);
  1166. if (gf != -1)
  1167. tailsprintf(buf, "%4dRPM ", gf);
  1168. else if ((gp = gpu_fanpercent(gpuid)) != -1)
  1169. tailsprintf(buf, "%3d%% ", gp);
  1170. else
  1171. tailsprintf(buf, " ");
  1172. tailsprintf(buf, "| ");
  1173. }
  1174. }
  1175. #endif
  1176. static void get_opencl_statline(char *buf, struct cgpu_info *gpu)
  1177. {
  1178. tailsprintf(buf, " I:%2d", gpu->intensity);
  1179. }
  1180. struct opencl_thread_data {
  1181. cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *, cl_uint);
  1182. uint32_t *res;
  1183. struct work *last_work;
  1184. struct work _last_work;
  1185. };
  1186. static uint32_t *blank_res;
  1187. static bool opencl_thread_prepare(struct thr_info *thr)
  1188. {
  1189. char name[256];
  1190. struct timeval now;
  1191. struct cgpu_info *cgpu = thr->cgpu;
  1192. int gpu = cgpu->device_id;
  1193. int virtual_gpu = cgpu->virtual_gpu;
  1194. int i = thr->id;
  1195. static bool failmessage = false;
  1196. if (!blank_res)
  1197. blank_res = calloc(BUFFERSIZE, 1);
  1198. if (!blank_res) {
  1199. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1200. return false;
  1201. }
  1202. strcpy(name, "");
  1203. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  1204. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  1205. if (!clStates[i]) {
  1206. if (use_curses)
  1207. enable_curses();
  1208. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  1209. if (!failmessage) {
  1210. char *buf;
  1211. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  1212. applog(LOG_ERR, "Try restarting cgminer.");
  1213. failmessage = true;
  1214. #ifdef HAVE_CURSES
  1215. if (use_curses) {
  1216. buf = curses_input("Press enter to continue");
  1217. if (buf)
  1218. free(buf);
  1219. }
  1220. #endif
  1221. }
  1222. cgpu->deven = DEV_DISABLED;
  1223. cgpu->status = LIFE_NOSTART;
  1224. cgpu->device_last_not_well = time(NULL);
  1225. cgpu->device_not_well_reason = REASON_DEV_NOSTART;
  1226. cgpu->dev_nostart_count++;
  1227. return false;
  1228. }
  1229. if (!cgpu->name)
  1230. cgpu->name = strdup(name);
  1231. if (!cgpu->kname)
  1232. {
  1233. switch (clStates[i]->chosen_kernel) {
  1234. case KL_DIABLO:
  1235. cgpu->kname = "diablo";
  1236. break;
  1237. case KL_DIAKGCN:
  1238. cgpu->kname = "diakgcn";
  1239. break;
  1240. case KL_PHATK:
  1241. cgpu->kname = "phatk";
  1242. break;
  1243. case KL_POCLBM:
  1244. cgpu->kname = "poclbm";
  1245. default:
  1246. break;
  1247. }
  1248. }
  1249. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1250. gettimeofday(&now, NULL);
  1251. get_datestamp(cgpu->init, &now);
  1252. have_opencl = true;
  1253. return true;
  1254. }
  1255. static bool opencl_thread_init(struct thr_info *thr)
  1256. {
  1257. const int thr_id = thr->id;
  1258. struct cgpu_info *gpu = thr->cgpu;
  1259. struct opencl_thread_data *thrdata;
  1260. _clState *clState = clStates[thr_id];
  1261. cl_int status;
  1262. thrdata = calloc(1, sizeof(*thrdata));
  1263. thr->cgpu_data = thrdata;
  1264. if (!thrdata) {
  1265. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1266. return false;
  1267. }
  1268. switch (clState->chosen_kernel) {
  1269. case KL_POCLBM:
  1270. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  1271. break;
  1272. case KL_PHATK:
  1273. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  1274. break;
  1275. case KL_DIAKGCN:
  1276. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  1277. break;
  1278. default:
  1279. case KL_DIABLO:
  1280. thrdata->queue_kernel_parameters = &queue_diablo_kernel;
  1281. break;
  1282. }
  1283. thrdata->res = calloc(BUFFERSIZE, 1);
  1284. if (!thrdata->res) {
  1285. free(thrdata);
  1286. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1287. return false;
  1288. }
  1289. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1290. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1291. if (unlikely(status != CL_SUCCESS)) {
  1292. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1293. return false;
  1294. }
  1295. gpu->status = LIFE_WELL;
  1296. gpu->device_last_well = time(NULL);
  1297. return true;
  1298. }
  1299. static void opencl_free_work(struct thr_info *thr, struct work *work)
  1300. {
  1301. const int thr_id = thr->id;
  1302. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1303. _clState *clState = clStates[thr_id];
  1304. clFinish(clState->commandQueue);
  1305. if (thrdata->res[FOUND]) {
  1306. thrdata->last_work = &thrdata->_last_work;
  1307. memcpy(thrdata->last_work, work, sizeof(*thrdata->last_work));
  1308. }
  1309. }
  1310. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  1311. {
  1312. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1313. return true;
  1314. }
  1315. extern int opt_dynamic_interval;
  1316. static uint64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1317. uint64_t __maybe_unused max_nonce)
  1318. {
  1319. const int thr_id = thr->id;
  1320. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1321. struct cgpu_info *gpu = thr->cgpu;
  1322. _clState *clState = clStates[thr_id];
  1323. const cl_kernel *kernel = &clState->kernel;
  1324. double gpu_ms_average = 7;
  1325. cl_int status;
  1326. size_t globalThreads[1];
  1327. size_t localThreads[1] = { clState->wsize };
  1328. unsigned int threads;
  1329. unsigned int hashes;
  1330. struct timeval tv_gpustart, tv_gpuend, diff;
  1331. suseconds_t gpu_us;
  1332. gettimeofday(&tv_gpustart, NULL);
  1333. timeval_subtract(&diff, &tv_gpustart, &tv_gpuend);
  1334. /* This finish flushes the readbuffer set with CL_FALSE later */
  1335. clFinish(clState->commandQueue);
  1336. gettimeofday(&tv_gpuend, NULL);
  1337. timeval_subtract(&diff, &tv_gpuend, &tv_gpustart);
  1338. gpu_us = diff.tv_sec * 1000000 + diff.tv_usec;
  1339. decay_time(&gpu_ms_average, gpu_us / 1000);
  1340. if (gpu->dynamic) {
  1341. /* Try to not let the GPU be out for longer than 6ms, but
  1342. * increase intensity when the system is idle, unless
  1343. * dynamic is disabled. */
  1344. if (gpu_ms_average > opt_dynamic_interval) {
  1345. if (gpu->intensity > MIN_INTENSITY)
  1346. --gpu->intensity;
  1347. } else if (gpu_ms_average < ((opt_dynamic_interval / 2) ? : 1)) {
  1348. if (gpu->intensity < MAX_INTENSITY)
  1349. ++gpu->intensity;
  1350. }
  1351. }
  1352. set_threads_hashes(clState->vwidth, &threads, &hashes, globalThreads,
  1353. localThreads[0], gpu->intensity);
  1354. if (hashes > gpu->max_hashes)
  1355. gpu->max_hashes = hashes;
  1356. status = thrdata->queue_kernel_parameters(clState, &work->blk, globalThreads[0]);
  1357. if (unlikely(status != CL_SUCCESS)) {
  1358. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1359. return 0;
  1360. }
  1361. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  1362. if (thrdata->res[FOUND]) {
  1363. /* Clear the buffer again */
  1364. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1365. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1366. if (unlikely(status != CL_SUCCESS)) {
  1367. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1368. return 0;
  1369. }
  1370. if (unlikely(thrdata->last_work)) {
  1371. applog(LOG_DEBUG, "GPU %d found something in last work?", gpu->device_id);
  1372. postcalc_hash_async(thr, thrdata->last_work, thrdata->res);
  1373. thrdata->last_work = NULL;
  1374. } else {
  1375. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1376. postcalc_hash_async(thr, work, thrdata->res);
  1377. }
  1378. memset(thrdata->res, 0, BUFFERSIZE);
  1379. clFinish(clState->commandQueue);
  1380. }
  1381. if (clState->goffset) {
  1382. size_t global_work_offset[1];
  1383. global_work_offset[0] = work->blk.nonce;
  1384. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, global_work_offset,
  1385. globalThreads, localThreads, 0, NULL, NULL);
  1386. } else
  1387. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1388. globalThreads, localThreads, 0, NULL, NULL);
  1389. if (unlikely(status != CL_SUCCESS)) {
  1390. applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)");
  1391. return 0;
  1392. }
  1393. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1394. BUFFERSIZE, thrdata->res, 0, NULL, NULL);
  1395. if (unlikely(status != CL_SUCCESS)) {
  1396. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)");
  1397. return 0;
  1398. }
  1399. /* The amount of work scanned can fluctuate when intensity changes
  1400. * and since we do this one cycle behind, we increment the work more
  1401. * than enough to prevent repeating work */
  1402. work->blk.nonce += gpu->max_hashes;
  1403. return hashes;
  1404. }
  1405. static void opencl_thread_shutdown(struct thr_info *thr)
  1406. {
  1407. const int thr_id = thr->id;
  1408. _clState *clState = clStates[thr_id];
  1409. clReleaseCommandQueue(clState->commandQueue);
  1410. clReleaseKernel(clState->kernel);
  1411. clReleaseProgram(clState->program);
  1412. clReleaseContext(clState->context);
  1413. }
  1414. struct device_api opencl_api = {
  1415. .dname = "opencl",
  1416. .name = "GPU",
  1417. .api_detect = opencl_detect,
  1418. .reinit_device = reinit_opencl_device,
  1419. #ifdef HAVE_ADL
  1420. .get_statline_before = get_opencl_statline_before,
  1421. #endif
  1422. .get_statline = get_opencl_statline,
  1423. .thread_prepare = opencl_thread_prepare,
  1424. .thread_init = opencl_thread_init,
  1425. .free_work = opencl_free_work,
  1426. .prepare_work = opencl_prepare_work,
  1427. .scanhash = opencl_scanhash,
  1428. .thread_shutdown = opencl_thread_shutdown,
  1429. };
  1430. #endif