ocl.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*
  2. * Copyright 2011-2012 Con Kolivas
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #ifdef HAVE_OPENCL
  11. #include <signal.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <sys/types.h>
  16. #ifdef WIN32
  17. #include <winsock2.h>
  18. #else
  19. #include <sys/socket.h>
  20. #include <netinet/in.h>
  21. #include <netdb.h>
  22. #endif
  23. #include <time.h>
  24. #include <sys/time.h>
  25. #include <pthread.h>
  26. #include <sys/stat.h>
  27. #include <unistd.h>
  28. #define OMIT_OPENCL_API
  29. #include "findnonce.h"
  30. #include "ocl.h"
  31. /* Platform API */
  32. extern
  33. CL_API_ENTRY cl_int CL_API_CALL
  34. (*clGetPlatformIDs)(cl_uint /* num_entries */,
  35. cl_platform_id * /* platforms */,
  36. cl_uint * /* num_platforms */) CL_API_SUFFIX__VERSION_1_0;
  37. extern
  38. CL_API_ENTRY cl_int CL_API_CALL
  39. (*clGetPlatformInfo)(cl_platform_id /* platform */,
  40. cl_platform_info /* param_name */,
  41. size_t /* param_value_size */,
  42. void * /* param_value */,
  43. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  44. /* Device APIs */
  45. extern
  46. CL_API_ENTRY cl_int CL_API_CALL
  47. (*clGetDeviceIDs)(cl_platform_id /* platform */,
  48. cl_device_type /* device_type */,
  49. cl_uint /* num_entries */,
  50. cl_device_id * /* devices */,
  51. cl_uint * /* num_devices */) CL_API_SUFFIX__VERSION_1_0;
  52. extern
  53. CL_API_ENTRY cl_int CL_API_CALL
  54. (*clGetDeviceInfo)(cl_device_id /* device */,
  55. cl_device_info /* param_name */,
  56. size_t /* param_value_size */,
  57. void * /* param_value */,
  58. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  59. /* Context APIs */
  60. extern
  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. extern
  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. extern
  72. CL_API_ENTRY cl_command_queue CL_API_CALL
  73. (*clCreateCommandQueue)(cl_context /* context */,
  74. cl_device_id /* device */,
  75. cl_command_queue_properties /* properties */,
  76. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  77. extern
  78. CL_API_ENTRY cl_int CL_API_CALL
  79. (*clReleaseCommandQueue)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  80. /* Memory Object APIs */
  81. extern
  82. CL_API_ENTRY cl_mem CL_API_CALL
  83. (*clCreateBuffer)(cl_context /* context */,
  84. cl_mem_flags /* flags */,
  85. size_t /* size */,
  86. void * /* host_ptr */,
  87. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  88. /* Program Object APIs */
  89. extern
  90. CL_API_ENTRY cl_program CL_API_CALL
  91. (*clCreateProgramWithSource)(cl_context /* context */,
  92. cl_uint /* count */,
  93. const char ** /* strings */,
  94. const size_t * /* lengths */,
  95. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  96. extern
  97. CL_API_ENTRY cl_program CL_API_CALL
  98. (*clCreateProgramWithBinary)(cl_context /* context */,
  99. cl_uint /* num_devices */,
  100. const cl_device_id * /* device_list */,
  101. const size_t * /* lengths */,
  102. const unsigned char ** /* binaries */,
  103. cl_int * /* binary_status */,
  104. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  105. extern
  106. CL_API_ENTRY cl_int CL_API_CALL
  107. (*clReleaseProgram)(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
  108. extern
  109. CL_API_ENTRY cl_int CL_API_CALL
  110. (*clBuildProgram)(cl_program /* program */,
  111. cl_uint /* num_devices */,
  112. const cl_device_id * /* device_list */,
  113. const char * /* options */,
  114. void (CL_CALLBACK * /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
  115. void * /* user_data */) CL_API_SUFFIX__VERSION_1_0;
  116. extern
  117. CL_API_ENTRY cl_int CL_API_CALL
  118. (*clGetProgramInfo)(cl_program /* program */,
  119. cl_program_info /* param_name */,
  120. size_t /* param_value_size */,
  121. void * /* param_value */,
  122. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  123. extern
  124. CL_API_ENTRY cl_int CL_API_CALL
  125. (*clGetProgramBuildInfo)(cl_program /* program */,
  126. cl_device_id /* device */,
  127. cl_program_build_info /* param_name */,
  128. size_t /* param_value_size */,
  129. void * /* param_value */,
  130. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  131. /* Kernel Object APIs */
  132. extern
  133. CL_API_ENTRY cl_kernel CL_API_CALL
  134. (*clCreateKernel)(cl_program /* program */,
  135. const char * /* kernel_name */,
  136. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  137. extern
  138. CL_API_ENTRY cl_int CL_API_CALL
  139. (*clReleaseKernel)(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0;
  140. extern
  141. CL_API_ENTRY cl_int CL_API_CALL
  142. (*clSetKernelArg)(cl_kernel /* kernel */,
  143. cl_uint /* arg_index */,
  144. size_t /* arg_size */,
  145. const void * /* arg_value */) CL_API_SUFFIX__VERSION_1_0;
  146. /* Flush and Finish APIs */
  147. extern
  148. CL_API_ENTRY cl_int CL_API_CALL
  149. (*clFinish)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  150. /* Enqueued Commands APIs */
  151. extern
  152. CL_API_ENTRY cl_int CL_API_CALL
  153. (*clEnqueueReadBuffer)(cl_command_queue /* command_queue */,
  154. cl_mem /* buffer */,
  155. cl_bool /* blocking_read */,
  156. size_t /* offset */,
  157. size_t /* size */,
  158. void * /* ptr */,
  159. cl_uint /* num_events_in_wait_list */,
  160. const cl_event * /* event_wait_list */,
  161. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  162. extern
  163. CL_API_ENTRY cl_int CL_API_CALL
  164. (*clEnqueueWriteBuffer)(cl_command_queue /* command_queue */,
  165. cl_mem /* buffer */,
  166. cl_bool /* blocking_write */,
  167. size_t /* offset */,
  168. size_t /* size */,
  169. const void * /* ptr */,
  170. cl_uint /* num_events_in_wait_list */,
  171. const cl_event * /* event_wait_list */,
  172. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  173. extern
  174. CL_API_ENTRY cl_int CL_API_CALL
  175. (*clEnqueueNDRangeKernel)(cl_command_queue /* command_queue */,
  176. cl_kernel /* kernel */,
  177. cl_uint /* work_dim */,
  178. const size_t * /* global_work_offset */,
  179. const size_t * /* global_work_size */,
  180. const size_t * /* local_work_size */,
  181. cl_uint /* num_events_in_wait_list */,
  182. const cl_event * /* event_wait_list */,
  183. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  184. int opt_platform_id = -1;
  185. char *file_contents(const char *filename, int *length)
  186. {
  187. char *fullpath = alloca(PATH_MAX);
  188. void *buffer;
  189. FILE *f;
  190. strcpy(fullpath, opt_kernel_path);
  191. strcat(fullpath, filename);
  192. /* Try in the optional kernel path or installed prefix first */
  193. f = fopen(fullpath, "rb");
  194. if (!f) {
  195. /* Then try from the path BFGMiner was called */
  196. strcpy(fullpath, cgminer_path);
  197. strcat(fullpath, filename);
  198. f = fopen(fullpath, "rb");
  199. }
  200. /* Finally try opening it directly */
  201. if (!f)
  202. f = fopen(filename, "rb");
  203. if (!f) {
  204. applog(LOG_ERR, "Unable to open %s or %s for reading", filename, fullpath);
  205. return NULL;
  206. }
  207. fseek(f, 0, SEEK_END);
  208. *length = ftell(f);
  209. fseek(f, 0, SEEK_SET);
  210. buffer = malloc(*length+1);
  211. *length = fread(buffer, 1, *length, f);
  212. fclose(f);
  213. ((char*)buffer)[*length] = '\0';
  214. return (char*)buffer;
  215. }
  216. int clDevicesNum(void) {
  217. cl_int status;
  218. char pbuff[256];
  219. cl_uint numDevices;
  220. cl_uint numPlatforms;
  221. cl_platform_id *platforms;
  222. cl_platform_id platform = NULL;
  223. unsigned int most_devices = 0, i, mdplatform = 0;
  224. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  225. /* If this fails, assume no GPUs. */
  226. if (status != CL_SUCCESS) {
  227. applog(LOG_ERR, "Error %d: clGetPlatformsIDs failed (no OpenCL SDK installed?)", status);
  228. return -1;
  229. }
  230. if (numPlatforms == 0) {
  231. applog(LOG_ERR, "clGetPlatformsIDs returned no platforms (no OpenCL SDK installed?)");
  232. return -1;
  233. }
  234. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  235. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  236. if (status != CL_SUCCESS) {
  237. applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
  238. return -1;
  239. }
  240. for (i = 0; i < numPlatforms; i++) {
  241. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  242. if (status != CL_SUCCESS) {
  243. applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
  244. return -1;
  245. }
  246. platform = platforms[i];
  247. applog(LOG_INFO, "CL Platform %d vendor: %s", i, pbuff);
  248. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  249. if (status == CL_SUCCESS)
  250. applog(LOG_INFO, "CL Platform %d name: %s", i, pbuff);
  251. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(pbuff), pbuff, NULL);
  252. if (status == CL_SUCCESS)
  253. applog(LOG_INFO, "CL Platform %d version: %s", i, pbuff);
  254. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  255. if (status != CL_SUCCESS) {
  256. applog(LOG_ERR, "Error %d: Getting Device IDs (num)", status);
  257. if ((int)i != opt_platform_id)
  258. continue;
  259. return -1;
  260. }
  261. applog(LOG_INFO, "Platform %d devices: %d", i, numDevices);
  262. if (numDevices > most_devices) {
  263. most_devices = numDevices;
  264. mdplatform = i;
  265. }
  266. if (numDevices) {
  267. unsigned int j;
  268. char pbuff[256];
  269. cl_device_id *devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  270. clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  271. for (j = 0; j < numDevices; j++) {
  272. clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  273. applog(LOG_INFO, "\t%i\t%s", j, pbuff);
  274. }
  275. free(devices);
  276. }
  277. }
  278. if (opt_platform_id < 0)
  279. opt_platform_id = mdplatform;;
  280. return most_devices;
  281. }
  282. static int advance(char **area, unsigned *remaining, const char *marker)
  283. {
  284. char *find = memmem(*area, *remaining, marker, strlen(marker));
  285. if (!find) {
  286. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  287. return 0;
  288. }
  289. *remaining -= find - *area;
  290. *area = find;
  291. return 1;
  292. }
  293. #define OP3_INST_BFE_UINT 4ULL
  294. #define OP3_INST_BFE_INT 5ULL
  295. #define OP3_INST_BFI_INT 6ULL
  296. #define OP3_INST_BIT_ALIGN_INT 12ULL
  297. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  298. void patch_opcodes(char *w, unsigned remaining)
  299. {
  300. uint64_t *opcode = (uint64_t *)w;
  301. int patched = 0;
  302. int count_bfe_int = 0;
  303. int count_bfe_uint = 0;
  304. int count_byte_align = 0;
  305. while (42) {
  306. int clamp = (*opcode >> (32 + 31)) & 0x1;
  307. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  308. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  309. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  310. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  311. int pred_sel = (*opcode >> 29) & 0x3;
  312. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  313. if (alu_inst == OP3_INST_BFE_INT) {
  314. count_bfe_int++;
  315. } else if (alu_inst == OP3_INST_BFE_UINT) {
  316. count_bfe_uint++;
  317. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  318. count_byte_align++;
  319. // patch this instruction to BFI_INT
  320. *opcode &= 0xfffc1fffffffffffULL;
  321. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  322. patched++;
  323. }
  324. }
  325. if (remaining <= 8)
  326. break;
  327. opcode++;
  328. remaining -= 8;
  329. }
  330. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  331. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  332. count_bfe_int, count_bfe_uint, count_byte_align);
  333. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  334. }
  335. _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
  336. {
  337. _clState *clState = calloc(1, sizeof(_clState));
  338. bool patchbfi = false, prog_built = false;
  339. struct cgpu_info *cgpu = &gpus[gpu];
  340. cl_platform_id platform = NULL;
  341. char pbuff[256], vbuff[255];
  342. cl_platform_id* platforms;
  343. cl_uint preferred_vwidth;
  344. cl_device_id *devices;
  345. cl_uint numPlatforms;
  346. cl_uint numDevices;
  347. cl_int status;
  348. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  349. if (status != CL_SUCCESS) {
  350. applog(LOG_ERR, "Error %d: Getting Platforms. (clGetPlatformsIDs)", status);
  351. return NULL;
  352. }
  353. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  354. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  355. if (status != CL_SUCCESS) {
  356. applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
  357. return NULL;
  358. }
  359. if (opt_platform_id >= (int)numPlatforms) {
  360. applog(LOG_ERR, "Specified platform that does not exist");
  361. return NULL;
  362. }
  363. status = clGetPlatformInfo(platforms[opt_platform_id], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  364. if (status != CL_SUCCESS) {
  365. applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
  366. return NULL;
  367. }
  368. platform = platforms[opt_platform_id];
  369. if (platform == NULL) {
  370. perror("NULL platform found!\n");
  371. return NULL;
  372. }
  373. applog(LOG_INFO, "CL Platform vendor: %s", pbuff);
  374. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  375. if (status == CL_SUCCESS)
  376. applog(LOG_INFO, "CL Platform name: %s", pbuff);
  377. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(vbuff), vbuff, NULL);
  378. if (status == CL_SUCCESS)
  379. applog(LOG_INFO, "CL Platform version: %s", vbuff);
  380. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  381. if (status != CL_SUCCESS) {
  382. applog(LOG_ERR, "Error %d: Getting Device IDs (num)", status);
  383. return NULL;
  384. }
  385. if (numDevices > 0 ) {
  386. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  387. /* Now, get the device list data */
  388. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  389. if (status != CL_SUCCESS) {
  390. applog(LOG_ERR, "Error %d: Getting Device IDs (list)", status);
  391. return NULL;
  392. }
  393. applog(LOG_INFO, "List of devices:");
  394. unsigned int i;
  395. for (i = 0; i < numDevices; i++) {
  396. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  397. if (status != CL_SUCCESS) {
  398. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  399. return NULL;
  400. }
  401. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  402. }
  403. if (gpu < numDevices) {
  404. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  405. if (status != CL_SUCCESS) {
  406. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  407. return NULL;
  408. }
  409. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  410. strncpy(name, pbuff, nameSize);
  411. } else {
  412. applog(LOG_ERR, "Invalid GPU %i", gpu);
  413. return NULL;
  414. }
  415. } else return NULL;
  416. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  417. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  418. if (status != CL_SUCCESS) {
  419. applog(LOG_ERR, "Error %d: Creating Context. (clCreateContextFromType)", status);
  420. return NULL;
  421. }
  422. /////////////////////////////////////////////////////////////////
  423. // Create an OpenCL command queue
  424. /////////////////////////////////////////////////////////////////
  425. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  426. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  427. if (status != CL_SUCCESS) /* Try again without OOE enable */
  428. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  429. if (status != CL_SUCCESS) {
  430. applog(LOG_ERR, "Error %d: Creating Command Queue. (clCreateCommandQueue)", status);
  431. return NULL;
  432. }
  433. /* Check for BFI INT support. Hopefully people don't mix devices with
  434. * and without it! */
  435. char * extensions = malloc(1024);
  436. const char * camo = "cl_amd_media_ops";
  437. char *find;
  438. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  439. if (status != CL_SUCCESS) {
  440. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS", status);
  441. return NULL;
  442. }
  443. find = strstr(extensions, camo);
  444. if (find)
  445. clState->hasBitAlign = true;
  446. /* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
  447. char * devoclver = malloc(1024);
  448. const char * ocl10 = "OpenCL 1.0";
  449. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
  450. if (status != CL_SUCCESS) {
  451. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION", status);
  452. return NULL;
  453. }
  454. find = strstr(devoclver, ocl10);
  455. if (!find)
  456. clState->hasOpenCL11plus = true;
  457. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&preferred_vwidth, NULL);
  458. if (status != CL_SUCCESS) {
  459. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT", status);
  460. return NULL;
  461. }
  462. applog(LOG_DEBUG, "Preferred vector width reported %d", preferred_vwidth);
  463. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  464. if (status != CL_SUCCESS) {
  465. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE", status);
  466. return NULL;
  467. }
  468. applog(LOG_DEBUG, "Max work group size reported %d", clState->max_work_size);
  469. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_MEM_ALLOC_SIZE , sizeof(cl_ulong), (void *)&cgpu->max_alloc, NULL);
  470. if (status != CL_SUCCESS) {
  471. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_MEM_ALLOC_SIZE", status);
  472. return NULL;
  473. }
  474. applog(LOG_DEBUG, "Max mem alloc size is %u", cgpu->max_alloc);
  475. /* Create binary filename based on parameters passed to opencl
  476. * compiler to ensure we only load a binary that matches what would
  477. * have otherwise created. The filename is:
  478. * name + kernelname +/- g(offset) + v + vectors + w + work_size + l + sizeof(long) + .bin
  479. * For scrypt the filename is:
  480. * name + kernelname + g + lg + lookup_gap + tc + thread_concurrency + w + work_size + l + sizeof(long) + .bin
  481. */
  482. char binaryfilename[255];
  483. char filename[255];
  484. char numbuf[10];
  485. if (cgpu->kernel == KL_NONE) {
  486. if (opt_scrypt) {
  487. applog(LOG_INFO, "Selecting scrypt kernel");
  488. clState->chosen_kernel = KL_SCRYPT;
  489. } else if (!strstr(name, "Tahiti") &&
  490. /* Detect all 2.6 SDKs not with Tahiti and use diablo kernel */
  491. (strstr(vbuff, "844.4") || // Linux 64 bit ATI 2.6 SDK
  492. strstr(vbuff, "851.4") || // Windows 64 bit ""
  493. strstr(vbuff, "831.4") ||
  494. strstr(vbuff, "898.1") || // 12.2 driver SDK
  495. strstr(vbuff, "923.1") || // 12.4
  496. strstr(vbuff, "938.2"))) { // SDK 2.7
  497. applog(LOG_INFO, "Selecting diablo kernel");
  498. clState->chosen_kernel = KL_DIABLO;
  499. /* Detect all 7970s, older ATI and NVIDIA and use poclbm */
  500. } else if (strstr(name, "Tahiti") || !clState->hasBitAlign) {
  501. applog(LOG_INFO, "Selecting poclbm kernel");
  502. clState->chosen_kernel = KL_POCLBM;
  503. /* Use phatk for the rest R5xxx R6xxx */
  504. } else {
  505. applog(LOG_INFO, "Selecting phatk kernel");
  506. clState->chosen_kernel = KL_PHATK;
  507. }
  508. cgpu->kernel = clState->chosen_kernel;
  509. } else {
  510. clState->chosen_kernel = cgpu->kernel;
  511. if (clState->chosen_kernel == KL_PHATK &&
  512. (strstr(vbuff, "844.4") || strstr(vbuff, "851.4") ||
  513. strstr(vbuff, "831.4") || strstr(vbuff, "898.1") ||
  514. strstr(vbuff, "923.1") || strstr(vbuff, "938.2"))) {
  515. applog(LOG_WARNING, "WARNING: You have selected the phatk kernel.");
  516. applog(LOG_WARNING, "You are running SDK 2.6+ which performs poorly with this kernel.");
  517. applog(LOG_WARNING, "Downgrade your SDK and delete any .bin files before starting again.");
  518. applog(LOG_WARNING, "Or allow BFGMiner to automatically choose a more suitable kernel.");
  519. }
  520. }
  521. /* For some reason 2 vectors is still better even if the card says
  522. * otherwise, and many cards lie about their max so use 256 as max
  523. * unless explicitly set on the command line. Tahiti prefers 1 */
  524. if (strstr(name, "Tahiti"))
  525. preferred_vwidth = 1;
  526. else if (preferred_vwidth > 2)
  527. preferred_vwidth = 2;
  528. switch (clState->chosen_kernel) {
  529. case KL_POCLBM:
  530. strcpy(filename, POCLBM_KERNNAME".cl");
  531. strcpy(binaryfilename, POCLBM_KERNNAME);
  532. break;
  533. case KL_PHATK:
  534. strcpy(filename, PHATK_KERNNAME".cl");
  535. strcpy(binaryfilename, PHATK_KERNNAME);
  536. break;
  537. case KL_DIAKGCN:
  538. strcpy(filename, DIAKGCN_KERNNAME".cl");
  539. strcpy(binaryfilename, DIAKGCN_KERNNAME);
  540. break;
  541. case KL_SCRYPT:
  542. strcpy(filename, SCRYPT_KERNNAME".cl");
  543. strcpy(binaryfilename, SCRYPT_KERNNAME);
  544. /* Scrypt only supports vector 1 */
  545. cgpu->vwidth = 1;
  546. break;
  547. case KL_NONE: /* Shouldn't happen */
  548. case KL_DIABLO:
  549. strcpy(filename, DIABLO_KERNNAME".cl");
  550. strcpy(binaryfilename, DIABLO_KERNNAME);
  551. break;
  552. }
  553. if (cgpu->vwidth)
  554. clState->vwidth = cgpu->vwidth;
  555. else {
  556. clState->vwidth = preferred_vwidth;
  557. cgpu->vwidth = preferred_vwidth;
  558. }
  559. if (((clState->chosen_kernel == KL_POCLBM || clState->chosen_kernel == KL_DIABLO || clState->chosen_kernel == KL_DIAKGCN) &&
  560. clState->vwidth == 1 && clState->hasOpenCL11plus) || opt_scrypt)
  561. clState->goffset = true;
  562. if (cgpu->work_size && cgpu->work_size <= clState->max_work_size)
  563. clState->wsize = cgpu->work_size;
  564. else if (strstr(name, "Tahiti"))
  565. clState->wsize = 64;
  566. else
  567. clState->wsize = (clState->max_work_size <= 256 ? clState->max_work_size : 256) / clState->vwidth;
  568. cgpu->work_size = clState->wsize;
  569. #ifdef USE_SCRYPT
  570. if (opt_scrypt) {
  571. cl_ulong ma = cgpu->max_alloc, mt;
  572. int pow2 = 0;
  573. if (!cgpu->opt_lg) {
  574. applog(LOG_DEBUG, "GPU %d: selecting lookup gap of 2", gpu);
  575. cgpu->lookup_gap = 2;
  576. } else
  577. cgpu->lookup_gap = cgpu->opt_lg;
  578. if (!cgpu->opt_tc) {
  579. cgpu->thread_concurrency = ma / 32768 / cgpu->lookup_gap;
  580. if (cgpu->shaders && cgpu->thread_concurrency > cgpu->shaders) {
  581. cgpu->thread_concurrency -= cgpu->thread_concurrency % cgpu->shaders;
  582. if (cgpu->thread_concurrency > cgpu->shaders * 5)
  583. cgpu->thread_concurrency = cgpu->shaders * 5;
  584. }
  585. applog(LOG_DEBUG, "GPU %d: selecting thread concurrency of %u",gpu, cgpu->thread_concurrency);
  586. } else
  587. cgpu->thread_concurrency = cgpu->opt_tc;
  588. /* If we have memory to spare, try to find a power of 2 value
  589. * >= required amount to map nicely to an intensity */
  590. mt = cgpu->thread_concurrency * 32768 * cgpu->lookup_gap;
  591. if (ma > mt) {
  592. while (ma >>= 1)
  593. pow2++;
  594. ma = 1;
  595. while (--pow2 && ma < mt)
  596. ma <<= 1;
  597. if (ma >= mt) {
  598. cgpu->max_alloc = ma;
  599. applog(LOG_DEBUG, "Max alloc decreased to %lu", cgpu->max_alloc);
  600. }
  601. }
  602. }
  603. #endif
  604. FILE *binaryfile;
  605. size_t *binary_sizes;
  606. char **binaries;
  607. int pl;
  608. char *source = file_contents(filename, &pl);
  609. size_t sourceSize[] = {(size_t)pl};
  610. cl_uint slot, cpnd;
  611. slot = cpnd = 0;
  612. if (!source)
  613. return NULL;
  614. binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
  615. if (unlikely(!binary_sizes)) {
  616. applog(LOG_ERR, "Unable to calloc binary_sizes");
  617. return NULL;
  618. }
  619. binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
  620. if (unlikely(!binaries)) {
  621. applog(LOG_ERR, "Unable to calloc binaries");
  622. return NULL;
  623. }
  624. strcat(binaryfilename, name);
  625. if (clState->goffset)
  626. strcat(binaryfilename, "g");
  627. if (opt_scrypt) {
  628. #ifdef USE_SCRYPT
  629. sprintf(numbuf, "lg%dtc%d", cgpu->lookup_gap, cgpu->thread_concurrency);
  630. strcat(binaryfilename, numbuf);
  631. #endif
  632. } else {
  633. sprintf(numbuf, "v%d", clState->vwidth);
  634. strcat(binaryfilename, numbuf);
  635. }
  636. sprintf(numbuf, "w%d", (int)clState->wsize);
  637. strcat(binaryfilename, numbuf);
  638. sprintf(numbuf, "l%d", (int)sizeof(long));
  639. strcat(binaryfilename, numbuf);
  640. strcat(binaryfilename, ".bin");
  641. binaryfile = fopen(binaryfilename, "rb");
  642. if (!binaryfile) {
  643. applog(LOG_DEBUG, "No binary found, generating from source");
  644. } else {
  645. struct stat binary_stat;
  646. if (unlikely(stat(binaryfilename, &binary_stat))) {
  647. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  648. fclose(binaryfile);
  649. goto build;
  650. }
  651. if (!binary_stat.st_size)
  652. goto build;
  653. binary_sizes[slot] = binary_stat.st_size;
  654. binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
  655. if (unlikely(!binaries[slot])) {
  656. applog(LOG_ERR, "Unable to calloc binaries");
  657. fclose(binaryfile);
  658. return NULL;
  659. }
  660. if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
  661. applog(LOG_ERR, "Unable to fread binaries");
  662. fclose(binaryfile);
  663. free(binaries[slot]);
  664. goto build;
  665. }
  666. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
  667. if (status != CL_SUCCESS) {
  668. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  669. fclose(binaryfile);
  670. free(binaries[slot]);
  671. goto build;
  672. }
  673. fclose(binaryfile);
  674. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  675. goto built;
  676. }
  677. /////////////////////////////////////////////////////////////////
  678. // Load CL file, build CL program object, create CL kernel object
  679. /////////////////////////////////////////////////////////////////
  680. build:
  681. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  682. if (status != CL_SUCCESS) {
  683. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithSource)", status);
  684. return NULL;
  685. }
  686. /* create a cl program executable for all the devices specified */
  687. char *CompilerOptions = calloc(1, 256);
  688. #ifdef USE_SCRYPT
  689. if (opt_scrypt)
  690. sprintf(CompilerOptions, "-D LOOKUP_GAP=%d -D CONCURRENT_THREADS=%d -D WORKSIZE=%d",
  691. cgpu->lookup_gap, cgpu->thread_concurrency, (int)clState->wsize);
  692. else
  693. #endif
  694. {
  695. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d -D WORKVEC=%d",
  696. (int)clState->wsize, clState->vwidth, (int)clState->wsize * clState->vwidth);
  697. }
  698. applog(LOG_DEBUG, "Setting worksize to %d", clState->wsize);
  699. if (clState->vwidth > 1)
  700. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->vwidth);
  701. if (clState->hasBitAlign) {
  702. strcat(CompilerOptions, " -D BITALIGN");
  703. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  704. if (strstr(name, "Cedar") ||
  705. strstr(name, "Redwood") ||
  706. strstr(name, "Juniper") ||
  707. strstr(name, "Cypress" ) ||
  708. strstr(name, "Hemlock" ) ||
  709. strstr(name, "Caicos" ) ||
  710. strstr(name, "Turks" ) ||
  711. strstr(name, "Barts" ) ||
  712. strstr(name, "Cayman" ) ||
  713. strstr(name, "Antilles" ) ||
  714. strstr(name, "Wrestler" ) ||
  715. strstr(name, "Zacate" ) ||
  716. strstr(name, "WinterPark" ) ||
  717. strstr(name, "BeaverCreek" ))
  718. patchbfi = true;
  719. } else
  720. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  721. if (patchbfi) {
  722. strcat(CompilerOptions, " -D BFI_INT");
  723. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  724. } else
  725. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  726. if (clState->goffset)
  727. strcat(CompilerOptions, " -D GOFFSET");
  728. if (!clState->hasOpenCL11plus)
  729. strcat(CompilerOptions, " -D OCL1");
  730. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  731. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  732. free(CompilerOptions);
  733. if (status != CL_SUCCESS) {
  734. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  735. size_t logSize;
  736. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  737. char *log = malloc(logSize);
  738. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  739. applog(LOG_ERR, "%s", log);
  740. return NULL;
  741. }
  742. prog_built = true;
  743. status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
  744. if (unlikely(status != CL_SUCCESS)) {
  745. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)", status);
  746. return NULL;
  747. }
  748. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
  749. if (unlikely(status != CL_SUCCESS)) {
  750. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)", status);
  751. return NULL;
  752. }
  753. /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
  754. * to iterate over all the binary slots and find where the real program
  755. * is. What the heck is this!? */
  756. for (slot = 0; slot < cpnd; slot++)
  757. if (binary_sizes[slot])
  758. break;
  759. /* copy over all of the generated binaries. */
  760. applog(LOG_DEBUG, "Binary size for gpu %d found in binary slot %d: %d", gpu, slot, binary_sizes[slot]);
  761. if (!binary_sizes[slot]) {
  762. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
  763. return NULL;
  764. }
  765. binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
  766. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
  767. if (unlikely(status != CL_SUCCESS)) {
  768. applog(LOG_ERR, "Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)", status);
  769. return NULL;
  770. }
  771. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  772. * be hacked in */
  773. if (patchbfi) {
  774. unsigned remaining = binary_sizes[slot];
  775. char *w = binaries[slot];
  776. unsigned int start, length;
  777. /* Find 2nd incidence of .text, and copy the program's
  778. * position and length at a fixed offset from that. Then go
  779. * back and find the 2nd incidence of \x7ELF (rewind by one
  780. * from ELF) and then patch the opcocdes */
  781. if (!advance(&w, &remaining, ".text"))
  782. goto build;
  783. w++; remaining--;
  784. if (!advance(&w, &remaining, ".text")) {
  785. /* 32 bit builds only one ELF */
  786. w--; remaining++;
  787. }
  788. memcpy(&start, w + 285, 4);
  789. memcpy(&length, w + 289, 4);
  790. w = binaries[slot]; remaining = binary_sizes[slot];
  791. if (!advance(&w, &remaining, "ELF"))
  792. goto build;
  793. w++; remaining--;
  794. if (!advance(&w, &remaining, "ELF")) {
  795. /* 32 bit builds only one ELF */
  796. w--; remaining++;
  797. }
  798. w--; remaining++;
  799. w += start; remaining -= start;
  800. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  801. w, remaining);
  802. patch_opcodes(w, length);
  803. status = clReleaseProgram(clState->program);
  804. if (status != CL_SUCCESS) {
  805. applog(LOG_ERR, "Error %d: Releasing program. (clReleaseProgram)", status);
  806. return NULL;
  807. }
  808. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
  809. if (status != CL_SUCCESS) {
  810. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  811. return NULL;
  812. }
  813. /* Program needs to be rebuilt */
  814. prog_built = false;
  815. }
  816. free(source);
  817. /* Save the binary to be loaded next time */
  818. binaryfile = fopen(binaryfilename, "wb");
  819. if (!binaryfile) {
  820. /* Not a fatal problem, just means we build it again next time */
  821. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  822. } else {
  823. if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
  824. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  825. return NULL;
  826. }
  827. fclose(binaryfile);
  828. }
  829. built:
  830. if (binaries[slot])
  831. free(binaries[slot]);
  832. free(binaries);
  833. free(binary_sizes);
  834. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %d vectors and worksize %d",
  835. filename, clState->hasBitAlign ? "" : "out", clState->vwidth, clState->wsize);
  836. if (!prog_built) {
  837. /* create a cl program executable for all the devices specified */
  838. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  839. if (status != CL_SUCCESS) {
  840. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  841. size_t logSize;
  842. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  843. char *log = malloc(logSize);
  844. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  845. applog(LOG_ERR, "%s", log);
  846. return NULL;
  847. }
  848. }
  849. /* get a kernel object handle for a kernel with the given name */
  850. clState->kernel = clCreateKernel(clState->program, "search", &status);
  851. if (status != CL_SUCCESS) {
  852. applog(LOG_ERR, "Error %d: Creating Kernel from program. (clCreateKernel)", status);
  853. return NULL;
  854. }
  855. #ifdef USE_SCRYPT
  856. if (opt_scrypt) {
  857. size_t ipt = (1024 / cgpu->lookup_gap + (1024 % cgpu->lookup_gap > 0));
  858. size_t bufsize = 128 * ipt * cgpu->thread_concurrency;
  859. /* Use the max alloc value which has been rounded to a power of
  860. * 2 greater >= required amount earlier */
  861. if (bufsize > cgpu->max_alloc) {
  862. applog(LOG_WARNING, "Maximum buffer memory device %d supports says %u, your scrypt settings come to %u",
  863. gpu, cgpu->max_alloc, bufsize);
  864. } else
  865. bufsize = cgpu->max_alloc;
  866. applog(LOG_DEBUG, "Creating scrypt buffer sized %d", bufsize);
  867. clState->padbufsize = bufsize;
  868. clState->padbuffer8 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, bufsize, NULL, &status);
  869. if (status != CL_SUCCESS) {
  870. applog(LOG_ERR, "Error %d: clCreateBuffer (padbuffer8), decrease CT or increase LG", status);
  871. return NULL;
  872. }
  873. clState->CLbuffer0 = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, 128, NULL, &status);
  874. if (status != CL_SUCCESS) {
  875. applog(LOG_ERR, "Error %d: clCreateBuffer (CLbuffer0)", status);
  876. return NULL;
  877. }
  878. }
  879. #endif
  880. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);
  881. if (status != CL_SUCCESS) {
  882. applog(LOG_ERR, "Error %d: clCreateBuffer (outputBuffer)", status);
  883. return NULL;
  884. }
  885. return clState;
  886. }
  887. #endif /* HAVE_OPENCL */