ocl.c 36 KB

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