ocl.c 36 KB

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