ocl.c 36 KB

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