ocl.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /*
  2. * Copyright 2011-2013 Con Kolivas
  3. * Copyright 2012-2013 Luke Dashjr
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #ifdef HAVE_OPENCL
  12. #include <ctype.h>
  13. #include <signal.h>
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <time.h>
  21. #include <sys/time.h>
  22. #include <pthread.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #define OMIT_OPENCL_API
  26. #include "deviceapi.h"
  27. #include "driver-opencl.h"
  28. #include "findnonce.h"
  29. #include "logging.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. /* Try in the optional kernel path or installed prefix first */
  191. f = open_bitstream("opencl", filename);
  192. if (!f) {
  193. /* Then try from the path BFGMiner was called */
  194. strcpy(fullpath, cgminer_path);
  195. strcat(fullpath, filename);
  196. f = fopen(fullpath, "rb");
  197. }
  198. /* Finally try opening it directly */
  199. if (!f)
  200. f = fopen(filename, "rb");
  201. if (!f) {
  202. applog(LOG_ERR, "Unable to open %s or %s for reading", filename, fullpath);
  203. return NULL;
  204. }
  205. fseek(f, 0, SEEK_END);
  206. *length = ftell(f);
  207. fseek(f, 0, SEEK_SET);
  208. buffer = malloc(*length+1);
  209. *length = fread(buffer, 1, *length, f);
  210. fclose(f);
  211. ((char*)buffer)[*length] = '\0';
  212. return (char*)buffer;
  213. }
  214. extern int opt_g_threads;
  215. int clDevicesNum(void) {
  216. cl_int status;
  217. char pbuff[256];
  218. cl_uint numDevices;
  219. cl_uint numPlatforms;
  220. int most_devices = -1;
  221. cl_platform_id *platforms;
  222. cl_platform_id platform = NULL;
  223. unsigned int i, mdplatform = 0;
  224. bool mdmesa = false;
  225. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  226. /* If this fails, assume no GPUs. */
  227. if (status != CL_SUCCESS) {
  228. applog(LOG_ERR, "Error %d: clGetPlatformsIDs failed (no OpenCL SDK installed?)", status);
  229. return -1;
  230. }
  231. if (numPlatforms == 0) {
  232. applog(LOG_ERR, "clGetPlatformsIDs returned no platforms (no OpenCL SDK installed?)");
  233. return -1;
  234. }
  235. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  236. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  237. if (status != CL_SUCCESS) {
  238. applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
  239. return -1;
  240. }
  241. for (i = 0; i < numPlatforms; i++) {
  242. if (opt_platform_id >= 0 && (int)i != opt_platform_id)
  243. continue;
  244. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  245. if (status != CL_SUCCESS) {
  246. applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
  247. return -1;
  248. }
  249. platform = platforms[i];
  250. applog(LOG_INFO, "CL Platform %d vendor: %s", i, pbuff);
  251. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  252. if (status == CL_SUCCESS)
  253. applog(LOG_INFO, "CL Platform %d name: %s", i, pbuff);
  254. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(pbuff), pbuff, NULL);
  255. if (status == CL_SUCCESS)
  256. applog(LOG_INFO, "CL Platform %d version: %s", i, pbuff);
  257. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  258. if (status != CL_SUCCESS) {
  259. applog(LOG_INFO, "Error %d: Getting Device IDs (num)", status);
  260. continue;
  261. }
  262. applog(LOG_INFO, "Platform %d devices: %d", i, numDevices);
  263. if ((int)numDevices > most_devices) {
  264. most_devices = numDevices;
  265. mdplatform = i;
  266. mdmesa = strstr(pbuff, "MESA");
  267. }
  268. if (numDevices) {
  269. unsigned int j;
  270. cl_device_id *devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  271. clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  272. for (j = 0; j < numDevices; j++) {
  273. clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  274. applog(LOG_INFO, "\t%i\t%s", j, pbuff);
  275. }
  276. free(devices);
  277. }
  278. }
  279. if (opt_platform_id < 0)
  280. opt_platform_id = mdplatform;
  281. if (mdmesa && opt_g_threads == -1)
  282. opt_g_threads = 1;
  283. return most_devices;
  284. }
  285. static int advance(char **area, unsigned *remaining, const char *marker)
  286. {
  287. char *find = memmem(*area, *remaining, marker, strlen(marker));
  288. if (!find) {
  289. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  290. return 0;
  291. }
  292. *remaining -= find - *area;
  293. *area = find;
  294. return 1;
  295. }
  296. #define OP3_INST_BFE_UINT 4ULL
  297. #define OP3_INST_BFE_INT 5ULL
  298. #define OP3_INST_BFI_INT 6ULL
  299. #define OP3_INST_BIT_ALIGN_INT 12ULL
  300. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  301. void patch_opcodes(char *w, unsigned remaining)
  302. {
  303. uint64_t *opcode = (uint64_t *)w;
  304. int patched = 0;
  305. int count_bfe_int = 0;
  306. int count_bfe_uint = 0;
  307. int count_byte_align = 0;
  308. while (42) {
  309. int clamp = (*opcode >> (32 + 31)) & 0x1;
  310. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  311. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  312. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  313. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  314. int pred_sel = (*opcode >> 29) & 0x3;
  315. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  316. if (alu_inst == OP3_INST_BFE_INT) {
  317. count_bfe_int++;
  318. } else if (alu_inst == OP3_INST_BFE_UINT) {
  319. count_bfe_uint++;
  320. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  321. count_byte_align++;
  322. // patch this instruction to BFI_INT
  323. *opcode &= 0xfffc1fffffffffffULL;
  324. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  325. patched++;
  326. }
  327. }
  328. if (remaining <= 8)
  329. break;
  330. opcode++;
  331. remaining -= 8;
  332. }
  333. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  334. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  335. count_bfe_int, count_bfe_uint, count_byte_align);
  336. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  337. }
  338. _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
  339. {
  340. _clState *clState = calloc(1, sizeof(_clState));
  341. bool patchbfi = false, prog_built = false;
  342. bool ismesa = false;
  343. struct cgpu_info *cgpu = &gpus[gpu];
  344. struct opencl_device_data * const data = cgpu->device_data;
  345. cl_platform_id platform = NULL;
  346. char pbuff[256], vbuff[255];
  347. char *s, *q;
  348. cl_platform_id* platforms;
  349. cl_uint preferred_vwidth;
  350. cl_device_id *devices;
  351. cl_uint numPlatforms;
  352. cl_uint numDevices;
  353. cl_int status;
  354. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  355. if (status != CL_SUCCESS) {
  356. applog(LOG_ERR, "Error %d: Getting Platforms. (clGetPlatformsIDs)", status);
  357. return NULL;
  358. }
  359. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  360. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  361. if (status != CL_SUCCESS) {
  362. applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
  363. return NULL;
  364. }
  365. if (opt_platform_id >= (int)numPlatforms) {
  366. applog(LOG_ERR, "Specified platform that does not exist");
  367. return NULL;
  368. }
  369. status = clGetPlatformInfo(platforms[opt_platform_id], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  370. if (status != CL_SUCCESS) {
  371. applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
  372. return NULL;
  373. }
  374. platform = platforms[opt_platform_id];
  375. if (platform == NULL) {
  376. perror("NULL platform found!\n");
  377. return NULL;
  378. }
  379. applog(LOG_INFO, "CL Platform vendor: %s", pbuff);
  380. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  381. if (status == CL_SUCCESS)
  382. applog(LOG_INFO, "CL Platform name: %s", pbuff);
  383. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(vbuff), vbuff, NULL);
  384. if (status == CL_SUCCESS)
  385. applog(LOG_INFO, "CL Platform version: %s", vbuff);
  386. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  387. if (status != CL_SUCCESS) {
  388. applog(LOG_ERR, "Error %d: Getting Device IDs (num)", status);
  389. return NULL;
  390. }
  391. if (numDevices > 0 ) {
  392. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  393. /* Now, get the device list data */
  394. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  395. if (status != CL_SUCCESS) {
  396. applog(LOG_ERR, "Error %d: Getting Device IDs (list)", status);
  397. return NULL;
  398. }
  399. applog(LOG_INFO, "List of devices:");
  400. unsigned int i;
  401. for (i = 0; i < numDevices; i++) {
  402. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  403. if (status != CL_SUCCESS) {
  404. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  405. return NULL;
  406. }
  407. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  408. }
  409. if (gpu < numDevices) {
  410. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  411. if (status != CL_SUCCESS) {
  412. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  413. return NULL;
  414. }
  415. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  416. strncpy(name, pbuff, nameSize);
  417. } else {
  418. applog(LOG_ERR, "Invalid GPU %i", gpu);
  419. return NULL;
  420. }
  421. } else return NULL;
  422. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  423. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  424. if (status != CL_SUCCESS) {
  425. applog(LOG_ERR, "Error %d: Creating Context. (clCreateContextFromType)", status);
  426. return NULL;
  427. }
  428. /////////////////////////////////////////////////////////////////
  429. // Create an OpenCL command queue
  430. /////////////////////////////////////////////////////////////////
  431. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  432. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  433. if (status != CL_SUCCESS) /* Try again without OOE enable */
  434. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  435. if (status != CL_SUCCESS) {
  436. applog(LOG_ERR, "Error %d: Creating Command Queue. (clCreateCommandQueue)", status);
  437. return NULL;
  438. }
  439. /* Check for BFI INT support. Hopefully people don't mix devices with
  440. * and without it! */
  441. char * extensions = malloc(1024);
  442. const char * camo = "cl_amd_media_ops";
  443. char *find;
  444. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  445. if (status != CL_SUCCESS) {
  446. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS", status);
  447. return NULL;
  448. }
  449. find = strstr(extensions, camo);
  450. if (find)
  451. clState->hasBitAlign = true;
  452. /* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
  453. char * devoclver = malloc(1024);
  454. const char * ocl10 = "OpenCL 1.0";
  455. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
  456. if (status != CL_SUCCESS) {
  457. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION", status);
  458. return NULL;
  459. }
  460. find = strstr(devoclver, ocl10);
  461. if (!find)
  462. clState->hasOpenCL11plus = true;
  463. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&preferred_vwidth, NULL);
  464. if (status != CL_SUCCESS) {
  465. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT", status);
  466. return NULL;
  467. }
  468. applog(LOG_DEBUG, "Preferred vector width reported %d", preferred_vwidth);
  469. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  470. if (status != CL_SUCCESS) {
  471. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE", status);
  472. return NULL;
  473. }
  474. applog(LOG_DEBUG, "Max work group size reported %"PRId64, (int64_t)clState->max_work_size);
  475. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(clState->max_compute_units), (void *)&clState->max_compute_units, NULL);
  476. if (status != CL_SUCCESS) {
  477. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_COMPUTE_UNITS", status);
  478. return NULL;
  479. }
  480. if (data->_init_intensity)
  481. {
  482. data->oclthreads = 1; // Needed to ensure we don't just try to re-save the string (which would free before strduping and segfault anyway)
  483. opencl_set_intensity_from_str(cgpu, data->_init_intensity);
  484. }
  485. else
  486. data->oclthreads = intensity_to_oclthreads(MIN_INTENSITY, !opt_scrypt);
  487. applog(LOG_DEBUG, "Max compute units reported %u", (unsigned)clState->max_compute_units);
  488. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_MEM_ALLOC_SIZE , sizeof(cl_ulong), (void *)&data->max_alloc, NULL);
  489. if (status != CL_SUCCESS) {
  490. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_MEM_ALLOC_SIZE", status);
  491. return NULL;
  492. }
  493. applog(LOG_DEBUG, "Max mem alloc size is %lu", (unsigned long)data->max_alloc);
  494. find = strstr(vbuff, "MESA");
  495. if (find)
  496. {
  497. long int major = strtol(&find[4], &s, 10), minor;
  498. if (!major)
  499. {} // No version number at all
  500. else
  501. if (s[0] == '.')
  502. minor = strtol(&s[1], NULL, 10);
  503. else
  504. minor = 0;
  505. if (major < 10 || (major == 10 && minor < 1))
  506. {
  507. if (data->opt_opencl_binaries == OBU_DEFAULT)
  508. {
  509. applog(LOG_DEBUG, "Mesa OpenCL platform detected (v%ld.%ld), disabling OpenCL kernel binaries and bitalign", major, minor);
  510. data->opt_opencl_binaries = OBU_NONE;
  511. }
  512. else
  513. applog(LOG_DEBUG, "Mesa OpenCL platform detected (v%ld.%ld), disabling bitalign", major, minor);
  514. clState->hasBitAlign = false;
  515. }
  516. else
  517. applog(LOG_DEBUG, "Mesa OpenCL platform detected (v%ld.%ld)", major, minor);
  518. ismesa = true;
  519. }
  520. if (data->opt_opencl_binaries == OBU_DEFAULT)
  521. {
  522. #ifdef __APPLE__
  523. // Apple OpenCL doesn't like using binaries this way
  524. data->opt_opencl_binaries = OBU_NONE;
  525. #else
  526. data->opt_opencl_binaries = OBU_LOADSAVE;
  527. #endif
  528. }
  529. /* Create binary filename based on parameters passed to opencl
  530. * compiler to ensure we only load a binary that matches what would
  531. * have otherwise created. The filename is:
  532. * kernelname + name +/- g(offset) + v + vectors + w + work_size + l + sizeof(long) + p + platform version + .bin
  533. * For scrypt the filename is:
  534. * kernelname + name + g + lg + lookup_gap + tc + thread_concurrency + w + work_size + l + sizeof(long) + p + platform version + .bin
  535. */
  536. char binaryfilename[255];
  537. char filename[255];
  538. char numbuf[32];
  539. if (!data->kernel_file)
  540. {
  541. if (opt_scrypt) {
  542. applog(LOG_INFO, "Selecting scrypt kernel");
  543. clState->chosen_kernel = KL_SCRYPT;
  544. }
  545. else if (ismesa)
  546. {
  547. applog(LOG_INFO, "Selecting phatk kernel for Mesa");
  548. clState->chosen_kernel = KL_PHATK;
  549. } else if (!strstr(name, "Tahiti") &&
  550. /* Detect all 2.6 SDKs not with Tahiti and use diablo kernel */
  551. (strstr(vbuff, "844.4") || // Linux 64 bit ATI 2.6 SDK
  552. strstr(vbuff, "851.4") || // Windows 64 bit ""
  553. strstr(vbuff, "831.4") ||
  554. strstr(vbuff, "898.1") || // 12.2 driver SDK
  555. strstr(vbuff, "923.1") || // 12.4
  556. strstr(vbuff, "938.2") || // SDK 2.7
  557. strstr(vbuff, "1113.2"))) {// SDK 2.8
  558. applog(LOG_INFO, "Selecting diablo kernel");
  559. clState->chosen_kernel = KL_DIABLO;
  560. /* Detect all 7970s, older ATI and NVIDIA and use poclbm */
  561. } else if (strstr(name, "Tahiti") || !clState->hasBitAlign) {
  562. applog(LOG_INFO, "Selecting poclbm kernel");
  563. clState->chosen_kernel = KL_POCLBM;
  564. /* Use phatk for the rest R5xxx R6xxx */
  565. } else {
  566. applog(LOG_INFO, "Selecting phatk kernel");
  567. clState->chosen_kernel = KL_PHATK;
  568. }
  569. data->kernel_file = strdup(opencl_get_default_kernel_filename(clState->chosen_kernel));
  570. }
  571. snprintf(filename, sizeof(filename), "%s.cl", data->kernel_file);
  572. snprintf(binaryfilename, sizeof(filename), "%s", data->kernel_file);
  573. int pl;
  574. char *source = file_contents(filename, &pl);
  575. if (!source)
  576. return NULL;
  577. s = strstr(source, "kernel-interface:");
  578. if (s)
  579. {
  580. for (s = &s[17]; s[0] && isspace(s[0]); ++s)
  581. if (s[0] == '\n' || s[0] == '\r')
  582. break;
  583. for (q = s; q[0] && !isspace(q[0]); ++q)
  584. {} // Find end of string
  585. const size_t kinamelen = q - s;
  586. char kiname[kinamelen + 1];
  587. memcpy(kiname, s, kinamelen);
  588. kiname[kinamelen] = '\0';
  589. clState->chosen_kernel = select_kernel(kiname);
  590. }
  591. switch (clState->chosen_kernel) {
  592. case KL_NONE:
  593. applog(LOG_ERR, "%s: Failed to identify kernel interface for %s",
  594. cgpu->dev_repr, data->kernel_file);
  595. free(source);
  596. return NULL;
  597. case KL_PHATK:
  598. if ((strstr(vbuff, "844.4") || strstr(vbuff, "851.4") ||
  599. strstr(vbuff, "831.4") || strstr(vbuff, "898.1") ||
  600. strstr(vbuff, "923.1") || strstr(vbuff, "938.2") ||
  601. strstr(vbuff, "1113.2"))) {
  602. applog(LOG_WARNING, "WARNING: You have selected the phatk kernel.");
  603. applog(LOG_WARNING, "You are running SDK 2.6+ which performs poorly with this kernel.");
  604. applog(LOG_WARNING, "Downgrade your SDK and delete any .bin files before starting again.");
  605. applog(LOG_WARNING, "Or allow BFGMiner to automatically choose a more suitable kernel.");
  606. }
  607. default:
  608. ;
  609. }
  610. applog(LOG_DEBUG, "%s: Using kernel %s with interface %s",
  611. cgpu->dev_repr, data->kernel_file,
  612. opencl_get_kernel_interface_name(clState->chosen_kernel));
  613. /* For some reason 2 vectors is still better even if the card says
  614. * otherwise, and many cards lie about their max so use 256 as max
  615. * unless explicitly set on the command line. Tahiti prefers 1 */
  616. if (strstr(name, "Tahiti"))
  617. preferred_vwidth = 1;
  618. else if (preferred_vwidth > 2)
  619. preferred_vwidth = 2;
  620. if (data->vwidth)
  621. clState->vwidth = data->vwidth;
  622. else {
  623. clState->vwidth = preferred_vwidth;
  624. data->vwidth = preferred_vwidth;
  625. }
  626. if (((clState->chosen_kernel == KL_POCLBM || clState->chosen_kernel == KL_DIABLO || clState->chosen_kernel == KL_DIAKGCN) &&
  627. clState->vwidth == 1 && clState->hasOpenCL11plus) || opt_scrypt)
  628. clState->goffset = true;
  629. if (data->work_size && data->work_size <= clState->max_work_size)
  630. clState->wsize = data->work_size;
  631. else if (opt_scrypt)
  632. clState->wsize = 256;
  633. else if (strstr(name, "Tahiti"))
  634. clState->wsize = 64;
  635. else
  636. clState->wsize = (clState->max_work_size <= 256 ? clState->max_work_size : 256) / clState->vwidth;
  637. data->work_size = clState->wsize;
  638. #ifdef USE_SCRYPT
  639. if (opt_scrypt) {
  640. if (!data->opt_lg) {
  641. applog(LOG_DEBUG, "GPU %d: selecting lookup gap of 2", gpu);
  642. data->lookup_gap = 2;
  643. } else
  644. data->lookup_gap = data->opt_lg;
  645. if (!data->opt_tc) {
  646. unsigned int sixtyfours;
  647. sixtyfours = data->max_alloc / 131072 / 64 - 1;
  648. data->thread_concurrency = sixtyfours * 64;
  649. if (data->shaders && data->thread_concurrency > data->shaders) {
  650. data->thread_concurrency -= data->thread_concurrency % data->shaders;
  651. if (data->thread_concurrency > data->shaders * 5)
  652. data->thread_concurrency = data->shaders * 5;
  653. }
  654. applog(LOG_DEBUG, "GPU %u: selecting thread concurrency of %lu", gpu, (unsigned long)data->thread_concurrency);
  655. } else
  656. data->thread_concurrency = data->opt_tc;
  657. }
  658. #endif
  659. FILE *binaryfile;
  660. size_t *binary_sizes;
  661. char **binaries;
  662. size_t sourceSize[] = {(size_t)pl};
  663. cl_uint slot, cpnd;
  664. slot = cpnd = 0;
  665. binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
  666. if (unlikely(!binary_sizes)) {
  667. applog(LOG_ERR, "Unable to calloc binary_sizes");
  668. return NULL;
  669. }
  670. binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
  671. if (unlikely(!binaries)) {
  672. applog(LOG_ERR, "Unable to calloc binaries");
  673. return NULL;
  674. }
  675. strcat(binaryfilename, name);
  676. if (clState->goffset)
  677. strcat(binaryfilename, "g");
  678. if (opt_scrypt) {
  679. #ifdef USE_SCRYPT
  680. sprintf(numbuf, "lg%utc%u", data->lookup_gap, (unsigned int)data->thread_concurrency);
  681. strcat(binaryfilename, numbuf);
  682. #endif
  683. } else {
  684. sprintf(numbuf, "v%d", clState->vwidth);
  685. strcat(binaryfilename, numbuf);
  686. }
  687. sprintf(numbuf, "w%d", (int)clState->wsize);
  688. strcat(binaryfilename, numbuf);
  689. sprintf(numbuf, "l%d", (int)sizeof(long));
  690. strcat(binaryfilename, numbuf);
  691. strcat(binaryfilename, "p");
  692. strcat(binaryfilename, vbuff);
  693. sanestr(binaryfilename, binaryfilename);
  694. applog(LOG_DEBUG, "OCL%2u: Configured OpenCL kernel name: %s", gpu, binaryfilename);
  695. strcat(binaryfilename, ".bin");
  696. if (!(data->opt_opencl_binaries & OBU_LOAD))
  697. goto build;
  698. binaryfile = fopen(binaryfilename, "rb");
  699. if (!binaryfile) {
  700. applog(LOG_DEBUG, "No binary found, generating from source");
  701. } else {
  702. struct stat binary_stat;
  703. if (unlikely(stat(binaryfilename, &binary_stat))) {
  704. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  705. fclose(binaryfile);
  706. goto build;
  707. }
  708. if (!binary_stat.st_size)
  709. goto build;
  710. binary_sizes[slot] = binary_stat.st_size;
  711. binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
  712. if (unlikely(!binaries[slot])) {
  713. applog(LOG_ERR, "Unable to calloc binaries");
  714. fclose(binaryfile);
  715. return NULL;
  716. }
  717. if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
  718. applog(LOG_ERR, "Unable to fread binaries");
  719. fclose(binaryfile);
  720. free(binaries[slot]);
  721. goto build;
  722. }
  723. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
  724. if (status != CL_SUCCESS) {
  725. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  726. fclose(binaryfile);
  727. free(binaries[slot]);
  728. goto build;
  729. }
  730. fclose(binaryfile);
  731. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  732. goto built;
  733. }
  734. /////////////////////////////////////////////////////////////////
  735. // Load CL file, build CL program object, create CL kernel object
  736. /////////////////////////////////////////////////////////////////
  737. build:
  738. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  739. if (status != CL_SUCCESS) {
  740. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithSource)", status);
  741. return NULL;
  742. }
  743. /* create a cl program executable for all the devices specified */
  744. char *CompilerOptions = calloc(1, 256);
  745. #ifdef USE_SCRYPT
  746. if (opt_scrypt)
  747. sprintf(CompilerOptions, "-D LOOKUP_GAP=%d -D CONCURRENT_THREADS=%d -D WORKSIZE=%d",
  748. data->lookup_gap, (unsigned int)data->thread_concurrency, (int)clState->wsize);
  749. else
  750. #endif
  751. {
  752. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d -D WORKVEC=%d",
  753. (int)clState->wsize, clState->vwidth, (int)clState->wsize * clState->vwidth);
  754. }
  755. applog(LOG_DEBUG, "Setting worksize to %"PRId64, (int64_t)clState->wsize);
  756. if (clState->vwidth > 1)
  757. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->vwidth);
  758. if (clState->hasBitAlign) {
  759. strcat(CompilerOptions, " -D BITALIGN");
  760. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  761. if (strstr(name, "Cedar") ||
  762. strstr(name, "Redwood") ||
  763. strstr(name, "Juniper") ||
  764. strstr(name, "Cypress" ) ||
  765. strstr(name, "Hemlock" ) ||
  766. strstr(name, "Caicos" ) ||
  767. strstr(name, "Turks" ) ||
  768. strstr(name, "Barts" ) ||
  769. strstr(name, "Cayman" ) ||
  770. strstr(name, "Antilles" ) ||
  771. strstr(name, "Wrestler" ) ||
  772. strstr(name, "Zacate" ) ||
  773. strstr(name, "WinterPark" ))
  774. {
  775. // BFI_INT patching only works with AMD-APP up to 1084
  776. if (strstr(vbuff, "ATI-Stream"))
  777. patchbfi = true;
  778. else
  779. if ((s = strstr(vbuff, "AMD-APP")) && (s = strchr(s, '(')) && atoi(&s[1]) < 1085)
  780. patchbfi = true;
  781. }
  782. } else
  783. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  784. if (patchbfi) {
  785. if (data->opt_opencl_binaries == OBU_LOADSAVE)
  786. {
  787. strcat(CompilerOptions, " -D BFI_INT");
  788. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  789. }
  790. else
  791. {
  792. patchbfi = false;
  793. applog(LOG_WARNING, "BFI_INT patch requiring device found, but OpenCL binary usage disabled; cannot BFI_INT patch");
  794. }
  795. } else
  796. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  797. if (clState->goffset)
  798. strcat(CompilerOptions, " -D GOFFSET");
  799. if (!clState->hasOpenCL11plus)
  800. strcat(CompilerOptions, " -D OCL1");
  801. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  802. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  803. free(CompilerOptions);
  804. if (status != CL_SUCCESS) {
  805. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  806. size_t logSize;
  807. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  808. char *log = malloc(logSize);
  809. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  810. applog(LOG_ERR, "%s", log);
  811. return NULL;
  812. }
  813. prog_built = true;
  814. if (!(data->opt_opencl_binaries & OBU_SAVE))
  815. goto built;
  816. status = clGetProgramInfo(clState->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
  817. if (unlikely(status != CL_SUCCESS)) {
  818. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)", status);
  819. return NULL;
  820. }
  821. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
  822. if (unlikely(status != CL_SUCCESS)) {
  823. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)", status);
  824. return NULL;
  825. }
  826. /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
  827. * to iterate over all the binary slots and find where the real program
  828. * is. What the heck is this!? */
  829. for (slot = 0; slot < cpnd; slot++)
  830. if (binary_sizes[slot])
  831. break;
  832. /* copy over all of the generated binaries. */
  833. applog(LOG_DEBUG, "Binary size for gpu %u found in binary slot %u: %"PRId64,
  834. gpu, (unsigned)slot, (int64_t)binary_sizes[slot]);
  835. if (!binary_sizes[slot]) {
  836. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
  837. return NULL;
  838. }
  839. binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
  840. status = clGetProgramInfo(clState->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
  841. if (unlikely(status != CL_SUCCESS)) {
  842. applog(LOG_ERR, "Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)", status);
  843. return NULL;
  844. }
  845. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  846. * be hacked in */
  847. if (patchbfi) {
  848. unsigned remaining = binary_sizes[slot];
  849. char *w = binaries[slot];
  850. unsigned int start, length;
  851. /* Find 2nd incidence of .text, and copy the program's
  852. * position and length at a fixed offset from that. Then go
  853. * back and find the 2nd incidence of \x7ELF (rewind by one
  854. * from ELF) and then patch the opcocdes */
  855. if (!advance(&w, &remaining, ".text"))
  856. goto build;
  857. w++; remaining--;
  858. if (!advance(&w, &remaining, ".text")) {
  859. /* 32 bit builds only one ELF */
  860. w--; remaining++;
  861. }
  862. memcpy(&start, w + 285, 4);
  863. memcpy(&length, w + 289, 4);
  864. w = binaries[slot]; remaining = binary_sizes[slot];
  865. if (!advance(&w, &remaining, "ELF"))
  866. goto build;
  867. w++; remaining--;
  868. if (!advance(&w, &remaining, "ELF")) {
  869. /* 32 bit builds only one ELF */
  870. w--; remaining++;
  871. }
  872. w--; remaining++;
  873. w += start; remaining -= start;
  874. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  875. w, remaining);
  876. patch_opcodes(w, length);
  877. status = clReleaseProgram(clState->program);
  878. if (status != CL_SUCCESS) {
  879. applog(LOG_ERR, "Error %d: Releasing program. (clReleaseProgram)", status);
  880. return NULL;
  881. }
  882. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
  883. if (status != CL_SUCCESS) {
  884. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  885. return NULL;
  886. }
  887. /* Program needs to be rebuilt */
  888. prog_built = false;
  889. }
  890. free(source);
  891. /* Save the binary to be loaded next time */
  892. binaryfile = fopen(binaryfilename, "wb");
  893. if (!binaryfile) {
  894. /* Not a fatal problem, just means we build it again next time */
  895. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  896. } else {
  897. if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
  898. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  899. return NULL;
  900. }
  901. fclose(binaryfile);
  902. }
  903. built:
  904. if (binaries[slot])
  905. free(binaries[slot]);
  906. free(binaries);
  907. free(binary_sizes);
  908. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %"PRId64" vectors and worksize %"PRIu64,
  909. filename, clState->hasBitAlign ? "" : "out", (int64_t)clState->vwidth, (uint64_t)clState->wsize);
  910. if (!prog_built) {
  911. /* create a cl program executable for all the devices specified */
  912. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  913. if (status != CL_SUCCESS) {
  914. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  915. size_t logSize;
  916. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  917. char *log = malloc(logSize);
  918. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  919. applog(LOG_ERR, "%s", log);
  920. return NULL;
  921. }
  922. }
  923. /* get a kernel object handle for a kernel with the given name */
  924. clState->kernel = clCreateKernel(clState->program, "search", &status);
  925. if (status != CL_SUCCESS) {
  926. applog(LOG_ERR, "Error %d: Creating Kernel from program. (clCreateKernel)", status);
  927. return NULL;
  928. }
  929. #ifdef USE_SCRYPT
  930. if (opt_scrypt) {
  931. size_t ipt = (1024 / data->lookup_gap + (1024 % data->lookup_gap > 0));
  932. size_t bufsize = 128 * ipt * data->thread_concurrency;
  933. /* Use the max alloc value which has been rounded to a power of
  934. * 2 greater >= required amount earlier */
  935. if (bufsize > data->max_alloc) {
  936. applog(LOG_WARNING, "Maximum buffer memory device %d supports says %lu", gpu, (unsigned long)data->max_alloc);
  937. applog(LOG_WARNING, "Your scrypt settings come to %lu", (unsigned long)bufsize);
  938. }
  939. applog(LOG_DEBUG, "Creating scrypt buffer sized %lu", (unsigned long)bufsize);
  940. clState->padbufsize = bufsize;
  941. /* This buffer is weird and might work to some degree even if
  942. * the create buffer call has apparently failed, so check if we
  943. * get anything back before we call it a failure. */
  944. clState->padbuffer8 = NULL;
  945. clState->padbuffer8 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, bufsize, NULL, &status);
  946. if (status != CL_SUCCESS && !clState->padbuffer8) {
  947. applog(LOG_ERR, "Error %d: clCreateBuffer (padbuffer8), decrease TC or increase LG", status);
  948. return NULL;
  949. }
  950. clState->CLbuffer0 = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, 128, NULL, &status);
  951. if (status != CL_SUCCESS) {
  952. applog(LOG_ERR, "Error %d: clCreateBuffer (CLbuffer0)", status);
  953. return NULL;
  954. }
  955. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, SCRYPT_BUFFERSIZE, NULL, &status);
  956. } else
  957. #endif
  958. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_WRITE_ONLY, BUFFERSIZE, NULL, &status);
  959. if (status != CL_SUCCESS) {
  960. applog(LOG_ERR, "Error %d: clCreateBuffer (outputBuffer)", status);
  961. return NULL;
  962. }
  963. return clState;
  964. }
  965. #endif /* HAVE_OPENCL */