ocl.c 36 KB

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