ocl.c 36 KB

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