ocl.c 36 KB

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