ocl.c 36 KB

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