ocl.c 36 KB

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